From e64417fc025ae855e3918ba8d27426a56e6996d5 Mon Sep 17 00:00:00 2001 From: Austin Macdonald Date: Wed, 15 Jan 2025 12:40:54 -0600 Subject: [PATCH] WIP: Hack solution with pyout --- src/con_duct/suite/ls.py | 47 ++++++++++++++++++++++++++++++++++++++ src/con_duct/suite/main.py | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/con_duct/suite/ls.py b/src/con_duct/suite/ls.py index a59d08f..16d4042 100644 --- a/src/con_duct/suite/ls.py +++ b/src/con_duct/suite/ls.py @@ -2,6 +2,7 @@ import glob import json from typing import List +import pyout import yaml from con_duct.__main__ import DUCT_OUTPUT_PREFIX, SummaryFormatter @@ -23,6 +24,50 @@ def load_duct_runs(info_files: List[str]) -> List[dict]: return loaded +def _flatten_dict(d, parent_key="", sep="."): + """Flatten a nested dictionary, creating keys as dot-separated paths.""" + items = [] + for k, v in d.items(): + new_key = f"{parent_key}{sep}{k}" if parent_key else k + if isinstance(v, dict): + items.extend(_flatten_dict(v, new_key, sep=sep).items()) + else: + items.append((new_key, v)) + return dict(items) + + +def _restrict_row(include_only, row): + restricted = {} + for k, v in row.items(): + if k in include_only: + restricted[k.split(".")[-1]] = v + return restricted + + +def pyout_ls(run_data_list): + # Generate Tabular table to output + table = pyout.Tabular( + style=dict( + header_=dict(bold=True, transform=str.upper), + # Default styling could be provided from some collection of styling files + default_=dict( + color=dict( + lookup={ + "Trix": "green", + "110": "red", + "100": "green", # since no grey for now + } + ) + ), + ), + ) + include_only = ["command", "execution_summary.exit_code"] + for row in run_data_list: + # table(row) + flattened = _flatten_dict(row) + table(_restrict_row(include_only, flattened)) + + def ls(args: argparse.Namespace) -> int: pattern = f"{DUCT_OUTPUT_PREFIX[:DUCT_OUTPUT_PREFIX.index('{')]}*_info.json" info_files = glob.glob(pattern) @@ -31,6 +76,8 @@ def ls(args: argparse.Namespace) -> int: formatter = SummaryFormatter() # TODO enable_colors=self.colors) for data in run_data_list: print(formatter.format(LS_SUMMARY_FORMAT, **data)) + elif args.format == "pyout": + pyout_ls(run_data_list) elif args.format == "json": print(json.dumps(run_data_list)) elif args.format == "json_pp": diff --git a/src/con_duct/suite/main.py b/src/con_duct/suite/main.py index 8134a8a..eb02b92 100644 --- a/src/con_duct/suite/main.py +++ b/src/con_duct/suite/main.py @@ -54,7 +54,7 @@ def main(argv: Optional[List[str]] = None) -> None: parser_ls.add_argument( "-f", "--format", - choices=("summaries", "json", "json_pp", "yaml"), + choices=("summaries", "json", "json_pp", "yaml", "pyout"), default="summaries", # TODO dry help="Output format. TODO Fixme", )