From 6549160985eed3077ce07f4dce26fce96c34e7de Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Sat, 2 Mar 2019 13:41:16 +0800 Subject: [PATCH] Add python extensions to cpp libraries --- comfortable-swipe.cpp | 5 +-- comfortable-swipe.python.cpp | 82 ----------------------------------- comfortable_swipe/__init__.py | 0 lib/comfortable_swipe | 44 ------------------- lib/index.hpp | 68 ----------------------------- lib/service/autostart.cpp | 2 +- lib/service/buffer.cpp | 3 +- lib/service/config.cpp | 2 +- lib/service/help.cpp | 2 +- lib/service/restart.cpp | 2 +- lib/service/start.cpp | 1 - lib/service/status.cpp | 3 +- lib/util/conf_filename.cpp | 2 - setup.py | 26 ++++++----- 14 files changed, 24 insertions(+), 218 deletions(-) delete mode 100644 comfortable-swipe.python.cpp create mode 100644 comfortable_swipe/__init__.py delete mode 100644 lib/comfortable_swipe delete mode 100644 lib/index.hpp diff --git a/comfortable-swipe.cpp b/comfortable-swipe.cpp index 590b4a2..9c75fa2 100644 --- a/comfortable-swipe.cpp +++ b/comfortable-swipe.cpp @@ -19,16 +19,13 @@ along with this program. If not, see . #include // std::ios #include // std::cin, std::cout, std::cerr #include // std::string -#include "lib/comfortable_swipe" +#include "lib/comfortable-swipe" /* A FORWARD DECLARATION */ int main(int argc, char** args); -/* Import python module here */ -#include "comfortable-swipe.python.cpp" /* MAIN DRIVER FUNCTION */ - int main(int argc, char** args) { // improve buffering by decoupling loggers from stdio diff --git a/comfortable-swipe.python.cpp b/comfortable-swipe.python.cpp deleted file mode 100644 index 31dd3ea..0000000 --- a/comfortable-swipe.python.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* -Comfortable Swipe -by Rico Tiongson - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -/* Porting to Python */ -#ifdef __COMFORTABLE_SWIPE__PYTHON__ -#ifndef __COMFORTABLE_SWIPE__PYTHON_MAIN__ -#define __COMFORTABLE_SWIPE__PYTHON_MAIN__ - -#include -#include - -// some useful readable macros -// http://python3porting.com/cextensions.html -#if PY_MAJOR_VERSION >= 3 - #define MOD_ERROR_VAL NULL - #define MOD_SUCCESS_VAL(val) val - #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void) - #define MOD_DEF(ob, name, doc, methods) \ - static struct PyModuleDef moduledef = { \ - PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \ - ob = PyModule_Create(&moduledef); -#else - #define MOD_ERROR_VAL - #define MOD_SUCCESS_VAL(val) - #define MOD_INIT(name) void init##name(void) - #define MOD_DEF(ob, name, doc, methods) \ - ob = Py_InitModule3(name, methods, doc); -#endif /* PY_MAJOR_VERSION >= 3 */ - -static PyObject * -test(PyObject * self, PyObject * args, PyObject * kwargs) -{ - std::puts("TESTING FUNCTION"); - Py_RETURN_NONE; -} - - -// python module methods -static PyMethodDef comfortable_swipe_methods[] = { - /* The cast of the function is necessary since PyCFunction values - * only take two PyObject* parameters, and test() takes - * three. - */ - { - "test", - (PyCFunction) test, - METH_VARARGS | METH_KEYWORDS, - "Test" - }, - {NULL, NULL, 0, NULL} /* sentinel */ -}; - -// how the python module is defined -MOD_INIT(comfortable_swipe) -{ - PyObject *m; - - MOD_DEF(m, "comfortable_swipe", "Comfortable swipe", comfortable_swipe_methods); - - if (m == NULL) - return MOD_ERROR_VAL; - - return MOD_SUCCESS_VAL(m); -} - -#endif /* __COMFORTABLE_SWIPE__PYTHON_MAIN__ */ -#endif /* __COMFORTABLE_SWIPE__PYTHON__ */ \ No newline at end of file diff --git a/comfortable_swipe/__init__.py b/comfortable_swipe/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/comfortable_swipe b/lib/comfortable_swipe deleted file mode 100644 index 3ee260a..0000000 --- a/lib/comfortable_swipe +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __COMFORTABLE_SWIPE__ -#define __COMFORTABLE_SWIPE__ - -/* -Comfortable Swipe -by Rico Tiongson - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#include "index.hpp" - -/** - * Make sure to include all implementation (.cpp) files below to be ready for export. - */ - -#include "gesture/xdo_gesture.cpp" -#include "gesture/swipe_gesture.cpp" -#include "gesture/swipe_gesture.regex.cpp" -#include "service/autostart.cpp" -#include "service/buffer.cpp" -#include "service/config.cpp" -#include "service/debug.cpp" -#include "service/help.cpp" -#include "service/restart.cpp" -#include "service/start.cpp" -#include "service/status.cpp" -#include "service/stop.cpp" -#include "util/autostart_filename.cpp" -#include "util/conf_filename.cpp" -#include "util/read_config_file.cpp" - -#endif /* __COMFORTABLE_SWIPE__ */ diff --git a/lib/index.hpp b/lib/index.hpp deleted file mode 100644 index 20d3981..0000000 --- a/lib/index.hpp +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef __COMFORTABLE_SWIPE__index_hpp__ -#define __COMFORTABLE_SWIPE__index_hpp__ - -/* -Comfortable Swipe -by Rico Tiongson - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -// global defines -#ifndef __COMFORTABLE_SWIPE__PROGRAM__ -#define __COMFORTABLE_SWIPE__PROGRAM__ "/usr/local/bin/comfortable-swipe" -#endif /* __COMFORTABLE_SWIPE__PROGRAM__ */ - -#ifndef __COMFORTABLE_SWIPE__CONFIG__ -#define __COMFORTABLE_SWIPE__CONFIG__ "/usr/local/share/comfortable-swipe/comfortable-swipe.conf" -#endif /* __COMFORTABLE_SWIPE__CONFIG__ */ - -#ifndef __COMFORTABLE_SWIPE__VERSION__ -#error __COMFORTABLE_SWIPE__VERSION__ must be defined. -#endif /* __COMFORTABLE_SWIPE__VERSION__ */ - -#include // std::map -#include // std::string - -/** - * Make sure to include your header files here so that they can be imported by other modules. - */ -#include "gesture/xdo_gesture.h" -#include "gesture/swipe_gesture.h" -extern "C" -{ - namespace comfortable_swipe - { - namespace util - { - const char* autostart_filename(); - constexpr const char* conf_filename(); - std::map read_config_file(const char*); - } - namespace service - { - void autostart(); - void buffer(); - void config(); - void debug(); - void help(); - void restart(); - void start(); - void status(); - void stop(); - } - } -} - -#endif /* __COMFORTABLE_SWIPE__index_hpp__ */ diff --git a/lib/service/autostart.cpp b/lib/service/autostart.cpp index 1a4575f..1f901fa 100644 --- a/lib/service/autostart.cpp +++ b/lib/service/autostart.cpp @@ -19,12 +19,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "../util/__index__.hpp" #include // std::cerr, std::cout, std::endl #include // std::ifstream, std::ofstream #include // std::string #include // std::remove #include // std::system -#include "../index.hpp" namespace comfortable_swipe::service { diff --git a/lib/service/buffer.cpp b/lib/service/buffer.cpp index 3c7064d..e183111 100644 --- a/lib/service/buffer.cpp +++ b/lib/service/buffer.cpp @@ -19,8 +19,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "../util/__index__.hpp" +#include "../gesture/__index__.hpp" #include // fgets_unlocked, stdin -#include "../index.hpp" /** * Starts the comfortable-swipe service by buffering libinput debug-events. diff --git a/lib/service/config.cpp b/lib/service/config.cpp index 6bb02bd..db2cff5 100644 --- a/lib/service/config.cpp +++ b/lib/service/config.cpp @@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "../index.hpp" +#include "../util/__index__.hpp" #include // std::puts namespace comfortable_swipe::service diff --git a/lib/service/help.cpp b/lib/service/help.cpp index 80c2c85..b3e5145 100644 --- a/lib/service/help.cpp +++ b/lib/service/help.cpp @@ -19,8 +19,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "../util/__index__.hpp" #include // std::puts, std::printf -#include "../index.hpp" namespace comfortable_swipe::service { diff --git a/lib/service/restart.cpp b/lib/service/restart.cpp index 28191c8..f4348f4 100644 --- a/lib/service/restart.cpp +++ b/lib/service/restart.cpp @@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "../index.hpp" +#include "../service/__index__.hpp" namespace comfortable_swipe::service { diff --git a/lib/service/start.cpp b/lib/service/start.cpp index 9ab566e..859c708 100644 --- a/lib/service/start.cpp +++ b/lib/service/start.cpp @@ -19,7 +19,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "../index.hpp" #include // std::system namespace comfortable_swipe::service diff --git a/lib/service/status.cpp b/lib/service/status.cpp index a084de4..0eb3be5 100644 --- a/lib/service/status.cpp +++ b/lib/service/status.cpp @@ -19,7 +19,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "../index.hpp" +#include "../util/__index__.hpp" +#include "../gesture/__index__.hpp" #include // std::runtime_error #include // popen, pclose, getpid, access, F_OK #include // std::unique_ptr diff --git a/lib/util/conf_filename.cpp b/lib/util/conf_filename.cpp index 5de5ae9..284a79f 100644 --- a/lib/util/conf_filename.cpp +++ b/lib/util/conf_filename.cpp @@ -19,8 +19,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "../index.hpp" - namespace comfortable_swipe::util { /** diff --git a/setup.py b/setup.py index 91a4bb3..9254b4e 100644 --- a/setup.py +++ b/setup.py @@ -7,14 +7,16 @@ __DIR__ = os.path.abspath(os.path.dirname(__file__)) __URL__ = 'https://github.com/Hikari9/comfortable-swipe-ubuntu' NAME = 'comfortable-swipe' +PYTHON_NAME = NAME.replace('-', '_') VERSION = '1.1.0' PROGRAM = os.path.join('/usr/local/bin', NAME) -CONFIG = os.path.join(PROGRAM, 'comfortable-swipe.conf') +CONFIG = os.path.join('/usr/local/share', NAME, NAME + '.conf') # for C++ library cpp_sources = ['comfortable-swipe.cpp'] cpp_macros = dict( __COMFORTABLE_SWIPE__PYTHON__='', + __COMFORTABLE_SWIPE__BOOST_PYTHON__='', __COMFORTABLE_SWIPE__PYTHON_MODULE_NAME__='"{}"'.format(NAME.replace('-', '_')), __COMFORTABLE_SWIPE__PROGRAM__='"{}"'.format(PROGRAM), __COMFORTABLE_SWIPE__VERSION__='"{}"'.format(VERSION), @@ -33,16 +35,18 @@ try: with open('LICENSE', 'r') as LICENSE_file: LICENSE = LICENSE_file.read() - # read C++ library for comfortable swipe - comfortable_swipe = Extension( - name=NAME.replace('-', '_'), - define_macros=list(cpp_macros.items()), - libraries=['xdo'], - include_dirs=['/usr/local/lib'], - sources=cpp_sources, - extra_compile_args=['-O2', '-Wno-unused-result'] + # read C++ libraries for comfortable swipe + extension_names = ['gesture', 'service', 'util'] + extensions = [] - ) + for extension_name in extension_names: + extensions.append(Extension( + name='{}.{}'.format(PYTHON_NAME, extension_name), + define_macros=list(cpp_macros.items()), + sources=['lib/__index__.cpp'], + extra_compile_args=['-O2', '-Wno-unused-result'], + libraries=['xdo', 'boost_python'] + )) # setup python script setup( @@ -56,7 +60,7 @@ try: url=__URL__, # import external modules (aka. C++) packages=find_packages(), - ext_modules=[comfortable_swipe] + ext_modules=extensions )