AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
agxPython::ScriptManager Class Referenceabstract

Class that manages the embedded Python interpreter and a script context. More...

#include <ScriptManager.h>

+ Inheritance diagram for agxPython::ScriptManager:

Public Types

enum  Priority { HIGHEST_PRIORITY = 2147483647, LOWEST_PRIORITY = 10000, DEFAULT_PRIORITY = 1073741824 }
 Specifies the ranges of the priority which can be assigned to a singleton at the time of creation. More...
 

Public Member Functions

void acquireReactiveScriptContext (const agx::Uuid &scriptId, const char *func)
 
void addScriptToSimulation (const agx::Uuid &scriptId, agxSDK::Simulation *simulation)
 
bool addSearchPath (const agx::String &path)
 Appends the full path to the directory to the Python sys.path list of paths to search for importable modules. More...
 
bool breakFlag ()
 
void clearReactiveScripts ()
 
void createMainModule (const agx::Uuid &scriptId)
 
void destroyMainModule (const agx::Uuid &scriptId)
 
void * doCompiledFunction (void *funcObject, void **args, int numArgs)
 
bool doCompiledScript (void *compiled)
 Run an already compiled script as a function global to the main module using the given locals dictionary. More...
 
bool doFile (const std::string &path)
 Load a Python script file. More...
 
bool doFunction (const std::string &function, void *args=nullptr)
 Invoke a global function. More...
 
bool doString (const std::string &code, const std::string &origin)
 Run a string containing Python code. More...
 
void enableEmbeddedMode (const std::string &programName)
 
virtual std::string getClassName () const =0
 
agxSDK::Simulation * getCurrentScriptSimulation ()
 
agxSDK::SimulationController * getCurrentScriptSimulationController ()
 
void * getGlobals ()
 Returns the script's "__main__" module dict dictionary, equivalen t to the globals() builtin function in Python, as a void-cast PyObject* pointer, valid only between calls to ScriptManager::doString or ScriptManager::doFile (which calls doString). More...
 
int getLastErrorLine () const
 Get the line of the last stored error message. More...
 
std::string getLastErrorMessage () const
 Get the last stored error message. More...
 
void * getMainModule ()
 
int getPriority () const
 Return the shutdown priority of the singleton instance. More...
 
std::string getProgramName ()
 Get the name of the program that instantiated the ScriptManager. More...
 
agxPython::ScriptContextInterface * getScriptContext ()
 Get the script context interface object to an already initialized script context. More...
 
bool getTreatInvalidAsErrors () const
 
bool hasFunction (const std::string &function)
 Check if global function exists. More...
 
bool hasMainModule (const agx::Uuid &uuid) const
 
bool hasReactiveScriptErrors (bool printWarnings=true)
 Checks if the stored reactive scripts have errors. More...
 
void init (const std::string &pythonPaths="")
 Initializes the Python interpreter state. More...
 
agxPython::ScriptContextInterface * initScriptContext (agxPython::ScriptContextType contextType)
 Initialize the script context in which scripts should execute and return the interface object to be used for setting up the script environment. More...
 
bool isRegisteredReactiveScript (const agx::Uuid &uuid) const
 
void onStartPlaybackReactiveScripts (agx::Real value)
 
void onStartRecordingReactiveScripts (agx::Real value)
 
void onStopPlaybackReactiveScripts (agx::Real value)
 
void onStopRecordingReactiveScripts (agx::Real value)
 
bool receivedErrorMessage () const
 Get if there has been an error since last reset. More...
 
void registerError (const std::string &prefix="")
 Fetches an error from the script context and call setLastErrorMessage for storing it. More...
 
bool registerReactiveScript (ReactiveScript *script)
 Register the given ReactiveScript to the ScriptManager for Simulation-driven execution using per-Script storage of context data about runtime state, unhandled errors and the access pointer for the ReactiveScript API to interface with the underlying agxSDK::Simulation containing the ReactiveScript. More...
 
void registerSimulationControllerCallbacks (agxSDK::SimulationController *simulationController)
 
void releaseReactiveScriptContext (const agx::Uuid &scriptId, bool unlock=true)
 
