Python Module Framework

.. Using SIP thats used in PyQt et al we have a module
   called `goldencheetah' which includes bindings.

   Currently there is only a single method `getValue()'
   that returns 1. It's to get the basic plumbing in place.

   src/Python/SIP contains all the files related to the
   module. The cpp files are generated by the `sip' utility
   which will need to be available if you want to work on the
   bindings. Run make -f Makefile.hack to regenerate the cpp
   files if you edit them.

   I prefer to distribute the generated files at this point
   whilst development occurs. We may change that at a later
   date.

.. Please note that the gcconfig.pri.in file has changed as
   we now include the python include path rather than set a
   macro for the include directive (See PYTHONINCLUDES in
   gcconfig.pri.in)

.. lastly, to test this is working in a python chart console:
   > print(GC.getValue())
   1
   >
This commit is contained in:
Mark Liversedge
2017-12-02 16:26:06 +00:00
parent 18c43fcebd
commit 00144effd8
11 changed files with 596 additions and 10 deletions

View File

@@ -30,12 +30,17 @@
#ifdef slots // clashes with python headers
#undef slots
#endif
#include GC_PYTHONHEADER
#include <Python.h>
// global instance of embedded python
PythonEmbed *python;
PyThreadState *mainThreadState;
// SIP module with GoldenCheetah Bindings
extern "C" {
extern PyObject *PyInit_goldencheetah(void);
};
PythonEmbed::~PythonEmbed()
{
}
@@ -49,6 +54,10 @@ PythonEmbed::PythonEmbed(const bool verbose, const bool interactive) : verbose(v
// tell python our program name
Py_SetProgramName((wchar_t*) name.toStdString().c_str());
// our own module
int rc= PyImport_AppendInittab("goldencheetah", PyInit_goldencheetah);
fprintf(stderr, "Add import module GoldenCheetah rc=%d\n", rc);
// need to load the interpreter etc
Py_InitializeEx(0);
@@ -63,7 +72,8 @@ PythonEmbed::PythonEmbed(const bool verbose, const bool interactive) : verbose(v
fprintf(stderr, "Python loaded [%s]\n", version.toStdString().c_str());
// our base code - currently just traps stdout
// our base code - traps stdout and loads goldencheetan module
// mapping all the bindings to a GC object.
std::string stdOutErr = ("import sys\n"
"class CatchOutErr:\n"
" def __init__(self):\n"
@@ -72,11 +82,14 @@ PythonEmbed::PythonEmbed(const bool verbose, const bool interactive) : verbose(v
" self.value += txt\n"
"catchOutErr = CatchOutErr()\n"
"sys.stdout = catchOutErr\n"
"sys.stderr = catchOutErr\n");
"sys.stderr = catchOutErr\n"
"import goldencheetah\n"
"GC=goldencheetah.Bindings()\n");
PyRun_SimpleString(stdOutErr.c_str()); //invoke code to redirect
// our own module
// setup trapping of output
PyObject *pModule = PyImport_AddModule("__main__"); //create main module
catcher = static_cast<void*>(PyObject_GetAttrString(pModule,"catchOutErr"));
clear = static_cast<void*>(PyObject_GetAttrString(static_cast<PyObject*>(catcher), "__init__"));