mirror of
https://github.com/GoldenCheetah/GoldenCheetah.git
synced 2026-02-15 17:09:56 +00:00
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:
@@ -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__"));
|
||||
|
||||
Reference in New Issue
Block a user