Browse Source

Totally untested and "mutmassliche" implementation of distribution codec

PYADPT-55
heck 5 years ago
parent
commit
69ad1f61ca
  1. 9
      src/message.cc
  2. 50
      src/message_api.cc
  3. 2
      src/message_api.hh
  4. 5
      src/pEpmodule.cc

9
src/message.cc

@ -114,10 +114,14 @@ namespace pEp {
{ {
if (encoding == "") { if (encoding == "") {
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; string _mime_type = _bl->mime_type ? _bl->mime_type : "";
encoding = "ascii";
if (_mime_type == "application/pEp.sync") if (_mime_type == "application/pEp.sync")
encoding = "pep.sync"; encoding = "pep.sync";
else
encoding = "ascii"; if (_mime_type == "application/pEp.keyreset")
encoding = "pep.distribution";
} }
object codecs = import("codecs"); object codecs = import("codecs");
object _decode = codecs.attr("decode"); object _decode = codecs.attr("decode");
@ -397,4 +401,3 @@ namespace pEp {
} }
} }
} }

50
src/message_api.cc

@ -99,6 +99,38 @@ namespace pEp {
return boost::python::make_tuple(object(handle<>(ba)), 0); return boost::python::make_tuple(object(handle<>(ba)), 0);
} }
boost::python::tuple Distribution_decode(object buffer)
{
Py_buffer src;
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO);
if (result)
throw invalid_argument("need a contiguous buffer to read");
char *dst = NULL;
PEP_STATUS status = PER_to_XER_Distribution_msg((char *) src.buf, src.len, &dst);
PyBuffer_Release(&src);
_throw_status(status);
string _dst(dst);
free(dst);
return boost::python::make_tuple(_dst, 0);
}
static boost::python::tuple Distribution_encode(string text)
{
char *data = NULL;
size_t size = 0;
PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size);
_throw_status(status);
PyObject *ba = PyBytes_FromStringAndSize(data, size);
free(data);
if (!ba)
throw bad_alloc();
return boost::python::make_tuple(object(handle<>(ba)), 0);
}
object sync_search(string name) object sync_search(string name)
{ {
if (name != "pep.sync") { if (name != "pep.sync") {
@ -114,6 +146,22 @@ namespace pEp {
return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode); return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode);
} }
} }
object distribution_search(string name)
{
if (name != "pep.distribution") {
return object();
}
else {
object codecs = import("codecs");
object CodecInfo = codecs.attr("CodecInfo");
object _distribution_decode = make_function(Distribution_decode);
object _distribution_encode = make_function(Distribution_encode);
return call< object >(CodecInfo.ptr(), _distribution_encode, _distribution_decode);
}
}
} }
} }

2
src/message_api.hh

@ -12,6 +12,6 @@ namespace pEp {
boost::python::tuple decrypt_message(Message src, int flags=0); boost::python::tuple decrypt_message(Message src, int flags=0);
int _color(int rating); int _color(int rating);
object sync_search(string name); object sync_search(string name);
object distribution_search(string name);
} }
} }

5
src/pEpmodule.cc

@ -269,6 +269,7 @@ BOOST_PYTHON_MODULE(pEp)
"decode Blob data into string depending on MIME type if encoding=''\n" "decode Blob data into string depending on MIME type if encoding=''\n"
"\n" "\n"
" mime_type='application/pEp.sync' decode as 'pEp.sync'\n" " mime_type='application/pEp.sync' decode as 'pEp.sync'\n"
" mime_type='application/pEp.keyreset' decode as 'pEp.keyreset'\n"
" other mime_type decode as 'ascii' by default\n" " other mime_type decode as 'ascii' by default\n"
) )
.add_property("mime_type", (string(Message::Blob::*)()) &Message::Blob::mime_type, .add_property("mime_type", (string(Message::Blob::*)()) &Message::Blob::mime_type,
@ -579,8 +580,8 @@ BOOST_PYTHON_MODULE(pEp)
"True if sync is active, False otherwise\n" "True if sync is active, False otherwise\n"
); );
// codecs
// codecs
call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(sync_search)); call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(sync_search));
call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(distribution_search));
} }

Loading…
Cancel
Save