From 868ff98baa445832fabdbdd46560aa3a30f1835a Mon Sep 17 00:00:00 2001 From: Markus Binsteiner Date: Sat, 2 Mar 2024 13:36:24 +0100 Subject: [PATCH] feat: add dev command 'write_value' This adds a 'write_value' command to the 'data' subcommand, but it only is available if the DEV environment command is set to 'true'. This command is only useful for debugging purposes, and it writes the serialized form of a value to the filesystem. --- .gitignore | 1 + CHANGELOG.md | 1 + src/kiara/interfaces/cli/data/commands.py | 51 ++++++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 301474d48..364e981e2 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,4 @@ store.sqlite **.kiarchive **.kontext dev/ +temp/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 76a7af976..1c69650e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - improved input options for 'store_values' API endpoint - 'beta' implementation of 'value_create' property on 'Value' instances - fix: plugin info for plugins with '-' +- add '--runtime-info' cli flag ## Version 0.5.9 diff --git a/src/kiara/interfaces/cli/data/commands.py b/src/kiara/interfaces/cli/data/commands.py index 4a0e572a0..b6f7e301d 100644 --- a/src/kiara/interfaces/cli/data/commands.py +++ b/src/kiara/interfaces/cli/data/commands.py @@ -16,7 +16,7 @@ import structlog from kiara.exceptions import InvalidCommandLineInvocation -from kiara.utils import log_exception, log_message +from kiara.utils import is_develop, log_exception, log_message from kiara.utils.cli import output_format_option, terminal_print, terminal_print_model from kiara.utils.cli.exceptions import handle_exception @@ -677,6 +677,7 @@ def export_data_archive( @click.pass_context @handle_exception() def import_data_store(ctx, archive: str, values: Tuple[str], no_aliases: bool = False): + """Import one or several values from a kiara archive.""" kiara_api: KiaraAPI = ctx.obj.kiara_api @@ -692,3 +693,51 @@ def import_data_store(ctx, archive: str, values: Tuple[str], no_aliases: bool = terminal_print(result) terminal_print("Done.") + + +if is_develop(): + + @data.command(name="write_value") + @click.argument("value_id_or_alias", nargs=1, required=True) + @click.option( + "--directory", + "-d", + help="The directory to write the serialized value to.", + required=False, + ) + @click.option( + "--force", "-f", help="Overwrite existing files.", is_flag=True, default=False + ) + @click.pass_context + @handle_exception() + def write_serialized(ctx, value_id_or_alias: str, directory: str, force: bool): + """Write the serialized form of a value to a directory""" + + kiara_api: KiaraAPI = ctx.obj.kiara_api + + value = kiara_api.get_value(value_id_or_alias) + serialized = value.serialized_data + + keys = serialized.get_keys() + + if not directory: + directory = "." + + path = Path(directory) + + for key in keys: + data = serialized.get_serialized_data(key) + + key_path = path / key + if key_path.exists() and not force: + terminal_print(f"Error writing file for '{key}': file already exists.") + sys.exit(1) + + key_path.parent.mkdir(parents=True, exist_ok=True) + + chunks = data.get_chunks(as_files=False) + + terminal_print(f"- writing file for: {key}") + with open(key_path, "wb") as f: + for chunk in chunks: + f.write(chunk) # type: ignore