scriptor

High-level command-line abstraction


Keywords
command-line, library, program, subprocess
License
MIT
Install
pip install scriptor==0.2.0

Documentation

Scriptor

Run command-line programs in Python


Pypi version build codecov Documentation Status PyPI pyversions

What is it?

Scriptor is a high-level library for command-line. Scriptor makes it easy to integrate other CLI programs to your Python application.

Core features:

  • Run programs sync or async using the same syntax
  • High-level program abstraction
  • Easy program parametrization

Install it from PyPI:

pip install scriptor

Why Scriptor?

Scriptor abstracts subprocess and asyncio.subprocess to the same syntax making it easy to use both of them and switch between.

>>> from scriptor import Program
>>> python = Program("python3")

>>> # Call the program (and wait for finish)
>>> python("myscript.py")

More Examples

Here are some examples:

>>> # Parametrize a script
>>> python("myscript.py", report_date="2022-11-11")

>>> # Use different current working directory
>>> python.use(cwd="path/to/dir")("myscript.py")

>>> # Run script with output (in stdout)
>>> python("print_hello.py")
'Hello world'

>>> # Run failing script
>>> python("failing.py")
Traceback (most recent call last):
...
scriptor.process.ProcessError: Traceback (most recent call last):
  File "failing.py", line 1, in <module>
    raise RuntimeError("Oops!")
RuntimeError: Oops!

Start a process:

>>> process = python.start("print_hello.py")
>>> process.finished
False

>>> # Wait for the process to finish
>>> process.wait()

>>> # Raise error if process failed
>>> process.raise_for_return()

>>> # Read the results
>>> process.read()
'Hello world'

Some more examples with async:

>>> # Parametrize a script
>>> await python.call_async("myscript.py", report_date="2022-11-11")

>>> # Run script with output (in stdout)
>>> await python.call_async("print_hello.py")
'Hello world'

Start with async:

>>> process = await python.start_async("print_hello.py")
>>> process.finished
False

>>> # Wait for the process to finish
>>> process.wait()

>>> # Raise error if process failed
>>> process.raise_for_return()

>>> # Read the results
>>> process.read()
'Hello world'

Change settings ie. the current working directory:

>>> git = Program('git')
>>> repo_1 = git.use(cwd="path/to/repo_1")
>>> repo_2 = git.use(cwd="path/to/repo_2")
>>> repo_1("status")
"""On branch main
nothing to commit, working tree clean"""

See more from the documentation.

If the library helped you, consider buying a coffee for the maintainer .

Author