void reportError (const std::string &errorReport) const
 Prints an error report, e.g from an exception, to the appropriate error stream. More...
 
ScriptIDERef requestVirtualIDE (const agx::Name &identifyingName)
 Return a ScriptIDE ref_ptr to the ScriptIDE with the given name, creating it if no ScriptIDE with such a name already exists. More...
 
void reset ()
 Resets the Python interpreter state and reinitializes it. More...
 
void resetError ()
 After errors been handled, reset the error message status. More...
 
void restoreGlobalMainModule (bool unlock=true)
 
void setCurrentScriptSimulationOverride (agxSDK::Simulation *simulation)
 This is a way to set the simulation for momentum api if not using it through reactive scripts. More...
 
void setLastErrorLine (int line)
 
void setLastErrorMessage (const std::string &errorMessage)
 Store an error message. More...
 
void setTreatInvalidAsErrors (const bool treatInvalidAsErrors)
 
void shutdown () override
 Deallocates any allocated memory. More...
 
void swapMainModule (const agx::Uuid &scriptId)
 
bool unregisterReactiveScript (const agx::Uuid &uuid)
 
void unregisterSimulationControllerCallbacks (agxSDK::SimulationController *simulationController)
 
void updateReactiveScripts (agxSDK::SimulationController *simulationController)
 
void updateReactiveScripts ()
 

Static Public Member Functions

static void abortProfiling (void *obj, const char *decl)
 Internal method. More...
 
static void endProfiling (void *obj, const char *decl)
 Internal method. More...
 
static ScriptContext * getContext ()
 
static bool hasInstance ()
 
static ScriptManager * instance (const std::string &requestor="", const std::string &pythonPaths="", const std::string &pythonHome="", bool ignoreEnvironment=false)
 Return the singleton object If ignoreEnvironment is true then environment variables and site packages will be ignored. More...
 
static void startProfiling (void *obj, const char *decl)
 Internal method. More...
 

Protected Types

typedef std::map< std::string,
std::string > 
PythonDefMap
 

Protected Member Functions

void _shutdown ()
 
PythonDefMap dumpGlobals (void *globals, std::ostream *logStream=nullptr)
 

Detailed Description

Class that manages the embedded Python interpreter and a script context.

Definition at line 170 of file agxPython/ScriptManager.h.

Member Typedef Documentation

typedef std::map<std::string, std::string> agxPython::ScriptManager::PythonDefMap
protected

Definition at line 418 of file agxPython/ScriptManager.h.

Member Enumeration Documentation

enum agx::Singleton::Priority
inherited

Specifies the ranges of the priority which can be assigned to a singleton at the time of creation.

Enumerator
HIGHEST_PRIORITY 

The highest priority that can be given to a singleton.

LOWEST_PRIORITY 

The lowest priority that can be given to a singleton.

DEFAULT_PRIORITY 

The default priority given to a singleton.

Definition at line 35 of file Singleton.h.

Member Function Documentation

void agx::Singleton::_shutdown ( )
protectedinherited
static void agxPython::ScriptManager::abortProfiling ( void *  obj,
const char *  decl 
)
static

Internal method.

void agxPython::ScriptManager::acquireReactiveScriptContext ( const agx::Uuid &  scriptId,
const char *  func 
)
void agxPython::ScriptManager::addScriptToSimulation ( const agx::Uuid &  scriptId,
agxSDK::Simulation *  simulation 
)
bool agxPython::ScriptManager::addSearchPath ( const agx::String &  path)

Appends the full path to the directory to the Python sys.path list of paths to search for importable modules.

Parameters
path- A ut8 string containing a path to a directory.
Returns
true if path is ut8 and to a directory and sys.path object exists
bool agxPython::ScriptManager::breakFlag ( )
void agxPython::ScriptManager::clearReactiveScripts ( )
void agxPython::ScriptManager::createMainModule ( const agx::Uuid &  scriptId)
void agxPython::ScriptManager::destroyMainModule ( const agx::Uuid &  scriptId)
void* agxPython::ScriptManager::doCompiledFunction ( void *  funcObject,
void **  args,
int  numArgs 
)
bool agxPython::ScriptManager::doCompiledScript ( void *  compiled)

