diff --git a/setup.py b/setup.py index ee8fa96..9637443 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,16 @@ # -*- coding: utf-8 -*- from distutils.core import setup, Extension +from glob import glob + +prefix = '/Users/vb' +boost = '/opt/local' module_pEp = Extension('pEp', - sources = ['src/pEpmodule.cc',], - include_dirs = ['/Users/vb/include',], - library_dirs = ['/Users/vb/lib',], - libraries = ['pEpEngine',], + sources = glob('src/*.cc'), + include_dirs = [prefix+'/include', boost+'/include',], + library_dirs = [prefix+'/lib', boost+'/lib',], + libraries = ['pEpEngine', 'boost_python-mt',], ) setup( diff --git a/src/Identity.cc b/src/Identity.cc new file mode 100644 index 0000000..7f3f1d8 --- /dev/null +++ b/src/Identity.cc @@ -0,0 +1,50 @@ +#include "Identity.hh" +#include + +namespace pEp { + namespace PythonAdapter { + using namespace std; + + Identity::Identity() + : _ident(new_identity(NULL, NULL, NULL, NULL)) + { + if (!_ident) + throw bad_alloc(); + } + + Identity::Identity(Identity& second) + : _ident(identity_dup(second._ident)) + { + if (!_ident) + throw bad_alloc(); + } + + Identity::~Identity() + { + free_identity(_ident); + } + + Identity::operator pEp_identity *() + { + if (!_ident) + throw bad_cast(); + return _ident; + } + + void Identity::lang(string value) + { + if (value == "") + memset(_ident->lang, 0, 3); + else if (value.length() != 2) + throw length_error("length of lang must be 2"); + else + memcpy(_ident->lang, value.data(), 2); + } + + string Identity::lang() + { + return _ident->lang; + } + } +} + diff --git a/src/Identity.hh b/src/Identity.hh new file mode 100644 index 0000000..69be7da --- /dev/null +++ b/src/Identity.hh @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include "str_attr.hh" + +namespace pEp { + namespace PythonAdapter { + using namespace utility; + + class Identity { + pEp_identity *_ident; + + public: + Identity(); + Identity(Identity& second); + ~Identity(); + operator pEp_identity *(); + + void address(string value) { str_attr(_ident->address, value); } + string address() { return str_attr(_ident->address); } + + void fpr(string value) { str_attr(_ident->fpr, value); } + string fpr() { return str_attr(_ident->fpr); } + + void user_id(string value) { str_attr(_ident->user_id, value); } + string user_id() { return str_attr(_ident->user_id); } + + void username(string value) { str_attr(_ident->username, value); } + string username() { return str_attr(_ident->username); } + + void comm_type(PEP_comm_type value) { _ident->comm_type = value; }; + PEP_comm_type comm_type() { return _ident->comm_type; } + + void lang(std::string value); + std::string lang(); + + void me(bool value) { _ident->me = value; } + bool me() { return _ident->me; } + + void me(identity_flags_t flags) { _ident->flags = flags; } + identity_flags_t flags() { return _ident->flags; } + }; + } +} + diff --git a/src/converting.cc b/src/converting.cc deleted file mode 100644 index 3ab9f5b..0000000 --- a/src/converting.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include "converting.hh" -#include -#include - -namespace pEp { - namespace utility { - using namespace std; - - } -} - diff --git a/src/converting.hh b/src/converting.hh deleted file mode 100644 index 0d54fa2..0000000 --- a/src/converting.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace pEp { - namespace utility { - - } -} - diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index d219e2d..5cee265 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -1,34 +1,24 @@ #include "pEpmodule.hh" #include +#include namespace pEp { namespace PythonAdapter { using namespace std; - PyObject *about(PyObject *self, PyObject *args) + string about(void) { string version = string(version_string) + "\np≡p version " + PEP_VERSION + "\n"; - return PyUnicode_FromString(version.c_str()); + return version; } } - - PEP_SESSION session; - - void module_free(void *) - { - release(session); - } } -using namespace pEp; - -PyMODINIT_FUNC PyInit_pEp(void) +BOOST_PYTHON_MODULE(pEp) { - PEP_STATUS status = init(&session); - if (status != PEP_STATUS_OK) - return NULL; - - return PyModule_Create(&pEpmodule); + using namespace boost::python; + using namespace pEp::PythonAdapter; + def("about", about); } diff --git a/src/pEpmodule.hh b/src/pEpmodule.hh index 3e4d042..5e1c59e 100644 --- a/src/pEpmodule.hh +++ b/src/pEpmodule.hh @@ -1,36 +1,12 @@ #pragma once -#include -#include +#include namespace pEp { namespace PythonAdapter { + using namespace std; const char *version_string = "p≡p Python adapter version 0.1"; - - PyObject *about(PyObject *self, PyObject *args); + string about(void); } - - void module_free(void *); - - struct PyMethodDef pEpMethods[] = { - {"about", pEp::PythonAdapter::about, METH_VARARGS, "about p≡p"}, - {NULL, NULL, 0, NULL} - }; - - struct PyModuleDef pEpmodule = { - PyModuleDef_HEAD_INIT, - "pEp", - "p≡p Python adapter", - -1, - pEpMethods, - NULL, - NULL, - NULL, - pEp::module_free - }; - - extern PEP_SESSION session; } -PyMODINIT_FUNC PyInit_pEp(void); - diff --git a/src/str_attr.cc b/src/str_attr.cc new file mode 100644 index 0000000..639be0d --- /dev/null +++ b/src/str_attr.cc @@ -0,0 +1,24 @@ +#include "str_attr.hh" +#include + +namespace pEp { + namespace utility { + using namespace std; + + void str_attr(char *&str, string value) + { + free(str); + str = strdup(value.c_str()); + if (!str) + throw bad_alloc(); + } + + string str_attr(char *&str) + { + if (!str) + return string(""); + return string(str); + } + } +} + diff --git a/src/str_attr.hh b/src/str_attr.hh new file mode 100644 index 0000000..c68e83c --- /dev/null +++ b/src/str_attr.hh @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace pEp { + namespace utility { + using namespace std; + + void str_attr(char *&str, string value); + string str_attr(char *&str); + } +} +