diff options
author | mgsergio <mgsergio@yandex.ru> | 2017-04-04 17:54:02 +0300 |
---|---|---|
committer | Sergey Yershov <syershov@maps.me> | 2017-04-04 17:54:02 +0300 |
commit | b5b35c2f3ca0e575506c3cba2b2879d5a16276b6 (patch) | |
tree | 8783b05e2490ed83b9ac346c4cea53ef8bd1986d /local_ads/pylocal_ads | |
parent | cec1817da3845ae94665dfe294d824766802972d (diff) |
Add Campaign serialization. Add python bindings.
Diffstat (limited to 'local_ads/pylocal_ads')
-rw-r--r-- | local_ads/pylocal_ads/CMakeLists.txt | 18 | ||||
-rw-r--r-- | local_ads/pylocal_ads/bindings.cpp | 52 |
2 files changed, 70 insertions, 0 deletions
diff --git a/local_ads/pylocal_ads/CMakeLists.txt b/local_ads/pylocal_ads/CMakeLists.txt new file mode 100644 index 0000000000..c26a5896be --- /dev/null +++ b/local_ads/pylocal_ads/CMakeLists.txt @@ -0,0 +1,18 @@ +project(pylocal_ads) + +set( + SRC + bindings.cpp +) + +add_library(${PROJECT_NAME} MODULE ${SRC}) + +omim_link_libraries( + ${PROJECT_NAME} + ${PYTHON_LIBRARIES} + ${Boost_LIBRARIES} + base + local_ads +) + +set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") diff --git a/local_ads/pylocal_ads/bindings.cpp b/local_ads/pylocal_ads/bindings.cpp new file mode 100644 index 0000000000..a3639f64c6 --- /dev/null +++ b/local_ads/pylocal_ads/bindings.cpp @@ -0,0 +1,52 @@ +#include "local_ads/campaign.hpp" +#include "local_ads/campaign_serialization.hpp" + +// This header should be included due to a python compilation error. +// pyport.h overwrites defined macros and replaces it with its own. +// However, in the OS X c++ libraries, these are not macros but functions, +// hence the error. See https://bugs.python.org/issue10910 +#include <locale> + +#include "pyhelpers/vector_uint8.hpp" +#include "pyhelpers/vector_list_conversion.hpp" + +#include <boost/python.hpp> +#include <boost/python/suite/indexing/vector_indexing_suite.hpp> + +using namespace local_ads; + +namespace +{ +std::vector<uint8_t> PySerialize(boost::python::list const & cs) +{ + auto const campaigns = python_list_to_std_vector<Campaign>(cs); + return Serialize(campaigns); +} + +boost::python::list PyDeserialize(std::vector<uint8_t> const & blob) +{ + auto const campaigns = Deserialize(blob); + return std_vector_to_python_list(campaigns); +} +} // namespace + +BOOST_PYTHON_MODULE(pylocal_ads) +{ + using namespace boost::python; + + // Register the to-python converters. + to_python_converter<std::vector<uint8_t>, vector_uint8t_to_str>(); + vector_uint8t_from_python_str(); + + class_<Campaign>("Campaign", init<uint32_t, uint16_t, uint8_t, bool>()) + .def_readonly("m_featureId", &Campaign::m_featureId) + .def_readonly("m_iconId", &Campaign::m_iconId) + .def_readonly("m_daysBeforeExpired", &Campaign::m_daysBeforeExpired) + .def_readonly("m_priorityBit", &Campaign::m_priorityBit); + + class_<std::vector<Campaign>>("CampaignList") + .def(vector_indexing_suite<std::vector<Campaign>>()); + + def("serialize", PySerialize); + def("deserialize", PyDeserialize); +} |