]> Shamusworld >> Repos - architektonas/blob - src/base/python.cpp
Initial removal of unnecessary rs_ prefixes from files.
[architektonas] / src / base / python.cpp
1 // python.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/02/2010  Added this text. :-)
15 //
16
17 #include "python.h"
18
19 #ifdef RS_OPT_PYTHON
20
21 //
22 // This is exported from the Boost::Python library declarations
23 // that are declared inside rs_python_wrappers.cpp.
24 //
25 extern "C" void initqcad();
26
27 /**
28  * The unique instance of the Python scripting engine
29  */
30 RS_Python* RS_Python::uniqueInstance = NULL;
31
32 /**
33  * Constructor
34  */
35 RS_Python::RS_Python()
36 {
37     graphic = NULL;
38     Py_Initialize();
39     initqcad();
40 }
41
42 /**
43  *  Gets the one and only RS_Python instance
44  *  (creates a new one on first call only)
45  *
46  *  @return Pointer to the single instance of this
47  * singleton class
48  */
49 RS_Python* RS_Python::instance() {
50     if(uniqueInstance==NULL) {
51         uniqueInstance = new RS_Python;
52     }
53     return uniqueInstance;
54 }
55
56
57 /**
58  * Launches the given script.
59  */
60 int RS_Python::launch(const QString& script) {
61     PyObject *modname, *mod, *mdict, *func, *rslt;
62     //Py_SetProgramName(argv[0]);
63
64     modname = PyString_FromString(script);
65     mod = PyImport_Import(modname);
66     if (mod) {
67         //printf( "mod\n");
68         mdict = PyModule_GetDict(mod);
69
70         // Borrowed reference to start function
71         func = PyDict_GetItemString(mdict, "start");
72         if (func) {
73             //printf( "func\n");
74             if (PyCallable_Check(func)) {
75                 //printf("calling..\n");
76                 rslt = PyObject_CallFunction(func, "(s)", "noparam");
77                 //printf("calling ok\n");
78                 if (rslt) {
79                     // The result value is currently not used
80                     Py_XDECREF(rslt);
81                 } else
82                 {
83                     // Give user some feed back what went wrong
84                     printf("*** PYTHON RUNTIME ERROR ***\n");
85                     PyErr_Print();
86                 }
87             }
88         } else {
89             printf("no such function: start\n");
90         }
91         Py_XDECREF(mod);
92     } else {
93         printf("*** ERROR LOADING SCRIPT '%s' ***\n", script.latin1());
94         PyErr_Print();
95     }
96     Py_XDECREF(modname);
97     //Py_Finalize();
98     return 0;
99 }
100
101 #endif