Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/gui
[modules/gui.git] / src / PyInterp / PyInterp_Interp.h
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : PyInterp_Interp.h
24 //  Author : Christian CAREMOLI, Paul RASCLE, Adrien BRUNETON
25 //  Module : SALOME
26 //
27 #ifndef PYINTERP_INTERP_H
28 #define PYINTERP_INTERP_H
29
30 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
31 #include "PyInterp_Utils.h"
32
33 #include <list>
34 #include <string>
35
36 typedef void PyOutChanged(void* data,char * c);
37
38 typedef struct {
39   PyObject_HEAD
40   int softspace;
41   PyOutChanged* _cb;
42   void* _data;
43   bool _iscerr;
44 } PyStdOut;
45
46 /**
47  * Main class representing a *virtual* Python interpreter. There is really only one true
48  * Python interpreter in the whole application (no call to Py_NewInterpreter),
49  * but the use of different execution contexts allow
50  * to split the execution lines, and hence to emulate (relatively) independent interpreters.
51  * This has some consequences: modules imported in one context are not re-imported in another context
52  * (only there namespace is made available when importing in another context).
53  * See also class PyConsole_Interp.
54  */
55 class PYINTERP_EXPORT PyInterp_Interp
56 {
57 public:
58   static int _argc;
59   static char* _argv[];
60   
61   PyInterp_Interp();
62   virtual ~PyInterp_Interp();
63   
64   void initialize();
65   void destroy();
66
67   virtual int run(const char *command); 
68   virtual void initStudy(){};
69
70   // [ABN] - the PyLockWrapper is no more attached to the interpreter
71   // PyLockWrapper GetLockWrapper() const;
72
73   std::string getbanner() const;
74   void setverrcb(PyOutChanged*,void*);
75   void setvoutcb(PyOutChanged*,void*);
76
77   const char * getPrevious();
78   const char * getNext();
79
80 protected:
81   /** Redirection of stdout and stderr */
82   PyObject * _vout;
83   PyObject * _verr;
84   /** Execution context (local and global variables) */
85   PyObject * _global_context;
86   PyObject * _local_context;
87
88   std::list<std::string> _history;
89   std::list<std::string>::iterator _ith;
90
91   virtual int beforeRun();
92   virtual int afterRun();
93   int simpleRun(const char* command, const bool addToHistory = true);
94
95   virtual void initPython();
96
97   /** Initialize execution context. */
98   virtual bool initContext();
99   virtual bool initRun();
100   virtual void closeContext();
101 };
102
103 #endif // PYINTERP_INTERP_H