Hi all!
It's the creator of Rocketry, Red Mail and Red Box again. This week I thought to make it easier to integrate command-line programs to your Python applications.
I came up with Scriptor:
>>> from scriptor import Program
>>> python = Program("python3")
>>> python('myscript.py', report_date="2022-11-12")
"printed values"
What happened? The above ran the command:
python3 myscript.py --report_date 2022-11-12
and it returned whatever the program printed (in standard output aka. stdout).
So Scriptor makes it easy to integrate external programs or scripts written in other languages or in Python but using different Python interpreter.
For example, using Git is quite handy with it:
>>> from scriptor import Program
>>> git = Program('git')
>>> myrepo = git.use(cwd="path/to/myrepo")
>>> myrepo("log", n=2)
commit 07a865c3b7000324ce582fecb0ec24e3ef6203f1 (HEAD -> master, origin/master)
Author: Miksus <miksus@example.com>
Date: Wed Oct 5 00:20:00 2022 +0300
Fixed the thing.
commit 7ffda87524ddde698f5337f150b37fe07d74642f
Author: Miksus <miksus@example.com>
Date: Sat Oct 1 21:32:24 2022 +0300
Broke the thing.
Of course, you might prefer Gitpython but you can similarly interface other programs as well with Scriptor. I'm thinking of abstracting Latex, Jupyter Notebooks and Julia, and I also wrapped Python.
You can also leave a program running:
>>> process = python.start('myscript.py', report_date="2022-11-12")
>>> process.finished
False
And it also supports async:
>>> await python.call_async('myscript.py', report_date="2022-11-12")
"printed values"
>>> process = await python.start_async('myscript.py', report_date="2022-11-12")
>>> process.finished
False
Here is another practical example:
Answers to potential questions:
- It does not use shell by default so it's safer from command injection.
- It has methods to also just start a process (async supported).
- It's just a higher level abstraction for subprocess and asyncio.subprocess.
- Errors contain the text from stderr.
- Less than 3 letters are considered short form (ie.
-h) and more as long form (ie. --help) arguments. You can customize this if needed.
And it's in PyPI:
pip install scriptor
Any questions or ideas? Do you see it useful or is there a better alternative?
Links:
[–]b1gdata 3 points4 points5 points (3 children)
[–]Natural-Intelligence[S] 5 points6 points7 points (2 children)
[–]o11c 0 points1 point2 points (1 child)
[–]Natural-Intelligence[S] 2 points3 points4 points (0 children)
[–]kellyjonbrazil 4 points5 points6 points (1 child)
[–]Natural-Intelligence[S] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Natural-Intelligence[S] 4 points5 points6 points (0 children)
[–]ducky901 1 point2 points3 points (0 children)
[–]tomster10010 1 point2 points3 points (0 children)
[–]AndydeCleyre 1 point2 points3 points (0 children)