OpenWalnut  1.5.0dev
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 <memory>
31 #include <queue>
32 #include <string>
33 #include <vector>
34 
35 #include <boost/python.hpp>
36 #include <boost/thread/mutex.hpp>
37 
38 #include "../../common/WThreadedRunner.h"
39 #include "../WScriptInterpreter.h"
40 #include "../wrappers/WLoggerWrapper.h"
41 #include "../wrappers/WModuleContainerWrapper.h"
42 
43 namespace pb = boost::python;
44 
45 /**
46  * \class WScriptInterpreterPython
47  *
48  * An interpreter for python scripts.
49  */
50 class WScriptInterpreterPython : public WScriptInterpreter
51 {
52 public:
53  /**
54  * Constructor. Creates the interpreter.
55  */
56  explicit WScriptInterpreterPython( std::shared_ptr< WModuleContainer > const& rootContainer );
57 
58  /**
59  * Destructor. Destroys the interpreter.
60  */
61  virtual ~WScriptInterpreterPython();
62 
63  /**
64  * Initializes bindings for OpenWalnut classes. Call this after starting the kernel.
65  */
66  virtual void initBindings();
67 
68  /**
69  * Sets the script parameters. These are the parameters you would normally call your script with, e.g.
70  * "./myscript.py param 1 param2".
71  *
72  * \param params The parameters to the script. In our example, they would be "./myscript.py", "param", "1" and "param2".
73  */
74  virtual void setParameters( std::vector< std::string > const& params );
75 
76  /**
77  * Execute some python code.
78  *
79  * \param line The code to execute.
80  */
81  virtual void execute( std::string const& line );
82 
83  /**
84  * Execute a script in a seperate thread. This function returns immediately.
85  *
86  * \param script The script to execute.
87  */
88  virtual void executeAsync( std::string const& script );
89 
90  /**
91  * Execute a file.
92  *
93  * \param filename The script file to execute.
94  */
95  virtual void executeFile( std::string const& filename );
96 
97  /**
98  * Execute a script file in a seperate thread. This function returns immediately.
99  *
100  * \param filename The script file to execute.
101  */
102  virtual void executeFileAsync( std::string const& filename );
103 
104  /**
105  * Get the name of the language interpreted by this interpreter.
106  *
107  * \return The name of the script language.
108  */
109  virtual std::string const getName() const;
110 
111  /**
112  * Get the default extension for script file belonging to the script interpreter's language.
113  *
114  * \return The default file extension.
115  */
116  virtual std::string const getExtension() const;
117 
118 private:
119  /**
120  * A thread that executes scripts from a queue.
121  */
122  class ScriptThread : public WThreadedRunner
123  {
124  public:
125  /**
126  * Create a thread.
127  */
128  explicit ScriptThread( WScriptInterpreterPython& interpreter ); // NOLINT reference
129 
130  /**
131  * Destructor.
132  */
133  virtual ~ScriptThread();
134 
135  /**
136  * Executes scripts stored in the script queue and sleeps as long as no
137  * scripts are in the queue.
138  */
139  virtual void threadMain();
140 
141  /**
142  * Adds a script string to the queue. This is a thread-safe operation.
143  *
144  * \param script The script to add.
145  */
146  void addToExecuteQueue( std::string const& script );
147 
148  /**
149  * Request this script thread to stop. Returns immediatly.
150  */
151  void requestStop();
152 
153  private:
154  //! A queue for scripts to be executed.
155  std::queue< std::string > m_scriptQueue;
156 
157  //! A mutex for thread-safe adding to the queue.
158  boost::mutex m_queueMutex;
159 
160  //! A condition to be notified when a new script is added.
161  std::shared_ptr< WCondition > m_condition;
162 
163  //! A condition set used for immidiate returns on wait() if it was notified beforehand.
164  WConditionSet m_conditionSet;
165 
166  //! A reference to the interpreter this thread belongs to.
167  WScriptInterpreterPython& m_interpreter;
168  };
169 
170  //! The python module.
171  pb::object m_pyModule;
172 
173  //! The namespace where we will be working. Bindings are declared in this workspace.
174  pb::object m_pyMainNamespace;
175 
176  //! A pointer to the kernel's root container. We can use this to manipulate modules.
177  WModuleContainerWrapper m_rootContainer;
178 
179  //! The logger.
180  WLoggerWrapper m_logger;
181 
182  //! The number of args passed when calling the script.
183  int m_argc;
184 
185  //! The args passed to the script.
186  char** m_argv;
187 
188  //! A mutex for safe execution of scripts.
189  boost::mutex m_mutex;
190 
191  //! A thread for asynchronous execution of scripts.
192  ScriptThread m_scriptThread;
193 };
194 
195 #endif // PYTHON_FOUND
196 
197 #endif // WSCRIPTINTERPRETERPYTHON_H
Class allowing multiple conditions to be used for one waiting cycle.
Definition: WConditionSet.h:44
A wrapper for WLogger.
Encapsulates a module container.
An abstract base class for a script interpreter.
virtual void initBindings()=0
Initialize OpenWalnut-bindings.
virtual void execute(std::string const &line)=0
Execute some code.
virtual void executeFile(std::string const &filename)=0
Execute a file.
virtual std::string const getExtension() const =0
Get the default extension for script file belonging to the script interpreter's language.
virtual void executeFileAsync(std::string const &filename)=0
Execute a script file in a seperate thread.
virtual void setParameters(std::vector< std::string > const &params)=0
Sets the script parameters.
virtual std::string const getName() const =0
Get the name of the language interpreted by this interpreter.
virtual void executeAsync(std::string const &script)=0
Execute a script in a seperate thread.
Base class for all classes needing to be executed in a separate thread.