Skip to content

Commit

Permalink
refactor: add some abstraction for external usage
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-v4s committed Jan 21, 2025
1 parent 937f99b commit 22dc359
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
13 changes: 8 additions & 5 deletions qualibrate_config/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

from qualibrate_config.cli.migrate import migrate_command
from qualibrate_config.cli.utils.content import (
get_config,
get_config_file_content,
simple_write,
write_config,
)
from qualibrate_config.cli.utils.defaults import (
get_qapp_static_file_path,
get_user_storage,
)
from qualibrate_config.cli.utils.from_sources import config_from_sources
from qualibrate_config.cli.utils.from_sources import (
qualibrate_config_from_sources,
)
from qualibrate_config.models.qualibrate import QualibrateTopLevelConfig
from qualibrate_config.models.storage_type import StorageType
from qualibrate_config.validation import (
Expand Down Expand Up @@ -193,7 +195,7 @@ def config_command(
app_static_site_files: Path,
check_generator: bool,
) -> None:
common_config, config_file = get_config(config_path)
common_config, config_file = get_config_file_content(config_path)
old_config = deepcopy(common_config)
try:
qualibrate_version_validator(common_config, False)
Expand All @@ -202,11 +204,11 @@ def config_command(
migrate_command(
["--config-path", config_path], standalone_mode=False
)
common_config, config_file = get_config(config_path)
common_config, config_file = get_config_file_content(config_path)
qualibrate_config = common_config.get(QUALIBRATE_CONFIG_KEY, {})
required_subconfigs = ("storage",)
optional_subconfigs = ("app", "runner", "composite", "calibration_library")
qualibrate_config = config_from_sources(
qualibrate_config = qualibrate_config_from_sources(
ctx,
qualibrate_config,
required_subconfigs,
Expand All @@ -219,6 +221,7 @@ def config_command(
config_file,
common_config,
qs.qualibrate,
QUALIBRATE_CONFIG_KEY,
confirm=not auto_accept,
check_generator=check_generator,
)
Expand Down
4 changes: 2 additions & 2 deletions qualibrate_config/cli/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click
import tomli_w

from qualibrate_config.cli.utils.content import get_config
from qualibrate_config.cli.utils.content import get_config_file_content
from qualibrate_config.cli.utils.migration_utils import make_migrations
from qualibrate_config.models import QualibrateConfig
from qualibrate_config.models.qualibrate import QualibrateTopLevelConfig
Expand Down Expand Up @@ -38,7 +38,7 @@
def migrate_command(
ctx: click.Context, config_path: Path, to_version: int
) -> None:
common_config, config_file = get_config(config_path)
common_config, config_file = get_config_file_content(config_path)
if common_config == {}:
click.secho("Config file wasn't found. Nothing to migrate", fg="yellow")
return
Expand Down
33 changes: 23 additions & 10 deletions qualibrate_config/cli/utils/content.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import sys
from pathlib import Path
from typing import Callable, Optional, TypeVar

import tomli_w

from qualibrate_config.cli.utils.approve import print_and_confirm
from qualibrate_config.file import get_config_file
from qualibrate_config.models import QualibrateConfig
from qualibrate_config.models import BaseConfig
from qualibrate_config.qulibrate_types import RawConfigType
from qualibrate_config.vars import (
DEFAULT_CONFIG_FILENAME,
QUALIBRATE_CONFIG_KEY,
)

if sys.version_info[:2] < (3, 11):
Expand All @@ -18,7 +18,10 @@
import tomllib


def get_config(config_path: Path) -> tuple[RawConfigType, Path]:
ConfigType = TypeVar("ConfigType", bound=BaseConfig)


def get_config_file_content(config_path: Path) -> tuple[RawConfigType, Path]:
"""Returns config and path to file"""
config_file = get_config_file(
config_path, DEFAULT_CONFIG_FILENAME, raise_not_exists=False
Expand All @@ -33,21 +36,31 @@ def simple_write(path: Path, config: RawConfigType) -> None:
tomli_w.dump(config, f_out)


def qualibrate_before_write_cb(config: ConfigType) -> None:
if config.project in config.storage.location.parts:
config.storage.location.mkdir(parents=True, exist_ok=True)
else:
(config.storage.location / config.project).mkdir(
parents=True, exist_ok=True
)


def write_config(
config_file: Path,
common_config: RawConfigType,
qs: QualibrateConfig,
config: ConfigType,
config_key: str,
before_write_cb: Optional[Callable[[ConfigType], None]] = None,
confirm: bool = True,
check_generator: bool = False,
) -> None:
exported_data = qs.serialize(exclude_none=True)
common_config[QUALIBRATE_CONFIG_KEY] = exported_data
exported_data = config.serialize(exclude_none=True)
common_config[config_key] = exported_data
if confirm or check_generator:
print_and_confirm(config_file, common_config, check_generator)
if qs.project in qs.storage.location.parts:
qs.storage.location.mkdir(parents=True, exist_ok=True)
else:
(qs.storage.location / qs.project).mkdir(parents=True, exist_ok=True)
if before_write_cb is None:
before_write_cb = qualibrate_before_write_cb
before_write_cb(config)
if not config_file.parent.exists():
config_file.parent.mkdir(parents=True)
simple_write(config_file, common_config)
29 changes: 18 additions & 11 deletions qualibrate_config/cli/utils/from_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

from qualibrate_config.qulibrate_types import RawConfigType

__all__ = [
"not_default",
"get_config_by_args_mapping",
"get_optional_config",
"qualibrate_config_from_sources",
]


def not_default(ctx: click.Context, arg_key: str) -> bool:
return ctx.get_parameter_source(arg_key) in (
Expand All @@ -13,7 +20,7 @@ def not_default(ctx: click.Context, arg_key: str) -> bool:
)


def _get_config(
def get_config_by_args_mapping(
args_mapping: dict[str, str],
from_file: RawConfigType,
ctx: click.Context,
Expand All @@ -28,12 +35,12 @@ def _get_config(
return from_file


def _get_optional_config(
def get_optional_config(
args_mapping: dict[str, str],
from_file: Optional[RawConfigType],
ctx: click.Context,
) -> Optional[RawConfigType]:
return _get_config(args_mapping, from_file or {}, ctx)
return get_config_by_args_mapping(args_mapping, from_file or {}, ctx)


def _get_storage_config(
Expand All @@ -43,7 +50,7 @@ def _get_storage_config(
"storage_type": "type",
"storage_location": "location",
}
return _get_config(storage_keys, from_file, ctx)
return get_config_by_args_mapping(storage_keys, from_file, ctx)


def _get_calibration_library_config(
Expand All @@ -53,7 +60,7 @@ def _get_calibration_library_config(
"calibration_library_resolver": "resolver",
"calibration_library_folder": "folder",
}
from_file = _get_optional_config(args_mapping, from_file, ctx)
from_file = get_optional_config(args_mapping, from_file, ctx)
return from_file


Expand All @@ -62,25 +69,25 @@ def _get_app_config(
) -> Optional[RawConfigType]:
args_mapping = {"app_static_site_files": "static_site_files"}
# TODO: JsonTimelineDB
return _get_optional_config(args_mapping, from_file, ctx)
return get_optional_config(args_mapping, from_file, ctx)


def _get_runner_config(
ctx: click.Context, from_file: RawConfigType
) -> Optional[RawConfigType]:
args_mapping = {"runner_address": "address", "runner_timeout": "timeout"}
return _get_optional_config(args_mapping, from_file, ctx)
return get_optional_config(args_mapping, from_file, ctx)


def _get_composite_config(
ctx: click.Context, from_file: Optional[RawConfigType]
) -> Optional[RawConfigType]:
app = _get_optional_config(
app = get_optional_config(
{"spawn_app": "spawn"},
from_file.get("app") if from_file is not None else None,
ctx,
)
runner = _get_optional_config(
runner = get_optional_config(
{"spawn_runner": "spawn"},
from_file.get("runner") if from_file is not None else None,
ctx,
Expand All @@ -93,7 +100,7 @@ def _get_composite_config(
}


def config_from_sources(
def qualibrate_config_from_sources(
ctx: click.Context,
from_file: RawConfigType,
required_subconfigs: tuple[str, ...],
Expand Down Expand Up @@ -121,7 +128,7 @@ def config_from_sources(
"project": "project",
"log_folder": "log_folder",
}
qualibrate_common = _get_config(
qualibrate_common = get_config_by_args_mapping(
qualibrate_common_mapping,
from_file,
ctx,
Expand Down

0 comments on commit 22dc359

Please sign in to comment.