From d41418272136def42a408d0e2cc44e0d6327924a Mon Sep 17 00:00:00 2001 From: John Zhao Date: Fri, 18 May 2018 00:16:04 +0800 Subject: [PATCH] Add api create to python wrapper --- wrappers/python/README.md | 2 +- wrappers/python/samples/mynteye.py | 37 ++++++++++ wrappers/python/src/mynteye_py.cc | 111 ++++++++++++++++++++++++++++- 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 wrappers/python/samples/mynteye.py diff --git a/wrappers/python/README.md b/wrappers/python/README.md index a7555b4..092da4d 100644 --- a/wrappers/python/README.md +++ b/wrappers/python/README.md @@ -14,5 +14,5 @@ make python ## Run ```bash -python +python wrappers/python/samples/mynteye.py ``` diff --git a/wrappers/python/samples/mynteye.py b/wrappers/python/samples/mynteye.py new file mode 100644 index 0000000..593720b --- /dev/null +++ b/wrappers/python/samples/mynteye.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright 2018 Slightech Co., Ltd. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# pylint: disable=missing-docstring + +from __future__ import print_function + +import os +import sys + +PY_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.append(os.path.join(PY_DIR, '_output/lib')) + +import mynteye_py # pylint: disable=import-error,wrong-import-position +# glog_init = mynteye_py.glog_init.create(sys.argv) + + +def main(): + # api = mynteye_py.api.create() # should glog_init + api = mynteye_py.api.create(sys.argv) # pylint: disable=unused-variable + + +if __name__ == '__main__': + main() diff --git a/wrappers/python/src/mynteye_py.cc b/wrappers/python/src/mynteye_py.cc index eac12b6..d966d49 100644 --- a/wrappers/python/src/mynteye_py.cc +++ b/wrappers/python/src/mynteye_py.cc @@ -1,5 +1,114 @@ +// Copyright 2018 Slightech Co., Ltd. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include +#include + +#include + +#include "mynteye/api.h" +#include "mynteye/glog_init.h" + +namespace { + +template +inline void std_vector_assign( + std::vector &l, const boost::python::object &o) { // NOLINT + l.assign( + boost::python::stl_input_iterator(o), + boost::python::stl_input_iterator()); +} + +template +inline std::vector py_list_to_std_vector(const boost::python::object &o) { + return std::vector( + boost::python::stl_input_iterator(o), + boost::python::stl_input_iterator()); +} + +template +inline boost::python::list std_vector_to_py_list(const std::vector &v) { + boost::python::list l; + for (auto &&val : v) { + l.append(val); + } + return l; +} + +template +char **new_cstrings(const Container &strings, std::size_t n) { + char **cstrings = new char *[n]; + for (std::size_t i = 0; i < n; i++) { + cstrings[i] = new char[strings[i].size() + 1]; + std::strcpy(cstrings[i], strings[i].c_str()); // NOLINT + } + return cstrings; +} + +void del_cstrings(char **cstrings, std::size_t n) { + for (std::size_t i = 0; i < n; i++) { + delete[] cstrings[i]; + } + delete[] cstrings; +} + +} // namespace using namespace boost::python; // NOLINT -BOOST_PYTHON_MODULE(mynteye_py) {} +MYNTEYE_USE_NAMESPACE + +// api create static methods + +std::shared_ptr (*api_create_1)() = &API::Create; + +std::shared_ptr api_create_2(list argv) { + auto &&args = py_list_to_std_vector(argv); + auto &&n = args.size(); + if (n == 0) { + return API::Create(); + } + char **cstrings = new_cstrings(args, n); + auto &&api = API::Create(args.size(), cstrings); + del_cstrings(cstrings, n); + return api; +} + +// glog_init create static methods + +std::shared_ptr glog_init_create(list argv) { + auto &&args = py_list_to_std_vector(argv); + auto &&n = args.size(); + assert(n > 0); + char **cstrings = new_cstrings(args, n); + auto &&ret = std::make_shared(args.size(), cstrings); + del_cstrings(cstrings, n); + return ret; +} + +// BOOST_PYTHON_MODULE + +BOOST_PYTHON_MODULE(mynteye_py) { + class_("api", no_init) + .def("create", api_create_1) + .def("create", &api_create_2) + .staticmethod("create"); + + register_ptr_to_python>(); + + class_("glog_init", no_init) + .def("create", &glog_init_create) + .staticmethod("create"); + + register_ptr_to_python>(); +}