Skip to main content

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

Project description

Py_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: Py_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

Install it using pip:

pip install py-scriptor

Note: Installation will take approximately 2 seconds at 100 kb/s of internet speed

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.

Comments: The py_scriptor supports two styles of comments: Style 1: Starts with # and ends before >>>#; Style 2: Starts with #>>># and ends before ;

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

Note: newlines are deleted inside RuleScript init code

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) -> bool

    • Executes the rules for up to max_steps iterations. Returns True on successful completion, False on import or execution errors.
  • getImports() -> str

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

Usage examples

Basic example:

from py_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)

IDE

Runing IDE:

from py_scriptor import scriptorIDE

Note: Scripts are saved as json files.

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.
  • 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.

Github

Link to github: https://github.com/libmaster169/py-scriptor

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.3.tar.gz (5.1 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.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_scriptor-0.1.3.tar.gz
  • Upload date:
  • Size: 5.1 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.3.tar.gz
Algorithm Hash digest
SHA256 22434e2cfc20be27b7af437be0e55f9081df16c579e65efdbfc44a6c67dc2580
MD5 ee996778e1e00b20e8d3986d5fc3564b
BLAKE2b-256 6c1a0be9ea98ba815313af7324bd4e44cd04a61c7a2d902a321482ca1478ce90

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_scriptor-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5912c994e434dee5bcc998bc40994dfafc2b591117b2c4fb90d90d49ed620b00
MD5 f1c8d592944ded8d8bd261a064c6c413
BLAKE2b-256 a9402fd264a5c3c8a11872256b7b4425939d2cc73849b163465217076e687144

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