|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import sys |
| 6 | +from typing import IO |
| 7 | + |
| 8 | +from dargs._version import __version__ |
| 9 | +from dargs.check import check |
| 10 | + |
| 11 | + |
| 12 | +def main_parser() -> argparse.ArgumentParser: |
| 13 | + """Create the main parser for the command line interface. |
| 14 | +
|
| 15 | + Returns |
| 16 | + ------- |
| 17 | + argparse.ArgumentParser |
| 18 | + The main parser |
| 19 | + """ |
| 20 | + parser = argparse.ArgumentParser( |
| 21 | + description="dargs: Argument checking for Python programs" |
| 22 | + ) |
| 23 | + subparsers = parser.add_subparsers(help="Sub-commands") |
| 24 | + parser_check = subparsers.add_parser( |
| 25 | + "check", |
| 26 | + help="Check a JSON file against an Argument", |
| 27 | + epilog="Example: dargs check -f dargs._test.test_arguments test_arguments.json", |
| 28 | + ) |
| 29 | + parser_check.add_argument( |
| 30 | + "-f", |
| 31 | + "--func", |
| 32 | + type=str, |
| 33 | + help="Function that returns an Argument object. E.g., `dargs._test.test_arguments`", |
| 34 | + required=True, |
| 35 | + ) |
| 36 | + parser_check.add_argument( |
| 37 | + "jdata", |
| 38 | + type=argparse.FileType("r"), |
| 39 | + default=[sys.stdin], |
| 40 | + nargs="*", |
| 41 | + help="Path to the JSON file. If not given, read from stdin.", |
| 42 | + ) |
| 43 | + parser_check.add_argument( |
| 44 | + "--no-strict", |
| 45 | + action="store_false", |
| 46 | + dest="strict", |
| 47 | + help="Do not raise an error if the key is not pre-defined", |
| 48 | + ) |
| 49 | + parser_check.add_argument( |
| 50 | + "--trim-pattern", |
| 51 | + type=str, |
| 52 | + default="_*", |
| 53 | + help="Pattern to trim the key", |
| 54 | + ) |
| 55 | + parser_check.set_defaults(entrypoint=check_cli) |
| 56 | + |
| 57 | + # --version |
| 58 | + parser.add_argument("--version", action="version", version=__version__) |
| 59 | + return parser |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + """Main entry point for the command line interface.""" |
| 64 | + parser = main_parser() |
| 65 | + args = parser.parse_args() |
| 66 | + |
| 67 | + args.entrypoint(**vars(args)) |
| 68 | + |
| 69 | + |
| 70 | +def check_cli( |
| 71 | + *, |
| 72 | + func: str, |
| 73 | + jdata: list[IO], |
| 74 | + strict: bool, |
| 75 | + **kwargs, |
| 76 | +) -> None: |
| 77 | + """Normalize and check input data. |
| 78 | +
|
| 79 | + Parameters |
| 80 | + ---------- |
| 81 | + func : str |
| 82 | + Function that returns an Argument object. E.g., `dargs._test.test_arguments` |
| 83 | + jdata : IO |
| 84 | + File object that contains the JSON data |
| 85 | + strict : bool |
| 86 | + If True, raise an error if the key is not pre-defined |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + dict |
| 91 | + normalized data |
| 92 | + """ |
| 93 | + module_name, attr_name = func.rsplit(".", 1) |
| 94 | + try: |
| 95 | + mod = __import__(module_name, globals(), locals(), [attr_name]) |
| 96 | + except ImportError as e: |
| 97 | + raise RuntimeError( |
| 98 | + f'Failed to import "{attr_name}" from "{module_name}".\n{sys.exc_info()[1]}' |
| 99 | + ) from e |
| 100 | + |
| 101 | + if not hasattr(mod, attr_name): |
| 102 | + raise RuntimeError(f'Module "{module_name}" has no attribute "{attr_name}"') |
| 103 | + func_obj = getattr(mod, attr_name) |
| 104 | + arginfo = func_obj() |
| 105 | + for jj in jdata: |
| 106 | + data = json.load(jj) |
| 107 | + check(arginfo, data, strict=strict) |
0 commit comments