Skip to content

Commit

Permalink
WIP: Hack solution with pyout
Browse files Browse the repository at this point in the history
  • Loading branch information
asmacdo committed Jan 15, 2025
1 parent 8a51694 commit e64417f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/con_duct/suite/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion src/con_duct/suite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down

0 comments on commit e64417f

Please sign in to comment.