Skip to content

Commit

Permalink
feat: add '--runtime-info' cli flag to display cache dir etc
Browse files Browse the repository at this point in the history
  • Loading branch information
makkus committed Mar 2, 2024
1 parent 549b6ad commit 4c1a809
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 41 deletions.
7 changes: 6 additions & 1 deletion src/kiara/interfaces/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
from kiara.interfaces import KiaraAPIWrap
from kiara.utils import is_debug, log_message
from kiara.utils.class_loading import find_all_cli_subcommands
from kiara.utils.cli import kiara_version_option, terminal_print
from kiara.utils.cli import (
kiara_runtime_info_option,
kiara_version_option,
terminal_print,
)

click.rich_click.USE_RICH_MARKUP = True
# TODO: rich_click backport this
Expand Down Expand Up @@ -75,6 +79,7 @@
default=False,
)
@kiara_version_option()
@kiara_runtime_info_option()
@click.pass_context
def cli(
ctx,
Expand Down
95 changes: 56 additions & 39 deletions src/kiara/interfaces/cli/context/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def list_contexts(ctx) -> None:
@click.option(
"--show-config", "-c", help="Also show kiara config.", is_flag=True, default=False
)
@click.option("--show-runtime-info", "-r", help="Also show runtime info.", is_flag=True)
@output_format_option()
@click.pass_context
def explain_context(
Expand Down Expand Up @@ -153,7 +154,7 @@ def delete_context(
)
terminal_print_model(summaries, in_panel="All contexts:")
user_input = get_console().input(
f"Deleting all contexts, are you sure? \[yes/no]: " # noqa
r"Deleting all contexts, are you sure? \[yes/no]: "
)

if user_input.lower() == "yes":
Expand Down Expand Up @@ -191,8 +192,11 @@ def delete_context(
in_panel=f"Context details: {_context_name}",
)
terminal_print()
txt_pre = r"Deleting context '[b i]"
txt_post = r"[/b i]', are you sure? \[yes/no]: "
txt_all = f"{txt_pre}{_context_name}{txt_post}"
user_input = get_console().input(
f"Deleting context '[b i]{_context_name}[/b i]', are you sure? \[yes/no]: "
txt_all,
)

if user_input.lower() == "yes":
Expand Down Expand Up @@ -248,17 +252,17 @@ def config_help(ctx):
terminal_print(Panel(table))


@info.group(name="runtime")
@info.group(name="environment")
@click.pass_context
def runtime(ctx):
"""Information about the current runtime (Python environment, available metadata models, etc)."""
def env_group(ctx):
"""Information about whats in the current environment details (Python virtual environment, available metadata models, etc)."""


@runtime.command("print")
@env_group.command("print")
@output_format_option()
@click.pass_context
def print_context(ctx, format: str):
"""Print all relevant models within the current runtime environment."""
"""Print all relevant models within the current (Python virtual) environment."""
kiara_obj: Kiara = ctx.obj.kiara

terminal_print_model(
Expand All @@ -268,10 +272,23 @@ def print_context(ctx, format: str):
)


@runtime.group(name="environment")
@click.pass_context
def env_group(ctx):
"""Environment-related sub-commands."""
# @info.group(name="runtime")
# @click.pass_context
# def runtime_group(ctx):
# """Runtime-related sub-commands."""
#
# @runtime_group.command("print")
# @output_format_option()
# @click.pass_context
# def print_runtime(ctx, format: str):
# """Print runtime information."""
# kiara_obj: Kiara = ctx.obj.kiara
#
# terminal_print_model(
# kiara_obj.runtime_config,
# format=format,
# in_panel=f"Runtime info for kiara id: {kiara_obj.id}",
# )


@env_group.command("list")
Expand Down Expand Up @@ -311,7 +328,7 @@ def explain_env(ctx, env_type: str, format: str) -> None:
)


@runtime.group()
@env_group.group()
@click.pass_context
def metadata(ctx):
"""Metadata-related sub-commands."""
Expand Down Expand Up @@ -363,33 +380,33 @@ def explain_metadata(ctx, metadata_key, format) -> None:
)


