Add working initial python porting definition for comfortable-swipe

This commit is contained in:
Rico Tiongson 2019-03-02 10:47:34 +08:00
parent da101a092a
commit 784577ebb9
3 changed files with 105 additions and 12 deletions

View File

@ -21,6 +21,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <string> // std::string #include <string> // 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 */ /* MAIN DRIVER FUNCTION */
int main(int argc, char** args) int main(int argc, char** args)

View File

@ -0,0 +1,82 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
/* Porting to Python */
#ifdef __COMFORTABLE_SWIPE__PYTHON__
#ifndef __COMFORTABLE_SWIPE__PYTHON_MAIN__
#define __COMFORTABLE_SWIPE__PYTHON_MAIN__
#include <iostream>
#include <Python.h>
// 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__ */

View File

@ -1,5 +1,5 @@
import os import os
from setuptools import setup from setuptools import setup, find_packages
from setuptools.extension import Extension from setuptools.extension import Extension
__CWD__ = os.getcwd() __CWD__ = os.getcwd()
@ -11,6 +11,16 @@ VERSION = '1.1.0'
PROGRAM = os.path.join('/usr/local/bin', NAME) PROGRAM = os.path.join('/usr/local/bin', NAME)
CONFIG = os.path.join(PROGRAM, 'comfortable-swipe.conf') CONFIG = os.path.join(PROGRAM, 'comfortable-swipe.conf')
# for C++ library
cpp_sources = ['comfortable-swipe.cpp']
cpp_macros = dict(
__COMFORTABLE_SWIPE__PYTHON__='',
__COMFORTABLE_SWIPE__PYTHON_MODULE_NAME__='"{}"'.format(NAME.replace('-', '_')),
__COMFORTABLE_SWIPE__PROGRAM__='"{}"'.format(PROGRAM),
__COMFORTABLE_SWIPE__VERSION__='"{}"'.format(VERSION),
__COMFORTABLE_SWIPE__CONFIG__='"{}"'.format(CONFIG)
)
try: try:
# make sure working directory is here # make sure working directory is here
os.chdir(__DIR__) os.chdir(__DIR__)
@ -23,20 +33,13 @@ try:
with open('LICENSE', 'r') as LICENSE_file: with open('LICENSE', 'r') as LICENSE_file:
LICENSE = LICENSE_file.read() LICENSE = LICENSE_file.read()
# have a dictionary of macros
macros = dict(
__COMFORTABLE_SWIPE__PROGRAM__=f'"{PROGRAM}"',
__COMFORTABLE_SWIPE__VERSION__=f'"{VERSION}"',
__COMFORTABLE_SWIPE__CONFIG__=f'"{CONFIG}"'
)
# read C++ library for comfortable swipe # read C++ library for comfortable swipe
comfortable_swipe = Extension( comfortable_swipe = Extension(
name='comfortable_swipe', name=NAME.replace('-', '_'),
define_macros=list(macros.items()), define_macros=list(cpp_macros.items()),
libraries=['xdo'], libraries=['xdo'],
include_dirs=['/usr/local/lib'], include_dirs=['/usr/local/lib'],
sources=['comfortable-swipe.cpp'], sources=cpp_sources,
extra_compile_args=['-O2', '-Wno-unused-result'] extra_compile_args=['-O2', '-Wno-unused-result']
) )
@ -52,9 +55,11 @@ try:
author_email='thericotiongson@gmail.com', author_email='thericotiongson@gmail.com',
url=__URL__, url=__URL__,
# import external modules (aka. C++) # import external modules (aka. C++)
ext_modules=[comfortable_swipe], packages=find_packages(),
ext_modules=[comfortable_swipe]
) )
finally: finally:
# move working directory back to where it was before # move working directory back to where it was before
os.chdir(__DIR__) os.chdir(__DIR__)