Script Manager¶
The script manager serves as the de facto core of PySpigot, handling the loading, running, and unloading of scripts. It also oversees interactions between scripts and all the other managers within PySpigot. Typically, you won't need to interact directly with the script manager, as most operations are performed by the script manager automatically. However, if needed, you can access the script manager from within your script to load, run, or unload scripts programmatically.
For instructions on importing the script manager into your script, visit the General Information page.
Warning
Use extreme caution when loading/unloading scripts through other scripts. This is a powerful but potentially destructive feature that can lead to unexpected behavior and errors.
Script Manager Usage¶
Using the script manager to load, run and unload scripts is relatively simple. There are three functions available to perform these operations:
script.loadScript(name)
: Loads a script with the given name. Returns aRunResult
which represents the outcome of loading the script. See the below section for more information onRunResult
.script.loadScript(path)
: Loads a script with the given file path. Returns aRunResult
which represents the outcome of loading the script. See the below section for more information onRunResult
.script.unloadScript(name)
: Unloads a script with he given name. This function will returnTrue
if the script unloaded successfully, orFalse
if the script did not unload successfully. Unsuccessful unloads are usually accompanied by an error message printed to the console and to the script's log file (if file logging is enabled for the script).
In addition, the script manager contains server other useful functions:
script.isScriptRunning(name)
: Check if a script is running with the given name. ReturnsTrue
orFalse
accordingly.script.getScriptPath(name)
: Gets the absolute path to a script with the given name. Useful if subfolders are being utilized within thescripts
folder.script.getScript(name)
: Get a running script. Returns aScript
object if a running script was found with the given name, otherwise returnsNone
. See the Script Object section below for more information on the Script object.script.getLoadedScripts()
: Returns aSet
of all loaded/running scripts (asScript
objects).script.getLoadedScriptNames()
: Returns aSet
of all loaded/running script names (asstr
s).script.getAllScriptPaths()
: Returns aSet
, sorted alphabetically, of all script paths (taking into account subfolders) within thescripts
folder.script.getAllScriptNames()
: Returns aSet
, sorted alphabetically, containing the names of all scripts within thescripts
foler (taking into account subfolders).
Danger
Never unload a script from within itself! This will lead to unexpected behavior and errors.
Run Result¶
The RunResult
is a value that represents the outcome of loading a script. Both of the loadScript
functions return a RunResult
representing the outcome of the load operation. There are four possible outcomes:
RunResult | Description |
---|---|
SUCCESS |
The script loaded and ran successfully. The script is currently running. |
FAIL_DISABLED |
Running the script failed because the script is disabled as per its script options (in script_options.yml or elsewhere) |
FAIL_PLUGIN_DEPENDENCY |
Running the script failed because it depends on one or more plugins that are not running on the server. |
FAIL_ERROR |
Running the script failed because there was an error when it ran. The error will be printed to the server console (and to the script's log file, if enabled). |
FAIL_DUPLICATE |
Running the script failed because there is a script already running with the same name. |
FAIL_SCRIPT_NOT_FOUND |
Running the script failed because a script was not found in the scripts folder with the given name. |
The RunResult
class is accessed by importing dev.magicmq.pyspigot.manager.script.RunResult
.
The Script Object¶
The Script
object is the in-memory representation of a running script. It holds several key values related to a script, including its name, log file, options, and underlying Jython interpreter object. These can be accessed with the following functions:
getFile()
: Returns theFile
pertaining to the script.getPath()
: Returns the absolute path of the script.getName()
: Returns the name of the script.getSimpleName()
: Returns the name of the script, without the extension (.py
) at the end.getOptions()
: Returns aScriptOptions
object representing the script's options. See the JavaDocs for a list of functions available inScriptOptions
.getInterpreter()
: Returns the script's underlying Jython interpreter object (PythonInterpreter
).getLogger()
: Returns the script'sScriptLogger
object.getLogFileName()
: Returns the script's log file name. This function always returns a file name for the script, even if file logging is disabled in its options.getUptime()
: Returns the script's uptime in milliseconds.
Warning
Again, exercise extreme caution when interacting with this portion of PySpigot, particularly the underlying Jython interpreter. Unexpected behavior can occur.
Code Examples¶
Let's look at some code that loads, runs and unloads a script:
Loading and Running a Script¶
-
Here, we import PySpigot as
ps
to utilize the script manager (script
). -
Here, we import
RunResult
to utilize it later to determine the outcome of loading the script. -
Here, we load the script with the name
script.py
, and we assign the outcome of loading the script (theRunResult
) torun_result
. -
Here, we check if the outcome of loading the script was
RunResult.SUCCESS
(which means the script loaded successfully and is currently running) and print a message to the console if this was the case. -
Here, we check if the outcome of loading the script was
RunResult.FAIL_ERROR
(which means the script did not load due to an error) and print a message to the console if this was the case.
Unloading a Script¶
-
Here, we import PySpigot as
ps
to utilize the script manager (script
). -
Here, we unload the script with the name
script.py
. We assign the returned value (True
orFalse
, depending on outcome) tounload_result
. -
Here, we check if the outcome of unloading the script was
True
(which means the script unloaded successfully without errors), and print a message to the console if this was the case.
Summary¶
- Use the script manager to load/run and unload a script from within another script.
- You must use both
loadScript
andrunScript
to load and run a script, as loading and running are two separate operations. loadScript
returns a script object that should be passed torunScript
. It will returnNone
if the script did not load successfully.runScript
returns aRunResult
which can be used to determine the outcome of running the script.unloadScript
returnsTrue
orFalse
,True
if the script unloaded successfully, orFalse
if it did not.- Never attempt to unload a script from within itself. This will lead to bugs/errors.