sigil-cli 1.1.1__tar.gz → 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sigil_cli-1.1.1/src/sigil_cli.egg-info → sigil_cli-1.2.0}/PKG-INFO +31 -8
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/README.md +30 -7
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/pyproject.toml +4 -2
- sigil_cli-1.2.0/src/sigil/__init__.py +31 -0
- sigil_cli-1.2.0/src/sigil/__main__.py +30 -0
- sigil_cli-1.2.0/src/sigil/cli_tools/validate.py +45 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/entrypoint.py +19 -5
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/models/argument.py +12 -10
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/models/command.py +1 -1
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/stages/builder.py +2 -3
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/stages/resolver.py +0 -2
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/stages/script_loader.py +22 -6
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/stages/yaml_reader.py +22 -14
- {sigil_cli-1.1.1 → sigil_cli-1.2.0/src/sigil_cli.egg-info}/PKG-INFO +31 -8
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil_cli.egg-info/SOURCES.txt +3 -1
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/tests/test_integration.py +46 -1
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/tests/test_reader.py +1 -1
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/tests/test_scriptloader.py +1 -1
- sigil_cli-1.2.0/tests/test_validate.py +263 -0
- sigil_cli-1.1.1/src/sigil/__init__.py +0 -27
- sigil_cli-1.1.1/src/sigil/__main__.py +0 -20
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/LICENSE +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/setup.cfg +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/cli_tools/__init__.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/cli_tools/init.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/models/__init__.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/models/argparser.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/models/script.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil/stages/__init__.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil_cli.egg-info/dependency_links.txt +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil_cli.egg-info/entry_points.txt +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil_cli.egg-info/requires.txt +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/src/sigil_cli.egg-info/top_level.txt +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/tests/test_builder.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/tests/test_cli_tools.py +0 -0
- {sigil_cli-1.1.1 → sigil_cli-1.2.0}/tests/test_resolver.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sigil-cli
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Configuration driven CLI builder with subcommands and script loading
|
|
5
5
|
Author-email: Kenzo Staelens <kenzostael@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -32,10 +32,11 @@ Dynamic: license-file
|
|
|
32
32
|
|
|
33
33
|
> Declarative argparse, without the CLI boilerplate.
|
|
34
34
|
|
|
35
|
+
|
|
35
36
|
Sigil is a lightweight, declarative CLI framework for Python. Define your command tree in YAML (or any other format), and sigil builds the `argparse` parser on the fly. Complete with subcommands and dynamic script loading.
|
|
36
37
|
It plays nicely with `argcomplete` out of the box.
|
|
37
38
|
|
|
38
|
-
[](https://pypi.python.org/pypi/sigil-cli)
|
|
39
40
|
[](https://pypi.org/project/sigil-cli/)
|
|
40
41
|
[](https://www.python.org/downloads/release/python-3100/)
|
|
41
42
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -43,6 +44,10 @@ It plays nicely with `argcomplete` out of the box.
|
|
|
43
44
|
[](https://github.com/kenzo-staelens/sigil/actions/workflows/tests.yml)
|
|
44
45
|
[](https://coveralls.io/github/kenzo-staelens/sigil?branch=main)
|
|
45
46
|
|
|
47
|
+
<div align="center">
|
|
48
|
+
<sub>in production since April 2026</sub>
|
|
49
|
+
</div>
|
|
50
|
+
|
|
46
51
|
---
|
|
47
52
|
|
|
48
53
|
## Features
|
|
@@ -80,7 +85,7 @@ pip install sigil-cli[completion]
|
|
|
80
85
|
|
|
81
86
|
```text
|
|
82
87
|
project_root/
|
|
83
|
-
├── mycli.py # drop‑in
|
|
88
|
+
├── mycli.py # drop‑in bootstrap script (alias this)
|
|
84
89
|
├── manifest.yml # lists all YAML config files to load
|
|
85
90
|
├── yml/
|
|
86
91
|
│ ├── root.yml # root command definition
|
|
@@ -91,12 +96,15 @@ project_root/
|
|
|
91
96
|
└── ... # other scripts
|
|
92
97
|
```
|
|
93
98
|
|
|
99
|
+
ps: don't shoot yourself in the foot, don't symlink the bootstrap script.
|
|
100
|
+
|
|
94
101
|
### 1. Entry script
|
|
95
102
|
|
|
96
103
|
Create `mycli.py`:
|
|
97
104
|
|
|
98
105
|
```python
|
|
99
106
|
#!/usr/bin/env python3
|
|
107
|
+
# PYTHON_ARGCOMPLETE_OK
|
|
100
108
|
from pathlib import Path
|
|
101
109
|
from sigil import run_from_config
|
|
102
110
|
|
|
@@ -166,7 +174,7 @@ chmod +x mycli.py
|
|
|
166
174
|
|
|
167
175
|
## Configuration Reference
|
|
168
176
|
|
|
169
|
-
### Root
|
|
177
|
+
### Root Command
|
|
170
178
|
|
|
171
179
|
| Field | Description |
|
|
172
180
|
| --- | --- |
|
|
@@ -180,12 +188,13 @@ chmod +x mycli.py
|
|
|
180
188
|
| `name` | Subcommand name |
|
|
181
189
|
| `parent` | Parent command (must exist elsewhere in a config) |
|
|
182
190
|
| `help` | Help text for this subcommand |
|
|
183
|
-
| `script` | Python module name (without `.py`) inside `script_dir
|
|
191
|
+
| `script` | Python module name (without `.py`) inside `script_dir`, absolute paths supported |
|
|
184
192
|
| `args` | List of argument definitions (see below) |
|
|
185
193
|
| `default` | If `true`, this subcommand is used when no subcommand is given |
|
|
186
|
-
|
|
|
194
|
+
| `load_ignore`| If `true` skips this command (or top level object) from being loaded into the command tree|
|
|
195
|
+
| any other parser kwarg | except for `dest`, `parents` and `formatter_class` they are all supported |
|
|
187
196
|
|
|
188
|
-
Note that `parent` does not refer to argparse's
|
|
197
|
+
Note that `parent` does not refer to argparse's `parents` parameter but is only used to resolve the parser tree. Parser (multi-)inheritance isn't supported but can be emulated by adding arguments to `parent` commands in the tree.
|
|
189
198
|
|
|
190
199
|
### Argument
|
|
191
200
|
|
|
@@ -215,10 +224,24 @@ Groups and mutex groups are also suppored via the "kind" parameter (defaults to
|
|
|
215
224
|
The `name` field can be `--flag` for flags or a string for positional arguments.
|
|
216
225
|
Both literal string and list of strings are supported.
|
|
217
226
|
|
|
227
|
+
Types (`type:`) only supports python builtins
|
|
228
|
+
|
|
229
|
+
### Script files
|
|
230
|
+
|
|
231
|
+
Each script files have as only requirement that they need to define a `def run(args: argparse.Namespace, ctx: dict[str, Any]) -> None` method.
|
|
232
|
+
|
|
233
|
+
args is the by argparse supplied namespace (parsed with parse_known_args), any additional args can be found in `ctx['other_args']`
|
|
234
|
+
|
|
235
|
+
scripts run in sequence from command -> subcommand -> sub sub command -> ... and each may add to, remove or otherwise modify args.namespace and ctx to enrich or modify the behaviour of supsequent scripts.
|
|
236
|
+
|
|
237
|
+
### Misc
|
|
238
|
+
|
|
239
|
+
`load_ignore` may be used to detaching commands from the command tree for any purpose (deprecation, development, etc) or for non schema-compliant objects at the top level of a file, this may be useful to define anchors or references that should not directly be read as a command.
|
|
240
|
+
|
|
218
241
|
## Tab‑Completion (argcomplete)
|
|
219
242
|
|
|
220
243
|
Sigil registers itself with `argcomplete` automatically if available on your system.
|
|
221
|
-
To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script:
|
|
244
|
+
To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script (or use the builtin argcomplete comment):
|
|
222
245
|
|
|
223
246
|
```bash
|
|
224
247
|
pip install argcomplete
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
> Declarative argparse, without the CLI boilerplate.
|
|
4
4
|
|
|
5
|
+
|
|
5
6
|
Sigil is a lightweight, declarative CLI framework for Python. Define your command tree in YAML (or any other format), and sigil builds the `argparse` parser on the fly. Complete with subcommands and dynamic script loading.
|
|
6
7
|
It plays nicely with `argcomplete` out of the box.
|
|
7
8
|
|
|
8
|
-
[](https://pypi.python.org/pypi/sigil-cli)
|
|
9
10
|
[](https://pypi.org/project/sigil-cli/)
|
|
10
11
|
[](https://www.python.org/downloads/release/python-3100/)
|
|
11
12
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -13,6 +14,10 @@ It plays nicely with `argcomplete` out of the box.
|
|
|
13
14
|
[](https://github.com/kenzo-staelens/sigil/actions/workflows/tests.yml)
|
|
14
15
|
[](https://coveralls.io/github/kenzo-staelens/sigil?branch=main)
|
|
15
16
|
|
|
17
|
+
<div align="center">
|
|
18
|
+
<sub>in production since April 2026</sub>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
16
21
|
---
|
|
17
22
|
|
|
18
23
|
## Features
|
|
@@ -50,7 +55,7 @@ pip install sigil-cli[completion]
|
|
|
50
55
|
|
|
51
56
|
```text
|
|
52
57
|
project_root/
|
|
53
|
-
├── mycli.py # drop‑in
|
|
58
|
+
├── mycli.py # drop‑in bootstrap script (alias this)
|
|
54
59
|
├── manifest.yml # lists all YAML config files to load
|
|
55
60
|
├── yml/
|
|
56
61
|
│ ├── root.yml # root command definition
|
|
@@ -61,12 +66,15 @@ project_root/
|
|
|
61
66
|
└── ... # other scripts
|
|
62
67
|
```
|
|
63
68
|
|
|
69
|
+
ps: don't shoot yourself in the foot, don't symlink the bootstrap script.
|
|
70
|
+
|
|
64
71
|
### 1. Entry script
|
|
65
72
|
|
|
66
73
|
Create `mycli.py`:
|
|
67
74
|
|
|
68
75
|
```python
|
|
69
76
|
#!/usr/bin/env python3
|
|
77
|
+
# PYTHON_ARGCOMPLETE_OK
|
|
70
78
|
from pathlib import Path
|
|
71
79
|
from sigil import run_from_config
|
|
72
80
|
|
|
@@ -136,7 +144,7 @@ chmod +x mycli.py
|
|
|
136
144
|
|
|
137
145
|
## Configuration Reference
|
|
138
146
|
|
|
139
|
-
### Root
|
|
147
|
+
### Root Command
|
|
140
148
|
|
|
141
149
|
| Field | Description |
|
|
142
150
|
| --- | --- |
|
|
@@ -150,12 +158,13 @@ chmod +x mycli.py
|
|
|
150
158
|
| `name` | Subcommand name |
|
|
151
159
|
| `parent` | Parent command (must exist elsewhere in a config) |
|
|
152
160
|
| `help` | Help text for this subcommand |
|
|
153
|
-
| `script` | Python module name (without `.py`) inside `script_dir
|
|
161
|
+
| `script` | Python module name (without `.py`) inside `script_dir`, absolute paths supported |
|
|
154
162
|
| `args` | List of argument definitions (see below) |
|
|
155
163
|
| `default` | If `true`, this subcommand is used when no subcommand is given |
|
|
156
|
-
|
|
|
164
|
+
| `load_ignore`| If `true` skips this command (or top level object) from being loaded into the command tree|
|
|
165
|
+
| any other parser kwarg | except for `dest`, `parents` and `formatter_class` they are all supported |
|
|
157
166
|
|
|
158
|
-
Note that `parent` does not refer to argparse's
|
|
167
|
+
Note that `parent` does not refer to argparse's `parents` parameter but is only used to resolve the parser tree. Parser (multi-)inheritance isn't supported but can be emulated by adding arguments to `parent` commands in the tree.
|
|
159
168
|
|
|
160
169
|
### Argument
|
|
161
170
|
|
|
@@ -185,10 +194,24 @@ Groups and mutex groups are also suppored via the "kind" parameter (defaults to
|
|
|
185
194
|
The `name` field can be `--flag` for flags or a string for positional arguments.
|
|
186
195
|
Both literal string and list of strings are supported.
|
|
187
196
|
|
|
197
|
+
Types (`type:`) only supports python builtins
|
|
198
|
+
|
|
199
|
+
### Script files
|
|
200
|
+
|
|
201
|
+
Each script files have as only requirement that they need to define a `def run(args: argparse.Namespace, ctx: dict[str, Any]) -> None` method.
|
|
202
|
+
|
|
203
|
+
args is the by argparse supplied namespace (parsed with parse_known_args), any additional args can be found in `ctx['other_args']`
|
|
204
|
+
|
|
205
|
+
scripts run in sequence from command -> subcommand -> sub sub command -> ... and each may add to, remove or otherwise modify args.namespace and ctx to enrich or modify the behaviour of supsequent scripts.
|
|
206
|
+
|
|
207
|
+
### Misc
|
|
208
|
+
|
|
209
|
+
`load_ignore` may be used to detaching commands from the command tree for any purpose (deprecation, development, etc) or for non schema-compliant objects at the top level of a file, this may be useful to define anchors or references that should not directly be read as a command.
|
|
210
|
+
|
|
188
211
|
## Tab‑Completion (argcomplete)
|
|
189
212
|
|
|
190
213
|
Sigil registers itself with `argcomplete` automatically if available on your system.
|
|
191
|
-
To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script:
|
|
214
|
+
To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script (or use the builtin argcomplete comment):
|
|
192
215
|
|
|
193
216
|
```bash
|
|
194
217
|
pip install argcomplete
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "sigil-cli"
|
|
7
|
-
version = "1.
|
|
7
|
+
version = "1.2.0"
|
|
8
8
|
description = "Configuration driven CLI builder with subcommands and script loading"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -42,5 +42,7 @@ where = ["src"]
|
|
|
42
42
|
|
|
43
43
|
[tool.coverage.run]
|
|
44
44
|
omit = [
|
|
45
|
-
"src/sigil/__main__.py"
|
|
45
|
+
"src/sigil/__main__.py",
|
|
46
|
+
"tests/*",
|
|
47
|
+
"*/scripts/*"
|
|
46
48
|
]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from importlib.metadata import version
|
|
2
|
+
|
|
3
|
+
__version__ = version('sigil-cli')
|
|
4
|
+
|
|
5
|
+
from .entrypoint import run_from_config
|
|
6
|
+
from .models import (
|
|
7
|
+
Argument,
|
|
8
|
+
ArgumentGroup,
|
|
9
|
+
LibArgParser,
|
|
10
|
+
ParserConfig,
|
|
11
|
+
SubcommandModule,
|
|
12
|
+
)
|
|
13
|
+
from .stages import (
|
|
14
|
+
Builder,
|
|
15
|
+
Resolver,
|
|
16
|
+
ScriptLoader,
|
|
17
|
+
YamlReader,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = (
|
|
21
|
+
'run_from_config',
|
|
22
|
+
'Argument',
|
|
23
|
+
'ArgumentGroup',
|
|
24
|
+
'LibArgParser',
|
|
25
|
+
'ParserConfig',
|
|
26
|
+
'SubcommandModule',
|
|
27
|
+
'YamlReader',
|
|
28
|
+
'Resolver',
|
|
29
|
+
'Builder',
|
|
30
|
+
'ScriptLoader',
|
|
31
|
+
)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
|
|
3
|
+
from sigil import __version__
|
|
4
|
+
from sigil.cli_tools.init import create_project
|
|
5
|
+
from sigil.cli_tools.validate import validate_project
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main():
|
|
9
|
+
# let's not start self hosting here
|
|
10
|
+
parser = argparse.ArgumentParser(prog='sigil')
|
|
11
|
+
parser.add_argument('--version', action='version', version=f'Sigil {__version__}')
|
|
12
|
+
sub = parser.add_subparsers(title="subcommands", dest='command')
|
|
13
|
+
|
|
14
|
+
init = sub.add_parser(name="init", help="generate a minimal sigil")
|
|
15
|
+
init.add_argument('name', help="project name to create")
|
|
16
|
+
|
|
17
|
+
validate = sub.add_parser(name="validate", help="validate structure of a sigil")
|
|
18
|
+
validate.add_argument('path', help="project to validate")
|
|
19
|
+
|
|
20
|
+
args = parser.parse_args()
|
|
21
|
+
|
|
22
|
+
if args.command == 'init':
|
|
23
|
+
create_project(args.name)
|
|
24
|
+
return
|
|
25
|
+
if args.command == 'validate':
|
|
26
|
+
validate_project(args.path)
|
|
27
|
+
return
|
|
28
|
+
|
|
29
|
+
if __name__ == "__main__":
|
|
30
|
+
main()
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from sigil import Builder, Resolver, YamlReader
|
|
6
|
+
|
|
7
|
+
_logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
def validate_project(projectroot):
|
|
10
|
+
projectpath = Path(projectroot)
|
|
11
|
+
|
|
12
|
+
if not (projectpath/'manifest.yml').exists():
|
|
13
|
+
_logger.error('missing manifest.yml, aborting.')
|
|
14
|
+
sys.exit(1)
|
|
15
|
+
|
|
16
|
+
raw = YamlReader.load(projectroot)
|
|
17
|
+
|
|
18
|
+
# these are easier for us to check
|
|
19
|
+
if 'root' not in raw:
|
|
20
|
+
_logger.error('missing required root command')
|
|
21
|
+
sys.exit(2)
|
|
22
|
+
|
|
23
|
+
resolved = Resolver.resolve_inheritance(raw) #alerts basic structural errors for us
|
|
24
|
+
Builder.build(resolved) # catches the case for "only one default subcommand"
|
|
25
|
+
|
|
26
|
+
script_root = None
|
|
27
|
+
if not raw['root'].script_dir or not (projectpath/raw['root'].script_dir).exists():
|
|
28
|
+
_logger.warning('script_dir may be missing or unreachable')
|
|
29
|
+
else:
|
|
30
|
+
script_root = projectpath/raw['root'].script_dir
|
|
31
|
+
|
|
32
|
+
for c_id, command_def in raw.items():
|
|
33
|
+
if not command_def.help and c_id != 'root':
|
|
34
|
+
_logger.warning(f'command {c_id} may be missing help text')
|
|
35
|
+
if command_def.script:
|
|
36
|
+
if script_root:
|
|
37
|
+
if not (
|
|
38
|
+
script_root/f'{command_def.script}.py'
|
|
39
|
+
).exists():
|
|
40
|
+
_logger.warning(
|
|
41
|
+
f'script "{command_def.script}" defined on '
|
|
42
|
+
f'command "{c_id}" not found'
|
|
43
|
+
)
|
|
44
|
+
else:
|
|
45
|
+
_logger.warning(f'script defined on "{c_id}" but no script dir defined')
|
|
@@ -1,17 +1,27 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sys
|
|
1
3
|
from typing import Any
|
|
2
4
|
|
|
3
5
|
from .stages import Builder, Resolver, ScriptLoader, YamlReader
|
|
4
6
|
|
|
7
|
+
_logger = logging.getLogger(__name__)
|
|
5
8
|
|
|
6
9
|
# default to yamlloader, use whatever datastore you feel like
|
|
10
|
+
tmp = 0
|
|
7
11
|
def run_from_config(config_root: str, loader_class = YamlReader):
|
|
8
12
|
raw_data = loader_class.load(config_root)
|
|
9
13
|
resolved = Resolver.resolve_inheritance(raw_data)
|
|
10
14
|
parser = Builder.build(resolved)
|
|
11
|
-
args, other_args = parser.parse_known_args()
|
|
12
|
-
scripts = ScriptLoader.get_scripts(args, 'root', parser, {'root': resolved})
|
|
13
15
|
|
|
14
|
-
execution_context
|
|
16
|
+
execution_context = {}
|
|
17
|
+
|
|
18
|
+
if resolved.known_args:
|
|
19
|
+
args, other_args = parser.parse_known_args()
|
|
20
|
+
execution_context: dict[str, Any] = {'other_args': other_args}
|
|
21
|
+
else:
|
|
22
|
+
args = parser.parse_args()
|
|
23
|
+
|
|
24
|
+
scripts = ScriptLoader.get_scripts(args, 'root', parser, {'root': resolved})
|
|
15
25
|
for script in scripts:
|
|
16
26
|
try:
|
|
17
27
|
module = ScriptLoader.import_module(
|
|
@@ -19,7 +29,11 @@ def run_from_config(config_root: str, loader_class = YamlReader):
|
|
|
19
29
|
resolved.script_dir,
|
|
20
30
|
script
|
|
21
31
|
)
|
|
32
|
+
if not module:
|
|
33
|
+
continue
|
|
22
34
|
except FileNotFoundError as e:
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
# prevent your subcommand from turning your environment
|
|
36
|
+
# into undefined soup by not continuing execution
|
|
37
|
+
_logger.critical(f'failed to load script "{script}"\n {e}')
|
|
38
|
+
sys.exit(2)
|
|
25
39
|
module.run(args, execution_context)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import builtins
|
|
2
2
|
import logging
|
|
3
3
|
from dataclasses import dataclass, field
|
|
4
|
-
from typing import Any
|
|
4
|
+
from typing import Any, cast
|
|
5
5
|
|
|
6
6
|
AGGREGATE_TYPES = [
|
|
7
7
|
'group',
|
|
@@ -18,21 +18,24 @@ _logger = logging.getLogger(__name__)
|
|
|
18
18
|
|
|
19
19
|
@dataclass
|
|
20
20
|
class ArgumentGroup:
|
|
21
|
-
args: 'list[Argument]'
|
|
21
|
+
args: 'list[Argument | ArgumentGroup]'
|
|
22
22
|
mutex: bool
|
|
23
23
|
# anything else not specifically in here
|
|
24
24
|
kw: dict[str, Any] = field(default_factory=dict)
|
|
25
25
|
|
|
26
26
|
@classmethod
|
|
27
|
-
def factory(cls, **kwargs: dict) -> 'ArgumentGroup':
|
|
28
|
-
kind = kwargs.pop('kind')
|
|
27
|
+
def factory(cls, **kwargs: dict[str, Any]) -> 'ArgumentGroup':
|
|
28
|
+
kind: str = cast(str, kwargs.pop('kind', 'argument'))
|
|
29
29
|
# yes argparse only supports these 2 groupings for now
|
|
30
30
|
# but it can't hurt to not write an if/else ladder in the future
|
|
31
31
|
is_mutex = {
|
|
32
32
|
'group': False,
|
|
33
33
|
'mutex': True
|
|
34
|
-
}.get(kind)
|
|
35
|
-
incoming_args =
|
|
34
|
+
}.get(kind, False)
|
|
35
|
+
incoming_args: list[dict[str, Any]] = cast(
|
|
36
|
+
list[dict[str, Any]],
|
|
37
|
+
kwargs.pop('args', [])
|
|
38
|
+
)
|
|
36
39
|
group_content = [
|
|
37
40
|
Argument.factory(**arg)
|
|
38
41
|
for arg in incoming_args
|
|
@@ -48,14 +51,13 @@ class Argument:
|
|
|
48
51
|
# help is so uber-common that while yes it could go into
|
|
49
52
|
# kw i still explicitly defined it
|
|
50
53
|
help: str | None = None
|
|
51
|
-
action: str | None = None
|
|
52
54
|
# anything we haven't explicitly defined.. yet
|
|
53
55
|
kw: dict[str, Any] = field(default_factory=dict)
|
|
54
56
|
|
|
55
57
|
# Source - https://stackoverflow.com/a/74459763
|
|
56
58
|
|
|
57
59
|
@classmethod
|
|
58
|
-
def factory(cls, **kwargs: dict) -> 'Argument | ArgumentGroup':
|
|
60
|
+
def factory(cls, **kwargs: dict[str, Any]) -> 'Argument | ArgumentGroup':
|
|
59
61
|
kind = kwargs.get('kind', 'argument') # default to argument
|
|
60
62
|
if kind not in ALLOWED_KINDS:
|
|
61
63
|
raise ValueError(f'invalid kind {kind}')
|
|
@@ -63,10 +65,10 @@ class Argument:
|
|
|
63
65
|
return ArgumentGroup.factory(**kwargs)
|
|
64
66
|
name = kwargs.get('name')
|
|
65
67
|
if isinstance(name, str):
|
|
66
|
-
kwargs['name'] = [name]
|
|
68
|
+
kwargs['name'] = [name] # type: ignore
|
|
67
69
|
uncaught_kwargs = {k: kwargs[k] for k in kwargs if k not in cls.__match_args__}
|
|
68
70
|
caught_kwargs = {k: kwargs[k] for k in kwargs if k in cls.__match_args__}
|
|
69
|
-
return cls(**caught_kwargs, kw=uncaught_kwargs)
|
|
71
|
+
return cls(**caught_kwargs, kw=uncaught_kwargs) # type: ignore
|
|
70
72
|
|
|
71
73
|
def __post_init__(self):
|
|
72
74
|
if argtype := self.kw.get('type'):
|
|
@@ -16,6 +16,7 @@ UNSUPPORTED_KWARGS = [
|
|
|
16
16
|
class ParserConfig:
|
|
17
17
|
name: str
|
|
18
18
|
script_dir: str | None = None # only for root
|
|
19
|
+
known_args: bool = False # whether to use parse_args, or parse_known_args
|
|
19
20
|
# help is so uber-common that while yes it could go into
|
|
20
21
|
# parser_kwargs i still explicitly defined it
|
|
21
22
|
help: str | None = None
|
|
@@ -25,7 +26,6 @@ class ParserConfig:
|
|
|
25
26
|
# this one is just for resolving the config files into a tree
|
|
26
27
|
parent: str | None = None
|
|
27
28
|
default: bool = False
|
|
28
|
-
load: bool = True
|
|
29
29
|
subparsers: 'dict[str, ParserConfig]' = field(default_factory=dict)
|
|
30
30
|
# anything not already in here
|
|
31
31
|
parser_kwargs: dict[str, Any] = field(default_factory=dict)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from sigil.models import Argument, ArgumentGroup, LibArgParser, ParserConfig
|
|
2
2
|
|
|
3
3
|
try:
|
|
4
|
-
import argcomplete
|
|
4
|
+
import argcomplete # type: ignore
|
|
5
5
|
USE_ARGCOMPLETE = True
|
|
6
6
|
except Exception:
|
|
7
7
|
USE_ARGCOMPLETE=False
|
|
@@ -62,7 +62,6 @@ class Builder:
|
|
|
62
62
|
parser.add_argument(
|
|
63
63
|
*argument_config.name,
|
|
64
64
|
help=argument_config.help,
|
|
65
|
-
action=argument_config.action,
|
|
66
65
|
**(argument_config.kw | conditional_args)
|
|
67
66
|
)
|
|
68
67
|
|
|
@@ -81,7 +80,7 @@ class Builder:
|
|
|
81
80
|
# for all intents and purposes in attach_argument(group)
|
|
82
81
|
# a group has the same interface used in these methods
|
|
83
82
|
# as a parser has; we can thus assume group is a parser
|
|
84
|
-
cls.attach_argument(grp, arg)
|
|
83
|
+
cls.attach_argument(grp, arg) # type: ignore
|
|
85
84
|
|
|
86
85
|
@classmethod
|
|
87
86
|
def build(
|
|
@@ -1,24 +1,40 @@
|
|
|
1
1
|
import importlib.util
|
|
2
|
+
import logging
|
|
2
3
|
import sys
|
|
3
4
|
from argparse import Namespace
|
|
5
|
+
from importlib.abc import Loader
|
|
4
6
|
from pathlib import Path
|
|
7
|
+
from typing import cast
|
|
5
8
|
|
|
6
9
|
from sigil.models import LibArgParser, ParserConfig, SubcommandModule
|
|
7
10
|
|
|
11
|
+
_logger = logging.getLogger(__name__)
|
|
8
12
|
|
|
9
13
|
class ScriptLoader:
|
|
10
14
|
@classmethod
|
|
11
|
-
def import_module(
|
|
15
|
+
def import_module(
|
|
16
|
+
cls,
|
|
17
|
+
config_root,
|
|
18
|
+
path:str | None,
|
|
19
|
+
module_name: str
|
|
20
|
+
) -> SubcommandModule | None:
|
|
21
|
+
if path is None:
|
|
22
|
+
_logger.warning(
|
|
23
|
+
f"no root path declared for script loading, skipping {module_name}"
|
|
24
|
+
)
|
|
25
|
+
return None
|
|
12
26
|
config_root_path = Path(config_root)
|
|
13
27
|
file_path = str(config_root_path/path/f'{module_name}.py')
|
|
14
28
|
|
|
15
29
|
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
|
30
|
+
if not spec:
|
|
31
|
+
raise FileNotFoundError(file_path)
|
|
16
32
|
module = importlib.util.module_from_spec(spec)
|
|
17
|
-
spec.loader.exec_module(module)
|
|
18
|
-
return module
|
|
33
|
+
cast(Loader, spec.loader).exec_module(module)
|
|
34
|
+
return cast(SubcommandModule, module) # or at least assumed to be by contract
|
|
19
35
|
|
|
20
36
|
@classmethod
|
|
21
|
-
def _get_next_parser_name(cls, args: Namespace, target: str, parser:
|
|
37
|
+
def _get_next_parser_name(cls, args: Namespace, target: str, parser: ParserConfig):
|
|
22
38
|
# get the name of the next subcommand
|
|
23
39
|
if target != 'root':
|
|
24
40
|
# normal case a subcommand just points to the next arg
|
|
@@ -36,8 +52,8 @@ class ScriptLoader:
|
|
|
36
52
|
args: Namespace,
|
|
37
53
|
target: str,
|
|
38
54
|
root_parser: LibArgParser,
|
|
39
|
-
data: ParserConfig
|
|
40
|
-
) -> list[
|
|
55
|
+
data: dict[str, ParserConfig]
|
|
56
|
+
) -> list[str]:
|
|
41
57
|
found_scripts = []
|
|
42
58
|
|
|
43
59
|
target_parser: ParserConfig = data[target]
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import logging
|
|
2
|
+
import sys
|
|
2
3
|
from pathlib import Path
|
|
3
4
|
from typing import Any
|
|
4
5
|
|
|
@@ -14,8 +15,8 @@ class YamlReader:
|
|
|
14
15
|
@classmethod
|
|
15
16
|
def handle_duplicates(
|
|
16
17
|
cls,
|
|
17
|
-
loaded_entry: dict[str],
|
|
18
|
-
loaded_config: dict[str],
|
|
18
|
+
loaded_entry: dict[str, Any],
|
|
19
|
+
loaded_config: dict[str, Any],
|
|
19
20
|
entry: str
|
|
20
21
|
):
|
|
21
22
|
# warning: in place mutation if you decide to make changes to loaded config
|
|
@@ -27,21 +28,26 @@ class YamlReader:
|
|
|
27
28
|
def load(
|
|
28
29
|
cls,
|
|
29
30
|
config_root: str,
|
|
30
|
-
manifest_file='manifest.yml'
|
|
31
|
+
manifest_file='manifest.yml',
|
|
31
32
|
) -> dict[str, ParserConfig]:
|
|
32
33
|
config_root_path = Path(config_root)
|
|
33
34
|
manifest = cls.read_file(config_root_path/manifest_file)
|
|
35
|
+
if not manifest:
|
|
36
|
+
_logger.critical("could not load manifest, aborting")
|
|
37
|
+
sys.exit(1)
|
|
34
38
|
loaded_config = {}
|
|
35
39
|
|
|
36
40
|
for entry in manifest:
|
|
37
41
|
loaded_entry = cls.read_file(config_root_path/entry)
|
|
38
42
|
if not loaded_entry:
|
|
39
43
|
continue
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
tmp = {}
|
|
45
|
+
for k, v in loaded_entry.items():
|
|
46
|
+
if v.get('load_ignore'):
|
|
47
|
+
_logger.warning(f'{k} marked as load_ignore, skipping')
|
|
48
|
+
continue
|
|
49
|
+
tmp[k] = v # pesky can't del in for loop :/
|
|
50
|
+
loaded_entry = tmp
|
|
45
51
|
cls.handle_duplicates(loaded_entry, loaded_config, entry)
|
|
46
52
|
cls.convert_args(loaded_entry)
|
|
47
53
|
# override loaded entry duplicates by already found
|
|
@@ -50,19 +56,21 @@ class YamlReader:
|
|
|
50
56
|
for k, v in loaded_config.items():
|
|
51
57
|
try:
|
|
52
58
|
loaded_config[k] = ParserConfig.factory(**v)
|
|
53
|
-
except Exception:
|
|
54
|
-
_logger.error(f'failed to parse config {k}, ignoring')
|
|
59
|
+
except Exception as e:
|
|
60
|
+
_logger.error(f'failed to parse config {k}, ignoring\n{e}')
|
|
55
61
|
# pesky "dictionary changed size during iteration"
|
|
56
62
|
loaded_config = {
|
|
57
63
|
k: v
|
|
58
64
|
for k,v in loaded_config.items()
|
|
59
65
|
if isinstance(v, ParserConfig)
|
|
60
66
|
}
|
|
61
|
-
|
|
62
67
|
return loaded_config
|
|
63
68
|
|
|
64
69
|
@classmethod
|
|
65
|
-
def convert_args(
|
|
70
|
+
def convert_args(
|
|
71
|
+
cls,
|
|
72
|
+
loaded_entry: dict[str, dict[str, Any]],
|
|
73
|
+
):
|
|
66
74
|
# while yes default factory this fixes the edge case where the key is defined
|
|
67
75
|
# but without values
|
|
68
76
|
for command_def in loaded_entry.values():
|
|
@@ -77,10 +85,10 @@ class YamlReader:
|
|
|
77
85
|
|
|
78
86
|
# actual IO, also adaptable
|
|
79
87
|
@classmethod
|
|
80
|
-
def read_file(cls, filename) -> dict:
|
|
88
|
+
def read_file(cls, filename) -> dict | None:
|
|
81
89
|
try:
|
|
82
90
|
with open(filename) as f:
|
|
83
91
|
return yaml.load(f.read(), Loader=yaml.SafeLoader)
|
|
84
92
|
except FileNotFoundError:
|
|
85
93
|
_logger.error(f'failed to load file {filename}, skipping')
|
|
86
|
-
return
|
|
94
|
+
return
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sigil-cli
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Configuration driven CLI builder with subcommands and script loading
|
|
5
5
|
Author-email: Kenzo Staelens <kenzostael@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -32,10 +32,11 @@ Dynamic: license-file
|
|
|
32
32
|
|
|
33
33
|
> Declarative argparse, without the CLI boilerplate.
|
|
34
34
|
|
|
35
|
+
|
|
35
36
|
Sigil is a lightweight, declarative CLI framework for Python. Define your command tree in YAML (or any other format), and sigil builds the `argparse` parser on the fly. Complete with subcommands and dynamic script loading.
|
|
36
37
|
It plays nicely with `argcomplete` out of the box.
|
|
37
38
|
|
|
38
|
-
[](https://pypi.python.org/pypi/sigil-cli)
|
|
39
40
|
[](https://pypi.org/project/sigil-cli/)
|
|
40
41
|
[](https://www.python.org/downloads/release/python-3100/)
|
|
41
42
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -43,6 +44,10 @@ It plays nicely with `argcomplete` out of the box.
|
|
|
43
44
|
[](https://github.com/kenzo-staelens/sigil/actions/workflows/tests.yml)
|
|
44
45
|
[](https://coveralls.io/github/kenzo-staelens/sigil?branch=main)
|
|
45
46
|
|
|
47
|
+
<div align="center">
|
|
48
|
+
<sub>in production since April 2026</sub>
|
|
49
|
+
</div>
|
|
50
|
+
|
|
46
51
|
---
|
|
47
52
|
|
|
48
53
|
## Features
|
|
@@ -80,7 +85,7 @@ pip install sigil-cli[completion]
|
|
|
80
85
|
|
|
81
86
|
```text
|
|
82
87
|
project_root/
|
|
83
|
-
├── mycli.py # drop‑in
|
|
88
|
+
├── mycli.py # drop‑in bootstrap script (alias this)
|
|
84
89
|
├── manifest.yml # lists all YAML config files to load
|
|
85
90
|
├── yml/
|
|
86
91
|
│ ├── root.yml # root command definition
|
|
@@ -91,12 +96,15 @@ project_root/
|
|
|
91
96
|
└── ... # other scripts
|
|
92
97
|
```
|
|
93
98
|
|
|
99
|
+
ps: don't shoot yourself in the foot, don't symlink the bootstrap script.
|
|
100
|
+
|
|
94
101
|
### 1. Entry script
|
|
95
102
|
|
|
96
103
|
Create `mycli.py`:
|
|
97
104
|
|
|
98
105
|
```python
|
|
99
106
|
#!/usr/bin/env python3
|
|
107
|
+
# PYTHON_ARGCOMPLETE_OK
|
|
100
108
|
from pathlib import Path
|
|
101
109
|
from sigil import run_from_config
|
|
102
110
|
|
|
@@ -166,7 +174,7 @@ chmod +x mycli.py
|
|
|
166
174
|
|
|
167
175
|
## Configuration Reference
|
|
168
176
|
|
|
169
|
-
### Root
|
|
177
|
+
### Root Command
|
|
170
178
|
|
|
171
179
|
| Field | Description |
|
|
172
180
|
| --- | --- |
|
|
@@ -180,12 +188,13 @@ chmod +x mycli.py
|
|
|
180
188
|
| `name` | Subcommand name |
|
|
181
189
|
| `parent` | Parent command (must exist elsewhere in a config) |
|
|
182
190
|
| `help` | Help text for this subcommand |
|
|
183
|
-
| `script` | Python module name (without `.py`) inside `script_dir
|
|
191
|
+
| `script` | Python module name (without `.py`) inside `script_dir`, absolute paths supported |
|
|
184
192
|
| `args` | List of argument definitions (see below) |
|
|
185
193
|
| `default` | If `true`, this subcommand is used when no subcommand is given |
|
|
186
|
-
|
|
|
194
|
+
| `load_ignore`| If `true` skips this command (or top level object) from being loaded into the command tree|
|
|
195
|
+
| any other parser kwarg | except for `dest`, `parents` and `formatter_class` they are all supported |
|
|
187
196
|
|
|
188
|
-
Note that `parent` does not refer to argparse's
|
|
197
|
+
Note that `parent` does not refer to argparse's `parents` parameter but is only used to resolve the parser tree. Parser (multi-)inheritance isn't supported but can be emulated by adding arguments to `parent` commands in the tree.
|
|
189
198
|
|
|
190
199
|
### Argument
|
|
191
200
|
|
|
@@ -215,10 +224,24 @@ Groups and mutex groups are also suppored via the "kind" parameter (defaults to
|
|
|
215
224
|
The `name` field can be `--flag` for flags or a string for positional arguments.
|
|
216
225
|
Both literal string and list of strings are supported.
|
|
217
226
|
|
|
227
|
+
Types (`type:`) only supports python builtins
|
|
228
|
+
|
|
229
|
+
### Script files
|
|
230
|
+
|
|
231
|
+
Each script files have as only requirement that they need to define a `def run(args: argparse.Namespace, ctx: dict[str, Any]) -> None` method.
|
|
232
|
+
|
|
233
|
+
args is the by argparse supplied namespace (parsed with parse_known_args), any additional args can be found in `ctx['other_args']`
|
|
234
|
+
|
|
235
|
+
scripts run in sequence from command -> subcommand -> sub sub command -> ... and each may add to, remove or otherwise modify args.namespace and ctx to enrich or modify the behaviour of supsequent scripts.
|
|
236
|
+
|
|
237
|
+
### Misc
|
|
238
|
+
|
|
239
|
+
`load_ignore` may be used to detaching commands from the command tree for any purpose (deprecation, development, etc) or for non schema-compliant objects at the top level of a file, this may be useful to define anchors or references that should not directly be read as a command.
|
|
240
|
+
|
|
218
241
|
## Tab‑Completion (argcomplete)
|
|
219
242
|
|
|
220
243
|
Sigil registers itself with `argcomplete` automatically if available on your system.
|
|
221
|
-
To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script:
|
|
244
|
+
To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script (or use the builtin argcomplete comment):
|
|
222
245
|
|
|
223
246
|
```bash
|
|
224
247
|
pip install argcomplete
|
|
@@ -6,6 +6,7 @@ src/sigil/__main__.py
|
|
|
6
6
|
src/sigil/entrypoint.py
|
|
7
7
|
src/sigil/cli_tools/__init__.py
|
|
8
8
|
src/sigil/cli_tools/init.py
|
|
9
|
+
src/sigil/cli_tools/validate.py
|
|
9
10
|
src/sigil/models/__init__.py
|
|
10
11
|
src/sigil/models/argparser.py
|
|
11
12
|
src/sigil/models/argument.py
|
|
@@ -27,4 +28,5 @@ tests/test_cli_tools.py
|
|
|
27
28
|
tests/test_integration.py
|
|
28
29
|
tests/test_reader.py
|
|
29
30
|
tests/test_resolver.py
|
|
30
|
-
tests/test_scriptloader.py
|
|
31
|
+
tests/test_scriptloader.py
|
|
32
|
+
tests/test_validate.py
|
|
@@ -48,11 +48,56 @@ def test_integration_run_from_config_with_capsys(tmp_path, capsys):
|
|
|
48
48
|
captured = capsys.readouterr()
|
|
49
49
|
assert captured.out.strip() == "Hello, Sigil!"
|
|
50
50
|
|
|
51
|
+
def test_integration_run_from_config_with_unknown_args(tmp_path, capsys):
|
|
52
|
+
"""Test run_from_config and capture output."""
|
|
53
|
+
# Same setup as above
|
|
54
|
+
manifest = tmp_path / "manifest.yml"
|
|
55
|
+
manifest.write_text("- root.yml\n- hello.yml\n")
|
|
56
|
+
|
|
57
|
+
root_data = {
|
|
58
|
+
"root":
|
|
59
|
+
{
|
|
60
|
+
"name": "hello",
|
|
61
|
+
"script_dir": "scripts",
|
|
62
|
+
"known_args": False,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
(tmp_path / "root.yml").write_text(yaml.dump(root_data))
|
|
66
|
+
|
|
67
|
+
hello_data = {
|
|
68
|
+
"hello_cmd": {
|
|
69
|
+
"name": "hello",
|
|
70
|
+
"parent": "root",
|
|
71
|
+
"script": "hello",
|
|
72
|
+
"args": [{"name": "--name", "default": "world"}],
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
(tmp_path / "hello.yml").write_text(yaml.dump(hello_data))
|
|
76
|
+
|
|
77
|
+
script_dir = tmp_path / "scripts"
|
|
78
|
+
script_dir.mkdir()
|
|
79
|
+
script_file = script_dir / "hello.py"
|
|
80
|
+
script_file.write_text(
|
|
81
|
+
"import argparse\n"
|
|
82
|
+
"def run(args, ctx):\n"
|
|
83
|
+
" print(ctx['other_args'])\n"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
with patch.object(sys, "argv", ["prog", "hello", "--name", "Sigil", "--unknown"]):
|
|
87
|
+
with pytest.raises(SystemExit):
|
|
88
|
+
run_from_config(str(tmp_path))
|
|
89
|
+
root_data['root']['known_args'] = True
|
|
90
|
+
(tmp_path / "root.yml").write_text(yaml.dump(root_data))
|
|
91
|
+
|
|
92
|
+
run_from_config(str(tmp_path))
|
|
93
|
+
|
|
94
|
+
captured = capsys.readouterr()
|
|
95
|
+
assert captured.out.strip() == "['--unknown']"
|
|
51
96
|
|
|
52
97
|
def test_builder_required_args():
|
|
53
98
|
"""Test that positional args are marked required."""
|
|
54
99
|
parser = LibArgParser()
|
|
55
|
-
arg = Argument.factory(name="positional", required=False)
|
|
100
|
+
arg = Argument.factory(name="positional", required=False) # type: ignore that's what kwargs is
|
|
56
101
|
Builder.attach_argument(parser, arg)
|
|
57
102
|
|
|
58
103
|
# Required positional arguments have required=True by argparse
|
|
@@ -24,7 +24,7 @@ def test_yaml_reader_load(tmp_path, sample_manifest):
|
|
|
24
24
|
assert sub.script == "sub_script"
|
|
25
25
|
assert len(sub.args) == 1
|
|
26
26
|
assert sub.args[0].name == ["--flag"]
|
|
27
|
-
assert sub.args[0].action == "store_true"
|
|
27
|
+
assert sub.args[0].kw['action'] == "store_true"
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
def test_yaml_reader_duplicate_warning(caplog, tmp_path):
|
|
@@ -35,9 +35,9 @@ def test_script_loader_import_module(tmp_path):
|
|
|
35
35
|
loader = ScriptLoader()
|
|
36
36
|
module = loader.import_module(str(tmp_path), "scripts", "my_script")
|
|
37
37
|
assert hasattr(module, "run")
|
|
38
|
+
assert module is not None
|
|
38
39
|
assert callable(module.run)
|
|
39
40
|
|
|
40
|
-
|
|
41
41
|
@patch("sigil.stages.script_loader.importlib.util")
|
|
42
42
|
def test_script_loader_import_module_mock(mock_importlib, tmp_path):
|
|
43
43
|
"""Test import_module with mocked importlib to avoid actual file I/O."""
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from sigil.cli_tools.validate import validate_project
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_validate_project_success(
|
|
10
|
+
tmp_path: Path,
|
|
11
|
+
caplog: pytest.LogCaptureFixture,
|
|
12
|
+
) -> None:
|
|
13
|
+
"""Test that validate_project succeeds with a valid project structure."""
|
|
14
|
+
# Arrange: create a minimal valid project
|
|
15
|
+
project_root = tmp_path / "mycli"
|
|
16
|
+
project_root.mkdir()
|
|
17
|
+
|
|
18
|
+
manifest = project_root / "manifest.yml"
|
|
19
|
+
manifest.write_text("- root.yml\n")
|
|
20
|
+
|
|
21
|
+
root_yml = project_root / "root.yml"
|
|
22
|
+
root_yml.write_text("""
|
|
23
|
+
root:
|
|
24
|
+
name: mycli
|
|
25
|
+
script_dir: scripts
|
|
26
|
+
""")
|
|
27
|
+
|
|
28
|
+
scripts_dir = project_root / "scripts"
|
|
29
|
+
scripts_dir.mkdir()
|
|
30
|
+
(scripts_dir / "root.py").touch()
|
|
31
|
+
|
|
32
|
+
# Act & Assert: should not raise or exit
|
|
33
|
+
with caplog.at_level(logging.WARNING):
|
|
34
|
+
validate_project(project_root)
|
|
35
|
+
|
|
36
|
+
# No warnings or errors should be logged
|
|
37
|
+
assert not caplog.records
|
|
38
|
+
|
|
39
|
+
def test_validate_project_missing_manifest( tmp_path: Path) -> None:
|
|
40
|
+
"""Test that validate_project exits with code 1 when manifest.yml is missing."""
|
|
41
|
+
project_root = tmp_path / "mycli"
|
|
42
|
+
project_root.mkdir()
|
|
43
|
+
|
|
44
|
+
with pytest.raises(SystemExit) as exc_info:
|
|
45
|
+
validate_project(project_root)
|
|
46
|
+
|
|
47
|
+
assert exc_info.value.code == 1
|
|
48
|
+
|
|
49
|
+
def test_validate_project_missing_root_key(
|
|
50
|
+
tmp_path: Path,
|
|
51
|
+
caplog: pytest.LogCaptureFixture
|
|
52
|
+
) -> None:
|
|
53
|
+
"""Test that validate_project logs an error and exits when
|
|
54
|
+
'root' key is missing."""
|
|
55
|
+
project_root = tmp_path / "mycli"
|
|
56
|
+
project_root.mkdir()
|
|
57
|
+
|
|
58
|
+
manifest = project_root / "manifest.yml"
|
|
59
|
+
manifest.write_text("- root.yml\n")
|
|
60
|
+
|
|
61
|
+
root_yml = project_root / "root.yml"
|
|
62
|
+
root_yml.write_text("""
|
|
63
|
+
not_root:
|
|
64
|
+
name: mycli
|
|
65
|
+
""")
|
|
66
|
+
|
|
67
|
+
with pytest.raises(SystemExit) as exc_info:
|
|
68
|
+
with caplog.at_level(logging.ERROR):
|
|
69
|
+
validate_project(project_root)
|
|
70
|
+
|
|
71
|
+
assert exc_info.value.code == 2
|
|
72
|
+
assert "missing required root command" in caplog.text
|
|
73
|
+
|
|
74
|
+
def test_validate_project_missing_script_dir(
|
|
75
|
+
tmp_path: Path,
|
|
76
|
+
caplog: pytest.LogCaptureFixture) -> None:
|
|
77
|
+
"""Test that validate_project logs a warning when script_dir is missing."""
|
|
78
|
+
project_root = tmp_path / "mycli"
|
|
79
|
+
project_root.mkdir()
|
|
80
|
+
|
|
81
|
+
manifest = project_root / "manifest.yml"
|
|
82
|
+
manifest.write_text("- root.yml\n")
|
|
83
|
+
|
|
84
|
+
root_yml = project_root / "root.yml"
|
|
85
|
+
root_yml.write_text("""
|
|
86
|
+
root:
|
|
87
|
+
name: mycli
|
|
88
|
+
script_dir: scripts
|
|
89
|
+
""")
|
|
90
|
+
|
|
91
|
+
# Do NOT create scripts/ directory
|
|
92
|
+
|
|
93
|
+
with caplog.at_level(logging.WARNING):
|
|
94
|
+
validate_project(project_root)
|
|
95
|
+
|
|
96
|
+
assert "script_dir may be missing or unreachable" in caplog.text
|
|
97
|
+
|
|
98
|
+
def test_validate_project_missing_help_text(
|
|
99
|
+
tmp_path: Path,
|
|
100
|
+
caplog: pytest.LogCaptureFixture
|
|
101
|
+
) -> None:
|
|
102
|
+
"""Test that validate_project logs a warning for commands missing help text."""
|
|
103
|
+
project_root = tmp_path / "mycli"
|
|
104
|
+
project_root.mkdir()
|
|
105
|
+
|
|
106
|
+
manifest = project_root / "manifest.yml"
|
|
107
|
+
manifest.write_text("""
|
|
108
|
+
- root.yml
|
|
109
|
+
- greet.yml
|
|
110
|
+
""")
|
|
111
|
+
|
|
112
|
+
root_yml = project_root / "root.yml"
|
|
113
|
+
root_yml.write_text("""
|
|
114
|
+
root:
|
|
115
|
+
name: mycli
|
|
116
|
+
script_dir: scripts
|
|
117
|
+
""")
|
|
118
|
+
|
|
119
|
+
greet_yml = project_root / "greet.yml"
|
|
120
|
+
greet_yml.write_text("""
|
|
121
|
+
greet:
|
|
122
|
+
# No help field
|
|
123
|
+
name: greet
|
|
124
|
+
script: greet
|
|
125
|
+
""")
|
|
126
|
+
|
|
127
|
+
scripts_dir = project_root / "scripts"
|
|
128
|
+
scripts_dir.mkdir()
|
|
129
|
+
(scripts_dir / "root.py").touch()
|
|
130
|
+
(scripts_dir / "greet.py").touch()
|
|
131
|
+
|
|
132
|
+
with caplog.at_level(logging.WARNING):
|
|
133
|
+
validate_project(project_root)
|
|
134
|
+
|
|
135
|
+
assert "command greet may be missing help text" in caplog.text
|
|
136
|
+
|
|
137
|
+
def test_validate_project_missing_script_file(
|
|
138
|
+
tmp_path: Path,
|
|
139
|
+
caplog: pytest.LogCaptureFixture
|
|
140
|
+
) -> None:
|
|
141
|
+
"""Test that validate_project logs a warning when
|
|
142
|
+
a referenced script file is missing."""
|
|
143
|
+
project_root = tmp_path / "mycli"
|
|
144
|
+
project_root.mkdir()
|
|
145
|
+
|
|
146
|
+
manifest = project_root / "manifest.yml"
|
|
147
|
+
manifest.write_text("""
|
|
148
|
+
- root.yml
|
|
149
|
+
- greet.yml
|
|
150
|
+
""")
|
|
151
|
+
|
|
152
|
+
root_yml = project_root / "root.yml"
|
|
153
|
+
root_yml.write_text("""
|
|
154
|
+
root:
|
|
155
|
+
name: mycli
|
|
156
|
+
script_dir: scripts
|
|
157
|
+
""")
|
|
158
|
+
|
|
159
|
+
greet_yml = project_root / "greet.yml"
|
|
160
|
+
greet_yml.write_text("""
|
|
161
|
+
greet:
|
|
162
|
+
help: Greet the user
|
|
163
|
+
name: greet
|
|
164
|
+
script: greet
|
|
165
|
+
""")
|
|
166
|
+
|
|
167
|
+
scripts_dir = project_root / "scripts"
|
|
168
|
+
scripts_dir.mkdir()
|
|
169
|
+
(scripts_dir / "root.py").touch()
|
|
170
|
+
# Do NOT create greet.py
|
|
171
|
+
|
|
172
|
+
with caplog.at_level(logging.WARNING):
|
|
173
|
+
validate_project(project_root)
|
|
174
|
+
|
|
175
|
+
assert 'script "greet" defined on command "greet" not found' in caplog.text
|
|
176
|
+
|
|
177
|
+
def test_validate_project_script_without_script_dir(
|
|
178
|
+
tmp_path: Path,
|
|
179
|
+
caplog: pytest.LogCaptureFixture
|
|
180
|
+
) -> None:
|
|
181
|
+
"""Test that validate_project logs a warning when a script
|
|
182
|
+
is defined but script_dir is missing."""
|
|
183
|
+
project_root = tmp_path / "mycli"
|
|
184
|
+
project_root.mkdir()
|
|
185
|
+
|
|
186
|
+
manifest = project_root / "manifest.yml"
|
|
187
|
+
manifest.write_text("""
|
|
188
|
+
- root.yml
|
|
189
|
+
- greet.yml
|
|
190
|
+
""")
|
|
191
|
+
|
|
192
|
+
root_yml = project_root / "root.yml"
|
|
193
|
+
root_yml.write_text("""
|
|
194
|
+
root:
|
|
195
|
+
name: mycli
|
|
196
|
+
# No script_dir defined
|
|
197
|
+
""")
|
|
198
|
+
|
|
199
|
+
greet_yml = project_root / "greet.yml"
|
|
200
|
+
greet_yml.write_text("""
|
|
201
|
+
greet:
|
|
202
|
+
help: Greet the user
|
|
203
|
+
name: greet
|
|
204
|
+
script: greet
|
|
205
|
+
""")
|
|
206
|
+
|
|
207
|
+
# Do NOT create scripts/ directory
|
|
208
|
+
|
|
209
|
+
with caplog.at_level(logging.WARNING):
|
|
210
|
+
validate_project(project_root)
|
|
211
|
+
|
|
212
|
+
assert 'script defined on "greet" but no script dir defined' in caplog.text
|
|
213
|
+
|
|
214
|
+
def test_validate_project_multiple_default_subcommands(
|
|
215
|
+
tmp_path: Path,
|
|
216
|
+
caplog: pytest.LogCaptureFixture
|
|
217
|
+
) -> None:
|
|
218
|
+
"""Test that validate_project exits when multiple subcommands have default: true."""
|
|
219
|
+
project_root = tmp_path / "mycli"
|
|
220
|
+
project_root.mkdir()
|
|
221
|
+
|
|
222
|
+
manifest = project_root / "manifest.yml"
|
|
223
|
+
manifest.write_text("""
|
|
224
|
+
- root.yml
|
|
225
|
+
- foo.yml
|
|
226
|
+
- bar.yml
|
|
227
|
+
""")
|
|
228
|
+
|
|
229
|
+
root_yml = project_root / "root.yml"
|
|
230
|
+
root_yml.write_text("""
|
|
231
|
+
root:
|
|
232
|
+
name: mycli
|
|
233
|
+
script_dir: scripts
|
|
234
|
+
""")
|
|
235
|
+
|
|
236
|
+
foo_yml = project_root / "foo.yml"
|
|
237
|
+
foo_yml.write_text("""
|
|
238
|
+
foo:
|
|
239
|
+
name: foo
|
|
240
|
+
parent: root
|
|
241
|
+
help: Foo command
|
|
242
|
+
default: true
|
|
243
|
+
script: foo
|
|
244
|
+
""")
|
|
245
|
+
|
|
246
|
+
bar_yml = project_root / "bar.yml"
|
|
247
|
+
bar_yml.write_text("""
|
|
248
|
+
bar:
|
|
249
|
+
name: bar
|
|
250
|
+
parent: root
|
|
251
|
+
help: Bar command
|
|
252
|
+
default: true
|
|
253
|
+
script: bar
|
|
254
|
+
""")
|
|
255
|
+
|
|
256
|
+
scripts_dir = project_root / "scripts"
|
|
257
|
+
scripts_dir.mkdir()
|
|
258
|
+
for cmd in ("root", "foo", "bar"):
|
|
259
|
+
(scripts_dir / f"{cmd}.py").touch()
|
|
260
|
+
|
|
261
|
+
validate_project(project_root)
|
|
262
|
+
|
|
263
|
+
assert 'last default bar overridden with foo' in caplog.text
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
from .entrypoint import run_from_config
|
|
2
|
-
from .models import (
|
|
3
|
-
Argument,
|
|
4
|
-
ArgumentGroup,
|
|
5
|
-
LibArgParser,
|
|
6
|
-
ParserConfig,
|
|
7
|
-
SubcommandModule,
|
|
8
|
-
)
|
|
9
|
-
from .stages import (
|
|
10
|
-
Builder,
|
|
11
|
-
Resolver,
|
|
12
|
-
ScriptLoader,
|
|
13
|
-
YamlReader,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
__all__ = (
|
|
17
|
-
run_from_config,
|
|
18
|
-
Argument,
|
|
19
|
-
ArgumentGroup,
|
|
20
|
-
LibArgParser,
|
|
21
|
-
ParserConfig,
|
|
22
|
-
SubcommandModule,
|
|
23
|
-
YamlReader,
|
|
24
|
-
Resolver,
|
|
25
|
-
Builder,
|
|
26
|
-
ScriptLoader,
|
|
27
|
-
)
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
|
|
3
|
-
from sigil.cli_tools.init import create_project
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def main():
|
|
7
|
-
args = sys.argv[1:]
|
|
8
|
-
if not args or args[0] == "--help":
|
|
9
|
-
print("Usage: sigil init <project_name>")
|
|
10
|
-
sys.exit(0)
|
|
11
|
-
|
|
12
|
-
if args[0] == "init" and len(args) >= 2:
|
|
13
|
-
create_project(args[1])
|
|
14
|
-
else:
|
|
15
|
-
print(f"Unknown command: {args[0]}")
|
|
16
|
-
print("Usage: sigil init <project_name>")
|
|
17
|
-
sys.exit(1)
|
|
18
|
-
|
|
19
|
-
if __name__ == "__main__":
|
|
20
|
-
main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|