Browse Source

Blobs

PYADPT-55
Volker Birk 5 years ago
parent
commit
6c173e26e6
6 changed files with 126 additions and 17 deletions
  1. +1
    -1
      setup.py
  2. +2
    -0
      src/Identity.hh
  3. +70
    -6
      src/message.cc
  4. +31
    -9
      src/message.hh
  5. +18
    -0
      src/pEpmodule.cc
  6. +4
    -1
      src/str_attr.cc

+ 1
- 1
setup.py View File

@ -10,7 +10,7 @@ module_pEp = Extension('pEp',
sources = glob('src/*.cc'),
include_dirs = [prefix+'/include', boost+'/include',],
library_dirs = [prefix+'/lib', boost+'/lib',],
libraries = ['pEpEngine', 'boost_python-mt',],
libraries = ['pEpEngine', 'boost_python-mt', 'boost_locale-mt',],
)
setup(


+ 2
- 0
src/Identity.hh View File

@ -8,6 +8,8 @@ namespace pEp {
namespace PythonAdapter {
using namespace utility;
// Identity is owning a pEp_identity
class Identity {
pEp_identity *_ident;


+ 70
- 6
src/message.cc View File

@ -1,19 +1,83 @@
#include <Python.h>
#include "message.hh"
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
namespace pEp {
namespace PythonAdapter {
using namespace std;
Message::Blob::Blob(bloblist_t *bl)
: data(bl->value, bl->value+bl->size)
Message::Blob::Blob(bloblist_t *bl) : _bl(bl), part_of_chain(false)
{
if (bl->mime_type)
mime_type = bl->mime_type;
if (bl->filename)
filename = bl->filename;
if (!_bl)
throw bad_alloc();
}
Message::Blob::Blob(object data) :
_bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false)
{
if (!_bl)
throw bad_alloc();
Py_buffer src;
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO);
if (result)
throw invalid_argument("need a contiguous buffer to read");
char *mem = (char *)malloc(src.len);
if (!mem) {
PyBuffer_Release(&src);
throw bad_alloc();
}
memcpy(mem, src.buf, src.len);
free(_bl->value);
_bl->size = src.len;
_bl->value = mem;
PyBuffer_Release(&src);
}
Message::Blob::Blob(const Message::Blob& second) :
_bl(bloblist_dup(second._bl)), part_of_chain(false)
{
if (!_bl)
throw bad_alloc();
}
Message::Blob::~Blob()
{
if (!part_of_chain) {
free(_bl->value);
free(_bl);
}
}
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) {
bloblist_t *bl = NULL;
try {
Message::Blob& blob = extract< Message::Blob& >(self);
bl = blob._bl;
}
catch (exception& e) {
PyErr_SetString(PyExc_RuntimeError, "extract not possible");
view->obj = NULL;
return -1;
}
if (!(bl && bl->value)) {
PyErr_SetString(PyExc_RuntimeError, "no data available");
view->obj = NULL;
return -1;
}
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags);
}
PyBufferProcs Message::Blob::bp = { getbuffer, NULL };
Message::Message(PEP_msg_direction dir)
: _msg(new_message(dir))
{


+ 31
- 9
src/message.hh View File

@ -1,28 +1,50 @@
#pragma once
#include <boost/python.hpp>
#include <pEp/message.h>
#include <string>
#include <list>
#include <vector>
#include <iterator>
#include "str_attr.hh"
namespace pEp {
namespace PythonAdapter {
using namespace utility;
using namespace boost::python;
class Message {
struct Blob {
vector<char> data;
string mime_type;
string filename;
Blob() { }
Blob(bloblist_t *bl);
operator bloblist_t *();
};
// Message is owning a message struct
class Message {
message *_msg;
public:
// Blob is owning a bloblist_t struct
class Blob {
bloblist_t *_bl;
bool part_of_chain;
public:
Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL));
Blob(object data);
Blob(const Blob& second);
~Blob();
string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; }
void mime_type(string value) { str_attr(_bl->mime_type, value); }
string filename() { return str_attr(_bl->filename); }
void filename(string value) { str_attr(_bl->filename, value); }
size_t size() { return _bl->size; }
static PyBufferProcs bp;
protected:
static int getbuffer(PyObject *self, Py_buffer *view, int flags);
};
Message(PEP_msg_direction dir = PEP_dir_outgoing);
Message(const Message& second);
Message(message *ident);


+ 18
- 0
src/pEpmodule.cc View File

@ -1,7 +1,9 @@
#include "pEpmodule.hh"
#include <boost/locale.hpp>
#include <string>
#include <pEp/pEpEngine.h>
#include "Identity.hh"
#include "Message.hh"
namespace pEp {
namespace PythonAdapter {
@ -19,10 +21,14 @@ namespace pEp {
BOOST_PYTHON_MODULE(pEp)
{
using namespace boost::python;
using namespace boost::locale;
using namespace pEp::PythonAdapter;
docstring_options doc_options(true, true, false);
generator gen;
std::locale::global(gen(""));
def("about", about, "delivers the p≡p about string");
class_<Identity>("Identity", "p≡p identity")
@ -51,5 +57,17 @@ BOOST_PYTHON_MODULE(pEp)
"true if own identity, false otherwise")
.add_property("flags", (identity_flags_t(Identity::*)()) &Identity::flags,
(void(Identity::*)(identity_flags_t)) &Identity::flags);
auto blob = class_<Message::Blob>("Blob", "Binary large object",
init< object >(args("data"), "init buffer with binary data") )
.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")
.add_property("filename", (string(Message::Blob::*)()) &Message::Blob::filename,
(void(Message::Blob::*)(string)) &Message::Blob::filename,
"filename of object in Blob")
.add_property("size", &Message::Blob::size, "size of Blob in bytes");
((PyTypeObject *)(void *)blob.ptr())->tp_as_buffer = &Message::Blob::bp;
}

+ 4
- 1
src/str_attr.cc View File

@ -1,14 +1,17 @@
#include "str_attr.hh"
#include <stdlib.h>
#include <boost/locale.hpp>
namespace pEp {
namespace utility {
using namespace std;
using namespace boost::locale;
void str_attr(char *&str, string value)
{
string normalized = normalize(value, norm_nfc);
free(str);
str = strdup(value.c_str());
str = strdup(normalized.c_str());
if (!str)
throw bad_alloc();
}


Loading…
Cancel
Save