Source code for pygeodes.cli.cli

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""cli.py

[your module's docstring]

"""
# -----------------------------------------------------------------------------
# Copyright (c) 2024, CNES
#
# REFERENCES:
# https://cnes.fr/
# -----------------------------------------------------------------------------

# stdlib imports -------------------------------------------------------
import sys

# third-party imports -----------------------------------------------
from rich.status import Status
from rich.console import Console
from rich.syntax import Syntax


# local imports ---------------------------------------------------
from pygeodes.cli.argparser import parse_args
from pygeodes.utils.io import file_exists
from pygeodes.utils.config import Config
from pygeodes.utils.profile import Profile
from pygeodes._info import version
from pygeodes.geodes import Geodes
from pygeodes.utils.formatting import export_dataframe
from pygeodes.utils.datetime_utils import (
    complete_datetime_from_str,
)

from pygeodes.cli.cli_utils import (
    deal_with_query_and_conf,
    table_from_dataframe,
)


[docs]def watch_downloads(args): Profile.watch_downloads(refresh_rate=args.rate, simplified=args.simplified)
[docs]def download(args): geodes = Geodes(conf=args.conf) items = geodes.search_items( query={"accessService:endpointURL": {"contains": args.id}}, quiet=True, return_df=False, get_all=False, ) if len(items) > 1: print(f"This id is not precise enough, it returns multiple results") sys.exit(1) elif len(items) == 1: geodes.download_item_archive(items[0]) exit() elif len(items) == 0: print(f"This id doesn't return any results") sys.exit(1)
[docs]def cli(): args_parsed = parse_args() if args_parsed.version: print(version) exit() if args_parsed.config: if file_exists(args_parsed.config): conf = Config.from_file(args_parsed.config) else: conf = Config() args_parsed.conf = conf command = args_parsed.command functions = { func.__name__.replace("_", "-"): func for func in [watch_downloads, search, download] } func_to_call = functions.get(command) if func_to_call is None: raise Exception( f"This command doesn't exist, availables : {list(functions.keys())}" ) else: func_to_call(args_parsed)