Run an already compiled script as a function global to the main module using the given locals dictionary.

This dictionary of locals will contain all declarations and objects the script defines, useful for measuring validity of state post-initialization.

bool agxPython::ScriptManager::doFile ( const std::string &  path)

Load a Python script file.

The file will be searched for using agxIO::Environment settings.

Parameters
path- full or partial path to a python script file
Returns
true if file was found and loaded successfully
bool agxPython::ScriptManager::doFunction ( const std::string &  function,
void *  args = nullptr 
)

Invoke a global function.

Parameters
function- name of the function to be called
args- Arguments to the function
Returns
true if the call to the function was successful
bool agxPython::ScriptManager::doString ( const std::string &  code,
const std::string &  origin 
)

Run a string containing Python code.

Returns true upon success

Parameters
code- String with python code
origin- An identification string shown if an error occurs
PythonDefMap agxPython::ScriptManager::dumpGlobals ( void *  globals,
std::ostream *  logStream = nullptr 
)
protected
void agxPython::ScriptManager::enableEmbeddedMode ( const std::string &  programName)
static void agxPython::ScriptManager::endProfiling ( void *  obj,
const char *  decl 
)
static

Internal method.

virtual std::string agx::Singleton::getClassName ( ) const
pure virtualinherited
static ScriptContext* agxPython::ScriptManager::getContext ( )
static
Returns
the current Python script context
agxSDK::Simulation* agxPython::ScriptManager::getCurrentScriptSimulation ( )
agxSDK::SimulationController* agxPython::ScriptManager::getCurrentScriptSimulationController ( )
void* agxPython::ScriptManager::getGlobals ( )

Returns the script's "__main__" module dict dictionary, equivalen t to the globals() builtin function in Python, as a void-cast PyObject* pointer, valid only between calls to ScriptManager::doString or ScriptManager::doFile (which calls doString).

Py_XINCREF is used on the pointer before returning, caller is responsible to use Py_XDECREF when done with it. Using Py_XDECREF on NULL pointers is completely valid (it does check internally), so no need to check for NULL.

Use "PyDict_"-family of Python C API functions to access it with keys guaranteed to be Python string objects.

Calling code will need to include Python.h for the Py_XDECREF macro and "PyDict_"-family of functions.

int agxPython::ScriptManager::getLastErrorLine ( ) const

Get the line of the last stored error message.

Returns
-1 if none, otherwise the line of the last stored error message
std::string agxPython::ScriptManager::getLastErrorMessage ( ) const

Get the last stored error message.

Returns
A string containing the last stored error message
void* agxPython::ScriptManager::getMainModule ( )
int agx::Singleton::getPriority ( ) const
inherited

Return the shutdown priority of the singleton instance.

std::string agxPython::ScriptManager::getProgramName ( )

Get the name of the program that instantiated the ScriptManager.

agxPython::ScriptContextInterface* agxPython::ScriptManager::getScriptContext ( )

Get the script context interface object to an already initialized script context.

bool agxPython::ScriptManager::getTreatInvalidAsErrors ( ) const
bool agxPython::ScriptManager::hasFunction ( const std::string &  function)

Check if global function exists.

Parameters
function- name of a function that we will be looking for
Returns
true if the function exists
static bool agxPython::ScriptManager::hasInstance ( )
static
bool agxPython::ScriptManager::hasMainModule ( const agx::Uuid &  uuid) const
bool agxPython::ScriptManager::hasReactiveScriptErrors ( bool  printWarnings = true)

Checks if the stored reactive scripts have errors.

Parameters
printWarnings- true if warnings should be printed to LOGGER_WARNING, false if not.
Returns
true if there are errors in the active reactive scripts, false otherwise.
void agxPython::ScriptManager::init ( const std::string &  pythonPaths = "")

Initializes the Python interpreter state.

agxPython::ScriptContextInterface* agxPython::ScriptManager::initScriptContext ( agxPython::ScriptContextType  contextType)

