|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from jam import common, config |
| 5 | + |
| 6 | +LOG = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +class ListArgs: |
| 10 | + """Class to handle `list` flag.""" |
| 11 | + |
| 12 | + name = "list" |
| 13 | + |
| 14 | + @staticmethod |
| 15 | + def build_args(sub_parser) -> None: |
| 16 | + """Setup arg parser for `list` flag. |
| 17 | +
|
| 18 | + Args: |
| 19 | + sub_parser (argparse._SubParsersAction): Parser to add arguments to. |
| 20 | +
|
| 21 | + """ |
| 22 | + parser = sub_parser.add_parser("list", help="List information from Jam config.") |
| 23 | + parser.add_argument("--all", action="store_true", help="List all packages in config.") |
| 24 | + parser.add_argument("--config", default=os.getenv("JAM_ENV"), help="Jam config name.") |
| 25 | + parser.add_argument("--configs", action="store_true", help="List existing Jam configs.") |
| 26 | + parser.add_argument("--package", help="Package name to query information from.") |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def execute(args) -> None: |
| 30 | + """Execute command option. |
| 31 | +
|
| 32 | + Args: |
| 33 | + args (argparse.Namespace): Parsed argument flags. |
| 34 | +
|
| 35 | + """ |
| 36 | + if args.configs: |
| 37 | + _print_configs() |
| 38 | + return |
| 39 | + |
| 40 | + if not args.config: |
| 41 | + common.no_config() |
| 42 | + return |
| 43 | + |
| 44 | + config_data = config.get_jam_config(args.config) |
| 45 | + if args.all: |
| 46 | + _print_table_of_content(config_data) |
| 47 | + elif args.package: |
| 48 | + _print_table_of_content({args.package: config_data.get(args.package, {})}) |
| 49 | + |
| 50 | + |
| 51 | +def _print_configs() -> None: |
| 52 | + """Print Existing jam config names.""" |
| 53 | + for _conf in os.listdir(common.CONFIG_ROOT): |
| 54 | + if _conf.endswith(".jam"): |
| 55 | + print(_conf.replace(".jam", "")) |
| 56 | + |
| 57 | + |
| 58 | +def _print_table_of_content(_config: dict) -> None: |
| 59 | + """Print data as table. |
| 60 | +
|
| 61 | + Args: |
| 62 | + _config (dict): Jam package or config dict to print as table. |
| 63 | +
|
| 64 | + """ |
| 65 | + columns = [] |
| 66 | + length = len(_config) + 1 # Add extra space for header. |
| 67 | + |
| 68 | + for column_key in ("name", "version", "path"): |
| 69 | + columns.append([column_key.capitalize()] + [v.get(column_key, "") for v in _config.values()]) |
| 70 | + |
| 71 | + splitter = "" |
| 72 | + for row in range(length): |
| 73 | + row_text = "|" |
| 74 | + splitter = "+" |
| 75 | + for column in columns: |
| 76 | + text_length = len(max(column, key=len)) |
| 77 | + text = column[row] |
| 78 | + row_text += f" {text}" + " " * (text_length - len(text)) + " |" |
| 79 | + splitter += "-" * (text_length + 1) + "-+" |
| 80 | + |
| 81 | + print(splitter) |
| 82 | + print(row_text) |
| 83 | + print(splitter) |
0 commit comments