Skip to content

Commit

Permalink
Change For Dynamic Table Output (#79)
Browse files Browse the repository at this point in the history
* Now using dynamic table output
  • Loading branch information
daralynnrhode authored Aug 19, 2024
1 parent 7657856 commit 82e73ce
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions imap_data_access/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ def _print_query_results_table(query_results: list[dict]):
print(f"Found [{num_files}] matching files")
if num_files == 0:
return
format_string = "| {:<10} | {:<10} | {:<15} | {:<10} | {:<10} | {:<7} | {:<50}|"
# Add hyphens for a separator between header and data
hyphens = "|" + "-" * 131 + "|"
print(hyphens)
header = [

# Use the keys of the first item in query_results for the header
headers = [
"Instrument",
"Data Level",
"Descriptor",
Expand All @@ -61,31 +59,56 @@ def _print_query_results_table(query_results: list[dict]):
"Version",
"Filename",
]
print(format_string.format(*header))

# Calculate the maximum width for each column based on the header and the data
column_widths = {}
for header in headers[:-1]:
column_widths[header] = max(
len(header),
*(len(str(item.get(header.lower(), ""))) for item in query_results),
)
# Calculate the maximum width for each column based on the header and the data

column_widths["Filename"] = max(
len("Filename"),
*(
len(os.path.basename(item.get("file_path", "")))
for item in query_results
),
)

# Create the format string dynamically based on the number of columns
format_string = (
"| "
+ " | ".join([f"{{:<{column_widths[header]}}}" for header in headers])
+ " |"
)

# Add hyphens for a separator between header and data
hyphens = "|" + "-" * (sum(column_widths.values()) + 3 * len(headers) - 1) + "|"
print(hyphens)

# Print header
print(format_string.format(*headers))
print(hyphens)

# Print data
for item in query_results:
values = [
item["instrument"],
item["data_level"],
item["descriptor"],
item["start_date"],
# Repointing is optional, so use an empty string if not present
item["repointing"] or "",
item["version"],
os.path.basename(item["file_path"]),
str(item.get("instrument", "")),
str(item.get("data_level", "")),
str(item.get("descriptor", "")),
str(item.get("start_date", "")),
str(item.get("repointing", "")) or "",
str(item.get("version", "")),
os.path.basename(item.get("file_path", "")),
]
print(format_string.format(*values))

# Close the table
print(hyphens)


def extract_version_number(str_version: str) -> int:
"""Take a version string 'vXXX' and return and int 'XXX'."""
return int(str_version[1:4])


def _query_parser(args: argparse.Namespace):
"""Query the IMAP SDC.
Expand Down

0 comments on commit 82e73ce

Please sign in to comment.