A small package to run external command lines.
Project description
# pyxtern
This package provides decorators and methods to run any external command line in a proper maner. It allows the creation of any command line python interface with ease.
## How to install
Since `pyxtern` is hosted on [PyPI](https://pypi.org/project/pyxtern/), it can be installed using:
```shell
pip install pyxtern
```
## How to use
The package offers two different ways to run a command line through python script:
1. Use the `run()` function:
```python
from pyxtern import run
# To run the following command:
# $ find . -name *.py
# Use either:
exit, stdout, stderr = run("find . -name *.py")
# Or:
exit, stdout, stderr = run(["find", ".", "-name", "*.py"])
```
2. Use the `@xtern` decorator:
```python
from pyxtern import xtern, format_arg
# To wrap the 'find' command:
# $ find . -name *.py
@xtern
def cmd_find(*args, **kwargs):
cmd = ["find"]
cmd.append(kwargs.get("path", "."))
cmd.extend(
format_arg(
"name",
val=kwargs.get("expr", None),
fmt="- "))
return cmd
# Now to use it simply write:
cmd_find(expr="*.py")
```
The more complete example for the `find` command is available [here](https://gitlab.com/mar.grignard/pyxtern/blob/master/examples/example_find.py).
## Arguments
The `run()` function accepts some arguments:
- `dir`: The directory where you want to create the temporary directory in which the external command will be ran. If `None`, the system default temporary directory is used.
- `tee`: If set to `True`, the stdout and stderr streams of the external command are rederected to the current stdout and stderr.
- `log`: A tuple givind (stdout, stderr) for the caller. If they are provided, the stdout and stderr streams of the external command are rederected to these.
Both `tee` and `log` can be used at the same time.
When you use the `@xtern` decorator, these arguments can be passed to the function through `**kwargs`.
## Returns
As shown before, the `run()` function has 3 return values:
- `exit`: The exit code of the external command.
- `stdo`: The stdout of the external command.
- `stde`: The stderr of the external command.
This package provides decorators and methods to run any external command line in a proper maner. It allows the creation of any command line python interface with ease.
## How to install
Since `pyxtern` is hosted on [PyPI](https://pypi.org/project/pyxtern/), it can be installed using:
```shell
pip install pyxtern
```
## How to use
The package offers two different ways to run a command line through python script:
1. Use the `run()` function:
```python
from pyxtern import run
# To run the following command:
# $ find . -name *.py
# Use either:
exit, stdout, stderr = run("find . -name *.py")
# Or:
exit, stdout, stderr = run(["find", ".", "-name", "*.py"])
```
2. Use the `@xtern` decorator:
```python
from pyxtern import xtern, format_arg
# To wrap the 'find' command:
# $ find . -name *.py
@xtern
def cmd_find(*args, **kwargs):
cmd = ["find"]
cmd.append(kwargs.get("path", "."))
cmd.extend(
format_arg(
"name",
val=kwargs.get("expr", None),
fmt="- "))
return cmd
# Now to use it simply write:
cmd_find(expr="*.py")
```
The more complete example for the `find` command is available [here](https://gitlab.com/mar.grignard/pyxtern/blob/master/examples/example_find.py).
## Arguments
The `run()` function accepts some arguments:
- `dir`: The directory where you want to create the temporary directory in which the external command will be ran. If `None`, the system default temporary directory is used.
- `tee`: If set to `True`, the stdout and stderr streams of the external command are rederected to the current stdout and stderr.
- `log`: A tuple givind (stdout, stderr) for the caller. If they are provided, the stdout and stderr streams of the external command are rederected to these.
Both `tee` and `log` can be used at the same time.
When you use the `@xtern` decorator, these arguments can be passed to the function through `**kwargs`.
## Returns
As shown before, the `run()` function has 3 return values:
- `exit`: The exit code of the external command.
- `stdo`: The stdout of the external command.
- `stde`: The stderr of the external command.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyxtern-1.0.1.tar.gz
(4.0 kB
view hashes)
Built Distribution
Close
Hashes for pyxtern-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a84c6c65ee06b6f6d460468a160be4e7c7e1e7ad0e29bef91529a18ba797e2d9 |
|
MD5 | f91b695899072265ac1b6898ff5ae17e |
|
BLAKE2b-256 | 54b1e1681d493cfb28cf68e33aaf3c28f369698ca20c24070a7a39c035d1bb06 |