sigil-cli 1.2.0__tar.gz → 1.4.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.
Files changed (40) hide show
  1. {sigil_cli-1.2.0/src/sigil_cli.egg-info → sigil_cli-1.4.0}/PKG-INFO +48 -17
  2. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/README.md +47 -16
  3. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/pyproject.toml +1 -1
  4. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/__init__.py +2 -2
  5. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/__main__.py +10 -0
  6. sigil_cli-1.4.0/src/sigil/cli_tools/tree.py +41 -0
  7. sigil_cli-1.4.0/src/sigil/cli_tools/validate.py +117 -0
  8. sigil_cli-1.4.0/src/sigil/datasource/__init__.py +7 -0
  9. sigil_cli-1.4.0/src/sigil/datasource/datasource.py +21 -0
  10. sigil_cli-1.4.0/src/sigil/datasource/yml_source.py +36 -0
  11. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/entrypoint.py +5 -4
  12. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/models/command.py +17 -0
  13. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/stages/__init__.py +5 -5
  14. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/stages/builder.py +19 -6
  15. sigil_cli-1.2.0/src/sigil/stages/yaml_reader.py → sigil_cli-1.4.0/src/sigil/stages/parser.py +21 -25
  16. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/stages/resolver.py +20 -4
  17. {sigil_cli-1.2.0 → sigil_cli-1.4.0/src/sigil_cli.egg-info}/PKG-INFO +48 -17
  18. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil_cli.egg-info/SOURCES.txt +9 -4
  19. sigil_cli-1.4.0/tests/test_cli_tree.py +80 -0
  20. sigil_cli-1.2.0/tests/test_validate.py → sigil_cli-1.4.0/tests/test_cli_validate.py +12 -7
  21. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/tests/test_reader.py +28 -21
  22. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/tests/test_resolver.py +18 -1
  23. sigil_cli-1.2.0/src/sigil/cli_tools/validate.py +0 -45
  24. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/LICENSE +0 -0
  25. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/setup.cfg +0 -0
  26. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/cli_tools/__init__.py +0 -0
  27. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/cli_tools/init.py +0 -0
  28. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/models/__init__.py +0 -0
  29. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/models/argparser.py +0 -0
  30. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/models/argument.py +0 -0
  31. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/models/script.py +0 -0
  32. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil/stages/script_loader.py +0 -0
  33. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil_cli.egg-info/dependency_links.txt +0 -0
  34. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil_cli.egg-info/entry_points.txt +0 -0
  35. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil_cli.egg-info/requires.txt +0 -0
  36. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/src/sigil_cli.egg-info/top_level.txt +0 -0
  37. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/tests/test_builder.py +0 -0
  38. /sigil_cli-1.2.0/tests/test_cli_tools.py → /sigil_cli-1.4.0/tests/test_cli_init.py +0 -0
  39. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/tests/test_integration.py +0 -0
  40. {sigil_cli-1.2.0 → sigil_cli-1.4.0}/tests/test_scriptloader.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sigil-cli
3
- Version: 1.2.0
3
+ Version: 1.4.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,8 +32,8 @@ Dynamic: license-file
32
32
 
33
33
  > Declarative argparse, without the CLI boilerplate.
34
34
 
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.
35
+ Sigil is a lightweight, declarative CLI framework for Python. Define your command tree in YAML (or any other format),
36
+ and sigil builds the `argparse` parser on the fly. Complete with subcommands and dynamic script loading.
37
37
  It plays nicely with `argcomplete` out of the box.
38
38
 
