Creating Program¶
There are two ways to create a program.
The simplest one is to just create one
from the Program
class:
from scriptor import Program
git = Program('git')
Alternatively you can subclass BaseProgram
if you want to make an interface for your
program, ie.:
from scriptor import BaseProgram
class Git(BaseProgram):
program = 'git'
def fetch(self):
"Run command: git fetch"
return self('fetch')
def log(self, n):
"Run command: git log -n ..."
return self('log', n=n)
Then to create a program instance:
git = Git()
Changing Settings¶
Often it is desired to have specific settings for running a program. For example, you may want to run a program with a different current working directory (cwd) than where the current program is.
Here is an example:
>>> repo_1 = git.use(cwd="path/to/repo_1")
>>> repo_2 = git.use(cwd="path/to/repo_2")
>>> # Run git status with repo_1 as CWD
>>> repo_1("status")
...
Note
Method program.use(...)
copies the instance
thus you can easily create copies to run programs
in different directories or settings.
Settings¶
- cwd¶
Current working directory used for running the program. By default same as current.
- timeout¶
Number of seconds to wait before terminating the program due to timeout. By default None.
- arg_form:
'long'
,'short'
orNone
¶ Form of the argument, either
long
orshort
. IfNone
, the argument form is interpret from the length of the key. By default None.- default_kwargs: Dict¶
Default keyword arguments to pass all calls.
- output_type:
'str'
or'bytes'
¶ Type of the program output (stdout), either
'str'
or'bytes'
. By default'str'
.- output_parser: callable¶
Output parser. By default None.
Note
By default, Scriptor does not use shell to avoid command injection.