From 5076586aee70f353b0cf1af25c100938b383f23f Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 6 Aug 2024 21:22:01 -0400 Subject: [PATCH] add yaze_python module --- src/CMakeLists.txt | 1 + src/py/CMakeLists.txt | 20 ++++++++++++++++++++ src/py/yaze_python.cc | 18 ++++++++++++++++++ src/py/yaze_python.h | 21 +++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 src/py/CMakeLists.txt create mode 100644 src/py/yaze_python.cc create mode 100644 src/py/yaze_python.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 81fd937b..7a671305 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -74,6 +74,7 @@ if(APPLE) endif() include(app/CMakeLists.txt) +include(py/CMakeLists.txt) # include(cli/CMakeLists.txt) Excluded for now, macOS include breaks action build if (UNIX) diff --git a/src/py/CMakeLists.txt b/src/py/CMakeLists.txt new file mode 100644 index 00000000..2a6a1af7 --- /dev/null +++ b/src/py/CMakeLists.txt @@ -0,0 +1,20 @@ +find_package(Python3 COMPONENTS Interpreter Development REQUIRED) + +add_library( + yaze_python + py/yaze_python.cc +) + +set(PYTHON_HEADERS /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers) + +target_include_directories( + yaze_python PUBLIC + lib/ + app/ + ${PYTHON_HEADERS} +) + +target_link_libraries( + yaze_python PUBLIC + Python +) \ No newline at end of file diff --git a/src/py/yaze_python.cc b/src/py/yaze_python.cc new file mode 100644 index 00000000..77ce79dc --- /dev/null +++ b/src/py/yaze_python.cc @@ -0,0 +1,18 @@ +#include "yaze_python.h" + +#include + +static PyObject *SpamError; + +static PyObject *yaze_init(PyObject *self, PyObject *args) { + const char *command; + int sts; + + if (!PyArg_ParseTuple(args, "s", &command)) return NULL; + sts = system(command); + if (sts < 0) { + PyErr_SetString(SpamError, "System command failed"); + return NULL; + } + return PyLong_FromLong(sts); +} \ No newline at end of file diff --git a/src/py/yaze_python.h b/src/py/yaze_python.h new file mode 100644 index 00000000..8b000b7d --- /dev/null +++ b/src/py/yaze_python.h @@ -0,0 +1,21 @@ +#ifndef YAZE_PYTHON_H +#define YAZE_PYTHON_H + +#include + +static PyObject *yaze_init(PyObject *self, PyObject *args); + +static PyMethodDef YazeMethods[] = { + {"system", yaze_init, METH_VARARGS, "Initialize the yaze lib."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +static struct PyModuleDef yaze_module = {PyModuleDef_HEAD_INIT, + "yaze", // Module title + NULL, // Documentation + -1, // Interpreter state size + YazeMethods}; + +PyMODINIT_FUNC PyInit_yaze(void) { return PyModule_Create(&yaze_module); } + +#endif // YAZE_PYTHON_H \ No newline at end of file