39
39
  [![PyPI Version](https://img.shields.io/pypi/v/sigil-cli)](https://pypi.python.org/pypi/sigil-cli)
@@ -75,12 +75,19 @@ your command structure modular and flexible.
75
75
  pip install sigil-cli
76
76
  ```
77
77
 
78
- or include argcomplete
78
+ *optionally include argcomplete with `sigil-cli[completion]`*
79
+
80
+ The `init` command generates a project directory with a sample configuration and entrypoint:
79
81
 
80
82
  ```bash
81
- pip install sigil-cli[completion]
83
+ sigil init demo
84
+ sigil validate demo/ # optional, should not output anything for correct configurations
85
+ cd demo
86
+ python main.py --help
82
87
  ```
83
88
 
89
+ For a full walkthrough with custom commands and arguments, jump to the Quick Start below.
90
+
84
91
  ### 0. Recommended file structure
85
92
 
86
93
  ```text
@@ -190,11 +197,12 @@ chmod +x mycli.py
190
197
  | `help` | Help text for this subcommand |
191
198
  | `script` | Python module name (without `.py`) inside `script_dir`, absolute paths supported |
192
199
  | `args` | List of argument definitions (see below) |
193
- | `default` | If `true`, this subcommand is used when no subcommand is given |
194
- | `load_ignore`| If `true` skips this command (or top level object) from being loaded into the command tree|
200
+ | `default` | If `True`, this subcommand is used when no subcommand is given |
201
+ | `load` | If `False` skips this command (or top level object) from being loaded into the command tree (default `True`) |
195
202
  | any other parser kwarg | except for `dest`, `parents` and `formatter_class` they are all supported |
196
203
 
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.
204
+ Note that `parent` does not refer to argparse's `parents` parameter but is only used to resolve the parser tree.
205
+ Parser (multi-)inheritance isn't supported but can be emulated by adding arguments to `parent` commands in the tree.
198
206
 
199
207
  ### Argument
200
208
 
@@ -224,24 +232,42 @@ Groups and mutex groups are also suppored via the "kind" parameter (defaults to
224
232
  The `name` field can be `--flag` for flags or a string for positional arguments.
225
233
  Both literal string and list of strings are supported.
226
234
 
227
- Types (`type:`) only supports python builtins
235
+ Types (`type:`) only supports python builtins
228
236
 
229
237
  ### Script files
230
238
 
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.
239
+ Each script files have as only requirement that they need to define a
240
+ `def run(args: argparse.Namespace, ctx: dict[str, Any]) -> None` method.
232
241
 
233
242
  args is the by argparse supplied namespace (parsed with parse_known_args), any additional args can be found in `ctx['other_args']`
234
243
 
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.
244
+ scripts run in sequence from command -> subcommand -> sub sub command -> ... and each may add to,
245
+ remove or otherwise modify args.namespace and ctx to enrich or modify the behaviour of supsequent scripts.
246
+
247
+ ## Sigil CLI Commands
248
+
249
+ `sigil` ships with its own lightweight toolbelt to manage your projects:
250
+
251
+ | Command | Purpose |
252
+ | --- | --- |
253
+ | `sigil init [project_name]` | Creates a new project folder with a sample ready-to-run Python entrypoint. |
254
+ | `sigil validate [project_path]` | Checks your sigil definition for schema errors and missing references. Run this after heavy edits to catch mistakes early. |
255
+ | `sigil tree [project_path]` | Print the command structure of a sigil. |
256
+
257
+ *(Note: Your generated CLI (the one you build with Sigil) is completely separate from the `sigil` management
258
+ commands above. You alias and run `main.py`. the `sigil` prefix is a different namespace.)*
236
259
 
237
260
  ### Misc
238
261
 
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.
262
+ `load_ignore` may be used to detaching commands from the command tree for any purpose
263
+ (deprecation, development, etc) or for non schema-compliant objects at the top level of a file, this may be useful to
264
+ define anchors or references that should not directly be read as a command.
240
265
 
241
266
  ## Tab‑Completion (argcomplete)
242
267
 
243
268
  Sigil registers itself with `argcomplete` automatically if available on your system.
244
- To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script (or use the builtin argcomplete comment):
269
+ To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry
270
+ script (or use the builtin argcomplete comment):
245
271
 
246
272
  ```bash
247
273
  pip install argcomplete
@@ -252,7 +278,7 @@ Then run your script and hit <kbd>Tab</kbd> – subcommands and flags will compl
252
278
 
253
279
  ## Pluggable Backends
254
280
 
255
- Sigil uses yaml by default, but you can supply any loader that returns a `dict[str, ParserConfig]`:
281
+ Sigil uses yaml by default, but you can supply any datasource that we can convert it's output into `ParserConfig`:
256
282
 
257
283
  ```python
258
284
  from sigil import run_from_config
@@ -260,9 +286,14 @@ from sigil import run_from_config
260
286
  # Use JSON instead:
261
287
  class JsonReader:
262
288
  @classmethod
263
- def load(cls, config_root):
264
- # read *.json, parse, convert to dict of ParserConfig
289
+ def read_manifest(cls, config_root: Path) -> list | None:
290
+ # read target paths for loading configuration
265
291
  ...
292
+
293
+ def read_configuration(cls, target: Path) -> dict | None:
294
+ # read *.json, parse, convert to dict of raw data
295
+ ...
296
+
266
297
 
267
298
  run_from_config("/path/to/config", loader_class=JsonReader)
268
299
  ```
@@ -270,5 +301,5 @@ run_from_config("/path/to/config", loader_class=JsonReader)
270
301
  You can also pass a pre‑loaded dictionary directly by wrapping it:
271
302
 
272
303
  ```python
273
- run_from_config(my_dict, loader_class=DictLoader)
304
+ run_from_config(my_dict, loader_class=DictReader)
274
305
  ```
@@ -2,8 +2,8 @@
2
2
 
3
3
  > Declarative argparse, without the CLI boilerplate.
4
4
 
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.
5
+ Sigil is a lightweight, declarative CLI framework for Python. Define your command tree in YAML (or any other format),
6
+ and sigil builds the `argparse` parser on the fly. Complete with subcommands and dynamic script loading.
7
7
  It plays nicely with `argcomplete` out of the box.
8
8
 
9
9
  [![PyPI Version](https://img.shields.io/pypi/v/sigil-cli)](https://pypi.python.org/pypi/sigil-cli)
@@ -45,12 +45,19 @@ your command structure modular and flexible.
45
45
  pip install sigil-cli
46
46
  ```
47
47
 
48
- or include argcomplete
48
+ *optionally include argcomplete with `sigil-cli[completion]`*
49
+
50
+ The `init` command generates a project directory with a sample configuration and entrypoint:
49
51
 
50
52
  ```bash
51
- pip install sigil-cli[completion]
53
+ sigil init demo
54
+ sigil validate demo/ # optional, should not output anything for correct configurations
55
+ cd demo
56
+ python main.py --help
52
57
  ```
53
58
 
59
+ For a full walkthrough with custom commands and arguments, jump to the Quick Start below.
60
+
54
61
  ### 0. Recommended file structure
55
62
 
56
63
  ```text
@@ -160,11 +167,12 @@ chmod +x mycli.py
160
167
  | `help` | Help text for this subcommand |
161
168
  | `script` | Python module name (without `.py`) inside `script_dir`, absolute paths supported |
162
169
  | `args` | List of argument definitions (see below) |
163
- | `default` | If `true`, this subcommand is used when no subcommand is given |
164
- | `load_ignore`| If `true` skips this command (or top level object) from being loaded into the command tree|
170
+ | `default` | If `True`, this subcommand is used when no subcommand is given |
171
+ | `load` | If `False` skips this command (or top level object) from being loaded into the command tree (default `True`) |
165
172
  | any other parser kwarg | except for `dest`, `parents` and `formatter_class` they are all supported |
166
173
 
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.
174
+ Note that `parent` does not refer to argparse's `parents` parameter but is only used to resolve the parser tree.
175
+ Parser (multi-)inheritance isn't supported but can be emulated by adding arguments to `parent` commands in the tree.
168
176
 
169
177
  ### Argument
170
178
 
@@ -194,24 +202,42 @@ Groups and mutex groups are also suppored via the "kind" parameter (defaults to
194
202
  The `name` field can be `--flag` for flags or a string for positional arguments.
195
203
  Both literal string and list of strings are supported.
196
204
 
197
- Types (`type:`) only supports python builtins
205
+ Types (`type:`) only supports python builtins
198
206
 
199
207
  ### Script files
200
208
 
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.
209
+ Each script files have as only requirement that they need to define a
210
+ `def run(args: argparse.Namespace, ctx: dict[str, Any]) -> None` method.
202
211
 
203
212
  args is the by argparse supplied namespace (parsed with parse_known_args), any additional args can be found in `ctx['other_args']`
204
213
 
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.
214
+ scripts run in sequence from command -> subcommand -> sub sub command -> ... and each may add to,
215
+ remove or otherwise modify args.namespace and ctx to enrich or modify the behaviour of supsequent scripts.
216
+
217
+ ## Sigil CLI Commands
218
+
219
+ `sigil` ships with its own lightweight toolbelt to manage your projects:
220
+
221
+ | Command | Purpose |
222
+ | --- | --- |
223
+ | `sigil init [project_name]` | Creates a new project folder with a sample ready-to-run Python entrypoint. |
224
+ | `sigil validate [project_path]` | Checks your sigil definition for schema errors and missing references. Run this after heavy edits to catch mistakes early. |
225
+ | `sigil tree [project_path]` | Print the command structure of a sigil. |
226
+
227
+ *(Note: Your generated CLI (the one you build with Sigil) is completely separate from the `sigil` management
228
+ commands above. You alias and run `main.py`. the `sigil` prefix is a different namespace.)*
206
229
 
207
230
  ### Misc
208
231
 
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.
232
+ `load_ignore` may be used to detaching commands from the command tree for any purpose
233
+ (deprecation, development, etc) or for non schema-compliant objects at the top level of a file, this may be useful to
234
+ define anchors or references that should not directly be read as a command.
210
235
 
211
236
  ## Tab‑Completion (argcomplete)
212
237
 
213
238
  Sigil registers itself with `argcomplete` automatically if available on your system.
214
- To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry script (or use the builtin argcomplete comment):
239
+ To enable completion, install [argcomplete](https://github.com/kislyuk/argcomplete) and activate it for your entry
240
+ script (or use the builtin argcomplete comment):
215
241
 
216
242
  ```bash
217
243
  pip install argcomplete
@@ -222,7 +248,7 @@ Then run your script and hit <kbd>Tab</kbd> – subcommands and flags will compl
222
248
 
223
249
  ## Pluggable Backends
224
250
 
225
- Sigil uses yaml by default, but you can supply any loader that returns a `dict[str, ParserConfig]`:
251
+ Sigil uses yaml by default, but you can supply any datasource that we can convert it's output into `ParserConfig`:
226
252
 
227
253
  ```python
228
254
  from sigil import run_from_config
@@ -230,9 +256,14 @@ from sigil import run_from_config
230
256
  # Use JSON instead:
231
257
  class JsonReader:
232
258
  @classmethod
233
- def load(cls, config_root):
234
- # read *.json, parse, convert to dict of ParserConfig
259
+ def read_manifest(cls, config_root: Path) -> list | None:
260
+ # read target paths for loading configuration
235
261
  ...
262
+
263
+ def read_configuration(cls, target: Path) -> dict | None:
264
+ # read *.json, parse, convert to dict of raw data
265
+ ...
266
+
236
267
 
237
268
  run_from_config("/path/to/config", loader_class=JsonReader)
238
269
  ```
@@ -240,5 +271,5 @@ run_from_config("/path/to/config", loader_class=JsonReader)
240
271
  You can also pass a pre‑loaded dictionary directly by wrapping it:
241
272
 
242
273
  ```python
243
- run_from_config(my_dict, loader_class=DictLoader)
274
+ run_from_config(my_dict, loader_class=DictReader)
244
275
  ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sigil-cli"
7
- version = "1.2.0"
7
+ version = "1.4.0"
8
8
  description = "Configuration driven CLI builder with subcommands and script loading"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -12,9 +12,9 @@ from .models import (
12
12
  )
13
13
  from .stages import (
14
14
  Builder,
15
+ Parser,
15
16
  Resolver,
16
17
  ScriptLoader,
17
- YamlReader,
18
18
  )
19
19
 
20
20
  __all__ = (
@@ -24,7 +24,7 @@ __all__ = (
24
24
  'LibArgParser',
25
25
  'ParserConfig',
26
26
  'SubcommandModule',
27
- 'YamlReader',
27
+ 'Parser',
28
28
  'Resolver',
29
29
  'Builder',
30
30
  'ScriptLoader',
@@ -2,6 +2,7 @@ import argparse
2
2
 
3
3
  from sigil import __version__
4
4
  from sigil.cli_tools.init import create_project
5
+ from sigil.cli_tools.tree import display_project
5
6
  from sigil.cli_tools.validate import validate_project
6
7
 
7
8
 
@@ -17,6 +18,12 @@ def main():
17
18
  validate = sub.add_parser(name="validate", help="validate structure of a sigil")
18
19
  validate.add_argument('path', help="project to validate")
19
20
 
21
+ validate = sub.add_parser(
22
+ name="tree",
23
+ help="display the command structure of a sigil."
24
+ )
25
+ validate.add_argument('path', help="project to validate")
26
+
20
27
  args = parser.parse_args()
21
28
 
22
29
  if args.command == 'init':
@@ -25,6 +32,9 @@ def main():
25
32
  if args.command == 'validate':
26
33
  validate_project(args.path)
27
34
  return
35
+ if args.command == 'tree':
36
+ display_project(args.path)
37
+ return
28
38
 
29
39
  if __name__ == "__main__":
30
40
  main()
@@ -0,0 +1,41 @@
1
+ import argparse
2
+
3
+ from sigil import Builder, Parser, Resolver
4
+ from sigil.datasource import YmlSource
5
+
6
+
7
+ # adapted from https://stackoverflow.com/questions/78166654/how-can-i-print-the-tree-structure-of-subparsers-of-an-argparse-parser-in-python/78246362#78246362
8
+ def parser_tree(
9
+ parser: argparse.ArgumentParser,
10
+ start: str = "",
11
+ down: str = "│ ",
12
+ leaf: tuple = ("├── ", "└── ")
13
+ ) -> str:
14
+ out = f"{parser.prog or 'root'}\n" if not start else ""
15
+ subparsers_actions = [
16
+ action for action in parser._actions
17
+ if isinstance(action, argparse._SubParsersAction)
18
+ ]
19
+
20
+ for subparsers_action in subparsers_actions:
21
+ choices = list(subparsers_action.choices.items())
22
+ for idx, (choice, subparser) in enumerate(choices):
23
+ is_last = (idx == len(choices) - 1)
24
+ # Prefix for this line: current indentation + branch symbol
25
+ prefix = start + leaf[is_last]
26
+ # Next indentation: if this is the last child, we stop vertical bars;
27
+ # otherwise continue
28
+ next_start = start + (down if not is_last else " " * len(down))
29
+ # Recurse and append
30
+ child_tree = parser_tree(subparser, next_start, down, leaf)
31
+ out += prefix + choice + "\n"
32
+ out += child_tree
33
+ return out
34
+
35
+
36
+ def display_project(projectroot):
37
+ raw = Parser(YmlSource).load(projectroot)
38
+ resolved = Resolver.resolve_inheritance(raw)
39
+ parser = Builder.build(resolved)
40
+ print(parser_tree(parser))
41
+
@@ -0,0 +1,117 @@
1
+ import contextlib
2
+ import logging
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ from sigil import Builder, Parser, Resolver
7
+ from sigil.datasource import YmlSource
8
+
9
+
10
+ # small helper to capture logs from in the guts of sigil
11
+ class WarningCollector(logging.Handler):
12
+ """A handler that stores all records at WARNING level or above."""
13
+ def __init__(self):
14
+ super().__init__()
15
+ self.messages = []
16
+
17
+ def emit(self, record):
18
+ # Only store messages (you could also filter by level here)
19
+ if record.levelno >= logging.INFO:
20
+ self.messages.append(record)
21
+
22
+ @contextlib.contextmanager
23
+ def capture_package_warnings(package_name):
24
+ """
25
+ Captures INFO-level (and higher) logs from the given package tree.
26
+ Example: capture_package_warnings('myproject')
27
+ Info doesn't cause nonzero exit
28
+
29
+ Note that default loglevel is Info and is therefore not displayed by default
30
+ """
31
+ collector = WarningCollector()
32
+ # Get the package root logger – children will propagate to it
33
+ package_logger = logging.getLogger(package_name)
34
+ package_logger.addHandler(collector)
35
+ try:
36
+ yield collector
37
+ finally:
38
+ package_logger.removeHandler(collector)
39
+
40
+ # might as well use a logger and have cleaner counting logic
41
+ _logger = logging.getLogger(__name__)
42
+
43
+
44
+ # actual validation logging
45
+ def _validate_project(projectroot):
46
+ projectpath = Path(projectroot)
47
+
48
+ if not (projectpath/'manifest.yml').exists():
49
+ _logger.error('missing manifest.yml, aborting.')
50
+ sys.exit(1)
51
+
52
+ raw = Parser(YmlSource).load(projectroot)
53
+
54
+ # these are easier for us to check
55
+ if 'root' not in raw:
56
+ _logger.error('missing required root command')
57
+ sys.exit(2)
58
+
59
+ resolved = Resolver.resolve_inheritance(raw) #alerts basic structural errors for us
60
+ Builder.build(resolved) # catches the case for "only one default subcommand"
61
+
62
+ script_root = None
63
+ if not raw['root'].script_dir or not (projectpath/raw['root'].script_dir).exists():
64
+ _logger.warning('script_dir may be missing or unreachable')
65
+ else:
66
+ script_root = projectpath/raw['root'].script_dir
67
+
68
+ for c_id, command_def in raw.items():
69
+ if not command_def.help and c_id != 'root':
70
+ _logger.warning(f'command {c_id} may be missing help text')
71
+ if command_def.script:
72
+ if script_root:
73
+ if not (
74
+ script_root/f'{command_def.script}.py'
75
+ ).exists():
76
+ _logger.warning(
77
+ f'script "{command_def.script}" defined on '
78
+ f'command "{c_id}" not found'
79
+ )
80
+ else:
81
+ _logger.warning(f'script defined on "{c_id}" but no script dir defined')
82
+
83
+ # exit code wrapper
84
+ # validation partially happens deep in the guts of sigil,
85
+ # this caputures all validation logs, counts and converts to exit code
86
+ def validate_project(projectroot):
87
+ package_name = __name__.split('.', 1)[0]
88
+ with capture_package_warnings(package_name) as collector:
89
+ try:
90
+ _validate_project(projectroot)
91
+ except SystemExit: # special case to not hard exit
92
+ pass
93
+
94
+ errors = [r for r in collector.messages if r.levelno >= logging.ERROR]
95
+ warnings = [r for r in collector.messages if r.levelno == logging.WARNING]
96
+
97
+ for rec in collector.messages:
98
+ print(f"{rec.levelname}: {rec.getMessage()}", file=sys.stderr)
99
+
100
+ error_count = len(errors)
101
+ warning_count = len(warnings)
102
+
103
+ if error_count > 0:
104
+ print(
105
+ f'Validation failed with {error_count} error(s) '
106
+ f'and {warning_count} warning(s).',
107
+ file=sys.stderr
108
+ )
109
+ sys.exit(2)
110
+ elif warning_count > 0:
111
+ print(
112
+ f"Validation had {warning_count} warning(s) (no errors).",
113
+ file=sys.stderr
114
+ )
115
+ sys.exit(1)
116
+ else:
117
+ print("Validation passed successfully.", file=sys.stderr)
@@ -0,0 +1,7 @@
1
+ from .datasource import DataSource
2
+ from .yml_source import YmlSource
3
+
4
+ __all__ = [
5
+ 'DataSource',
6
+ 'YmlSource',
7
+ ]
@@ -0,0 +1,21 @@
1
+ from abc import ABC, abstractmethod
2
+ from pathlib import Path
3
+
4
+
5
+ # currently network based datasources are not planned
6
+ # but if you do *need* one for some reason, feel free to make a PR
7
+ class DataSource(ABC):
8
+ @classmethod
9
+ @abstractmethod
10
+ def read_manifest(cls, target: Path) -> list | None:
11
+ # current implementation expects a list of files, or none on error
12
+ ...
13
+
14
+ @classmethod
15
+ @abstractmethod
16
+ def read_configuration(cls, target: Path) -> dict | None:
17
+ # raw data roughly mapping to internal models
18
+ # validation happens in a later stage
19
+ # datasources shouldn't need to validate
20
+ # for duplicates or malformed data
21
+ ...
@@ -0,0 +1,36 @@
1
+ import logging
2
+ from pathlib import Path
3
+
4
+ import yaml
5
+
6
+ from .datasource import DataSource
7
+
8
+ _logger = logging.getLogger(__name__)
9
+
10
+
11
+ class YmlSource(DataSource):
12
+
13
+ @classmethod
14
+ def read(cls, target):
15
+ try:
16
+ with open(target) as f:
17
+ return yaml.load(f.read(), Loader=yaml.SafeLoader)
18
+ except FileNotFoundError:
19
+ _logger.error(f"file '{target}' not found")
20
+ return
21
+ except yaml.error.YAMLError:
22
+ _logger.error(f"malformed yaml file ({target})")
23
+ return
24
+
25
+
26
+ @classmethod
27
+ def read_manifest(cls, manifest_file: Path) -> list | None:
28
+ # indirection? yes
29
+ # can new datasources implement a better manifest vs data? also yes
30
+ return cls.read(manifest_file)
31
+
32
+
33
+ @classmethod
34
+ def read_configuration(cls, target: Path) -> dict | None:
35
+ return cls.read(target)
36
+
@@ -2,15 +2,16 @@ import logging
2
2
  import sys
3
3
  from typing import Any
4
4
 
5
- from .stages import Builder, Resolver, ScriptLoader, YamlReader
5
+ from .datasource import YmlSource
6
+ from .stages import Builder, Parser, Resolver, ScriptLoader
6
7
 
7
8
  _logger = logging.getLogger(__name__)
8
9
 
9
10
  # default to yamlloader, use whatever datastore you feel like
10
- tmp = 0
11
- def run_from_config(config_root: str, loader_class = YamlReader):
12
- raw_data = loader_class.load(config_root)
11
+ def run_from_config(config_root: str, datasource = YmlSource):
12
+ raw_data = Parser(YmlSource).load(config_root)
13
13
  resolved = Resolver.resolve_inheritance(raw_data)
14
+ # parser beyond here is an argparser
14
15
  parser = Builder.build(resolved)
15
16
 
16
17
  execution_context = {}
@@ -29,9 +29,12 @@ class ParserConfig:
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)
32
+ load: bool = True
32
33
 
33
34
  @classmethod
34
35
  def factory(cls, **kwargs):
36
+ if 'name' not in kwargs:
37
+ raise ValueError("required argument 'name' not defined.")
35
38
  uncaught_kwargs = {k: kwargs[k] for k in kwargs if k not in cls.__match_args__}
36
39
  caught_kwargs = {k: kwargs[k] for k in kwargs if k in cls.__match_args__}
37
40
  parser_kwargs = {}
@@ -46,3 +49,17 @@ class ParserConfig:
46
49
  else:
47
50
  parser_kwargs[kwarg] = value
48
51
  return cls(**caught_kwargs, parser_kwargs=parser_kwargs)
52
+
53
+ @classmethod
54
+ def construct_unloaded_data(cls, key: str, source: dict):
55
+ # this dataclass requires several attributes for either validation checks
56
+ # or instantiation requirements
57
+ # all get defined here, regardless of source data
58
+ # since this object is later required to validate and build tree
59
+ # structure minimal data is required
60
+ return {
61
+ 'name': key,
62
+ 'help': source.get('help', 'missing'),
63
+ 'load': source.get('load'),
64
+ 'parent': source.get('parent')
65
+ }
@@ -1,11 +1,11 @@
1
1
  from .builder import Builder
2
+ from .parser import Parser
2
3
  from .resolver import Resolver
3
4
  from .script_loader import ScriptLoader
4
- from .yaml_reader import YamlReader
5
5
 
6
6
  __all__ = [
7
- YamlReader,
8
- Resolver,
9
- Builder,
10
- ScriptLoader,
7
+ 'Parser',
8
+ 'Resolver',
9
+ 'Builder',
10
+ 'ScriptLoader',
11
11
  ]
@@ -25,13 +25,26 @@ class Builder:
25
25
  )
26
26
  last_default = None # find the last default
27
27
  for _, subcommand in parser_data.subparsers.items():
28
- argparser = argparse_group.add_parser(
29
- subcommand.name,
30
- help=subcommand.help,
31
- **subcommand.parser_kwargs,
32
- )
28
+ try:
29
+ argparser = argparse_group.add_parser(
30
+ subcommand.name,
31
+ help=subcommand.help,
32
+ **subcommand.parser_kwargs,
33
+ )
34
+ except Exception as e:
35
+ _logger.error(
36
+ f"unable to instantiate subcommand '{subcommand.name}'\n {e}"
37
+ )
38
+ continue # parser cannot be resqued
33
39
  for arg in subcommand.args:
34
- cls.attach_argument(argparser, arg)
40
+ try:
41
+ cls.attach_argument(argparser, arg)
42
+ except Exception as e:
43
+ _logger.error(
44
+ f"unable to attach argument {arg.name}"
45
+ f"to command '{subcommand.name}'\n {e}"
46
+ )
47
+ # parser valid, just some args missing
35
48
  if subcommand.default:
36
49
  if last_default:
37
50
  _logger.warning(