Skip to content

Commit

Permalink
chore: start work on alias export
Browse files Browse the repository at this point in the history
  • Loading branch information
makkus committed Feb 2, 2024
1 parent fcc8d50 commit b4ac06b
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 79 deletions.
11 changes: 6 additions & 5 deletions src/kiara/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def register_external_archive(
)

result = {}
for _archive_inst in archive_instances:
for archive_type, _archive_inst in archive_instances.items():
log_message(
"register.external.archive",
archive=_archive_inst.archive_alias,
Expand All @@ -343,13 +343,14 @@ def register_external_archive(

_archive_inst.set_force_read_only(not allow_write_access)

supported_item_types = _archive_inst.supported_item_types()
if "data" in supported_item_types:
if archive_type == "data":
result["data"] = self.data_registry.register_data_archive(_archive_inst) # type: ignore
if "alias" in supported_item_types:
if archive_type == "alias":
result["alias"] = self.alias_registry.register_archive(_archive_inst) # type: ignore
if "job_record" in supported_item_types:
if archive_type == "job_record":
result["job_record"] = self.job_registry.register_job_archive(_archive_inst) # type: ignore
else:
raise Exception(f"Can't register archive of type '{archive_type}'.")

return result

Expand Down
22 changes: 11 additions & 11 deletions src/kiara/context/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def load_existing_archive(
archive_configs: List[KiaraArchiveConfig] = []
archives: List[KiaraArchive] = []

archive_alias = archive_uri
archive_alias = None

if store_type:
if isinstance(store_type, str):
Expand All @@ -111,9 +111,7 @@ def load_existing_archive(
**kwargs,
)
archive_config = archive_cls._config_cls(**data)
archive: KiaraArchive = archive_cls(
archive_alias=archive_alias, archive_config=archive_config
)
archive: KiaraArchive = archive_cls(archive_config=archive_config)
wrapped_archive_config = KiaraArchiveConfig(
archive_type=store_type, config=data
)
Expand All @@ -133,7 +131,7 @@ def load_existing_archive(
)
archive_config = archive_cls._config_cls(**data)
archive: KiaraArchive = archive_cls(
archive_alias=archive_alias, archive_config=archive_config
archive_config=archive_config, archive_alias=archive_alias
)
wrapped_archive_config = KiaraArchiveConfig(
archive_type=st, config=data
Expand All @@ -152,7 +150,7 @@ def load_existing_archive(
continue
archive_config = archive_cls._config_cls(**data)
archive: KiaraArchive = archive_cls(
archive_alias=archive_alias, archive_config=archive_config
archive_config=archive_config, archive_alias=archive_alias
)
wrapped_archive_config = KiaraArchiveConfig(
archive_type=archive_type, config=data
Expand All @@ -167,17 +165,17 @@ def load_existing_archive(

result = cls(
archive_uri=archive_uri,
archive_alias=archive_alias,
allow_write_access=allow_write_access,
archive_configs=archive_configs,
# archive_alias=archive_alias,
)
result._archives = archives
return result

archive_uri: str = Field(description="The uri that points to the archive.")
archive_alias: str = Field(
description="The alias that is used for the archives contained in here."
)
# archive_alias: str = Field(
# description="The alias that is used for the archives contained in here."
# )
allow_write_access: bool = Field(
description="Whether to allow write access to the archives contained here.",
default=False,
Expand All @@ -197,6 +195,8 @@ def archives(self) -> List["KiaraArchive"]:

archive_types = find_all_archive_types()

archive_alias = None

result = []
for config in self.archive_configs:
if config.archive_type not in archive_types.keys():
Expand All @@ -211,7 +211,7 @@ def archives(self) -> List["KiaraArchive"]:
)
archive_config = archive_cls._config_cls(**archive_config_data)
archive = archive_cls(
archive_alias=self.archive_alias, archive_config=archive_config
archive_config=archive_config, archive_alias=archive_alias
)
result.append(archive)

Expand Down
158 changes: 147 additions & 11 deletions src/kiara/interfaces/cli/data/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
# Mozilla Public License, version 2.0 (see LICENSE or https://www.mozilla.org/en-US/MPL/2.0/)

"""Data-related sub-commands for the cli."""
import os.path
import sys
from typing import TYPE_CHECKING, Iterable, Tuple
import uuid
from pathlib import Path
from typing import TYPE_CHECKING, Iterable, Tuple, Union

import rich_click as click
import structlog
Expand Down Expand Up @@ -509,33 +512,166 @@ def filter_value(


@data.command(name="export")
@click.argument("alias", nargs=1, required=True)
@click.option(
"--archive-alias",
"-a",
help="The alias to use for the exported archive. If not provided, the first alias will be used.",
required=False,
)
@click.option(
"--path",
"-p",
help="The path of the exported archive. If not provided, '<archive_alias>.karchive' will be used.",
required=False,
)
@click.option(
"--compression",
"-c",
help="The compression inside the archive. If not provided, 'zstd' will be used.",
type=click.Choice(["zstd", "lz4", "lzma", "none"]),
default="zstd",
)
@click.option(
"--force", "-f", help="Force overwriting an existing archive.", is_flag=True
)
@click.argument("aliases", nargs=-1, required=True)
@click.pass_context
def export_data_store(ctx, alias: str):
def export_data_store(
ctx,
aliases: str,
archive_alias: Union[None, str],
path: Union[str, None],
compression: str,
force: bool,
):
"""Export one or several values into a new data store."""

from kiara.utils.stores import create_new_store
from kiara.utils.stores import create_new_archive

kiara_api: KiaraAPI = ctx.obj.kiara_api

value = kiara_api.get_value(alias)
base_path = "."
values = []
for idx, alias in enumerate(aliases, start=1):
if "=" in alias:
old_alias, new_alias = alias.split("=", maxsplit=1)
else:
try:
uuid.UUID(alias)
old_alias = alias
new_alias = None
except Exception:
old_alias = alias
new_alias = alias

value = kiara_api.get_value(old_alias)
values.append((value, new_alias))

if not archive_alias:
archive_alias = values[0][1]

if not archive_alias:
archive_alias = str(values[0][0].value_id)

if not path:
base_path = "."
file_name = f"{archive_alias}.kiarchive"
terminal_print(f"Creating new store '{file_name}'...")
else:
base_path = os.path.dirname(path)
file_name = os.path.basename(path)
if "." not in file_name:
file_name = f"{file_name}.kiarchive"

terminal_print(f"Creating new store '{path}'...")

full_path = Path(base_path) / file_name
if full_path.is_file() and force:
full_path.unlink()

store = create_new_store(
archive_alias=f"export_store_{alias}",
if full_path.exists():
terminal_print(f"[red]Error[/red]: File '{full_path}' already exists.")
sys.exit(1)

store: DataArchive = create_new_archive( # type: ignore

Check failure on line 595 in src/kiara/interfaces/cli/data/commands.py

View workflow job for this annotation

GitHub Actions / linting-linux

Ruff (F821)

src/kiara/interfaces/cli/data/commands.py:595:12: F821 Undefined name `DataArchive`
archive_alias=archive_alias,
store_base_path=base_path,
store_type="sqlite_data_store",
file_name=f"{alias}.sqlite",
file_name=file_name,
default_chunk_compression=compression,
allow_write_access=True,
)

terminal_print("Registering store...")
store_alias = kiara_api.context.data_registry.register_data_archive(store)

terminal_print("Exporting value into new store...")

no_default_value = False

if not no_default_value:
try:
store.set_archive_metadata_value(
"default_value", str(values[0][0].value_id)
)
except Exception as e:
store.delete_archive(archive_id=store.archive_id)
log_exception(e)
terminal_print(f"[red]Error setting value[/red]: {e}")
sys.exit(1)

values_to_store = {}
alias_map = {}
for idx, (value, value_alias) in enumerate(values, start=1):
key = f"value_{idx}"
values_to_store[key] = value
if value_alias:
alias_map[key] = value_alias

alias_store_alias = None
try:
persisted_data = kiara_api.context.data_registry.store_value(
value, store_id=store_alias

persisted_data = kiara_api.store_values(
values=values_to_store,
alias_map=alias_map,
data_store_id=store_alias,
alias_store_id=alias_store_alias,
)

dbg(persisted_data)

Check failure on line 640 in src/kiara/interfaces/cli/data/commands.py

View workflow job for this annotation

GitHub Actions / linting-linux

Ruff (F821)

src/kiara/interfaces/cli/data/commands.py:640:9: F821 Undefined name `dbg`
terminal_print("Done.")

except Exception as e:
store.delete_archive(archive_id=store.archive_id)
log_exception(e)
terminal_print(f"[red]Error saving results[/red]: {e}")
sys.exit(1)


@data.command(name="import")
@click.argument("archive", nargs=1, required=True)
@click.pass_context
def import_data_store(ctx, archive: str):

from kiara.registries.data import DataArchive
from kiara.utils.stores import check_external_archive

kiara_api: KiaraAPI = ctx.obj.kiara_api

terminal_print(f"Loading store '{archive}'...")

archives = check_external_archive(archive)

data_archive: DataArchive = archives.get("data", None) # type: ignore

if not data_archive:
terminal_print(f"[red]Error[/red]: No data archives found in '{archive}'")
sys.exit(1)

terminal_print("Registering data archive...")
store_alias = kiara_api.context.data_registry.register_data_archive(data_archive)

print(store_alias)

Check failure on line 673 in src/kiara/interfaces/cli/data/commands.py

View workflow job for this annotation

GitHub Actions / linting-linux

Ruff (T201)

src/kiara/interfaces/cli/data/commands.py:673:5: T201 `print` found
result = kiara_api.store_values(data_archive.value_ids)
dbg(result)

Check failure on line 675 in src/kiara/interfaces/cli/data/commands.py

View workflow job for this annotation

GitHub Actions / linting-linux

Ruff (F821)

src/kiara/interfaces/cli/data/commands.py:675:5: F821 Undefined name `dbg`

terminal_print("Done.")
Loading

0 comments on commit b4ac06b

Please sign in to comment.