|
AGXDynamics
2.31.1.0
|
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) |
Class that manages the embedded Python interpreter and a script context.
Definition at line 170 of file agxPython/ScriptManager.h.
|
protected |
Definition at line 418 of file agxPython/ScriptManager.h.
|
inherited |
Specifies the ranges of the priority which can be assigned to a singleton at the time of creation.
Definition at line 35 of file Singleton.h.
|
protectedinherited |
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.
| path | - A ut8 string containing a path to a directory. |
| bool agxPython::ScriptManager::breakFlag | ( | ) |
| void agxPython::ScriptManager::clearReactiveScripts | ( | ) |
| 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.
| path | - full or partial path to a python script file |
| bool agxPython::ScriptManager::doFunction | ( | const std::string & | function, |
| void * | args = nullptr |
||
| ) |
Invoke a global function.
| function | - name of the function to be called |
| args | - Arguments to the function |
| bool agxPython::ScriptManager::doString | ( | const std::string & | code, |
| const std::string & | origin | ||
| ) |
Run a string containing Python code.
Returns true upon success
| code | - String with python code |
| origin | - An identification string shown if an error occurs |
|
protected |
| void agxPython::ScriptManager::enableEmbeddedMode | ( | const std::string & | programName | ) |
Internal method.
|
pure virtualinherited |
|
static |
| 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.
| std::string agxPython::ScriptManager::getLastErrorMessage | ( | ) | const |
Get the last stored error message.
| void* agxPython::ScriptManager::getMainModule | ( | ) |
|
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.
| function | - name of a function that we will be looking for |
|
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.
| printWarnings | - true if warnings should be printed to LOGGER_WARNING, false if not. |
| 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 |
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 |
| bool agxPython::ScriptManager::receivedErrorMessage | ( | ) | const |
Get if there has been an error since last reset.
| void agxPython::ScriptManager::registerError | ( | const std::string & | prefix = "" | ) |
Fetches an error from the script context and call setLastErrorMessage for storing it.
| 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 | ) |
|
overridevirtual |
Deallocates any allocated memory.
Reimplemented from agx::Singleton.
Internal method.
| 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 | ( | ) |