sigil-cli 1.5.0__tar.gz → 1.5.2__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.5.0 → sigil_cli-1.5.2}/PKG-INFO +3 -3
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/README.md +2 -2
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/pyproject.toml +3 -1
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/__init__.py +14 -0
- sigil_cli-1.5.2/src/sigil/__main__.py +22 -0
- sigil_cli-1.5.2/src/sigil/cli_tools/__init__.py +9 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/cli_tools/init.py +8 -1
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/cli_tools/tree.py +7 -0
- sigil_cli-1.5.2/src/sigil/cli_tools/util.py +24 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/cli_tools/validate.py +9 -0
- sigil_cli-1.5.2/src/sigil/datasource/__init__.py +15 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/datasource/datasource.py +3 -2
- sigil_cli-1.5.2/src/sigil/datasource/helper.py +35 -0
- sigil_cli-1.5.2/src/sigil/datasource/json_source.py +45 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/datasource/yml_source.py +17 -7
- sigil_cli-1.5.2/src/sigil/entrypoint.py +32 -0
- sigil_cli-1.5.2/src/sigil/script_sources/__init__.py +7 -0
- sigil_cli-1.5.0/src/sigil/stages/script_loader.py → sigil_cli-1.5.2/src/sigil/script_sources/filesystem_script_source.py +7 -53
- sigil_cli-1.5.2/src/sigil/script_sources/script_source.py +14 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/stages/parser.py +2 -47
- sigil_cli-1.5.2/src/sigil/stages/script_loader.py +84 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil_cli.egg-info/PKG-INFO +3 -3
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil_cli.egg-info/SOURCES.txt +7 -0
- sigil_cli-1.5.2/tests/test_cli_util.py +27 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_integration.py +4 -1
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_scriptloader.py +10 -7
- sigil_cli-1.5.0/src/sigil/__main__.py +0 -40
- sigil_cli-1.5.0/src/sigil/cli_tools/__init__.py +0 -0
- sigil_cli-1.5.0/src/sigil/datasource/__init__.py +0 -7
- sigil_cli-1.5.0/src/sigil/entrypoint.py +0 -44
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/LICENSE +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/setup.cfg +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/models/__init__.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/models/argparser.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/models/argument.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/models/command.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/models/script.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/stages/__init__.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/stages/builder.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil/stages/resolver.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil_cli.egg-info/dependency_links.txt +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil_cli.egg-info/entry_points.txt +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil_cli.egg-info/requires.txt +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/src/sigil_cli.egg-info/top_level.txt +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_builder.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_cli_init.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_cli_tree.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_cli_validate.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_reader.py +0 -0
- {sigil_cli-1.5.0 → sigil_cli-1.5.2}/tests/test_resolver.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sigil-cli
|
|
3
|
-
Version: 1.5.
|
|
3
|
+
Version: 1.5.2
|
|
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
|
|
@@ -286,11 +286,11 @@ from sigil import run_from_config
|
|
|
286
286
|
# Use JSON instead:
|
|
287
287
|
class JsonReader:
|
|
288
288
|
@classmethod
|
|
289
|
-
def read_manifest(cls, config_root: Path) -> list | None:
|
|
289
|
+
def read_manifest(cls, config_root: Path, target: str) -> list | None:
|
|
290
290
|
# read target paths for loading configuration
|
|
291
291
|
...
|
|
292
292
|
|
|
293
|
-
def read_configuration(cls, target: Path) -> dict | None:
|
|
293
|
+
def read_configuration(cls, target: Path, target: str) -> dict | None:
|
|
294
294
|
# read *.json, parse, convert to dict of raw data
|
|
295
295
|
...
|
|
296
296
|
|
|
@@ -256,11 +256,11 @@ from sigil import run_from_config
|
|
|
256
256
|
# Use JSON instead:
|
|
257
257
|
class JsonReader:
|
|
258
258
|
@classmethod
|
|
259
|
-
def read_manifest(cls, config_root: Path) -> list | None:
|
|
259
|
+
def read_manifest(cls, config_root: Path, target: str) -> list | None:
|
|
260
260
|
# read target paths for loading configuration
|
|
261
261
|
...
|
|
262
262
|
|
|
263
|
-
def read_configuration(cls, target: Path) -> dict | None:
|
|
263
|
+
def read_configuration(cls, target: Path, target: str) -> dict | None:
|
|
264
264
|
# read *.json, parse, convert to dict of raw data
|
|
265
265
|
...
|
|
266
266
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "sigil-cli"
|
|
7
|
-
version = "1.5.
|
|
7
|
+
version = "1.5.2"
|
|
8
8
|
description = "Configuration driven CLI builder with subcommands and script loading"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -28,6 +28,7 @@ dependencies = [
|
|
|
28
28
|
[project.optional-dependencies]
|
|
29
29
|
completion = ["argcomplete>=3.0"]
|
|
30
30
|
dev = ["pytest", "pytest-cov", "ruff"]
|
|
31
|
+
# yaml = ["pyyaml>=6.0"] # planned default for PEP 771
|
|
31
32
|
|
|
32
33
|
[project.urls]
|
|
33
34
|
Homepage = "https://github.com/kenzo-staelens/sigil"
|
|
@@ -43,6 +44,7 @@ where = ["src"]
|
|
|
43
44
|
[tool.coverage.run]
|
|
44
45
|
omit = [
|
|
45
46
|
"src/sigil/__main__.py",
|
|
47
|
+
"src/sigil/datasource/json_source.py", # nearly 1:1 with yml source
|
|
46
48
|
"tests/*",
|
|
47
49
|
"*/scripts/*"
|
|
48
50
|
]
|
|
@@ -2,6 +2,11 @@ from importlib.metadata import version
|
|
|
2
2
|
|
|
3
3
|
__version__ = version('sigil-cli')
|
|
4
4
|
|
|
5
|
+
from .datasource import (
|
|
6
|
+
DataSource,
|
|
7
|
+
JSONSource,
|
|
8
|
+
YmlSource,
|
|
9
|
+
)
|
|
5
10
|
from .entrypoint import run_from_config
|
|
6
11
|
from .models import (
|
|
7
12
|
Argument,
|
|
@@ -10,6 +15,10 @@ from .models import (
|
|
|
10
15
|
ParserConfig,
|
|
11
16
|
SubcommandModule,
|
|
12
17
|
)
|
|
18
|
+
from .script_sources import (
|
|
19
|
+
FilesystemScriptSource,
|
|
20
|
+
ScriptSource,
|
|
21
|
+
)
|
|
13
22
|
from .stages import (
|
|
14
23
|
Builder,
|
|
15
24
|
Parser,
|
|
@@ -28,4 +37,9 @@ __all__ = (
|
|
|
28
37
|
'Resolver',
|
|
29
38
|
'Builder',
|
|
30
39
|
'ScriptLoader',
|
|
40
|
+
'DataSource',
|
|
41
|
+
'JSONSource',
|
|
42
|
+
'YmlSource',
|
|
43
|
+
'FilesystemScriptSource',
|
|
44
|
+
'ScriptSource',
|
|
31
45
|
)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
|
|
3
|
+
from sigil import __version__
|
|
4
|
+
from sigil.cli_tools.util import COMMAND_REGISTRY, REGISTER_CALLABLES
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def main():
|
|
8
|
+
parser = argparse.ArgumentParser(prog='sigil')
|
|
9
|
+
parser.add_argument('--version', action='version', version=f'Sigil {__version__}')
|
|
10
|
+
sub = parser.add_subparsers(title="subcommands", dest='command')
|
|
11
|
+
for fn in REGISTER_CALLABLES:
|
|
12
|
+
fn(sub)
|
|
13
|
+
|
|
14
|
+
args = parser.parse_args()
|
|
15
|
+
if args.command not in COMMAND_REGISTRY:
|
|
16
|
+
parser.parse_args(['-h'])
|
|
17
|
+
return
|
|
18
|
+
COMMAND_REGISTRY[args.command](args)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
if __name__ == "__main__":
|
|
22
|
+
main()
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
# for everyone's sanity: not to be confused with __init__
|
|
2
|
-
|
|
2
|
+
import argparse
|
|
3
3
|
import sys
|
|
4
4
|
from pathlib import Path
|
|
5
5
|
|
|
6
|
+
from .util import registers
|
|
7
|
+
|
|
6
8
|
# initial file contents
|
|
7
9
|
MANIFEST="""- yml/root.yml
|
|
8
10
|
"""
|
|
@@ -75,3 +77,8 @@ def create_project(project_name: str, target_dir: str = "."):
|
|
|
75
77
|
print(f" cd {project_name}")
|
|
76
78
|
print(" python main.py hello --help")
|
|
77
79
|
print(" create an alias")
|
|
80
|
+
|
|
81
|
+
@registers('init', 'generate a minimal sigil')
|
|
82
|
+
def add_sigil_init(command: argparse.ArgumentParser):
|
|
83
|
+
command.add_argument('name', help="project name to create")
|
|
84
|
+
return lambda args: create_project(args.name)
|
|
@@ -3,6 +3,8 @@ import argparse
|
|
|
3
3
|
from sigil import Builder, Parser, Resolver
|
|
4
4
|
from sigil.datasource import YmlSource
|
|
5
5
|
|
|
6
|
+
from .util import registers
|
|
7
|
+
|
|
6
8
|
|
|
7
9
|
# 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
10
|
def parser_tree(
|
|
@@ -39,3 +41,8 @@ def display_project(projectroot):
|
|
|
39
41
|
parser = Builder.build(resolved)
|
|
40
42
|
print(parser_tree(parser))
|
|
41
43
|
|
|
44
|
+
|
|
45
|
+
@registers('tree', 'display the command structure of a sigil.')
|
|
46
|
+
def add_sigil_tree(command: argparse.ArgumentParser):
|
|
47
|
+
command.add_argument('path', help="project to validate")
|
|
48
|
+
return lambda args: display_project(args.path)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import functools
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
|
|
5
|
+
COMMAND_REGISTRY: 'dict[str, Callable[[argparse.Namespace]]]' = {}
|
|
6
|
+
REGISTER_CALLABLES = []
|
|
7
|
+
|
|
8
|
+
def registers(name: str, cmd_help: str):
|
|
9
|
+
def decorator_fn(f: 'Callable[[argparse.ArgumentParser], Callable[[argparse.Namespace]]]'): # noqa: E501
|
|
10
|
+
@functools.wraps(f)
|
|
11
|
+
def wrapper(parser: argparse._SubParsersAction):
|
|
12
|
+
if name in COMMAND_REGISTRY:
|
|
13
|
+
raise RuntimeError(f'{name} already registered')
|
|
14
|
+
subparser: argparse.ArgumentParser = parser.add_parser(
|
|
15
|
+
name=name,
|
|
16
|
+
help=cmd_help
|
|
17
|
+
)
|
|
18
|
+
callable = f(subparser)
|
|
19
|
+
COMMAND_REGISTRY[name] = callable
|
|
20
|
+
return subparser
|
|
21
|
+
# note: requires import in cli_tools.__init__ to auto trigger registration
|
|
22
|
+
REGISTER_CALLABLES.append(wrapper)
|
|
23
|
+
return wrapper
|
|
24
|
+
return decorator_fn
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import argparse
|
|
1
2
|
import contextlib
|
|
2
3
|
import logging
|
|
3
4
|
import sys
|
|
@@ -6,6 +7,8 @@ from pathlib import Path
|
|
|
6
7
|
from sigil import Builder, Parser, Resolver
|
|
7
8
|
from sigil.datasource import YmlSource
|
|
8
9
|
|
|
10
|
+
from .util import registers
|
|
11
|
+
|
|
9
12
|
|
|
10
13
|
# small helper to capture logs from in the guts of sigil
|
|
11
14
|
class WarningCollector(logging.Handler):
|
|
@@ -115,3 +118,9 @@ def validate_project(projectroot):
|
|
|
115
118
|
sys.exit(1)
|
|
116
119
|
else:
|
|
117
120
|
print("Validation passed successfully.", file=sys.stderr)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@registers('validate', 'validate structure of a sigil')
|
|
124
|
+
def add_sigil_validate(command: argparse.ArgumentParser):
|
|
125
|
+
command.add_argument('path', help="project to validate")
|
|
126
|
+
return lambda args: validate_project(args.path)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .datasource import DataSource
|
|
2
|
+
from .json_source import JSONSource
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
'DataSource',
|
|
6
|
+
'JSONSource',
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
# preparation for PEP 771 or conversion to `pip install sigil-cli[yaml]` install
|
|
11
|
+
# any datasources with dependencies should import like this
|
|
12
|
+
from .yml_source import YmlSource # noqa: F401
|
|
13
|
+
__all__.append('YmlSource')
|
|
14
|
+
except ImportError:
|
|
15
|
+
pass
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
from abc import ABC, abstractmethod
|
|
2
2
|
from pathlib import Path
|
|
3
|
+
from typing import Any
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
# currently network based datasources are not planned
|
|
6
7
|
# but if you do *need* one for some reason, feel free to make a PR
|
|
7
8
|
class DataSource(ABC):
|
|
8
9
|
@abstractmethod
|
|
9
|
-
def read_manifest(self, target:
|
|
10
|
+
def read_manifest(self, root_path: Path, target: Any) -> list | None:
|
|
10
11
|
# current implementation expects a list of files, or none on error
|
|
11
12
|
...
|
|
12
13
|
|
|
13
14
|
@abstractmethod
|
|
14
|
-
def read_configuration(self, target:
|
|
15
|
+
def read_configuration(self, root_path: Path, target: Any) -> dict | None:
|
|
15
16
|
# raw data roughly mapping to internal models
|
|
16
17
|
# validation happens in a later stage
|
|
17
18
|
# datasources shouldn't need to validate
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def _expandpath(config_root: Path, path_pattern: Path) -> Iterable[Path]:
|
|
6
|
+
_has_glob = lambda part: any(ch in part for ch in "*?[") # noqa: E731
|
|
7
|
+
p: Path = (config_root/path_pattern).resolve()
|
|
8
|
+
str_p = str(p)
|
|
9
|
+
|
|
10
|
+
# Quick check: does this path contain any glob pattern at all?
|
|
11
|
+
if not _has_glob(str_p):
|
|
12
|
+
yield p.resolve()
|
|
13
|
+
return # is this cursed? yes, does it work somehow? also yes
|
|
14
|
+
parts = p.parts
|
|
15
|
+
|
|
16
|
+
# Find the first part that contains a wildcard
|
|
17
|
+
first_glob_idx = 0
|
|
18
|
+
for i, part in enumerate(parts):
|
|
19
|
+
if _has_glob(part):
|
|
20
|
+
first_glob_idx = i
|
|
21
|
+
break
|
|
22
|
+
|
|
23
|
+
# Build the literal base directory and the relative glob pattern
|
|
24
|
+
if first_glob_idx == 0:
|
|
25
|
+
base = Path('.')
|
|
26
|
+
if parts[0] == '/': # pesky absolute paths
|
|
27
|
+
base = Path('/')
|
|
28
|
+
parts = parts[1:]
|
|
29
|
+
else:
|
|
30
|
+
base = Path(*parts[:first_glob_idx])
|
|
31
|
+
|
|
32
|
+
pattern = "/".join(parts[first_glob_idx:])
|
|
33
|
+
# Expand the pattern relative to the base, and resolve each match
|
|
34
|
+
for match in base.glob(pattern):
|
|
35
|
+
yield match.resolve()
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .datasource import DataSource
|
|
7
|
+
from .helper import _expandpath
|
|
8
|
+
|
|
9
|
+
_logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# base defines abstract as not-classmethod
|
|
13
|
+
# though instantiation is not a requirement
|
|
14
|
+
# if you don't need instance data, classmethods are fine too
|
|
15
|
+
class JSONSource(DataSource):
|
|
16
|
+
def read(self, root_path: Path, filename: str) -> dict:
|
|
17
|
+
try:
|
|
18
|
+
target = root_path/filename
|
|
19
|
+
with open(target) as f:
|
|
20
|
+
return json.load(f)
|
|
21
|
+
# return yaml.load(f.read(), Loader=yaml.SafeLoader)
|
|
22
|
+
except FileNotFoundError:
|
|
23
|
+
_logger.error(f"file '{target}' not found")
|
|
24
|
+
return
|
|
25
|
+
except json.JSONDecodeError as e:
|
|
26
|
+
_logger.error(f"malformed json file ({target})\n {e}")
|
|
27
|
+
return
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def read_manifest(self, root_path: Path, filename: str) -> list | None:
|
|
31
|
+
# indirection? yes
|
|
32
|
+
# can new datasources implement a better manifest vs data? also yes
|
|
33
|
+
raw_manifest = self.read(root_path, filename)
|
|
34
|
+
res_manifest = []
|
|
35
|
+
for line in raw_manifest:
|
|
36
|
+
res_manifest = itertools.chain(
|
|
37
|
+
res_manifest,
|
|
38
|
+
_expandpath(root_path, Path(line))
|
|
39
|
+
)
|
|
40
|
+
return res_manifest
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def read_configuration(self, root_path: Path, filename: str) -> dict | None:
|
|
44
|
+
return self.read(root_path, filename)
|
|
45
|
+
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import itertools
|
|
1
2
|
import logging
|
|
2
3
|
from pathlib import Path
|
|
3
4
|
|
|
4
5
|
import yaml
|
|
5
6
|
|
|
6
7
|
from .datasource import DataSource
|
|
8
|
+
from .helper import _expandpath
|
|
7
9
|
|
|
8
10
|
_logger = logging.getLogger(__name__)
|
|
9
11
|
|
|
@@ -12,8 +14,9 @@ _logger = logging.getLogger(__name__)
|
|
|
12
14
|
# though instantiation is not a requirement
|
|
13
15
|
# if you don't need instance data, classmethods are fine too
|
|
14
16
|
class YmlSource(DataSource):
|
|
15
|
-
def read(self,
|
|
17
|
+
def read(self, root_path: Path, filename: str) -> dict:
|
|
16
18
|
try:
|
|
19
|
+
target = root_path/filename
|
|
17
20
|
with open(target) as f:
|
|
18
21
|
return yaml.load(f.read(), Loader=yaml.SafeLoader)
|
|
19
22
|
except FileNotFoundError:
|
|
@@ -24,12 +27,19 @@ class YmlSource(DataSource):
|
|
|
24
27
|
return
|
|
25
28
|
|
|
26
29
|
|
|
27
|
-
def read_manifest(self,
|
|
30
|
+
def read_manifest(self, root_path: Path, filename: str) -> list | None:
|
|
28
31
|
# indirection? yes
|
|
29
32
|
# can new datasources implement a better manifest vs data? also yes
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
raw_manifest = self.read(root_path, filename)
|
|
34
|
+
res_manifest = []
|
|
35
|
+
for line in raw_manifest:
|
|
36
|
+
res_manifest = itertools.chain(
|
|
37
|
+
res_manifest,
|
|
38
|
+
_expandpath(root_path, Path(line))
|
|
39
|
+
)
|
|
40
|
+
return res_manifest
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def read_configuration(self, root_path: Path, filename: str) -> dict | None:
|
|
44
|
+
return self.read(root_path, filename)
|
|
35
45
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from .datasource import DataSource, YmlSource
|
|
5
|
+
from .script_sources import FilesystemScriptSource, ScriptSource
|
|
6
|
+
from .stages import Builder, Parser, Resolver, ScriptLoader
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# default to yamlloader, use whatever datastore you feel like
|
|
10
|
+
def run_from_config(
|
|
11
|
+
config_root: str | Path,
|
|
12
|
+
datasource: DataSource | type[DataSource] = YmlSource,
|
|
13
|
+
manifest_target='manifest.yml',
|
|
14
|
+
script_source: ScriptSource=FilesystemScriptSource # also default to FS scripts
|
|
15
|
+
) -> None:
|
|
16
|
+
raw_data = Parser(datasource).load(config_root, manifest_target)
|
|
17
|
+
resolved = Resolver.resolve_inheritance(raw_data)
|
|
18
|
+
# parser beyond here is an argparser
|
|
19
|
+
parser = Builder.build(resolved)
|
|
20
|
+
|
|
21
|
+
execution_context = {}
|
|
22
|
+
|
|
23
|
+
if resolved.known_args:
|
|
24
|
+
args, other_args = parser.parse_known_args()
|
|
25
|
+
execution_context: dict[str, Any] = {'other_args': other_args}
|
|
26
|
+
else:
|
|
27
|
+
args = parser.parse_args()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
scriptloader = ScriptLoader(config_root, resolved.script_dir, script_source)
|
|
31
|
+
scripts = scriptloader.get_scripts(args, 'root', parser, {'root': resolved})
|
|
32
|
+
scriptloader.run_scripts(scripts, args, execution_context)
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import importlib
|
|
1
|
+
import importlib
|
|
2
2
|
import logging
|
|
3
3
|
import sys
|
|
4
|
-
from argparse import Namespace
|
|
5
4
|
from importlib.abc import Loader
|
|
6
5
|
from pathlib import Path
|
|
7
6
|
from typing import cast
|
|
8
7
|
|
|
9
|
-
from sigil.models import
|
|
8
|
+
from sigil.models import SubcommandModule
|
|
9
|
+
|
|
10
|
+
from .script_source import ScriptSource
|
|
10
11
|
|
|
11
12
|
_logger = logging.getLogger(__name__)
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
|
|
15
|
+
class FilesystemScriptSource(ScriptSource):
|
|
14
16
|
@classmethod
|
|
15
17
|
def _construct_package_name(
|
|
16
18
|
cls,
|
|
@@ -40,11 +42,10 @@ class ScriptLoader:
|
|
|
40
42
|
)
|
|
41
43
|
return fully_qualified_name
|
|
42
44
|
|
|
43
|
-
|
|
44
45
|
@classmethod
|
|
45
46
|
def import_module(
|
|
46
47
|
cls,
|
|
47
|
-
config_root,
|
|
48
|
+
config_root: Path,
|
|
48
49
|
path:str | None,
|
|
49
50
|
module_name: str
|
|
50
51
|
) -> SubcommandModule | None:
|
|
@@ -69,50 +70,3 @@ class ScriptLoader:
|
|
|
69
70
|
module = importlib.util.module_from_spec(spec)
|
|
70
71
|
cast(Loader, spec.loader).exec_module(module)
|
|
71
72
|
return cast(SubcommandModule, module) # or at least assumed to be by contract
|
|
72
|
-
|
|
73
|
-
@classmethod
|
|
74
|
-
def _get_next_parser_name(cls, args: Namespace, target: str, parser: ParserConfig):
|
|
75
|
-
# get the name of the next subcommand
|
|
76
|
-
if target != 'root':
|
|
77
|
-
# normal case a subcommand just points to the next arg
|
|
78
|
-
next_value = getattr(args, target)
|
|
79
|
-
else:
|
|
80
|
-
# root is a special case because the top level parser
|
|
81
|
-
# is required to be called root (id, not name parameter)
|
|
82
|
-
# therefore root needs to grab the next part by name instead
|
|
83
|
-
next_value = getattr(args, parser.name)
|
|
84
|
-
return next_value
|
|
85
|
-
|
|
86
|
-
@classmethod
|
|
87
|
-
def get_scripts(
|
|
88
|
-
cls,
|
|
89
|
-
args: Namespace,
|
|
90
|
-
target: str,
|
|
91
|
-
root_parser: LibArgParser,
|
|
92
|
-
data: dict[str, ParserConfig]
|
|
93
|
-
) -> list[str]:
|
|
94
|
-
found_scripts = []
|
|
95
|
-
|
|
96
|
-
target_parser: ParserConfig = data[target]
|
|
97
|
-
subparsers = target_parser.subparsers
|
|
98
|
-
script = target_parser.script
|
|
99
|
-
|
|
100
|
-
if script:
|
|
101
|
-
found_scripts.append(script)
|
|
102
|
-
if not subparsers:
|
|
103
|
-
# nothing to keep exploring anymore -> early exit
|
|
104
|
-
return found_scripts
|
|
105
|
-
|
|
106
|
-
next_value = cls._get_next_parser_name(args, target, target_parser)
|
|
107
|
-
|
|
108
|
-
if next_value is None:
|
|
109
|
-
# if subcommands exist but no subcommand is used print help instead
|
|
110
|
-
root_parser.parse_args(sys.argv[1:] + ['--help'])
|
|
111
|
-
sys.exit(2) # nothing to do here
|
|
112
|
-
|
|
113
|
-
return found_scripts + cls.get_scripts(
|
|
114
|
-
args,
|
|
115
|
-
next_value,
|
|
116
|
-
root_parser,
|
|
117
|
-
subparsers,
|
|
118
|
-
)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from sigil.models import SubcommandModule
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ScriptSource(ABC):
|
|
8
|
+
@abstractmethod
|
|
9
|
+
def import_module(
|
|
10
|
+
cls,
|
|
11
|
+
config_root: Path, # config root
|
|
12
|
+
script_dir: str | None, # script dir according to root config
|
|
13
|
+
module_name: str # data in config file
|
|
14
|
+
) -> SubcommandModule | None: ...
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import itertools
|
|
2
1
|
import logging
|
|
3
2
|
import sys
|
|
4
|
-
from collections.abc import Iterable
|
|
5
3
|
from pathlib import Path
|
|
6
4
|
from typing import Any
|
|
7
5
|
|
|
@@ -30,48 +28,6 @@ class Parser:
|
|
|
30
28
|
for duplicate in duplicates:
|
|
31
29
|
_logger.warning(f"{entry}[{duplicate}] already defined, ignoring")
|
|
32
30
|
|
|
33
|
-
def _expandpath(self, config_root: Path, path_pattern: Path) -> Iterable[Path]:
|
|
34
|
-
_has_glob = lambda part: any(ch in part for ch in "*?[") # noqa: E731
|
|
35
|
-
p: Path = (config_root/path_pattern).resolve()
|
|
36
|
-
str_p = str(p)
|
|
37
|
-
|
|
38
|
-
# Quick check: does this path contain any glob pattern at all?
|
|
39
|
-
if not _has_glob(str_p):
|
|
40
|
-
yield p.resolve()
|
|
41
|
-
return # is this cursed? yes, does it work somehow? also yes
|
|
42
|
-
parts = p.parts
|
|
43
|
-
|
|
44
|
-
# Find the first part that contains a wildcard
|
|
45
|
-
first_glob_idx = 0
|
|
46
|
-
for i, part in enumerate(parts):
|
|
47
|
-
if _has_glob(part):
|
|
48
|
-
first_glob_idx = i
|
|
49
|
-
break
|
|
50
|
-
|
|
51
|
-
# Build the literal base directory and the relative glob pattern
|
|
52
|
-
if first_glob_idx == 0:
|
|
53
|
-
base = Path('.')
|
|
54
|
-
if parts[0] == '/': # pesky absolute paths
|
|
55
|
-
base = Path('/')
|
|
56
|
-
parts = parts[1:]
|
|
57
|
-
else:
|
|
58
|
-
base = Path(*parts[:first_glob_idx])
|
|
59
|
-
|
|
60
|
-
pattern = "/".join(parts[first_glob_idx:])
|
|
61
|
-
# Expand the pattern relative to the base, and resolve each match
|
|
62
|
-
for match in base.glob(pattern):
|
|
63
|
-
yield match.resolve()
|
|
64
|
-
|
|
65
|
-
def _resolve_manifest(self, config_root, manifest: list) -> Iterable[Path]:
|
|
66
|
-
# because iterable unpacking is not supported in list comprehension
|
|
67
|
-
res_manifest = []
|
|
68
|
-
for line in manifest:
|
|
69
|
-
res_manifest = itertools.chain(
|
|
70
|
-
res_manifest,
|
|
71
|
-
self._expandpath(config_root, Path(line))
|
|
72
|
-
)
|
|
73
|
-
return res_manifest
|
|
74
|
-
|
|
75
31
|
def load(
|
|
76
32
|
self,
|
|
77
33
|
config_root: str | Path,
|
|
@@ -79,15 +35,14 @@ class Parser:
|
|
|
79
35
|
) -> dict[str, ParserConfig]:
|
|
80
36
|
config_root_path = Path(config_root)
|
|
81
37
|
|
|
82
|
-
manifest = self.datasource.read_manifest(config_root_path
|
|
38
|
+
manifest = self.datasource.read_manifest(config_root_path, manifest_file)
|
|
83
39
|
if not manifest:
|
|
84
40
|
_logger.critical("could not load manifest, aborting")
|
|
85
41
|
sys.exit(1)
|
|
86
42
|
loaded_config = {}
|
|
87
|
-
manifest = self._resolve_manifest(config_root, manifest)
|
|
88
43
|
|
|
89
44
|
for entry in manifest:
|
|
90
|
-
loaded_entry = self.datasource.read_configuration(config_root_path
|
|
45
|
+
loaded_entry = self.datasource.read_configuration(config_root_path, entry)
|
|
91
46
|
if not loaded_entry:
|
|
92
47
|
continue
|
|
93
48
|
tmp = {}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sys
|
|
3
|
+
from argparse import Namespace
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from sigil.models import LibArgParser, ParserConfig
|
|
7
|
+
from sigil.script_sources import ScriptSource
|
|
8
|
+
|
|
9
|
+
_logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
class ScriptLoader:
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
config_root: Path,
|
|
15
|
+
script_dir: str | None,
|
|
16
|
+
script_source: ScriptSource | type[ScriptSource]
|
|
17
|
+
):
|
|
18
|
+
self.config_root = config_root
|
|
19
|
+
self.script_dir = script_dir
|
|
20
|
+
if isinstance(script_source, type):
|
|
21
|
+
script_source = script_source()
|
|
22
|
+
self.script_source = script_source
|
|
23
|
+
|
|
24
|
+
def _get_next_parser_name(cls, args: Namespace, target: str, parser: ParserConfig):
|
|
25
|
+
# get the name of the next subcommand
|
|
26
|
+
if target != 'root':
|
|
27
|
+
# normal case a subcommand just points to the next arg
|
|
28
|
+
next_value = getattr(args, target)
|
|
29
|
+
else:
|
|
30
|
+
# root is a special case because the top level parser
|
|
31
|
+
# is required to be called root (id, not name parameter)
|
|
32
|
+
# therefore root needs to grab the next part by name instead
|
|
33
|
+
next_value = getattr(args, parser.name)
|
|
34
|
+
return next_value
|
|
35
|
+
|
|
36
|
+
def get_scripts(
|
|
37
|
+
cls,
|
|
38
|
+
args: Namespace,
|
|
39
|
+
target: str,
|
|
40
|
+
root_parser: LibArgParser,
|
|
41
|
+
data: dict[str, ParserConfig]
|
|
42
|
+
) -> list[str]:
|
|
43
|
+
found_scripts = []
|
|
44
|
+
|
|
45
|
+
target_parser: ParserConfig = data[target]
|
|
46
|
+
subparsers = target_parser.subparsers
|
|
47
|
+
script = target_parser.script
|
|
48
|
+
|
|
49
|
+
if script:
|
|
50
|
+
found_scripts.append(script)
|
|
51
|
+
if not subparsers:
|
|
52
|
+
# nothing to keep exploring anymore -> early exit
|
|
53
|
+
return found_scripts
|
|
54
|
+
|
|
55
|
+
next_value = cls._get_next_parser_name(args, target, target_parser)
|
|
56
|
+
|
|
57
|
+
if next_value is None:
|
|
58
|
+
# if subcommands exist but no subcommand is used print help instead
|
|
59
|
+
root_parser.parse_args(sys.argv[1:] + ['--help'])
|
|
60
|
+
sys.exit(2) # nothing to do here
|
|
61
|
+
|
|
62
|
+
return found_scripts + cls.get_scripts(
|
|
63
|
+
args,
|
|
64
|
+
next_value,
|
|
65
|
+
root_parser,
|
|
66
|
+
subparsers,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def run_scripts(self, script_targets, namespace, context):
|
|
70
|
+
for script in script_targets:
|
|
71
|
+
try:
|
|
72
|
+
module = self.script_source.import_module(
|
|
73
|
+
self.config_root,
|
|
74
|
+
self.script_dir,
|
|
75
|
+
script
|
|
76
|
+
)
|
|
77
|
+
if not module:
|
|
78
|
+
continue
|
|
79
|
+
except Exception as e:
|
|
80
|
+
# prevent your subcommand from turning your environment
|
|
81
|
+
# into undefined soup by not continuing execution
|
|
82
|
+
_logger.critical(f'failed to load script "{script}"\n {e}')
|
|
83
|
+
sys.exit(2)
|
|
84
|
+
module.run(namespace, context)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sigil-cli
|
|
3
|
-
Version: 1.5.
|
|
3
|
+
Version: 1.5.2
|
|
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
|
|
@@ -286,11 +286,11 @@ from sigil import run_from_config
|
|
|
286
286
|
# Use JSON instead:
|
|
287
287
|
class JsonReader:
|
|
288
288
|
@classmethod
|
|
289
|
-
def read_manifest(cls, config_root: Path) -> list | None:
|
|
289
|
+
def read_manifest(cls, config_root: Path, target: str) -> list | None:
|
|
290
290
|
# read target paths for loading configuration
|
|
291
291
|
...
|
|
292
292
|
|
|
293
|
-
def read_configuration(cls, target: Path) -> dict | None:
|
|
293
|
+
def read_configuration(cls, target: Path, target: str) -> dict | None:
|
|
294
294
|
# read *.json, parse, convert to dict of raw data
|
|
295
295
|
...
|
|
296
296
|
|
|
@@ -7,15 +7,21 @@ src/sigil/entrypoint.py
|
|
|
7
7
|
src/sigil/cli_tools/__init__.py
|
|
8
8
|
src/sigil/cli_tools/init.py
|
|
9
9
|
src/sigil/cli_tools/tree.py
|
|
10
|
+
src/sigil/cli_tools/util.py
|
|
10
11
|
src/sigil/cli_tools/validate.py
|
|
11
12
|
src/sigil/datasource/__init__.py
|
|
12
13
|
src/sigil/datasource/datasource.py
|
|
14
|
+
src/sigil/datasource/helper.py
|
|
15
|
+
src/sigil/datasource/json_source.py
|
|
13
16
|
src/sigil/datasource/yml_source.py
|
|
14
17
|
src/sigil/models/__init__.py
|
|
15
18
|
src/sigil/models/argparser.py
|
|
16
19
|
src/sigil/models/argument.py
|
|
17
20
|
src/sigil/models/command.py
|
|
18
21
|
src/sigil/models/script.py
|
|
22
|
+
src/sigil/script_sources/__init__.py
|
|
23
|
+
src/sigil/script_sources/filesystem_script_source.py
|
|
24
|
+
src/sigil/script_sources/script_source.py
|
|
19
25
|
src/sigil/stages/__init__.py
|
|
20
26
|
src/sigil/stages/builder.py
|
|
21
27
|
src/sigil/stages/parser.py
|
|
@@ -30,6 +36,7 @@ src/sigil_cli.egg-info/top_level.txt
|
|
|
30
36
|
tests/test_builder.py
|
|
31
37
|
tests/test_cli_init.py
|
|
32
38
|
tests/test_cli_tree.py
|
|
39
|
+
tests/test_cli_util.py
|
|
33
40
|
tests/test_cli_validate.py
|
|
34
41
|
tests/test_integration.py
|
|
35
42
|
tests/test_reader.py
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from sigil.cli_tools.util import COMMAND_REGISTRY, REGISTER_CALLABLES
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_cli_util(
|
|
9
|
+
capsys: pytest.CaptureFixture
|
|
10
|
+
) -> None:
|
|
11
|
+
parser = argparse.ArgumentParser(prog='sigil')
|
|
12
|
+
parser.add_argument('--version', action='version', version='Sigil x.x.x')
|
|
13
|
+
sub = parser.add_subparsers(title="subcommands", dest='command')
|
|
14
|
+
|
|
15
|
+
assert len(REGISTER_CALLABLES) == 3
|
|
16
|
+
for fn in REGISTER_CALLABLES:
|
|
17
|
+
fn(sub)
|
|
18
|
+
|
|
19
|
+
with pytest.raises(SystemExit):
|
|
20
|
+
# argparse raises on -h :/
|
|
21
|
+
parser.parse_args(['-h'])
|
|
22
|
+
|
|
23
|
+
# # Act & Assert: should not raise or exit
|
|
24
|
+
captured = capsys.readouterr().out.strip()
|
|
25
|
+
print('xxx',captured)
|
|
26
|
+
for k in COMMAND_REGISTRY.keys():
|
|
27
|
+
assert k in captured
|
|
@@ -7,6 +7,7 @@ import yaml
|
|
|
7
7
|
from sigil import (
|
|
8
8
|
Argument,
|
|
9
9
|
Builder,
|
|
10
|
+
FilesystemScriptSource,
|
|
10
11
|
LibArgParser,
|
|
11
12
|
ParserConfig,
|
|
12
13
|
ScriptLoader,
|
|
@@ -119,6 +120,8 @@ def test_script_loader_no_subcommand(capsys):
|
|
|
119
120
|
args = parser.parse_args([]) # no subcommand
|
|
120
121
|
# get_scripts will print help and sys.exit
|
|
121
122
|
with pytest.raises(SystemExit):
|
|
122
|
-
ScriptLoader
|
|
123
|
+
ScriptLoader(
|
|
124
|
+
None, None, FilesystemScriptSource
|
|
125
|
+
).get_scripts(args, "root", parser, data)
|
|
123
126
|
captured = capsys.readouterr()
|
|
124
127
|
assert "usage:" in captured.out or "usage:" in captured.err
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from unittest.mock import MagicMock, patch
|
|
2
2
|
|
|
3
|
-
from sigil import Builder, ScriptLoader
|
|
3
|
+
from sigil import Builder, FilesystemScriptSource, ScriptLoader
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
def test_script_loader_get_scripts(resolved_config):
|
|
@@ -13,7 +13,10 @@ def test_script_loader_get_scripts(resolved_config):
|
|
|
13
13
|
# data structure: we have root as ParserConfig;
|
|
14
14
|
# we need to pass as dict for recursive lookup
|
|
15
15
|
data = {"root": resolved_config}
|
|
16
|
-
scripts = ScriptLoader
|
|
16
|
+
scripts = ScriptLoader(
|
|
17
|
+
None, None, FilesystemScriptSource
|
|
18
|
+
).get_scripts(args, "root", parser, data)
|
|
19
|
+
# scripts = ScriptLoader.get_scripts(args, "root", parser, data)
|
|
17
20
|
|
|
18
21
|
# The leaf script should be found
|
|
19
22
|
assert "leaf_script" in scripts
|
|
@@ -32,24 +35,24 @@ def test_script_loader_import_module(tmp_path):
|
|
|
32
35
|
" print('Hello')\n"
|
|
33
36
|
)
|
|
34
37
|
|
|
35
|
-
loader = ScriptLoader()
|
|
36
|
-
module = loader.import_module(
|
|
38
|
+
loader = ScriptLoader(None, None, FilesystemScriptSource)
|
|
39
|
+
module = loader.script_source.import_module(tmp_path, "scripts", "my_script")
|
|
37
40
|
assert hasattr(module, "run")
|
|
38
41
|
assert module is not None
|
|
39
42
|
assert callable(module.run)
|
|
40
43
|
|
|
41
|
-
@patch("sigil.
|
|
44
|
+
@patch("sigil.script_sources.filesystem_script_source.importlib.util")
|
|
42
45
|
def test_script_loader_import_module_mock(mock_importlib, tmp_path):
|
|
43
46
|
"""Test import_module with mocked importlib to avoid actual file I/O."""
|
|
44
47
|
# We'll just verify it builds the correct path and calls spec_from_file_location
|
|
45
|
-
loader = ScriptLoader()
|
|
48
|
+
loader = ScriptLoader(None, None, FilesystemScriptSource)
|
|
46
49
|
# We need to mock the spec and loader
|
|
47
50
|
mock_spec = MagicMock()
|
|
48
51
|
mock_importlib.spec_from_file_location.return_value = mock_spec
|
|
49
52
|
mock_module = MagicMock()
|
|
50
53
|
mock_importlib.module_from_spec.return_value = mock_module
|
|
51
54
|
|
|
52
|
-
loader.import_module(
|
|
55
|
+
loader.script_source.import_module(tmp_path, "scripts", "my_script")
|
|
53
56
|
|
|
54
57
|
mock_importlib.spec_from_file_location.assert_called_once_with(
|
|
55
58
|
"scripts.my_script", str(tmp_path / "scripts" / "my_script.py")
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import argparse
|
|
2
|
-
|
|
3
|
-
from sigil import __version__
|
|
4
|
-
from sigil.cli_tools.init import create_project
|
|
5
|
-
from sigil.cli_tools.tree import display_project
|
|
6
|
-
from sigil.cli_tools.validate import validate_project
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def main():
|
|
10
|
-
# let's not start self hosting here
|
|
11
|
-
parser = argparse.ArgumentParser(prog='sigil')
|
|
12
|
-
parser.add_argument('--version', action='version', version=f'Sigil {__version__}')
|
|
13
|
-
sub = parser.add_subparsers(title="subcommands", dest='command')
|
|
14
|
-
|
|
15
|
-
init = sub.add_parser(name="init", help="generate a minimal sigil")
|
|
16
|
-
init.add_argument('name', help="project name to create")
|
|
17
|
-
|
|
18
|
-
validate = sub.add_parser(name="validate", help="validate structure of a sigil")
|
|
19
|
-
validate.add_argument('path', help="project to validate")
|
|
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
|
-
|
|
27
|
-
args = parser.parse_args()
|
|
28
|
-
|
|
29
|
-
if args.command == 'init':
|
|
30
|
-
create_project(args.name)
|
|
31
|
-
return
|
|
32
|
-
if args.command == 'validate':
|
|
33
|
-
validate_project(args.path)
|
|
34
|
-
return
|
|
35
|
-
if args.command == 'tree':
|
|
36
|
-
display_project(args.path)
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
if __name__ == "__main__":
|
|
40
|
-
main()
|
|
File without changes
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
import sys
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from typing import Any
|
|
5
|
-
|
|
6
|
-
from .datasource import DataSource, YmlSource
|
|
7
|
-
from .stages import Builder, Parser, Resolver, ScriptLoader
|
|
8
|
-
|
|
9
|
-
_logger = logging.getLogger(__name__)
|
|
10
|
-
|
|
11
|
-
# default to yamlloader, use whatever datastore you feel like
|
|
12
|
-
def run_from_config(
|
|
13
|
-
config_root: str | Path,
|
|
14
|
-
datasource: DataSource | type[DataSource] = YmlSource
|
|
15
|
-
) -> None:
|
|
16
|
-
raw_data = Parser(datasource).load(config_root)
|
|
17
|
-
resolved = Resolver.resolve_inheritance(raw_data)
|
|
18
|
-
# parser beyond here is an argparser
|
|
19
|
-
parser = Builder.build(resolved)
|
|
20
|
-
|
|
21
|
-
execution_context = {}
|
|
22
|
-
|
|
23
|
-
if resolved.known_args:
|
|
24
|
-
args, other_args = parser.parse_known_args()
|
|
25
|
-
execution_context: dict[str, Any] = {'other_args': other_args}
|
|
26
|
-
else:
|
|
27
|
-
args = parser.parse_args()
|
|
28
|
-
|
|
29
|
-
scripts = ScriptLoader.get_scripts(args, 'root', parser, {'root': resolved})
|
|
30
|
-
for script in scripts:
|
|
31
|
-
try:
|
|
32
|
-
module = ScriptLoader.import_module(
|
|
33
|
-
config_root,
|
|
34
|
-
resolved.script_dir,
|
|
35
|
-
script
|
|
36
|
-
)
|
|
37
|
-
if not module:
|
|
38
|
-
continue
|
|
39
|
-
except FileNotFoundError as e:
|
|
40
|
-
# prevent your subcommand from turning your environment
|
|
41
|
-
# into undefined soup by not continuing execution
|
|
42
|
-
_logger.critical(f'failed to load script "{script}"\n {e}')
|
|
43
|
-
sys.exit(2)
|
|
44
|
-
module.run(args, execution_context)
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|