Skip to main content

Scriptor is a small Python library for defining and running simple rule-based scripts

Project description

Scriptor

Scriptor is a small Python library for defining and running simple rule-based scripts. A script is a sequence of conditional rules that, when a condition is met, execute one or more actions.

Security: Scriptor uses eval() and exec(). Do not run untrusted or unreviewed scripts.

Features

  • Compact rule format (condition -> actions).
  • Injection of variables into the script execution environment.
  • Optional importing of modules via a prefix in the script.
  • Minimal IDE environment (scriptorIDE.py) for editing and running scripts.

Installation

Clone the repository and (optionally) install it in editable mode:

git clone https://github.com/libmaster169/py-scriptor.git
cd py-scriptor
pip install -e .

Or add the repository folder to PYTHONPATH.

Script format

A script is a single string of rules separated by semicolons (;). Each rule uses >>> to separate the condition from the actions. Multiple actions are separated by commas.

Optional imports: prefix the script with module names separated by ::, followed by the script body.

Format: module1::module2::...::condition >>> action1, action2; condition2 >>> action3

  • condition: a Python expression evaluated with the provided variables (eval).
  • action: a Python statement executed with the provided variables (exec).

Examples:

  • x > 0 >>> x -= 1
  • math::x > 0 >>> print(math.sqrt(x)), x -= 1

API

Class: RuleScript (in py_scriptor/init.py)

  • RuleScript(script: str, vars: dict)

    • script: the script string. To import modules, use the prefix "module::...::script_body".
    • vars: a dict of variables available to conditions and actions. Imported modules are added to this dict under the module name.
  • setVar(var: str, value)

    • Sets a variable in the runtime dictionary.
  • getVar(var: str) -> Any

    • Retrieves a variable from the runtime dictionary.
  • run(max_steps: int = 1000, cps: int = 0) -> bool

    • Executes the rules for up to max_steps iterations. Returns True on successful completion, False on import or execution errors.
    • Note: the cps parameter is not currently used.
  • getImports() -> str

    • Returns the import line text for the imported modules (e.g., import math).

Usage examples

Basic example:

from scriptor import RuleScript

script = "x > 0 >>> print(x), x -= 1; x == 0 >>> print('done')"
vars = {"x": 3}

rs = RuleScript(script, vars)
rs.run(max_steps=10)

With module import:

script = "math::x > 0 >>> print(math.sqrt(x)), x -= 1"
vars = {"x": 4}
rs = RuleScript(script, vars)
rs.run(max_steps=10)

Running the IDE (scriptorIDE.py):

python scriptorIDE.py

Security

Scriptor executes Python code using eval/exec. Do not run scripts from unknown sources. For safer execution consider:

  • Running in an isolated process/container.
  • Parsing the AST and restricting allowed operations.
  • Limiting available globals.

Contributing

Contributions are welcome. Suggestions:

  • Improve the parser (multi-line scripts, comments).
  • Add a safe execution mode (restricted globals/AST).
  • Add unit tests and CI.

Open an issue before larger changes, then submit a pull request with tests.

License

The LICENSE file contains the MIT license.

Issues / Contact

Report bugs or feature requests at: https://github.com/libmaster169/py-scriptor/issues

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_scriptor-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_scriptor-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file py_scriptor-0.1.0.tar.gz.

File metadata

  • Download URL: py_scriptor-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.14

File hashes

Hashes for py_scriptor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 58e6715bba03c85f11a41258fbff7829aa650353f0dc1c01e6394f2185535329
MD5 0275b3b808c4997b7c3925fd26fce201
BLAKE2b-256 ca758e1be9fc397124cf55fd11b01c9ad8bdf2cb97cabea20cc9a9d12d952dfe

See more details on using hashes here.

File details

Details for the file py_scriptor-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_scriptor-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.14

File hashes

Hashes for py_scriptor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef0723fda96b974db01df83ac555bcfffa6eb484b83b363c0427220de7388e8f
MD5 b811932eb2d6dd7c5378557d32156442
BLAKE2b-256 65614304825094ab77db96db8bd8b9def3093e83786f3f350cf75fc41e802fe1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page