Skip to content

Commit

Permalink
add feature to load commands from pyqwe.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
CheeseCake87 committed Aug 30, 2024
1 parent 188d667 commit c880137
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The Quick Work Environment for Python.
![Downloads](https://static.pepy.tech/badge/pyqwe)
![black](https://img.shields.io/badge/code%20style-black-000000.svg)

Run commands quickly from the pyproject.toml file.
Run commands quickly from the pyproject.toml (or pyqwe.toml) file.

```bash
pip install pyqwe
Expand All @@ -32,6 +32,17 @@ Add commands to the pyproject.toml file.
flask = "flask_app:run"
say_hello = "*:echo Hello World"
```
**If you're using a pyqwe.toml file you can drop the `[tool.pyqwe]`**

```toml
flask = "flask_app:run"
say_hello = "*:echo Hello World"
```

🚨 **NOTE** 🚨

**If you have both a pyproject.toml and a pyqwe.toml file, the pyqwe.toml
file will be used and the pyproject.toml file will be ignored.**

You will be able to see what commands you have set in the pyproject.toml file by running:

Expand Down
42 changes: 35 additions & 7 deletions src/pyqwe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import traceback
from pathlib import Path

from .exceptions import InvalidRunner
from .helpers import _run, _split_runner, Colr
from .parser import ArgumentParser

Expand All @@ -13,16 +14,32 @@
except ImportError:
raise ImportError("pyqwe requires toml, install it with 'pip install toml'")

__version__ = "1.6.2"
__version__ = "1.7.0"

_cwd = Path().cwd()
_pyproject_file = _cwd / "pyproject.toml"
_known_toml_files = [
_cwd / "pyqwe.toml",
_cwd / "pyproject.toml",
]

if not _pyproject_file.exists():
raise FileNotFoundError("pyproject.toml not found")

_pyproject = tomllib.loads(_pyproject_file.read_text())
_qwe = _pyproject.get("tool", {}).get("pyqwe", {})
def _find_toml_file() -> Path:
for file in _known_toml_files:
if file.exists():
return file
raise FileNotFoundError("pyproject.toml or pyqwe.toml file not found")


_toml_file = _find_toml_file()
_pyproject = tomllib.loads(_toml_file.read_text())

if _toml_file.name == "pyqwe.toml":
if _pyproject.get("tool", {}).get("pyqwe", {}):
_qwe = _pyproject.get("tool", {}).get("pyqwe", {})
else:
_qwe = _pyproject
else:
_qwe = _pyproject.get("tool", {}).get("pyqwe", {})


def main():
Expand All @@ -49,14 +66,25 @@ def main():
if hasattr(args, "list") or hasattr(args, "ls"):
print("")
if not pars.options:
print(f" {Colr.WARNING}No commands found in pyproject.toml{Colr.END}")
print(f" {Colr.WARNING}No commands found, looking in: {_toml_file.name}{Colr.END}")
else:
for option in pars.options:

try:
if not isinstance(option[1], str):
raise InvalidRunner()
except Exception as e:
_ = e
print(f"💥🏎️⁉️ {Colr.FAIL}Invalid runner: {option}{Colr.END}")
print("")
sys.exit(0)

print(
f" {Colr.OKCYAN}{option[0]}{Colr.END} "
f"{Colr.BOLD}=>{Colr.END} "
f"{Colr.HEADER}{option[1]}{Colr.END}"
)

print("")
sys.exit(0)

Expand Down
4 changes: 4 additions & 0 deletions src/pyqwe/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ class NotAFunction(Exception):
pass


class InvalidRunner(Exception):
pass


class EnvVarNotFound(Exception):
pass

0 comments on commit c880137

Please sign in to comment.