30 #include "../wrappers/WColorWrapper.h"
31 #include "../wrappers/WLoggerWrapper.h"
32 #include "../wrappers/WModuleWrapper.h"
33 #include "../wrappers/WPropertyGroupWrapper.h"
34 #include "../wrappers/WPropertyWrapper.h"
35 #include "../wrappers/WUtilityFunctions.h"
36 #include "WScriptInterpreterPython.h"
37 #include "core/kernel/WKernel.h"
41 WScriptInterpreterPython::WScriptInterpreterPython( std::shared_ptr< WModuleContainer >
const& rootContainer )
43 m_rootContainer( rootContainer ),
46 m_scriptThread( *this )
51 m_pyModule = pb::import(
"__main__" );
52 m_pyMainNamespace = m_pyModule.attr(
"__dict__" );
54 catch( pb::error_already_set
const& )
59 execute(
"import signal" );
60 execute(
"signal.signal( signal.SIGINT, signal.SIG_DFL )" );
65 WScriptInterpreterPython::~WScriptInterpreterPython()
67 m_scriptThread.requestStop();
68 m_scriptThread.wait();
74 for(
int k = 0; k < m_argc; ++k )
82 void WScriptInterpreterPython::initBindings()
84 std::unique_lock< boost::mutex > lock( m_mutex );
86 m_pyMainNamespace[
"WColor" ] = pb::class_< WColorWrapper >(
"WColor", pb::init< float, float, float, float >() )
94 m_pyMainNamespace[
"WProperty" ] = pb::class_< WPropertyWrapper >(
"WProperty", pb::no_init )
114 m_pyMainNamespace[
"WPropertyGroup" ] = pb::class_< WPropertyGroupWrapper >(
"WPropertyGroup", pb::no_init )
120 m_pyMainNamespace[
"WModuleContainer" ] = pb::class_< WModuleContainerWrapper >(
"WModuleContainer", pb::no_init )
125 m_pyMainNamespace[
"WOutputConnector" ] = pb::class_< WOutputConnectorWrapper >(
"WOutputConnectorWrapper", pb::no_init )
128 m_pyMainNamespace[
"WInputConnector" ] = pb::class_< WInputConnectorWrapper >(
"WInputConnectorWrapper", pb::no_init )
133 m_pyMainNamespace[
"WModule" ] = pb::class_< WModuleWrapper >(
"WModule", pb::no_init )
143 m_pyMainNamespace[
"rootContainer" ] = &m_rootContainer;
145 m_pyMainNamespace[
"WLogger" ] = pb::class_< WLoggerWrapper >(
"WLogger", pb::no_init )
155 m_pyMainNamespace[
"screenshot" ] = pb::make_function( &screenshot );
156 m_pyMainNamespace[
"initCamera" ] = pb::make_function( &initCamera );
159 m_pyMainNamespace[
"logger" ] = &m_logger;
162 void WScriptInterpreterPython::setParameters( std::vector< std::string >
const& params )
164 std::unique_lock< boost::mutex > lock( m_mutex );
166 if( params.size() == 0 )
171 m_argc = params.size();
172 m_argv =
new char*[ params.size() ];
174 for( std::size_t k = 0; k < params.size(); ++k )
176 m_argv[ k ] =
new char[ params[ k ].length() + 1 ];
177 std::snprintf( m_argv[ k ], params[ k ].length() + 1,
"%s", params[ k ].c_str() );
178 m_argv[ k ][ params[ k ].length() ] =
'\0';
182 wchar_t** w_argv =
static_cast<wchar_t**
>( PyMem_Malloc(
sizeof(
wchar_t* ) * m_argc ) );
183 for(
int i = 0; i < m_argc; ++i )
185 wchar_t* arg = Py_DecodeLocale( m_argv[i], NULL );
189 PySys_SetArgv( m_argc, w_argv );
192 void WScriptInterpreterPython::execute( std::string
const& line )
194 std::unique_lock< boost::mutex > lock( m_mutex );
198 pb::exec( line.c_str(), m_pyMainNamespace );
200 catch( pb::error_already_set
const& )
206 void WScriptInterpreterPython::executeAsync( std::string
const& script )
208 m_scriptThread.addToExecuteQueue( script );
211 void WScriptInterpreterPython::executeFile( std::string
const& filename )
214 std::ifstream in( filename.c_str() );
217 while( std::getline( in, line ) )
219 script += line +
"\n";
230 wlog::error(
"Walnut" ) <<
"Error while executing script: " << e.
what();
234 void WScriptInterpreterPython::executeFileAsync( std::string
const& filename )
237 std::ifstream in( filename.c_str() );
240 while( std::getline( in, line ) )
242 script += line +
"\n";
247 executeAsync( script );
250 std::string
const WScriptInterpreterPython::getName()
const
255 std::string
const WScriptInterpreterPython::getExtension()
const
260 WScriptInterpreterPython::ScriptThread::ScriptThread( WScriptInterpreterPython& interpreter )
266 m_interpreter( interpreter )
268 m_conditionSet.setResetable(
true,
true );
269 m_conditionSet.add( m_condition );
272 WScriptInterpreterPython::ScriptThread::~ScriptThread()
276 void WScriptInterpreterPython::ScriptThread::requestStop()
279 m_condition->notify();
282 void WScriptInterpreterPython::ScriptThread::threadMain()
284 while( !m_shutdownFlag )
286 m_conditionSet.wait();
291 std::size_t numScripts = 0;
293 std::unique_lock< boost::mutex > lock( m_queueMutex );
294 numScripts = m_scriptQueue.size();
297 while( numScripts > 0 )
303 std::unique_lock< boost::mutex > lock( m_queueMutex );
304 script = m_scriptQueue.front();
308 if( script.length() != 0 )
310 wlog::info(
"WScriptInterpreterPython::ScriptThread" ) <<
"Executing script asyncronously.";
312 m_interpreter.execute( script );
313 wlog::info(
"WScriptInterpreterPython::ScriptThread" ) <<
"Done executing script.";
316 std::unique_lock< boost::mutex > lock( m_queueMutex );
317 numScripts = m_scriptQueue.size();
323 void WScriptInterpreterPython::ScriptThread::addToExecuteQueue( std::string
const& script )
325 wlog::info(
"WScriptInterpreterPython::ScriptThread" ) <<
"Queueing script for asynchronous execution.";
327 std::unique_lock< boost::mutex > lock( m_queueMutex );
328 m_scriptQueue.push( script );
329 m_condition->notify();
void setG(float g)
Set the green component.
float getB() const
Get the blue component.
float getG() const
Get the green component.
float getR() const
Get the red component.
void setA(float a)
Set the alpha component.
void setR(float r)
Set the red component.
float getA() const
Get the alpha component.
void setB(float b)
Set the blue component.
Class to encapsulate boost::condition_variable_any.
virtual const char * what() const
Returns the message string set on throw.
void warning(std::string const &location, std::string const &message)
Output a warning to the logs.
void debug(std::string const &location, std::string const &message)
Output a debug message.
void removeAllFileStreams()
Remove all files to which the logger writes (and which were added by this wrapper).
void error(std::string const &location, std::string const &message)
Output an error to the logs.
bool addFileStream(std::string filename)
Add a file to which the logger output will be written.
bool removeFileStream(std::string filename)
Remove a file to which the logger writes.
void info(std::string const &location, std::string const &message)
Output information to the logs.
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
WModuleWrapper create(std::string const &name)
Creates a module from the prototype with the given name.
void remove(WModuleWrapper module)
Remove a module from the container.
WModuleWrapper createDataModule(std::string const &filename)
Creates a data module and load the file given via filename.
WInputConnectorWrapper getInputConnector(std::string const &name)
Get an input connector by name.
WPropertyGroupWrapper getProperties()
Returns a WPropertyGroupWrapper containing the module's properties.
WOutputConnectorWrapper getOutputConnector(std::string const &name)
Get an output connector by name.
WPropertyGroupWrapper getInformationProperties()
Returns a WPropertyGroupWrapper containing the module's info properties.
std::string getName() const
Get the name of the module.
std::string getDescription() const
Get the description of the module.
void disconnect()
Disconnect this connector.
WPropertyGroupWrapper getGroup(std::string const &name)
Retrieve a property group by name.
std::string getName() const
Return the name of the property group.
WPropertyWrapper getProperty(std::string const &name)
Retrieve a property by name.
std::string getDescription() const
Return the description of the property group.
std::string getName() const
Return the name of the property.
void setColor(WColorWrapper col)
Set the color of a color property.
void click()
Trigger a trigger property.
void setInt(int i)
Set the value of an integer property.
std::string getString(bool notify=false) const
Get the value of a string property.
std::string getDescription() const
Return the description of the property.
void setString(std::string const &s)
Set the value of a string property.
void setBool(bool b)
Set the value of a boolean property.
void waitForUpdate()
Wait for the property to update its value.
bool getBool(bool notify=false) const
Get the value of a boolean property.
std::string getFilename(bool notify=false) const
Get the filename of a filename property.
void setFilename(std::string const &fn)
Set the filename of the filename property.
double getDouble(bool notify=false) const
Get the value of a double property.
WColorWrapper getColor(bool notify=false) const
Get the color of a color property.
int getSelection(bool notify=false) const
Get the (first) selected item of a selection property.
void setSelection(int s)
Sets the selected item of a selection.
int getInt(bool notify=false) const
Get the value of an integer property.
void setDouble(double d)
Set the value of a double property.
An abstract base class for a script interpreter.
Base class for all classes needing to be executed in a separate thread.
virtual void requestStop()
This method's purpose is to request a stop without waiting for it.
WStreamedLogger info(const std::string &source)
Logging an information message.
WStreamedLogger error(const std::string &source)
Logging an error message.