Browse Source

Identity and Blob nicer

master
Volker Birk 9 years ago
parent
commit
6a0db88991
  1. 35
      src/Identity.cc
  2. 6
      src/Identity.hh
  3. 23
      src/message.cc
  4. 6
      src/message.hh
  5. 14
      src/pEpmodule.cc
  6. 2
      src/pEpmodule.hh
  7. 13
      src/str_attr.cc
  8. 3
      src/str_attr.hh

35
src/Identity.cc

@ -1,16 +1,21 @@
#include "Identity.hh" #include "Identity.hh"
#include <typeinfo> #include <typeinfo>
#include <sstream>
#include <pEp/identity_list.h> #include <pEp/identity_list.h>
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
Identity::Identity() Identity::Identity(string address, string fpr, string user_id, string
: _ident(new_identity(NULL, NULL, NULL, NULL)) username, int comm_type, string lang)
: _ident(new_identity(address.c_str(), fpr.c_str(),
user_id.c_str(), username.c_str()))
{ {
if (!_ident) if (!_ident)
throw bad_alloc(); throw bad_alloc();
_ident->comm_type = (PEP_comm_type) comm_type;
this->lang(lang);
} }
Identity::Identity(const Identity& second) Identity::Identity(const Identity& second)
@ -49,6 +54,32 @@ namespace pEp {
return ident; 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) void Identity::lang(string value)
{ {
if (value == "") if (value == "")

6
src/Identity.hh

@ -15,13 +15,17 @@ namespace pEp {
pEp_identity *_ident; pEp_identity *_ident;
public: public:
Identity(); Identity(string address = "", string fpr = "", string user_id = "",
string username = "", int comm_type = 0, string lang = "");
Identity(const Identity& second); Identity(const Identity& second);
Identity(pEp_identity *ident); Identity(pEp_identity *ident);
~Identity(); ~Identity();
void attach(pEp_identity *ident); void attach(pEp_identity *ident);
pEp_identity *detach(); pEp_identity *detach();
string _repr();
string address() { return str_attr(_ident->address); } string address() { return str_attr(_ident->address); }
void address(string value) { str_attr(_ident->address, value); } void address(string value) { str_attr(_ident->address, value); }

23
src/message.cc

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdexcept> #include <stdexcept>
#include <sstream>
namespace pEp { namespace pEp {
namespace PythonAdapter { 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) { int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) {
bloblist_t *bl = NULL; bloblist_t *bl = NULL;

6
src/message.hh

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/lexical_cast.hpp>
#include <pEp/message.h> #include <pEp/message.h>
#include <string> #include <string>
#include "Identity.hh" #include "Identity.hh"
@ -10,6 +11,7 @@ namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace utility; using namespace utility;
using namespace boost::python; using namespace boost::python;
using boost::lexical_cast;
// Message is owning a message struct // Message is owning a message struct
@ -27,10 +29,12 @@ namespace pEp {
public: public:
Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL), Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL),
bool chained = false); bool chained = false);
Blob(object data, string mime_type, string filename); Blob(object data, string mime_type = "", string filename = "");
Blob(const Blob& second); Blob(const Blob& second);
~Blob(); ~Blob();
string _repr();
string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; } string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; }
void mime_type(string value) { str_attr(_bl->mime_type, value); } void mime_type(string value) { str_attr(_bl->mime_type, value); }

14
src/pEpmodule.cc

@ -40,6 +40,13 @@ BOOST_PYTHON_MODULE(pEp)
scope().attr("about") = about(); scope().attr("about") = about();
auto identity_class = class_<Identity>("Identity", "p≡p identity") auto identity_class = class_<Identity>("Identity", "p≡p identity")
.def(init<string>())
.def(init<string, string>())
.def(init<string, string, string>())
.def(init<string, string, string, string>())
.def(init<string, string, string, string, int>())
.def(init<string, string, string, string, int, string>())
.def("__repr__", &Identity::_repr)
.add_property("address", (string(Identity::*)()) &Identity::address, .add_property("address", (string(Identity::*)()) &Identity::address,
(void(Identity::*)(string)) &Identity::address, (void(Identity::*)(string)) &Identity::address,
"email address or URI") "email address or URI")
@ -72,6 +79,9 @@ BOOST_PYTHON_MODULE(pEp)
auto blob_class = class_<Message::Blob>("Blob", "Binary large object", auto blob_class = class_<Message::Blob>("Blob", "Binary large object",
init< object, char const*, char const* >(args("data", "mime_type", "filename"), init< object, char const*, char const* >(args("data", "mime_type", "filename"),
"init buffer with binary data") ) "init buffer with binary data") )
.def(init<object, string>())
.def(init<object>())
.def("__repr__", &Message::Blob::_repr)
.add_property("mime_type", (string(Message::Blob::*)()) &Message::Blob::mime_type, .add_property("mime_type", (string(Message::Blob::*)()) &Message::Blob::mime_type,
(void(Message::Blob::*)(string)) &Message::Blob::mime_type, (void(Message::Blob::*)(string)) &Message::Blob::mime_type,
"MIME type of object in Blob") "MIME type of object in Blob")
@ -148,8 +158,8 @@ BOOST_PYTHON_MODULE(pEp)
(void(Message::*)(PEP_enc_format)) &Message::enc_format, (void(Message::*)(PEP_enc_format)) &Message::enc_format,
"0: unencrypted, 1: inline PGP, 2: S/MIME, 3: PGP/MIME, 4: p≡p format"); "0: unencrypted, 1: inline PGP, 2: S/MIME, 3: PGP/MIME, 4: p≡p format");
PyModuleDef * def = PyModule_GetDef(scope().ptr()); PyModuleDef * _def = PyModule_GetDef(scope().ptr());
def->m_free = free_module; _def->m_free = free_module;
PEP_STATUS status = ::init(&session); PEP_STATUS status = ::init(&session);
if (status != PEP_STATUS_OK) { if (status != PEP_STATUS_OK) {

2
src/pEpmodule.hh

@ -5,8 +5,6 @@
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std;
const char *version_string = "p≡p Python adapter version 0.1"; const char *version_string = "p≡p Python adapter version 0.1";
extern PEP_SESSION session; extern PEP_SESSION session;
} }

13
src/str_attr.cc

@ -8,6 +8,19 @@ namespace pEp {
using namespace std; using namespace std;
using namespace boost::locale; 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) string str_attr(char *&str)
{ {
if (!str) if (!str)

3
src/str_attr.hh

@ -11,6 +11,9 @@ namespace pEp {
using namespace std; using namespace std;
using namespace boost::python; using namespace boost::python;
object repr(object s);
string repr(string s);
string str_attr(char *&str); string str_attr(char *&str);
void str_attr(char *&str, string value); void str_attr(char *&str, string value);

Loading…
Cancel
Save