Skip to content

Commit

Permalink
refactor: extract base api and add renderer to copy methods selective…
Browse files Browse the repository at this point in the history
…ly to kiaraapi
  • Loading branch information
makkus committed Mar 23, 2024
1 parent b61e7f8 commit 7fd6715
Show file tree
Hide file tree
Showing 46 changed files with 5,198 additions and 3,699 deletions.
1 change: 0 additions & 1 deletion dev/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# %%


from kiara.interfaces.python_api import Step
from kiara.utils.cli import terminal_print

step_read_files_in_folder = Step(
Expand Down
4 changes: 3 additions & 1 deletion src/kiara/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"ValueMap",
"ValueMapSchema",
"ValueSchema",
"KiArchive",
]

from .context import Kiara
from .context.config import KiaraConfig
from .interfaces.python_api import KiaraAPI
from .interfaces.python_api.kiara_api import KiaraAPI
from .interfaces.python_api.models.archive import KiArchive
from .interfaces.python_api.models.job import JobDesc, RunSpec
from .models.module.pipeline.pipeline import Pipeline, PipelineStructure
from .models.values.value import Value, ValueMap
Expand Down
4 changes: 2 additions & 2 deletions src/kiara/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from kiara.data_types import DataType
from kiara.exceptions import KiaraContextException
from kiara.interfaces import get_console
from kiara.interfaces.python_api.base_api import BaseAPI
from kiara.interfaces.python_api.models.info import (
DataTypeClassesInfo,
InfoItemGroup,
Expand Down Expand Up @@ -104,9 +105,8 @@ class Kiara(object):
@classmethod
def instance(cls) -> "Kiara":
"""The default *kiara* context. In most cases, it's recommended you create and manage your own, though."""
from kiara.interfaces.python_api import KiaraAPI

return KiaraAPI.instance().context
return BaseAPI.instance().context

def __init__(
self,
Expand Down
11 changes: 5 additions & 6 deletions src/kiara/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

from kiara.context import Kiara
from kiara.context.config import KiaraConfig
from kiara.interfaces.python_api import KiaraAPI

from kiara.interfaces.python_api.base_api import BaseAPI

# log = structlog.getLogger()

Expand Down Expand Up @@ -244,7 +243,7 @@ def __init__(
self._ensure_plugins: Union[str, Iterable[str], None] = ensure_plugins

self._kiara_config: Union["KiaraConfig", None] = None
self._api: Union[KiaraAPI, None] = None
self._api: Union[BaseAPI, None] = None

self._reload_process_if_plugins_installed = True

Expand Down Expand Up @@ -344,7 +343,7 @@ def lock_file(self, context: str) -> str:
return "asdf"

@property
def kiara_api(self) -> "KiaraAPI":
def kiara_api(self) -> "BaseAPI":

if self._api is not None:
return self._api
Expand All @@ -355,9 +354,9 @@ def kiara_api(self) -> "KiaraAPI":
if not context:
context = self.kiara_config.default_context

from kiara.interfaces.python_api import KiaraAPI
from kiara.interfaces.python_api.base_api import BaseAPI

api = KiaraAPI(kiara_config=self.kiara_config)
api = BaseAPI(kiara_config=self.kiara_config)
if self._ensure_plugins:
installed = api.ensure_plugin_packages(self._ensure_plugins, update=False)
if installed and self._reload_process_if_plugins_installed:
Expand Down
12 changes: 6 additions & 6 deletions src/kiara/interfaces/cli/archive/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def explain_archive(
):
"""Print details of an archive file."""

from kiara.api import KiaraAPI
from kiara.api import BaseAPI

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

info = kiara_api.retrieve_archive_info(archive)

Expand All @@ -58,9 +58,9 @@ def explain_archive(
@handle_exception()
def export_archive(ctx, path: str, compression: str, append: bool, no_aliases: bool):

from kiara.api import KiaraAPI
from kiara.api import BaseAPI

api: KiaraAPI = ctx.obj.kiara_api
api: BaseAPI = ctx.obj.kiara_api

target_store_params = {"compression": CHUNK_COMPRESSION_TYPE[compression.upper()]}
result = api.export_archive(
Expand Down Expand Up @@ -88,9 +88,9 @@ def export_archive(ctx, path: str, compression: str, append: bool, no_aliases: b
def import_archive(ctx, path: str, no_aliases: bool):
"""Import an archive file."""

from kiara.interfaces.python_api import KiaraAPI
from kiara.interfaces.python_api.base_api import BaseAPI

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

result = kiara_api.import_archive(source_archive=path, no_aliases=no_aliases)

Expand Down
4 changes: 2 additions & 2 deletions src/kiara/interfaces/cli/context/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)

if TYPE_CHECKING:
from kiara.api import Kiara, KiaraAPI, KiaraConfig
from kiara.api import BaseAPI, Kiara, KiaraConfig


@click.group("context")
Expand All @@ -33,7 +33,7 @@ def context(ctx):
@click.pass_context
def list_contexts(ctx) -> None:
"""List existing contexts."""
kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

summaries = kiara_api.retrieve_context_infos()

Expand Down
21 changes: 10 additions & 11 deletions src/kiara/interfaces/cli/data/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


if TYPE_CHECKING:
from kiara.api import Kiara, KiaraAPI
from kiara.api import BaseAPI, Kiara
from kiara.operations.included_core_operations.filter import FilterOperationType

logger = structlog.getLogger()
Expand Down Expand Up @@ -147,10 +147,9 @@ def list_values(
) -> None:
"""List all data items that are stored in kiara."""

from kiara.interfaces.python_api import ValuesInfo
from kiara.interfaces.python_api.models.info import RENDER_FIELDS
from kiara.interfaces.python_api.models.info import RENDER_FIELDS, ValuesInfo

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

if include_internal:
all_values = True
Expand Down Expand Up @@ -270,7 +269,7 @@ def explain_value(
All of the 'show-additional-information' flags are only applied when the 'terminal' output format is selected. This might change in the future.
"""

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

render_config = {
"show_pedigree": pedigree,
Expand Down Expand Up @@ -322,7 +321,7 @@ def explain_value(
def load_value(ctx, value: str):
"""Load a stored value and print it in a format suitable for the terminal."""
# kiara_obj: Kiara = ctx.obj["kiara"]
kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

try:
_value = kiara_api.get_value(value=value)
Expand Down Expand Up @@ -413,7 +412,7 @@ def filter_value(
silent = True

kiara_obj: Kiara = ctx.obj.kiara
api: KiaraAPI = ctx.obj.kiara_api
api: BaseAPI = ctx.obj.kiara_api

cmd_help = "[yellow bold]Usage: [/yellow bold][bold]kiara data filter VALUE FILTER_1:FILTER_2 [FILTER ARGS...][/bold]"

Expand Down Expand Up @@ -573,7 +572,7 @@ def export_data_archive(
Aliases that already exist in the target archve will be overwritten.
"""

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

values = []
for idx, alias in enumerate(aliases, start=1):
Expand Down Expand Up @@ -621,7 +620,7 @@ def export_data_archive(
elif full_path.exists():
if append and replace:
terminal_print(
f"[red]Error[/red]: Can't specify both '--append' and '--replace'."
"[red]Error[/red]: Can't specify both '--append' and '--replace'."
)
sys.exit(1)
if append:
Expand Down Expand Up @@ -705,7 +704,7 @@ def export_data_archive(
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
kiara_api: BaseAPI = ctx.obj.kiara_api

archive_path = Path(archive)
if not archive_path.exists():
Expand Down Expand Up @@ -739,7 +738,7 @@ def import_data_store(ctx, archive: str, values: Tuple[str], no_aliases: bool =
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
kiara_api: BaseAPI = ctx.obj.kiara_api

value = kiara_api.get_value(value_id_or_alias)
serialized = value.serialized_data
Expand Down
8 changes: 4 additions & 4 deletions src/kiara/interfaces/cli/info/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from kiara.utils.cli.exceptions import handle_exception

if TYPE_CHECKING:
from kiara.interfaces import KiaraAPI, KiaraAPIWrap
from kiara.interfaces import BaseAPI, KiaraAPIWrap


@click.group("info")
Expand Down Expand Up @@ -55,9 +55,9 @@ def plugin(ctx):
def list_plugins(ctx, filter_regex: str, format):
"""List installed kiara plugins."""

from kiara.interfaces.python_api import KiaraPluginInfos
from kiara.interfaces.python_api.models.info import KiaraPluginInfos

api: KiaraAPI = ctx.obj.kiara_api
api: BaseAPI = ctx.obj.kiara_api

title = "All available plugins"
if filter_regex:
Expand All @@ -75,7 +75,7 @@ def list_plugins(ctx, filter_regex: str, format):
@click.pass_context
def explain_plugin_info(ctx, plugin_name: str, format: str):

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

plugin_info = kiara_api.retrieve_plugin_info(plugin_name)
title = f"Info for plugin: [i]{plugin_name}[/i]"
Expand Down
8 changes: 4 additions & 4 deletions src/kiara/interfaces/cli/module/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

if TYPE_CHECKING:
from kiara.api import KiaraAPI
from kiara.api import BaseAPI


@click.group()
Expand Down Expand Up @@ -51,7 +51,7 @@ def list_modules(
python_package: Union[str, None],
):
"""List available module data_types."""
kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

module_types_info = kiara_api.retrieve_module_types_info(
filter=filter, python_package=python_package
Expand Down Expand Up @@ -79,7 +79,7 @@ def explain_module_type(ctx, module_type: str, format: str):
instantiated with configuration, before we can query all their properties (like
input/output data_types).
"""
kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api
info = kiara_api.retrieve_module_type_info(module_type=module_type)

terminal_print_model(
Expand All @@ -106,7 +106,7 @@ def explain_module(ctx, module_type: str, module_config: Iterable[Any], format:
else:
module_config = {}

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

operation = kiara_api.create_operation(
module_type=module_type, module_config=module_config
Expand Down
10 changes: 5 additions & 5 deletions src/kiara/interfaces/cli/operation/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from kiara.utils.cli.exceptions import handle_exception

if TYPE_CHECKING:
from kiara.api import Kiara, KiaraAPI
from kiara.api import BaseAPI, Kiara


@click.group()
Expand Down Expand Up @@ -72,7 +72,7 @@ def list_types(ctx, full_doc: bool, format: str, filter: Iterable[str]):
@handle_exception()
def explain_type(ctx, operation_type: str, format: str):

kiara_api: KiaraAPI = ctx.obj.kiara_api
kiara_api: BaseAPI = ctx.obj.kiara_api

op_type = kiara_api.retrieve_operation_type_info(operation_type)

Expand Down Expand Up @@ -120,7 +120,7 @@ def list_operations(
):

kiara_obj: Kiara = ctx.obj.kiara
api: KiaraAPI = ctx.obj.kiara_api
api: BaseAPI = ctx.obj.kiara_api

operations = api.list_operations(
filter=filter, include_internal=include_internal, python_packages=python_package
Expand Down Expand Up @@ -149,7 +149,7 @@ def list_operations(
#
# operations = temp

from kiara.interfaces.python_api import OperationGroupInfo
from kiara.interfaces.python_api.models.info import OperationGroupInfo

ops_info = OperationGroupInfo.create_from_operations(
kiara=kiara_obj, group_title=title, **operations.root
Expand Down Expand Up @@ -182,7 +182,7 @@ def list_operations(
def explain(ctx, operation_id: str, source: bool, format: str, module_info: bool):

kiara_obj: Kiara = ctx.obj.kiara
api: KiaraAPI = ctx.obj.kiara_api
api: BaseAPI = ctx.obj.kiara_api

if os.path.isfile(os.path.realpath(operation_id)):
operation = api.get_operation(operation_id)
Expand Down
2 changes: 1 addition & 1 deletion src/kiara/interfaces/cli/pipeline/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def list_pipelines(ctx, full_doc: bool, filter: typing.Iterable[str], format: st
op_id: kiara_obj.operation_registry.get_operation(op_id) for op_id in op_ids
}

from kiara.interfaces.python_api import OperationGroupInfo
from kiara.interfaces.python_api.models.info import OperationGroupInfo

ops_info = OperationGroupInfo.create_from_operations(
kiara=kiara_obj, group_title=title, **operations
Expand Down
6 changes: 3 additions & 3 deletions src/kiara/interfaces/cli/render/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from kiara.utils.cli.exceptions import handle_exception

if typing.TYPE_CHECKING:
from kiara.api import KiaraAPI
from kiara.api import BaseAPI
from kiara.interfaces import KiaraAPIWrap


Expand Down Expand Up @@ -63,7 +63,7 @@ def render(
def list_render_combinations(ctx, format: str):

api_wrap: KiaraAPIWrap = ctx.obj
kiara_api: KiaraAPI = api_wrap.kiara_api
kiara_api: BaseAPI = api_wrap.kiara_api

source_type = api_wrap.get_item("source_type")
target_type = api_wrap.get_item("target_type")
Expand Down Expand Up @@ -104,7 +104,7 @@ def render_item(
"""Render an internal kiara item."""

api_wrap: KiaraAPIWrap = ctx.obj
kiara_api: KiaraAPI = api_wrap.kiara_api
kiara_api: BaseAPI = api_wrap.kiara_api

source_type = api_wrap.get_item("source_type")
target_type = api_wrap.get_item("target_type")
Expand Down
4 changes: 2 additions & 2 deletions src/kiara/interfaces/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from kiara.utils.files import get_data_from_file

if typing.TYPE_CHECKING:
from kiara.api import KiaraAPI
from kiara.api import BaseAPI


@click.command()
Expand Down Expand Up @@ -121,7 +121,7 @@ def run(

sys.exit(1)

api: KiaraAPI = ctx.obj.kiara_api # type: ignore
api: BaseAPI = ctx.obj.kiara_api # type: ignore

cmd_arg = ctx.params["module_or_operation"]
cmd_help = f"[yellow bold]Usage: [/yellow bold][bold]kiara run [OPTIONS] [i]{cmd_arg}[/i] [INPUTS][/bold]"
Expand Down
2 changes: 1 addition & 1 deletion src/kiara/interfaces/cli/type/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def list_types(
):
"""List available data_types."""
from kiara.data_types import DataType
from kiara.interfaces.python_api import DataTypeClassesInfo
from kiara.interfaces.python_api.models.info import DataTypeClassesInfo

kiara_obj: Kiara = ctx.obj.kiara

Expand Down
Loading

0 comments on commit 7fd6715

Please sign in to comment.