Skip to content

Commit

Permalink
feat: add dev command 'write_value'
Browse files Browse the repository at this point in the history
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.
makkus committed Mar 2, 2024
1 parent 4c1a809 commit 868ff98
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -75,3 +75,4 @@ store.sqlite
**.kiarchive
**.kontext
dev/
temp/
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

51 changes: 50 additions & 1 deletion src/kiara/interfaces/cli/data/commands.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 868ff98

Please sign in to comment.