OpenWalnut  1.4.0
WScriptInterpreterPython.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSCRIPTINTERPRETERPYTHON_H
26 #define WSCRIPTINTERPRETERPYTHON_H
27 
28 #ifdef PYTHON_FOUND
29 
30 #include <queue>
31 #include <string>
32 #include <vector>
33 
34 #ifndef Q_MOC_RUN
35 #include <boost/thread/mutex.hpp>
36 #endif
37 #ifndef Q_MOC_RUN
38 #include <boost/python.hpp>
39 #endif
40 
41 #include "../../common/WThreadedRunner.h"
42 
43 #include "../wrappers/WLoggerWrapper.h"
44 #include "../wrappers/WModuleContainerWrapper.h"
45 
46 #include "../WScriptInterpreter.h"
47 
48 namespace pb = boost::python;
49 
50 /**
51  * \class WScriptInterpreterPython
52  *
53  * An interpreter for python scripts.
54  */
55 class WScriptInterpreterPython : public WScriptInterpreter
56 {
57 public:
58  /**
59  * Constructor. Creates the interpreter.
60  */
61  explicit WScriptInterpreterPython( boost::shared_ptr< WModuleContainer > const& rootContainer );
62 
63  /**
64  * Destructor. Destroys the interpreter.
65  */
66  virtual ~WScriptInterpreterPython();
67 
68  /**
69  * Initializes bindings for OpenWalnut classes. Call this after starting the kernel.
70  */
71  virtual void initBindings();
72 
73  /**
74  * Sets the script parameters. These are the parameters you would normally call your script with, e.g.
75  * "./myscript.py param 1 param2".
76  *
77  * \param params The parameters to the script. In our example, they would be "./myscript.py", "param", "1" and "param2".
78  */
79  virtual void setParameters( std::vector< std::string > const& params );
80 
81  /**
82  * Execute some python code.
83  *
84  * \param line The code to execute.
85  */
86  virtual void execute( std::string const& line );
87 
88  /**
89  * Execute a script in a seperate thread. This function returns immediately.
90  *
91  * \param script The script to execute.
92  */
93  virtual void executeAsync( std::string const& script );
94 
95  /**
96  * Execute a file.
97  *
98  * \param filename The script file to execute.
99  */
100  virtual void executeFile( std::string const& filename );
101 
102  /**
103  * Execute a script file in a seperate thread. This function returns immediately.
104  *
105  * \param filename The script file to execute.
106  */
107  virtual void executeFileAsync( std::string const& filename );
108 
109  /**
110  * Get the name of the language interpreted by this interpreter.
111  *
112  * \return The name of the script language.
113  */
114  virtual std::string const getName() const;
115 
116  /**
117  * Get the default extension for script file belonging to the script interpreter's language.
118  *
119  * \return The default file extension.
120  */
121  virtual std::string const getExtension() const;
122 
123 private:
124  /**
125  * A thread that executes scripts from a queue.
126  */
127  class ScriptThread : public WThreadedRunner
128  {
129  public:
130  /**
131  * Create a thread.
132  */
133  explicit ScriptThread( WScriptInterpreterPython& interpreter ); // NOLINT reference
134 
135  /**
136  * Destructor.
137  */
138  virtual ~ScriptThread();
139 
140  /**
141  * Executes scripts stored in the script queue and sleeps as long as no
142  * scripts are in the queue.
143  */
144  virtual void threadMain();
145 
146  /**
147  * Adds a script string to the queue. This is a thread-safe operation.
148  *
149  * \param script The script to add.
150  */
151  void addToExecuteQueue( std::string const& script );
152 
153  /**
154  * Request this script thread to stop. Returns immediatly.
155  */
156  void requestStop();
157 
158  private:
159  //! A queue for scripts to be executed.
160  std::queue< std::string > m_scriptQueue;
161 
162  //! A mutex for thread-safe adding to the queue.
163  boost::mutex m_queueMutex;
164 
165  //! A condition to be notified when a new script is added.
166  boost::shared_ptr< WCondition > m_condition;
167 
168  //! A condition set used for immidiate returns on wait() if it was notified beforehand.
169  WConditionSet m_conditionSet;
170 
171  //! A reference to the interpreter this thread belongs to.
172  WScriptInterpreterPython& m_interpreter;
173  };
174 
175  //! The python module.
176  pb::object m_pyModule;
177 
178  //! The namespace where we will be working. Bindings are declared in this workspace.
179  pb::object m_pyMainNamespace;
180 
181  //! A pointer to the kernel's root container. We can use this to manipulate modules.
182  WModuleContainerWrapper m_rootContainer;
183 
184  //! The logger.
185  WLoggerWrapper m_logger;
186 
187  //! The number of args passed when calling the script.
188  int m_argc;
189 
190  //! The args passed to the script.
191  char** m_argv;
192 
193  //! A mutex for safe execution of scripts.
194  boost::mutex m_mutex;
195 
196  //! A thread for asynchronous execution of scripts.
197  ScriptThread m_scriptThread;
198 };
199 
200 #endif // PYTHON_FOUND
201 
202 #endif // WSCRIPTINTERPRETERPYTHON_H
A wrapper for WLogger.
virtual std::string const getExtension() const =0
Get the default extension for script file belonging to the script interpreter's language.
virtual void execute(std::string const &line)=0
Execute some code.
virtual void executeFile(std::string const &filename)=0
Execute a file.
Base class for all classes needing to be executed in a separate thread.
An abstract base class for a script interpreter.
Class allowing multiple conditions to be used for one waiting cycle.
Definition: WConditionSet.h:46
virtual void executeAsync(std::string const &script)=0
Execute a script in a seperate thread.
virtual std::string const getName() const =0
Get the name of the language interpreted by this interpreter.
virtual void setParameters(std::vector< std::string > const &params)=0
Sets the script parameters.
virtual void executeFileAsync(std::string const &filename)=0
Execute a script file in a seperate thread.
Encapsulates a module container.
virtual void initBindings()=0
Initialize OpenWalnut-bindings.