Class ScriptManager
- java.lang.Object
- 
- com.inductiveautomation.ignition.common.script.ScriptManager
 
- 
 public class ScriptManager extends java.lang.Object
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classScriptManager.ExecutionInfostatic classScriptManager.ScriptCanceledErrorprotected classScriptManager.ScriptFunctionImplstatic classScriptManager.UndefinedFunctionException
 - 
Field SummaryFields Modifier and Type Field Description static java.lang.ThreadLocal<java.lang.Boolean>EDGE_SCRIPT_BYPASSprotected static LoggerExlog
 - 
Constructor SummaryConstructors Constructor Description ScriptManager(java.lang.String contextName)Initializes ScriptManager without a path to 3rd party system Python modules.ScriptManager(java.lang.String contextName, java.lang.String pathToExternalLibs)
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddGlobalVariable(java.lang.String name, org.python.core.PyObject value)voidaddScriptModule(java.lang.String path, java.lang.Class<?> javaFunctionClass)Expose all static methods of javaFunctionClass as scripting functions.voidaddScriptModule(java.lang.String path, java.lang.Class<?> javaFunctionClass, ScriptFunctionDocProvider docProvider)Expose all static methods of javaFunctionClass as scripting functions.voidaddScriptModule(java.lang.String path, java.lang.Object obj)Expose all static and instance methods of obj as scripting functions.voidaddScriptModule(java.lang.String path, java.lang.Object obj, ScriptFunctionDocProvider docProvider)Expose all static and instance methods of obj as scripting functions.voidaddScriptModule(java.lang.String path, java.lang.String moduleCode)Adds a script module on the given path (e.g.voidaddScriptModule(java.lang.String path, java.lang.String moduleCode, ScriptScopeStyle scopeStyle)voidaddStaticFields(java.lang.String path, java.lang.Class<?> clazz)Adds all public static final fields defined by this class as python variables in the given path.voidaddStdErrStream(java.io.OutputStream os)voidaddStdOutStream(java.io.OutputStream os)static voidasynchInit()static voidasynchInit(java.lang.String pathToExternalLibs)Initialize the Jython System.com.codahale.metrics.Timer.ContextbeginCompileTimer()If you are gonig to callPy.compile_flags(String, String, CompileMode, CompilerFlags), you should associate the effort with your intended ScriptManager's timer metric.voidclearAppModule()voidclearModule(java.lang.String moduleName)Clears the given module's internal mappingvoidclearProjectScriptModules()Removes all project resource script modulesvoidclearThirdPartyModules()Removes all modules that are not default to Ignition or have been added from project resourcesScriptFunctioncompileFunction(java.lang.String code)Compiles a function definition (normally generated through the Extension Function style scripting system), and returns a compiled function that can be used and reused.ScriptFunctioncompileFunction(java.lang.String id, java.lang.String functionName, java.lang.String code)Overload of compileFunction that allows the caller to define the identifier used for the compiled object, and the method to extract from the code.protected voidconfigureScriptContext()org.python.core.PyStringMapcreateLocalsMap()Creates a new local namespace with the 'system', 'fpmi', ['app'], and project resource packages imported automatically.static org.python.core.PySystemStatecreateUtf8PySystemState(java.io.OutputStream stdOut, java.io.OutputStream stdErr)Initializes aPySystemStateand sets the default encoding to utf-8.static java.util.Set<ScriptManager.ExecutionInfo>executingScripts()com.codahale.metrics.TimergetCompileTimer()com.codahale.metrics.TimergetExecutionTimer()org.python.core.PyStringMapgetGlobals()Provides programmatic access to the global mapjava.util.Map<java.lang.String,java.util.List<ScriptFunctionHint>>getHintsMap()Maps the first element of hint path to the entire path, e.g.:org.python.core.PyStringMapgetModules()java.util.UUIDgetUUID()static voidinterrupt(java.lang.Long threadId)static voidmain(java.lang.String[] args)voidremoveStdErrStream(java.io.OutputStream os)voidremoveStdOutStream(java.io.OutputStream os)voidrunCode(java.lang.String code, org.python.core.PyObject locals, java.lang.String filename)Compiles and then runs some python code with the given locals map.voidrunCode(java.lang.String code, org.python.core.PyObject locals, org.python.core.PyObject globals, java.lang.String filename)Compiles and then runs some python code with the given locals map and globals map.voidrunCode(org.python.core.PyCode code, org.python.core.PyObject locals)Runs compiled python code with the given locals map.voidrunCode(org.python.core.PyCode code, org.python.core.PyObject locals, org.python.core.PyObject globals)Runs compiled python code with the given locals map and globals map.org.python.core.PyObjectrunFunction(org.python.core.PyObject function, org.python.core.PyObject... arguments)org.python.core.PyObjectrunFunction(org.python.core.PyObject function, org.python.core.PyObject[] arguments, java.lang.String[] keywords)voidsetContextName(java.lang.String name)Resets this script manager's context name, which only affects loggingvoidsetPaused(boolean paused)Sets the script manager to be 'paused'.protected voidsetState()Sets the python thread state to our PySystemStatevoidshutdown()booleanvalidatePackageName(java.lang.String newName)Deprecated.PreferModuleLibrary's static validation methods - you can usegetModules()to retrieve this manager's loaded modules to check against.
 
- 
- 
- 
Field Detail- 
EDGE_SCRIPT_BYPASSpublic static final java.lang.ThreadLocal<java.lang.Boolean> EDGE_SCRIPT_BYPASS 
 - 
logprotected static LoggerEx log 
 
- 
 - 
Constructor Detail- 
ScriptManagerpublic ScriptManager(java.lang.String contextName) Initializes ScriptManager without a path to 3rd party system Python modules. Use this constructor if asynchInit has already been called and the path initialized, as a second call will be ignored.
 - 
ScriptManagerpublic ScriptManager(java.lang.String contextName, java.lang.String pathToExternalLibs)
 
- 
 - 
Method Detail- 
shutdownpublic void shutdown() 
 - 
addStdOutStreampublic void addStdOutStream(java.io.OutputStream os) 
 - 
removeStdOutStreampublic void removeStdOutStream(java.io.OutputStream os) 
 - 
addStdErrStreampublic void addStdErrStream(java.io.OutputStream os) 
 - 
removeStdErrStreampublic void removeStdErrStream(java.io.OutputStream os) 
 - 
setContextNamepublic void setContextName(java.lang.String name) Resets this script manager's context name, which only affects logging
 - 
setStateprotected void setState() Sets the python thread state to our PySystemState
 - 
asynchInitpublic static void asynchInit(java.lang.String pathToExternalLibs) Initialize the Jython System. If this is the first time that the system is initialized, Then it will be a somewhat lengthy processes, as the system package manager reads all system jars and caches them to a temp directory. This method allows the init method to safely be run in a background thread, because the constructor will wait for it to be done
 - 
asynchInitpublic static void asynchInit() 
 - 
createUtf8PySystemStatepublic static org.python.core.PySystemState createUtf8PySystemState(java.io.OutputStream stdOut, java.io.OutputStream stdErr)Initializes aPySystemStateand sets the default encoding to utf-8. Also wraps the supplied output stream in aPyFilethat has the encoding set to utf-8.- Parameters:
- stdOut- the standard output stream
- stdErr- the standard error stream
- Returns:
- a new PySystemState
 
 - 
addScriptModulepublic void addScriptModule(java.lang.String path, java.lang.String moduleCode)Adds a script module on the given path (e.g. "app.foo" or "shared.mystuff"). Uses legacy Python21 scoping style.
 - 
addScriptModulepublic void addScriptModule(java.lang.String path, java.lang.String moduleCode, ScriptScopeStyle scopeStyle)
 - 
addScriptModulepublic void addScriptModule(java.lang.String path, java.lang.Class<?> javaFunctionClass)Expose all static methods of javaFunctionClass as scripting functions. Also exposes all static fields. These functions will be part of the auto-complete system, but will not have any documentation provided. To provide documentation as well use addScriptModule(String, Class, ScriptFunctionDocProvider).- Parameters:
- path- The namespace to add these functions to i.e. "system.net".
- javaFunctionClass- The- Classto search for static methods.
 
 - 
addScriptModulepublic void addScriptModule(java.lang.String path, java.lang.Class<?> javaFunctionClass, ScriptFunctionDocProvider docProvider)Expose all static methods of javaFunctionClass as scripting functions. Also exposes all static fields. - Parameters:
- path- The namespace to add these functions to i.e. "system.net".
- javaFunctionClass- The- Classto search for static methods.
- docProvider- A- ScriptFunctionDocProviderthat can produce documentation for use in the hints system.
 
 - 
addScriptModulepublic void addScriptModule(java.lang.String path, java.lang.Object obj)Expose all static and instance methods of obj as scripting functions. Also exposes all static fields. These functions will be part of the auto-complete system, but will not have any documentation provided. To provide documentation as well use addScriptModule(String, Class, ScriptFunctionDocProvider).- Parameters:
- path- The namespace to add these functions to i.e. "system.net".
- obj- The- Objectto search for static methods.
 
 - 
addScriptModulepublic void addScriptModule(java.lang.String path, java.lang.Object obj, ScriptFunctionDocProvider docProvider)Expose all static and instance methods of obj as scripting functions. Also exposes all static fields. - Parameters:
- path- The namespace to add these functions to i.e. "system.net".
- obj- The- Objectto search for static methods.
- docProvider- A- ScriptFunctionDocProviderthat can produce documentation for use in the hints system
 
 - 
addStaticFieldspublic void addStaticFields(java.lang.String path, java.lang.Class<?> clazz)Adds all public static final fields defined by this class as python variables in the given path.
 - 
clearAppModulepublic void clearAppModule() 
 - 
clearModulepublic void clearModule(java.lang.String moduleName) Clears the given module's internal mapping
 - 
clearProjectScriptModulespublic void clearProjectScriptModules() Removes all project resource script modules
 - 
clearThirdPartyModulespublic void clearThirdPartyModules() Removes all modules that are not default to Ignition or have been added from project resources
 - 
createLocalsMappublic org.python.core.PyStringMap createLocalsMap() Creates a new local namespace with the 'system', 'fpmi', ['app'], and project resource packages imported automatically.
 - 
getGlobalspublic org.python.core.PyStringMap getGlobals() Provides programmatic access to the global map
 - 
addGlobalVariablepublic void addGlobalVariable(java.lang.String name, org.python.core.PyObject value)
 - 
runCodepublic void runCode(java.lang.String code, org.python.core.PyObject locals, java.lang.String filename) throws JythonExecExceptionCompiles and then runs some python code with the given locals map. Uses Python21 style scoping (shared global namespace)- Parameters:
- code- the python script
- locals- locals map
- filename- source file name
- Throws:
- JythonExecException- ex
- See Also:
- ScriptScopeStyle.Python21
 
 - 
runCodepublic void runCode(org.python.core.PyCode code, org.python.core.PyObject locals) throws JythonExecExceptionRuns compiled python code with the given locals map. Uses Python21 style scoping (shared global namespace)- Parameters:
- code- the compiled python code
- locals- locals map
- Throws:
- JythonExecException- ex
- See Also:
- ScriptScopeStyle.Python21
 
 - 
runCodepublic void runCode(java.lang.String code, org.python.core.PyObject locals, org.python.core.PyObject globals, java.lang.String filename) throws JythonExecExceptionCompiles and then runs some python code with the given locals map and globals map. Uses Python21 or Python25 style scoping, depending on the supplied globals map.- Parameters:
- code- the python script
- locals- locals map
- globals- globals map
- filename- source file name
- Throws:
- JythonExecException- ex
- See Also:
- ScriptScopeStyle.Python21,- ScriptScopeStyle.Python25
 
 - 
runCodepublic void runCode(org.python.core.PyCode code, org.python.core.PyObject locals, org.python.core.PyObject globals) throws JythonExecExceptionRuns compiled python code with the given locals map and globals map.- Parameters:
- code- the compiled python code
- locals- locals map
- globals- globals map. If null, Python21 style scoping is used (shared global namespace).
- Throws:
- JythonExecException- ex
 
 - 
configureScriptContextprotected void configureScriptContext() 
 - 
runFunctionpublic org.python.core.PyObject runFunction(org.python.core.PyObject function, org.python.core.PyObject... arguments) throws JythonExecException- Throws:
- JythonExecException
 
 - 
runFunctionpublic org.python.core.PyObject runFunction(org.python.core.PyObject function, org.python.core.PyObject[] arguments, java.lang.String[] keywords) throws JythonExecException- Throws:
- JythonExecException
 
 - 
compileFunctionpublic ScriptFunction compileFunction(java.lang.String code) throws org.python.core.PyException, JythonExecException, ScriptManager.UndefinedFunctionException Compiles a function definition (normally generated through the Extension Function style scripting system), and returns a compiled function that can be used and reused. The function name will be extracted from the definition, so the code is expected to start with "def funcName(..."- Throws:
- org.python.core.PyException- ex
- JythonExecException- ex
- ScriptManager.UndefinedFunctionException- ex
 
 - 
compileFunctionpublic ScriptFunction compileFunction(java.lang.String id, java.lang.String functionName, java.lang.String code) throws org.python.core.PyException, JythonExecException, ScriptManager.UndefinedFunctionException Overload of compileFunction that allows the caller to define the identifier used for the compiled object, and the method to extract from the code. This version is necessary if the code provided might define multiple functions.- Throws:
- org.python.core.PyException- ex
- JythonExecException- ex
- ScriptManager.UndefinedFunctionException- ex
 
 - 
beginCompileTimerpublic com.codahale.metrics.Timer.Context beginCompileTimer() If you are gonig to callPy.compile_flags(String, String, CompileMode, CompilerFlags), you should associate the effort with your intended ScriptManager's timer metric.
 - 
getCompileTimerpublic com.codahale.metrics.Timer getCompileTimer() 
 - 
getExecutionTimerpublic com.codahale.metrics.Timer getExecutionTimer() 
 - 
setPausedpublic void setPaused(boolean paused) Sets the script manager to be 'paused'. When paused, scripts simply don't run at all.
 - 
getHintsMappublic java.util.Map<java.lang.String,java.util.List<ScriptFunctionHint>> getHintsMap() Maps the first element of hint path to the entire path, e.g.:"path" -> ["path.to.autocomplete", "path.for.somethingelse"] - Returns:
- The hints map.
 
 - 
interruptpublic static void interrupt(@Nonnull java.lang.Long threadId)
 - 
getUUIDpublic java.util.UUID getUUID() - Returns:
- The UUID of this script manager, which can be used by other systems to determine if the script manager has recently been replaced (perhaps due to installing an updated module).
 
 - 
validatePackageName@Deprecated public boolean validatePackageName(java.lang.String newName) Deprecated.PreferModuleLibrary's static validation methods - you can usegetModules()to retrieve this manager's loaded modules to check against.- Parameters:
- newName- the proposed new package name
- Returns:
- true if the package does not exist in the system modules, and thus can safely be used as a new package name without conflicts
 
 - 
getModulespublic org.python.core.PyStringMap getModules() 
 - 
mainpublic static void main(java.lang.String[] args) 
 - 
executingScriptspublic static java.util.Set<ScriptManager.ExecutionInfo> executingScripts() 
 
- 
 
-