Skip to content

Commit

Permalink
Upgrade to typer 0.9
Browse files Browse the repository at this point in the history
Fixes #17
Fixes #18
  • Loading branch information
pmav99 committed May 15, 2023
1 parent c439b97 commit 285f64c
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 153 deletions.
64 changes: 34 additions & 30 deletions inspectds/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

import enum
import pathlib
import typing
import warnings
from importlib.metadata import version
from pathlib import Path
from typing import Annotated
from typing import Any

import typer
import xarray as xr
Expand All @@ -24,7 +24,7 @@
"zarr",
}

class DATASET_TYPE(enum.Enum):
class DATASET_TYPE(str, enum.Enum):
AUTO = "auto"
GRIB = "grib"
NETCDF = "netcdf"
Expand All @@ -36,15 +36,12 @@ class DATASET_TYPE(enum.Enum):
"zarr",
}

class DATASET_TYPE(enum.Enum): # type: ignore[no-redef]
class DATASET_TYPE(str, enum.Enum): # type: ignore[no-redef]
AUTO = "auto"
NETCDF = "netcdf"
ZARR = "zarr"


app = typer.Typer(add_completion=False, invoke_without_command=True, add_help_option=True)


def echo_variable_attributes(ds: xr.Dataset) -> None:
lines = []
lines.append("Variable Attributes:")
Expand Down Expand Up @@ -91,7 +88,7 @@ def echo_dataset(
# 2. we want to pass extra arguments to `xr.open_dataset()`
# 3. we want to allow the user to override the inferring
# This is why we keep this function
def infer_dataset_type(path: pathlib.Path) -> DATASET_TYPE:
def infer_dataset_type(path: Path) -> DATASET_TYPE:
if path.suffix in (".grib", ".grib2"):
if IS_GRIB_AVAILABLE:
dataset_type = DATASET_TYPE.GRIB
Expand All @@ -106,31 +103,38 @@ def infer_dataset_type(path: pathlib.Path) -> DATASET_TYPE:
return dataset_type


@app.command(help=f"Print a dataset's metadata. Supports: {SUPPORTED_DATASETS}", no_args_is_help=True)
def version_callback(value: bool) -> None:
if value:
typer.echo(f"inspectds version: {version('inspectds')}")
raise typer.Exit(code=0)


app = typer.Typer(
add_completion=False,
add_help_option=True,
help=f"Print a dataset's metadata. Supports: {SUPPORTED_DATASETS}",
)


@app.command(no_args_is_help=True)
def inspect_dataset(
path: pathlib.Path = typer.Argument(
...,
dir_okay=True,
file_okay=True,
exists=True,
readable=True,
help="The path to the dataset",
),
# fmt: off
dataset_type: DATASET_TYPE = typer.Option(DATASET_TYPE.AUTO.value, help="The dataset type. If 'auto', then it gets inferred from PATH"),
mask_and_scale: bool = typer.Option(False, help="Whether to mask and scale the dataset"),
dimensions: bool = typer.Option(True, help="Whether to include 'Dimensions' in the output"),
coordinates: bool = typer.Option(True, help="Whether to include 'Coordinates' in the output"),
variables: bool = typer.Option(True, help="Whether to include 'Variables' in the output"),
variable_attributes: bool = typer.Option(False, help="Whether to include the variable attributes in the output"),
global_attributes: bool = typer.Option( False, help="Whether to include the global attributes in the output"),
full: bool = typer.Option(False, help="Display full output. Overrides any other option"),
path: Annotated[Path, typer.Argument(dir_okay=True, file_okay=True, exists=True, readable=True, help="The path to the dataset")],
dataset_type: Annotated[DATASET_TYPE, typer.Option(help="The dataset type. If 'auto', then it gets inferred from PATH")] = DATASET_TYPE.AUTO,
mask_and_scale: Annotated[bool, typer.Option(help="Whether to mask and scale the dataset")] = False,
dimensions: Annotated[bool, typer.Option(help="Whether to include 'Dimensions' in the output")] = True,
coordinates: Annotated[bool, typer.Option(help="Whether to include 'Coordinates' in the output")] = True,
variables: Annotated[bool, typer.Option(help="Whether to include 'Variables' in the output")] = True,
variable_attributes: Annotated[bool, typer.Option(help="Whether to include the variable attributes in the output")] = False,
global_attributes: Annotated[bool, typer.Option(help="Whether to include the global attributes in the output")] =False,
full: Annotated[bool, typer.Option(help="Display full output. Overrides any other option")] = False,
version: Annotated[bool, typer.Option("--version", help="Display the version", callback=version_callback, is_eager=True)] = False,
# fmt: on
) -> int:
if dataset_type is DATASET_TYPE.AUTO:
if dataset_type == DATASET_TYPE.AUTO:
dataset_type = infer_dataset_type(path)

open_dataset_kwargs: dict[str, typing.Any] = {}
open_dataset_kwargs: dict[str, Any] = {}
if IS_GRIB_AVAILABLE and dataset_type == DATASET_TYPE.GRIB:
open_dataset_kwargs.update(
dict(
Expand Down Expand Up @@ -174,7 +178,7 @@ def inspect_dataset(
warnings.warn(f"Dropping scalar variable: {to_be_dropped}", RuntimeWarning)
else:
typer.echo(f"Couldn't open {dataset_type.value} dataset: {str(exc)}")
raise typer.Exit()
raise typer.Exit(code=33)
else:
break

Expand Down
Loading

0 comments on commit 285f64c

Please sign in to comment.