Initialize the script context in which scripts should execute and return the interface object to be used for setting up the script environment.

static ScriptManager* agxPython::ScriptManager::instance ( const std::string &  requestor = "",
const std::string &  pythonPaths = "",
const std::string &  pythonHome = "",
bool  ignoreEnvironment = false 
)
static

Return the singleton object If ignoreEnvironment is true then environment variables and site packages will be ignored.

bool agxPython::ScriptManager::isRegisteredReactiveScript ( const agx::Uuid &  uuid) const
void agxPython::ScriptManager::onStartPlaybackReactiveScripts ( agx::Real  value)
void agxPython::ScriptManager::onStartRecordingReactiveScripts ( agx::Real  value)
void agxPython::ScriptManager::onStopPlaybackReactiveScripts ( agx::Real  value)
void agxPython::ScriptManager::onStopRecordingReactiveScripts ( agx::Real  value)
bool agxPython::ScriptManager::receivedErrorMessage ( ) const

Get if there has been an error since last reset.

Returns
true if there has been an error, otherwise false.
void agxPython::ScriptManager::registerError ( const std::string &  prefix = "")

Fetches an error from the script context and call setLastErrorMessage for storing it.

Parameters
prefix- A string that will begin the error message
bool agxPython::ScriptManager::registerReactiveScript ( ReactiveScript *  script)

Register the given ReactiveScript to the ScriptManager for Simulation-driven execution using per-Script storage of context data about runtime state, unhandled errors and the access pointer for the ReactiveScript API to interface with the underlying agxSDK::Simulation containing the ReactiveScript.

Registering a ReactiveScript makes it accessible via its Uuid and prepares the internal context data with a clean runtime state.

void agxPython::ScriptManager::registerSimulationControllerCallbacks ( agxSDK::SimulationController *  simulationController)
void agxPython::ScriptManager::releaseReactiveScriptContext ( const agx::Uuid &  scriptId,
bool  unlock = true 
)
void agxPython::ScriptManager::reportError ( const std::string &  errorReport) const

Prints an error report, e.g from an exception, to the appropriate error stream.

ScriptIDERef agxPython::ScriptManager::requestVirtualIDE ( const agx::Name &  identifyingName)

Return a ScriptIDE ref_ptr to the ScriptIDE with the given name, creating it if no ScriptIDE with such a name already exists.

ScriptIDE instances represent a virtual IDE in order to help with writing code editors tailored for writing agxPython scripts, providing facilities such as a debugger and support SDK for source code navigation and autocompletion.

void agxPython::ScriptManager::reset ( )

Resets the Python interpreter state and reinitializes it.

void agxPython::ScriptManager::resetError ( )

After errors been handled, reset the error message status.

void agxPython::ScriptManager::restoreGlobalMainModule ( bool  unlock = true)
void agxPython::ScriptManager::setCurrentScriptSimulationOverride ( agxSDK::Simulation *  simulation)

This is a way to set the simulation for momentum api if not using it through reactive scripts.

void agxPython::ScriptManager::setLastErrorLine ( int  line)
void agxPython::ScriptManager::setLastErrorMessage ( const std::string &  errorMessage)

Store an error message.

void agxPython::ScriptManager::setTreatInvalidAsErrors ( const bool  treatInvalidAsErrors)
void agxPython::ScriptManager::shutdown ( )
overridevirtual

Deallocates any allocated memory.

Reimplemented from agx::Singleton.

static void agxPython::ScriptManager::startProfiling ( void *  obj,
const char *  decl 
)
static

Internal method.

void agxPython::ScriptManager::swapMainModule ( const agx::Uuid &  scriptId)
bool agxPython::ScriptManager::unregisterReactiveScript ( const agx::Uuid &  uuid)
void agxPython::ScriptManager::unregisterSimulationControllerCallbacks ( agxSDK::SimulationController *  simulationController)
void agxPython::ScriptManager::updateReactiveScripts ( agxSDK::SimulationController *  simulationController)
void agxPython::ScriptManager::updateReactiveScripts ( )

The documentation for this class was generated from the following file: