Identity and Blob nicer

AllGroupDevicesShowHandshake
Volker Birk 7 years ago
parent b81c21aae6
commit 6a0db88991

@ -1,16 +1,21 @@
#include "Identity.hh"
#include <typeinfo>
#include <sstream>
#include <pEp/identity_list.h>
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 == "")

@ -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); }

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#include <sstream>
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;

@ -1,6 +1,7 @@
#pragma once
#include <boost/python.hpp>
#include <boost/lexical_cast.hpp>
#include <pEp/message.h>
#include <string>
#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); }

@ -40,6 +40,13 @@ BOOST_PYTHON_MODULE(pEp)
scope().attr("about") = about();
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,
(void(Identity::*)(string)) &Identity::address,
"email address or URI")
@ -72,6 +79,9 @@ BOOST_PYTHON_MODULE(pEp)
auto blob_class = class_<Message::Blob>("Blob", "Binary large object",
init< object, char const*, char const* >(args("data", "mime_type", "filename"),
"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,
(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) {

@ -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;
}

@ -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)

@ -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);

Loading…
Cancel
Save