]> Shamusworld >> Repos - architektonas/blob - src/base/rs_python.cpp.bak
Refactoring: Moved RS_GraphicView to GraphicView.
[architektonas] / src / base / rs_python.cpp.bak
1 /****************************************************************************
2 ** $Id: rs_python.cpp 1741 2003-09-30 22:50:17Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use 
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_python.h"
28
29 #ifdef RS_OPT_PYTHON
30
31 //
32 // This is exported from the Boost::Python library declarations
33 // that are declared inside rs_python_wrappers.cpp.
34 //
35 extern "C" void initqcad();
36
37 /**
38  * The unique instance of the Python scripting engine
39  */
40 RS_Python* RS_Python::uniqueInstance = NULL;
41
42 /**
43  * Constructor
44  */
45 RS_Python::RS_Python()
46 {
47     graphic = NULL;
48     Py_Initialize();
49     initqcad();
50 }
51
52 /**
53  *  Gets the one and only RS_Python instance
54  *  (creates a new one on first call only)
55  *
56  *  @return Pointer to the single instance of this
57  * singleton class
58  */
59 RS_Python* RS_Python::instance() {
60     if(uniqueInstance==NULL) {
61         uniqueInstance = new RS_Python;
62     }
63     return uniqueInstance;
64 }
65
66
67 /**
68  * Launches the given script.
69  */
70 int RS_Python::launch(const RS_String& script) {
71     PyObject *modname, *mod, *mdict, *func, *rslt;
72     //Py_SetProgramName(argv[0]);
73
74     modname = PyString_FromString(script);
75     mod = PyImport_Import(modname);
76     if (mod) {
77         //printf( "mod\n");
78         mdict = PyModule_GetDict(mod);
79
80         // Borrowed reference to start function
81         func = PyDict_GetItemString(mdict, "start");
82         if (func) {
83             //printf( "func\n");
84             if (PyCallable_Check(func)) {
85                 //printf("calling..\n");
86                 rslt = PyObject_CallFunction(func, "(s)", "noparam");
87                 //printf("calling ok\n");
88                 if (rslt) {
89                     // The result value is currently not used
90                     Py_XDECREF(rslt);
91                 } else
92                 {
93                     // Give user some feed back what went wrong
94                     printf("*** PYTHON RUNTIME ERROR ***\n");
95                     PyErr_Print();
96                 }
97             }
98         } else {
99             printf("no such function: start\n");
100         }
101         Py_XDECREF(mod);
102     } else {
103         printf("*** ERROR LOADING SCRIPT '%s' ***\n", script.latin1());
104         PyErr_Print();  
105     }
106     Py_XDECREF(modname);
107     //Py_Finalize();
108     return 0;
109 }
110
111 #endif