From 6a0db889914c5faac39f603600e5603a3aa6cec3 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sun, 7 Aug 2016 15:08:54 +0200 Subject: [PATCH] Identity and Blob nicer --- src/Identity.cc | 35 +++++++++++++++++++++++++++++++++-- src/Identity.hh | 6 +++++- src/message.cc | 23 +++++++++++++++++++++++ src/message.hh | 6 +++++- src/pEpmodule.cc | 14 ++++++++++++-- src/pEpmodule.hh | 2 -- src/str_attr.cc | 13 +++++++++++++ src/str_attr.hh | 3 +++ 8 files changed, 94 insertions(+), 8 deletions(-) diff --git a/src/Identity.cc b/src/Identity.cc index 708f765..7392b43 100644 --- a/src/Identity.cc +++ b/src/Identity.cc @@ -1,16 +1,21 @@ #include "Identity.hh" #include +#include #include namespace pEp { namespace PythonAdapter { using namespace std; - Identity::Identity() - : _ident(new_identity(NULL, NULL, NULL, NULL)) + Identity::Identity(string address, string fpr, string user_id, string + username, int comm_type, string lang) + : _ident(new_identity(address.c_str(), fpr.c_str(), + user_id.c_str(), username.c_str())) { if (!_ident) throw bad_alloc(); + _ident->comm_type = (PEP_comm_type) comm_type; + this->lang(lang); } Identity::Identity(const Identity& second) @@ -49,6 +54,32 @@ namespace pEp { return ident; } + string Identity::_repr() + { + stringstream build; + build << "Identity("; + string address; + if (_ident->address) + address = string(_ident->address); + build << repr(address) << ", "; + string fpr; + if (_ident->fpr) + fpr = string(_ident->fpr); + build << repr(fpr) << ", "; + string user_id; + if (_ident->user_id) + user_id = string(_ident->user_id); + build << repr(user_id) << ", "; + string username; + if (_ident->username) + username = string(_ident->username); + build << repr(username) << ", "; + build << (int) _ident->comm_type << ", "; + string lang = _ident->lang; + build << repr(lang) << ")"; + return build.str(); + } + void Identity::lang(string value) { if (value == "") diff --git a/src/Identity.hh b/src/Identity.hh index 5e08fc9..901b943 100644 --- a/src/Identity.hh +++ b/src/Identity.hh @@ -15,13 +15,17 @@ namespace pEp { pEp_identity *_ident; public: - Identity(); + Identity(string address = "", string fpr = "", string user_id = "", + string username = "", int comm_type = 0, string lang = ""); + Identity(const Identity& second); Identity(pEp_identity *ident); ~Identity(); void attach(pEp_identity *ident); pEp_identity *detach(); + string _repr(); + string address() { return str_attr(_ident->address); } void address(string value) { str_attr(_ident->address, value); } diff --git a/src/message.cc b/src/message.cc index 7623702..0aaab37 100644 --- a/src/message.cc +++ b/src/message.cc @@ -3,6 +3,7 @@ #include #include #include +#include namespace pEp { namespace PythonAdapter { @@ -58,6 +59,28 @@ namespace pEp { } } + string Message::Blob::_repr() + { + stringstream build; + build << "Blob("; + if (!_bl) { + build << "b'', '', ''"; + } + else { + build << "bytes(" << _bl->size << "), "; + string mime_type; + if (_bl->mime_type) + mime_type = string(_bl->mime_type); + string filename; + if (_bl->filename) + filename = string(_bl->filename); + build << repr(mime_type) << ", "; + build << repr(filename); + } + build << ")"; + return build.str(); + } + int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { bloblist_t *bl = NULL; diff --git a/src/message.hh b/src/message.hh index 1d7808b..72c62e2 100644 --- a/src/message.hh +++ b/src/message.hh @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include "Identity.hh" @@ -10,6 +11,7 @@ namespace pEp { namespace PythonAdapter { using namespace utility; using namespace boost::python; + using boost::lexical_cast; // Message is owning a message struct @@ -27,10 +29,12 @@ namespace pEp { public: Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL), bool chained = false); - Blob(object data, string mime_type, string filename); + Blob(object data, string mime_type = "", string filename = ""); Blob(const Blob& second); ~Blob(); + string _repr(); + string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; } void mime_type(string value) { str_attr(_bl->mime_type, value); } diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index 9f02676..c369e44 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -40,6 +40,13 @@ BOOST_PYTHON_MODULE(pEp) scope().attr("about") = about(); auto identity_class = class_("Identity", "p≡p identity") + .def(init()) + .def(init()) + .def(init()) + .def(init()) + .def(init()) + .def(init()) + .def("__repr__", &Identity::_repr) .add_property("address", (string(Identity::*)()) &Identity::address, (void(Identity::*)(string)) &Identity::address, "email address or URI") @@ -72,6 +79,9 @@ BOOST_PYTHON_MODULE(pEp) auto blob_class = class_("Blob", "Binary large object", init< object, char const*, char const* >(args("data", "mime_type", "filename"), "init buffer with binary data") ) + .def(init()) + .def(init()) + .def("__repr__", &Message::Blob::_repr) .add_property("mime_type", (string(Message::Blob::*)()) &Message::Blob::mime_type, (void(Message::Blob::*)(string)) &Message::Blob::mime_type, "MIME type of object in Blob") @@ -148,8 +158,8 @@ BOOST_PYTHON_MODULE(pEp) (void(Message::*)(PEP_enc_format)) &Message::enc_format, "0: unencrypted, 1: inline PGP, 2: S/MIME, 3: PGP/MIME, 4: p≡p format"); - PyModuleDef * def = PyModule_GetDef(scope().ptr()); - def->m_free = free_module; + PyModuleDef * _def = PyModule_GetDef(scope().ptr()); + _def->m_free = free_module; PEP_STATUS status = ::init(&session); if (status != PEP_STATUS_OK) { diff --git a/src/pEpmodule.hh b/src/pEpmodule.hh index d7b3c7d..eea21b3 100644 --- a/src/pEpmodule.hh +++ b/src/pEpmodule.hh @@ -5,8 +5,6 @@ namespace pEp { namespace PythonAdapter { - using namespace std; - const char *version_string = "p≡p Python adapter version 0.1"; extern PEP_SESSION session; } diff --git a/src/str_attr.cc b/src/str_attr.cc index 71722d4..10ec6ed 100644 --- a/src/str_attr.cc +++ b/src/str_attr.cc @@ -8,6 +8,19 @@ namespace pEp { using namespace std; using namespace boost::locale; + object repr(object s) + { + return s.attr("__repr__")(); + } + + string repr(string s) + { + str _s = s.c_str(); + object _r = _s.attr("__repr__")(); + string r = extract< string >(_r); + return r; + } + string str_attr(char *&str) { if (!str) diff --git a/src/str_attr.hh b/src/str_attr.hh index 3f48da0..77fd18b 100644 --- a/src/str_attr.hh +++ b/src/str_attr.hh @@ -11,6 +11,9 @@ namespace pEp { using namespace std; using namespace boost::python; + object repr(object s); + string repr(string s); + string str_attr(char *&str); void str_attr(char *&str, string value);