@runtime.group()
@click.pass_context
def api(ctx):
"""API-related sub-commands."""


@api.command()
@click.argument("filter", nargs=-1, required=False)
@click.option(
"--full-doc",
"-d",
is_flag=True,
help="Display the full doc for all operations (when using 'terminal' as format).",
)
@click.pass_context
def list_endpoints(ctx, filter, full_doc):
"""List all available API endpoints."""
from kiara.interfaces.python_api import KiaraAPI
from kiara.interfaces.python_api.proxy import ApiEndpoints

exclude = ["get_runtime_config", "retrieve_workflow_info"]
endpoints = ApiEndpoints(api_cls=KiaraAPI, filters=filter, exclude=exclude)

terminal_print()
terminal_print(endpoints, in_panel="API endpoints", full_doc=full_doc)


# @env_group.group()
# @click.pass_context
# def api(ctx):
# """API-related sub-commands."""
#
#
# @api.command()
# @click.argument("filter", nargs=-1, required=False)
# @click.option(
# "--full-doc",
# "-d",
# is_flag=True,
# help="Display the full doc for all operations (when using 'terminal' as format).",
# )
# @click.pass_context
# def list_endpoints(ctx, filter, full_doc):
# """List all available API endpoints."""
# from kiara.interfaces.python_api import KiaraAPI
# from kiara.interfaces.python_api.proxy import ApiEndpoints
#
# exclude = ["get_runtime_config", "retrieve_workflow_info"]
# endpoints = ApiEndpoints(api_cls=KiaraAPI, filters=filter, exclude=exclude)
#
# terminal_print()
# terminal_print(endpoints, in_panel="API endpoints", full_doc=full_doc)
#
#
@context.group("service")
@click.pass_context
def service(ctx):
Expand Down
46 changes: 45 additions & 1 deletion src/kiara/utils/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from rich.rule import Rule
from rich.syntax import Syntax

from kiara.defaults import KIARA_MAIN_CONFIG_FILE, kiara_app_dirs

# ======================================================================================================================
# click helper methods
from kiara.utils import logger
Expand Down Expand Up @@ -391,7 +393,8 @@ def callback(ctx: Context, param: Parameter, value: bool) -> None:

table.add_row("", "")
table.add_row("python", python_env.python_version)
terminal_print(table)
terminal_print()
terminal_print(table, in_panel="Version information")

ctx.exit()

Expand All @@ -406,3 +409,44 @@ def callback(ctx: Context, param: Parameter, value: bool) -> None:
kwargs["callback"] = callback
result: Callable[[FC], FC] = option(*param_decls, **kwargs)
return result


def kiara_runtime_info_option(
**kwargs: Any,
) -> Callable[[FC], FC]:
"""Add a ``--runtime-info`` option which immediately prints information about the current application environment."""

def callback(ctx: Context, param: Parameter, value: bool) -> None:

from rich.table import Table

if not value or ctx.resilient_parsing:
return

table = Table(show_header=False, box=box.SIMPLE)
table.add_column("key")
table.add_column("value", style="i")
config_file = KIARA_MAIN_CONFIG_FILE
table.add_row("config file", config_file)
table.add_row("", "")
data_path = kiara_app_dirs.user_data_dir
table.add_row("data path", data_path)
table.add_row("", "")
cache_path = kiara_app_dirs.user_cache_dir
table.add_row("cache path", cache_path)
table.add_row("", "")

terminal_print()
terminal_print(table, in_panel="Application runtime information")

ctx.exit()

param_decls = ("--runtime-info", "-ri")

kwargs.setdefault("is_flag", True)
kwargs.setdefault("expose_value", False)
kwargs.setdefault("is_eager", True)
kwargs.setdefault("help", ("Show current environment information, then exit."))
kwargs["callback"] = callback
result: Callable[[FC], FC] = option(*param_decls, **kwargs)
return result

0 comments on commit 4c1a809

Please sign in to comment.