Skip to content

Commit 279fbb4

Browse files
authored
Nicer command line (#4459)
1 parent 1706fb8 commit 279fbb4

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

esmvaltool/cmorizers/data/cmorizer.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
from esmvalcore.config import CFG
2323
from esmvalcore.config._dask import get_distributed_client
2424
from esmvalcore.config._logging import configure_logging
25+
from rich.console import Console
26+
from rich.markdown import Markdown
27+
from rich.table import Table
2528

2629
from esmvaltool.cmorizers.data.utilities import read_cmor_config
2730

@@ -624,26 +627,41 @@ class DataCommand:
624627
def __init__(self) -> None:
625628
self._info = yaml.safe_load(DATASETS_FILE.read_text(encoding="utf8"))
626629
self.formatter = _Formatter(self._info)
630+
self._console = Console(soft_wrap=True)
627631

628632
def _has_downloader(self, dataset: str) -> Literal["Yes", "No"]:
629633
return "Yes" if self.formatter.has_downloader(dataset) else "No"
630634

631635
def list(self) -> None:
632636
"""List all supported datasets."""
633-
print()
634-
print(f"| {'Dataset name':30} | Tier | Auto-download | Last access |")
635-
print("-" * 71)
637+
table = Table(title="Datasets that can be CMORized with ESMValTool")
638+
table.add_column("Dataset")
639+
table.add_column("Tier")
640+
table.add_column("Auto-download")
641+
table.add_column("Last access")
642+
636643
for dataset, dataset_info in self._info["datasets"].items():
637644
date = datetime.datetime.strptime(
638645
str(dataset_info["last_access"]),
639646
"%Y-%m-%d",
640647
)
641-
print(
642-
f"| {dataset:30} | {dataset_info['tier']:4} "
643-
f"| {self._has_downloader(dataset):13} "
644-
f"| {date.strftime('%Y-%m-%d')} |",
648+
age = datetime.datetime.now(datetime.UTC).year - date.year
649+
shade = max(100, 255 - age * 15)
650+
color = f"rgb({shade},{shade},{shade})"
651+
tier = dataset_info["tier"]
652+
unrestricted_tier = 2
653+
has_downloader = self._has_downloader(dataset)
654+
table.add_row(
655+
dataset,
656+
f"[green]{tier}[/green]"
657+
if tier == unrestricted_tier
658+
else f"[orange3]{tier}[/orange3]",
659+
f"[green]{has_downloader}[/green]"
660+
if has_downloader == "Yes"
661+
else f"[red]{has_downloader}[/red]",
662+
f"[{color}]{date.strftime('%Y-%m-%d')}[/{color}]",
645663
)
646-
print("-" * 71)
664+
self._console.print(table)
647665

648666
def info(self, dataset: str) -> None:
649667
"""Show detailed info about a specific dataset.
@@ -654,13 +672,14 @@ def info(self, dataset: str) -> None:
654672
dataset to show
655673
"""
656674
dataset_info = self._info["datasets"][dataset]
657-
print(dataset)
658-
print()
659-
print(f"Tier: {dataset_info['tier']}")
660-
print(f"Source: {dataset_info['source']}")
661-
print(f"Automatic download: {self._has_downloader(dataset)}")
662-
print()
663-
print(dataset_info["info"])
675+
lines = [f"# {dataset}"]
676+
lines.append(f"- Dataset: `{dataset}`")
677+
lines.append(f"- Tier: {dataset_info['tier']}")
678+
lines.append(f"- Source: {dataset_info['source']}")
679+
lines.append(f"- Automatic download: {self._has_downloader(dataset)}")
680+
lines.append("")
681+
lines.append(dataset_info["info"])
682+
self._console.print(Markdown("\n".join(lines)))
664683

665684
def download(
666685
self,

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ dependencies = [
6464
"pyyaml",
6565
"rasterio>=1.3.10",
6666
"requests",
67+
"rich",
6768
"scikit-image",
6869
"scikit-learn>=1.4.0", # github.com/ESMValGroup/ESMValTool/issues/3504
6970
"scipy",
@@ -250,6 +251,7 @@ convention = "numpy"
250251
"pyyaml" = "*"
251252
"rasterio" = ">=1.3.10"
252253
"requests" = "*"
254+
"rich" = "*"
253255
"scikit-image" = "*"
254256
"scikit-learn" = ">=1.4.0" # github.com/ESMValGroup/ESMValTool/issues/3504
255257
"scipy" = "*"

0 commit comments

Comments
 (0)