-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.py
61 lines (50 loc) · 2.03 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import argparse
import functools
import importlib
import logging
import sys
from types import ModuleType
import warnings
from STACpopulator import __version__, implementations
from STACpopulator.exceptions import STACPopulatorError
from STACpopulator.log import setup_logging
def add_parser_args(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--version",
"-V",
action="version",
version=f"%(prog)s {__version__}",
help="prints the version of the library and exits",
)
commands_subparser = parser.add_subparsers(
title="command", dest="command", description="STAC populator command to execute.", required=True
)
run_parser = commands_subparser.add_parser("run", description="Run a STACpopulator implementation")
populators_subparser = run_parser.add_subparsers(
title="populator", dest="populator", description="Implementation to run."
)
for implementation_module_name, module in implementation_modules().items():
implementation_parser = populators_subparser.add_parser(implementation_module_name)
module.add_parser_args(implementation_parser)
@functools.cache
def implementation_modules() -> dict[str, ModuleType]:
modules = {}
for implementation_module_name in implementations.__all__:
try:
modules[implementation_module_name] = importlib.import_module(
f".{implementation_module_name}", implementations.__package__
)
except STACPopulatorError as e:
warnings.warn(f"Could not load extension {implementation_module_name} because of error {e}")
return modules
def run(ns: argparse.Namespace) -> int:
if ns.command == "run":
setup_logging(ns.log_file, ns.debug or logging.INFO)
return implementation_modules()[ns.populator].runner(ns) or 0
def main(*args: str) -> int:
parser = argparse.ArgumentParser()
add_parser_args(parser)
ns = parser.parse_args(args or None)
return run(ns)
if __name__ == "__main__":
sys.exit(main())