|
GnuCash 2.4.99
|
00001 /********************************************************************* 00002 * gncmod-python.c 00003 * Python in GnuCash?! Sweet. 00004 * 00005 * Copyright (c) 2011 Andy Clayton 00006 *********************************************************************/ 00007 00008 #include <Python.h> 00009 #include "config.h" 00010 #include <gmodule.h> 00011 #include <stdio.h> 00012 00013 #include "gnc-module.h" 00014 #include "gnc-module-api.h" 00015 #include "gnc-path.h" 00016 00017 GNC_MODULE_API_DECL(libgncmod_python) 00018 00019 /* version of the gnc module system interface we require */ 00020 int libgncmod_python_gnc_module_system_interface = 0; 00021 00022 /* module versioning uses libtool semantics. */ 00023 int libgncmod_python_gnc_module_current = 0; 00024 int libgncmod_python_gnc_module_revision = 0; 00025 int libgncmod_python_gnc_module_age = 0; 00026 00027 00028 char * 00029 libgncmod_python_gnc_module_path(void) 00030 { 00031 return g_strdup("gnucash/python"); 00032 } 00033 00034 char * 00035 libgncmod_python_gnc_module_description(void) 00036 { 00037 return g_strdup("An embedded Python interpreter"); 00038 } 00039 00040 #if PY_VERSION_HEX >= 0x03000000 00041 extern PyObject* PyInit__sw_app_utils(void); 00042 extern PyObject* PyInit__sw_core_utils(void); 00043 #else 00044 extern void init_sw_app_utils(void); 00045 extern void init_sw_core_utils(void); 00046 #endif 00047 00048 int 00049 libgncmod_python_gnc_module_init(int refcount) 00050 { 00051 PyObject *pName, *pModule; 00052 FILE *fp; 00053 gchar *pkgdatadir, *init_filename; 00054 00055 Py_Initialize(); 00056 #if PY_VERSION_HEX >= 0x03000000 00057 PyInit__sw_app_utils(); 00058 PyInit__sw_core_utils(); 00059 #else 00060 init_sw_app_utils(); 00061 init_sw_core_utils(); 00062 #endif 00063 00064 /* 00065 pName = PyString_FromString("path/to/init.py"); 00066 pModule = PyImport_Import(pName); 00067 00068 if (!pModule) { 00069 PyErr_Print(); 00070 return FALSE; 00071 } 00072 00073 Py_DECREF(pName); 00074 Py_DECREF(pModule); 00075 */ 00076 00077 pkgdatadir = gnc_path_get_pkgdatadir(); 00078 init_filename = g_build_filename(pkgdatadir, "python/init.py", (char*)NULL); 00079 g_debug("Looking for python init script at %s", (init_filename ? init_filename : "<null>")); 00080 fp = fopen(init_filename, "r+"); 00081 if (fp) 00082 { 00083 PyRun_SimpleFile(fp, init_filename); 00084 fclose(fp); 00085 00086 /* PyRun_InteractiveLoop(stdin, "foo"); */ 00087 } 00088 else 00089 { 00090 g_warning("Unable to initialize Python module (unable to open %s)", init_filename); 00091 } 00092 g_free(init_filename); 00093 g_free(pkgdatadir); 00094 00095 return TRUE; 00096 } 00097 00098 int 00099 libgncmod_python_gnc_module_end(int refcount) 00100 { 00101 Py_Finalize(); 00102 return TRUE; 00103 }
1.7.4