Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change For Dynamic Table Output #79

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 42 additions & 19 deletions imap_data_access/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,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 @@ -62,31 +60,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])
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved


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

Expand Down
Loading