Skip to content

Commit

Permalink
Exp: separate value formatting from output type
Browse files Browse the repository at this point in the history
  • Loading branch information
asmacdo committed Jan 21, 2025
1 parent eab6762 commit 39e0e0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/con_duct/suite/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import yaml
from con_duct.__main__ import DUCT_OUTPUT_PREFIX, SummaryFormatter

LS_SUMMARY_FORMAT = {
"prefix": "Prefix: {value}",
"command": "\tCommand: {value}",
"exit_code": "\tExit Code {value!E}",
"wall_clock_time": "\tWall Clock Time: {value:.3f} sec",
"peak_rss": "\tMemory Peak Usage (RSS): {value!S}",
VALUE_TRANSFORMATION_MAP = {
"exit_code": "{value!E}",
"wall_clock_time": "{value:.3f} sec",
"peak_rss": "{value!S}",
}


Expand All @@ -24,6 +22,18 @@ def load_duct_runs(info_files: List[str]) -> List[dict]:
return loaded

Check warning on line 22 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L20-L22

Added lines #L20 - L22 were not covered by tests


def process_run_data(
run_data_list: List[str], fields: List[str], formatter
) -> List[OrderedDict]:
output_rows = []

Check warning on line 28 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L28

Added line #L28 was not covered by tests
for row in run_data_list:
flattened = _flatten_dict(row)
restricted = _restrict_row(fields, flattened)
formatted = _format_row(restricted, formatter)
output_rows.append(formatted)
return output_rows

Check warning on line 34 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L30-L34

Added lines #L30 - L34 were not covered by tests


def _flatten_dict(d, parent_key="", sep="."):
"""Flatten a nested dictionary, creating keys as dot-separated paths."""
items = []

Check warning on line 39 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L39

Added line #L39 was not covered by tests
Expand All @@ -45,21 +55,20 @@ def _restrict_row(field_list, row):
return restricted

Check warning on line 55 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L54-L55

Added lines #L54 - L55 were not covered by tests


def _format_row(row, formatter):
transformed = OrderedDict()

Check warning on line 59 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L59

Added line #L59 was not covered by tests
for col, value in row.items():
if transformation := VALUE_TRANSFORMATION_MAP.get(col):
value = formatter.format(transformation, value=value)
transformed[col] = value
return transformed

Check warning on line 64 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L62-L64

Added lines #L62 - L64 were not covered by tests


def pyout_ls(run_data_list):
# Generate Tabular table to output
table = pyout.Tabular(

Check warning on line 69 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L69

Added line #L69 was not covered by tests
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
}
)
),
),
)
for row in run_data_list:
Expand All @@ -71,17 +80,16 @@ def pyout_ls(run_data_list):
def ls(args: argparse.Namespace) -> int:
pattern = f"{DUCT_OUTPUT_PREFIX[:DUCT_OUTPUT_PREFIX.index('{')]}*_info.json"
info_files = glob.glob(pattern)
run_data_list = load_duct_runs(info_files)
output_rows = []
for row in run_data_list:
flattened = _flatten_dict(row)
output_rows.append(_restrict_row(args.fields, flattened))
run_data_raw = load_duct_runs(info_files)
formatter = SummaryFormatter(enable_colors=args.colors)
output_rows = process_run_data(run_data_raw, args.fields, formatter)

Check warning on line 85 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L81-L85

Added lines #L81 - L85 were not covered by tests

if args.format == "summaries":
formatter = SummaryFormatter() # TODO enable_colors=self.colors)
for row in output_rows:
for col, value in row.items():
print(formatter.format(LS_SUMMARY_FORMAT[col], value=value))
if not col == "prefix":
col = f"\t{col}"
print(f"{col.replace('_', ' ').title()}: {value}")

Check warning on line 92 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L91-L92

Added lines #L91 - L92 were not covered by tests
elif args.format == "pyout":
pyout_ls(output_rows)

Check warning on line 94 in src/con_duct/suite/ls.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/suite/ls.py#L94

Added line #L94 was not covered by tests
elif args.format == "json":
Expand Down
7 changes: 7 additions & 0 deletions src/con_duct/suite/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import os
import sys
from typing import List, Optional
from con_duct.suite.ls import ls
Expand Down Expand Up @@ -69,6 +70,12 @@ def main(argv: Optional[List[str]] = None) -> None:
"execution_summary.peak_rss",
],
)
parser_ls.add_argument(
"--colors",
action="store_true",
default=os.getenv("DUCT_COLORS", False),
help="Use colors in duct output.",
)
parser_ls.set_defaults(func=ls)

args = parser.parse_args(argv)
Expand Down

0 comments on commit 39e0e0f

Please sign in to comment.