Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ test:
dev:
python run.py

debug:
python -m ideas demo -a pybash -s

clean:
rm -rf build/ dist/ *.egg-info .pytest_cache
find . -name '*.pyc' -type f -exec rm -rf {} +
Expand All @@ -40,6 +37,6 @@ shell:
source $(poetry env info --path)/bin/activate

debug:
python -m ideas demo -a pybash.hook -s
python -m ideas examples.demo -a pybash.hook -s

.PHONY: test clean
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ from pybash.transformer import transform
transform(">echo hello world") # returns the python code for the bash command as string
```

## As ideas hook
`pip install "pybash[ideas]"`
## As script runner
`pip install "pybash[script]"`

See [run.py](run.py) for an example.

# Usage
### Example
```py
#
text = "HELLO WORLD"
>echo f{text}
```

### Run script:
```bash
$ python -m pybash hello.py
```

# Supported transforms

### 1. Simple execution with output
```python
Expand Down Expand Up @@ -155,7 +166,8 @@ cp_test()
# Dev

#### Demo
`python run.py`
`python -m pybash examples/hello.py`
`python -m pybash demo`

#### Debug
`make debug` to view the transformed source code
1 change: 1 addition & 0 deletions demo.py → examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#print(f"test: {a}")

# 1. use inside methods
>echo "SORT TEST\nHELLO WORLD" > test.txt
def cp_test():
print("1. >cp test.txt test_copy.txt")
>cp test.txt test_copy.txt
Expand Down
3 changes: 3 additions & 0 deletions examples/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

text = "HELLO WORLD"
>echo f{text}
26 changes: 26 additions & 0 deletions pybash/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
##
# Setup hook
import importlib.util
import sys
from pathlib import Path

from pybash.hook import add_hook

hook = add_hook()

# Run script dynamically
if __name__ == "__main__":
spec = None

if path_or_module := sys.argv[1]:
file_path = Path(path_or_module)
if file_path.exists() and file_path.is_file():
spec = hook.find_spec(path_or_module.split('.py')[0], None)
else:
spec = importlib.util.find_spec(path_or_module)

if spec and spec.loader:
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
else:
print(f"Could not import {path_or_module}")
2 changes: 1 addition & 1 deletion pybash/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def source_init():
return "import subprocess"


def add_hook(**_kwargs):
def add_hook():
"""Creates and automatically adds the import hook in sys.meta_path"""
return import_hook.create_hook(
hook_name=__name__,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "PyBash"
version = "0.3.0"
version = "0.3.1"
description = ">execute bash commands from python easily"
authors = ["Jay <jay.github0@gmail.com>"]
readme = "README.md"
Expand All @@ -12,7 +12,7 @@ ideas = { version = "^0.1.5", optional = true }
token-utils = "^0.1.8"

[tool.poetry.extras]
ideas = ["ideas"]
script = ["ideas"]

[tool.poetry.group.dev.dependencies]
black = "^22.12.0"
Expand Down
2 changes: 0 additions & 2 deletions test.txt

This file was deleted.