From 3f46563618d73479289a2c8d2357cc820b23f117 Mon Sep 17 00:00:00 2001 From: Maxim V4S Date: Wed, 6 Nov 2024 15:28:01 +0300 Subject: [PATCH 1/2] refactor: partially move config to separated repo --- poetry.lock | 23 +- pyproject.toml | 1 + qualibrate_runner/cli/config.py | 20 +- qualibrate_runner/cli/start.py | 7 +- qualibrate_runner/config/__init__.py | 11 +- qualibrate_runner/config/file.py | 58 ----- .../config/references/__init__.py | 0 qualibrate_runner/config/references/models.py | 19 -- .../config/references/resolvers.py | 202 ------------------ qualibrate_runner/config/resolvers.py | 19 +- qualibrate_runner/config/validation.py | 46 ---- qualibrate_runner/config/vars.py | 6 +- 12 files changed, 55 insertions(+), 357 deletions(-) delete mode 100644 qualibrate_runner/config/file.py delete mode 100644 qualibrate_runner/config/references/__init__.py delete mode 100644 qualibrate_runner/config/references/models.py delete mode 100644 qualibrate_runner/config/references/resolvers.py delete mode 100644 qualibrate_runner/config/validation.py diff --git a/poetry.lock b/poetry.lock index b1e0b29..f8400d1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1950,6 +1950,27 @@ configbuilder = ["dash (>=2.0.0,<3.0.0)", "dash-bootstrap-components (>=1.0.0,<2 datahandler = ["xarray (>=2023.0.0,<2024.0.0)"] interplot = ["dill (>=0.3.4,<0.4.0)", "ipython (>=8.10.0,<9.0.0)", "pypiwin32 (>=223,<224)"] +[[package]] +name = "qualibrate-config" +version = "0.1.0" +description = "" +optional = false +python-versions = "^3.9" +files = [] +develop = true + +[package.dependencies] +click = "^8.1.7" +jsonpatch = "^1.33" +pydantic = "^2.9.2" +pydantic-settings = "^2.6.1" +tomli = {version = "^2.0.1", markers = "python_version < \"3.11\""} +tomli-w = "^1.0.0" + +[package.source] +type = "directory" +url = "../qualibrate-config" + [[package]] name = "qualibrate-core" version = "0.2.3" @@ -2315,4 +2336,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "91d16249b5f4468c0f7d423fa0bc3ce671c475700ae121d472831f3778892c6b" +content-hash = "745cbb4fdc18c77334c07cac182d461c3cf7f5d76dbf34aa6a63c547b3bd7f8c" diff --git a/pyproject.toml b/pyproject.toml index 51f5a3e..ef6b0e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ tomli = { version = "^2.0.1", python = "<3.11" } tomli-w = "^1.0.0" jsonpatch = "^1.33" qualibrate-core = "^0.2.0" +qualibrate-config = {path = "../qualibrate-config", develop = true} [tool.poetry.group.dev.dependencies] mypy = "^1.10.0" diff --git a/qualibrate_runner/cli/config.py b/qualibrate_runner/cli/config.py index bb3e5a0..6afc8ed 100644 --- a/qualibrate_runner/cli/config.py +++ b/qualibrate_runner/cli/config.py @@ -7,17 +7,17 @@ import click import tomli_w from click.core import ParameterSource +from qualibrate_config.file import get_config_file +from qualibrate_config.validation import get_config_model_or_print_error +from qualibrate_config.vars import DEFAULT_CONFIG_FILENAME, QUALIBRATE_PATH from qualibrate_runner.config import ( CONFIG_KEY as QUALIBRATE_RUNNER_CONFIG_KEY, ) from qualibrate_runner.config import ( - DEFAULT_CONFIG_FILENAME, - QUALIBRATE_PATH, + DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME, QualibrateRunnerSettings, - get_config_file, ) -from qualibrate_runner.config.validation import get_config_model_or_print_error if sys.version_info[:2] < (3, 11): import tomli as tomllib @@ -36,7 +36,11 @@ def not_default(ctx: click.Context, arg_key: str) -> bool: def get_config(config_path: Path) -> tuple[dict[str, Any], Path]: """Returns config and path to file""" - config_file = get_config_file(config_path, raise_not_exists=False) + config_file = get_config_file( + config_path=config_path, + default_config_specific_filename=DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME, + raise_not_exists=False, + ) if config_file.is_file(): return tomllib.loads(config_file.read_text()), config_path return {}, config_file @@ -156,7 +160,11 @@ def config_command( ) runner_config = _config_from_sources(ctx, runner_config) - qrs = get_config_model_or_print_error(runner_config) + qrs = get_config_model_or_print_error( + runner_config, + QualibrateRunnerSettings, + QUALIBRATE_RUNNER_CONFIG_KEY, + ) if qrs is None: return write_config(config_file, common_config, qrs, confirm=not auto_accept) diff --git a/qualibrate_runner/cli/start.py b/qualibrate_runner/cli/start.py index bd96bfb..78ef04e 100644 --- a/qualibrate_runner/cli/start.py +++ b/qualibrate_runner/cli/start.py @@ -2,12 +2,9 @@ from pathlib import Path import click +from qualibrate_config.vars import DEFAULT_CONFIG_FILENAME, QUALIBRATE_PATH -from qualibrate_runner.config import ( - CONFIG_PATH_ENV_NAME, - DEFAULT_CONFIG_FILENAME, - QUALIBRATE_PATH, -) +from qualibrate_runner.config import CONFIG_PATH_ENV_NAME @click.command(name="start") diff --git a/qualibrate_runner/config/__init__.py b/qualibrate_runner/config/__init__.py index 076ad1d..52532cd 100644 --- a/qualibrate_runner/config/__init__.py +++ b/qualibrate_runner/config/__init__.py @@ -1,24 +1,17 @@ -from .file import get_config_file, read_config_file from .models import QualibrateRunnerSettings, State from .resolvers import get_config_path, get_settings from .vars import ( CONFIG_KEY, CONFIG_PATH_ENV_NAME, - DEFAULT_CONFIG_FILENAME, - DEFAULT_QUALIBRATE_CONFIG_FILENAME, - QUALIBRATE_PATH, + DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME, ) __all__ = [ "CONFIG_KEY", - "DEFAULT_CONFIG_FILENAME", - "DEFAULT_QUALIBRATE_CONFIG_FILENAME", - "QUALIBRATE_PATH", + "DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME", "CONFIG_PATH_ENV_NAME", "State", "QualibrateRunnerSettings", - "get_config_file", - "read_config_file", "get_config_path", "get_settings", ] diff --git a/qualibrate_runner/config/file.py b/qualibrate_runner/config/file.py deleted file mode 100644 index c9bed22..0000000 --- a/qualibrate_runner/config/file.py +++ /dev/null @@ -1,58 +0,0 @@ -import sys -from pathlib import Path -from typing import Any, Optional, Union - -from qualibrate_runner.config.references.resolvers import resolve_references -from qualibrate_runner.config.vars import ( - DEFAULT_CONFIG_FILENAME, - DEFAULT_QUALIBRATE_CONFIG_FILENAME, - QUALIBRATE_PATH, -) - -if sys.version_info[:2] < (3, 11): - import tomli as tomllib -else: - import tomllib - - -__all__ = ["get_config_file", "read_config_file"] - - -def _get_config_file_from_dir( - dir_path: Path, raise_not_exists: bool = True -) -> Path: - default_qualibrate = dir_path / DEFAULT_QUALIBRATE_CONFIG_FILENAME - if default_qualibrate.is_file(): - return default_qualibrate - default_common = dir_path / DEFAULT_CONFIG_FILENAME - if default_common.is_file(): - return default_common - if raise_not_exists: - raise FileNotFoundError(f"Config file in dir {dir_path} does not exist") - return default_common - - -def get_config_file( - config_path: Optional[Union[str, Path]], raise_not_exists: bool = True -) -> Path: - if config_path is not None: - config_path_ = Path(config_path) - if config_path_.is_file(): - return config_path_ - if config_path_.is_dir(): - return _get_config_file_from_dir(config_path_) - if raise_not_exists: - raise OSError("Unexpected config file path") - return config_path_ - if config_path is None: - return _get_config_file_from_dir(QUALIBRATE_PATH) - - -def read_config_file( - config_file: Path, solve_references: bool = True -) -> dict[str, Any]: - with config_file.open("rb") as fin: - config = tomllib.load(fin) - if not solve_references: - return config - return resolve_references(config) diff --git a/qualibrate_runner/config/references/__init__.py b/qualibrate_runner/config/references/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/qualibrate_runner/config/references/models.py b/qualibrate_runner/config/references/models.py deleted file mode 100644 index 888a87b..0000000 --- a/qualibrate_runner/config/references/models.py +++ /dev/null @@ -1,19 +0,0 @@ -from typing import Any - -from pydantic import BaseModel, Field - - -class Reference(BaseModel): - config_path: str - reference_path: str - index_start: int - index_end: int - value: Any = None - solved: bool = False - - -class PathWithSolvingReferences(BaseModel): - config_path: str - value: Any = None - solved: bool = False - references: list[Reference] = Field(default_factory=list) diff --git a/qualibrate_runner/config/references/resolvers.py b/qualibrate_runner/config/references/resolvers.py deleted file mode 100644 index 7c8cf11..0000000 --- a/qualibrate_runner/config/references/resolvers.py +++ /dev/null @@ -1,202 +0,0 @@ -from collections import defaultdict -from collections.abc import Mapping, Sequence -from typing import Any, Optional, cast - -import jsonpatch -import jsonpointer - -from qualibrate_runner.config.references.models import ( - PathWithSolvingReferences, - Reference, -) - -TEMPLATE_START = "${#" - - -def find_all_references( - document: Mapping[str, Any], current_path: Optional[list[str]] = None -) -> Sequence[Reference]: - if current_path is None: - current_path = [] - to_resolve: list[Reference] = [] - for key, value in document.items(): - if isinstance(value, Mapping): - to_resolve.extend(find_all_references(value, current_path + [key])) - elif isinstance(value, str): - config_path = "/" + "/".join(current_path + [key]) - template_start_index = value.find(TEMPLATE_START) - while template_start_index != -1: - template_end_index = value.find("}", template_start_index) - if template_end_index == -1: - break - to_resolve.append( - Reference( - config_path=config_path, - reference_path=value[ - template_start_index + 3 : template_end_index - ].strip(), - index_start=template_start_index, - index_end=template_end_index, - ) - ) - template_start_index = value.find( - TEMPLATE_START, template_end_index + 1 - ) - return to_resolve - - -def check_cycles_in_references( - references: Mapping[str, Sequence[str]], -) -> tuple[bool, Optional[Sequence[str]]]: - """Return True if the references has a cycle. - - >>> check_cycles_in_references({"a": ("b",), "b": ("c",), "c": ("a",)}) - (True, ['a', 'b', 'c', 'a']) - >>> check_cycles_in_references({"a": ("b",), "b": ("c",), "c": ("d",)}) - (False, None) - - """ - path: list[str] = [] - visited: set[str] = set() - cycled_item: str = "" - - def visit(vertex: str) -> bool: - nonlocal cycled_item - if vertex in visited: - return False - visited.add(vertex) - path.append(vertex) - for neighbour in references.get(vertex, ()): - if neighbour in path or visit(neighbour): - cycled_item = vertex - return True - path.pop() # == path.remove(vertex): - return False - - if any(visit(v) for v in references): - return True, [*path, cycled_item] - return False, None - - -def _resolve_references( - path: str, - path_with_references: dict[str, PathWithSolvingReferences], - original_config: Mapping[str, Any], - solved_references: dict[str, Any], -) -> None: - if path_with_references[path].solved: - # path already solved - return - config_item = path_with_references[path] - references: list[Reference] = config_item.references - not_solved_refs = filter(lambda ref: not ref.solved, references) - for ref in not_solved_refs: - if ref.reference_path in solved_references: - ref.value = solved_references[ref.reference_path] - ref.solved = True - elif ref.reference_path in path_with_references: - _resolve_references( - ref.reference_path, - path_with_references, - original_config, - solved_references, - ) - if ref.reference_path not in solved_references: - raise ValueError( - f"Subreference '{ref.reference_path}' " - f"for '{ref.config_path}' not solved." - ) - ref.value = solved_references[ref.reference_path] - ref.solved = True - else: - value = jsonpointer.resolve_pointer( - original_config, ref.reference_path, None - ) - if value is None: - raise ValueError( - f"Can't resolve reference item '{ref.reference_path}' " - f"for config path '{ref.config_path}'" - ) - ref.value = value - ref.solved = True - solved_references[ref.reference_path] = value - verify_all_solved = all(map(lambda ref: ref.solved, references)) - if not verify_all_solved: - not_solved_refs = filter(lambda ref: not ref.solved, references) - references_errors = ( - ( - f"- config path: '{ref.config_path}', " - f"reference: '{ref.reference_path}';" - ) - for ref in not_solved_refs - ) - raise ValueError( - "\n".join( - [ - "Some issues with solving references. Issued references:", - *references_errors, - ] - ) - ) - config_value = jsonpointer.resolve_pointer( - original_config, config_item.config_path, None - ) - if config_value is None or not isinstance(config_value, str): - raise ValueError( - f"Can't resolve config item '{config_item.config_path}'" - ) - for ref in sorted( - references, key=lambda ref: ref.index_start, reverse=True - ): - config_value = ( - f"{config_value[:ref.index_start]}" - f"{ref.value}{config_value[ref.index_end + 1:]}" - ) - config_item.value = config_value - config_item.solved = True - solved_references[config_item.config_path] = config_value - - -def resolve_references(config: dict[str, Any]) -> dict[str, Any]: - references = find_all_references(config) - references_seq = defaultdict(list) - for reference in references: - references_seq[reference.config_path].append(reference.reference_path) - has_cycles, cycle = check_cycles_in_references(references_seq) - if has_cycles: - raise ValueError(f"Config contains cycle: {cycle}") - solved_references: dict[str, Any] = {} - path_with_references: dict[str, PathWithSolvingReferences] = {} - for reference in references: - path_with_refs = path_with_references.setdefault( - reference.config_path, - PathWithSolvingReferences(config_path=reference.config_path), - ) - path_with_refs.references.append(reference) - for path in path_with_references: - _resolve_references( - path, - path_with_references, - config, - solved_references, - ) - patches = [ - {"op": "replace", "path": path.config_path, "value": path.value} - for path in path_with_references.values() - ] - return cast(dict[str, Any], jsonpatch.apply_patch(config, patches)) - - -if __name__ == "__main__": - _config = { - "qualibrate": {"project": "my_project"}, - # "qualibrate": {"project": "${#/data_handler/project}"}, - "data_handler": { - "root_data_folder": "/data/${#/data_handler/project}/subpath", - "project": "${#/qualibrate/project}", - # "project": "${#/qualibrate/project_}", - }, - } - res = resolve_references(_config) - print(_config) - print(res) diff --git a/qualibrate_runner/config/resolvers.py b/qualibrate_runner/config/resolvers.py index 39def5c..e26e834 100644 --- a/qualibrate_runner/config/resolvers.py +++ b/qualibrate_runner/config/resolvers.py @@ -4,16 +4,17 @@ from typing import Annotated from fastapi import Depends - -from qualibrate_runner.config.file import get_config_file -from qualibrate_runner.config.models import QualibrateRunnerSettings -from qualibrate_runner.config.validation import ( +from qualibrate_config.file import get_config_file +from qualibrate_config.validation import ( get_config_model_or_print_error, get_config_solved_references_or_print_error, ) + +from qualibrate_runner.config.models import QualibrateRunnerSettings from qualibrate_runner.config.vars import ( CONFIG_KEY, CONFIG_PATH_ENV_NAME, + DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME, ) __all__ = [ @@ -24,7 +25,11 @@ @lru_cache def get_config_path() -> Path: - return get_config_file(os.environ.get(CONFIG_PATH_ENV_NAME)) + return get_config_file( + config_path=os.environ.get(CONFIG_PATH_ENV_NAME), + default_config_specific_filename=DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME, + raise_not_exists=True, + ) @lru_cache @@ -34,7 +39,9 @@ def get_settings( config = get_config_solved_references_or_print_error(config_path) if config is None: raise RuntimeError("Couldn't read config file") - qrs = get_config_model_or_print_error(config.get(CONFIG_KEY, {})) + qrs = get_config_model_or_print_error( + config.get(CONFIG_KEY, {}), QualibrateRunnerSettings, CONFIG_KEY + ) if qrs is None: raise RuntimeError("Couldn't read config file") return qrs diff --git a/qualibrate_runner/config/validation.py b/qualibrate_runner/config/validation.py deleted file mode 100644 index bb091dd..0000000 --- a/qualibrate_runner/config/validation.py +++ /dev/null @@ -1,46 +0,0 @@ -from collections.abc import Mapping -from pathlib import Path -from typing import Any, Optional - -import click -from pydantic import ValidationError - -from qualibrate_runner.config.file import read_config_file -from qualibrate_runner.config.models import QualibrateRunnerSettings -from qualibrate_runner.config.vars import CONFIG_KEY - -SUGGEST_MSG = ( - "Can't parse existing config. Fix it or overwrite " - "by default values using `--overwrite` flag." -) - - -def get_config_solved_references_or_print_error( - config_path: Path, -) -> Optional[dict[str, Any]]: - try: - return read_config_file(config_path, solve_references=True) - except ValueError as ex: - click.secho(str(ex), fg="red") - click.secho(SUGGEST_MSG, fg="yellow") - return None - - -def get_config_model_or_print_error( - config: Mapping[str, Any], -) -> Optional[QualibrateRunnerSettings]: - try: - return QualibrateRunnerSettings(**config) - except ValidationError as ex: - errors = [ - ( - f"Message: {error.get('msg')}. " - "Path: " - f"{'.'.join([CONFIG_KEY, *map(str, error.get('loc', []))])}. " - f"Value: {error.get('input')}" - ) - for error in ex.errors() - ] - click.secho("\n".join(errors), fg="red") - click.secho(SUGGEST_MSG, fg="yellow") - return None diff --git a/qualibrate_runner/config/vars.py b/qualibrate_runner/config/vars.py index e529b53..9919c85 100644 --- a/qualibrate_runner/config/vars.py +++ b/qualibrate_runner/config/vars.py @@ -1,7 +1,3 @@ -from pathlib import Path - CONFIG_KEY = "qualibrate_runner" -QUALIBRATE_PATH = Path().home() / ".qualibrate" -DEFAULT_CONFIG_FILENAME = "config.toml" -DEFAULT_QUALIBRATE_CONFIG_FILENAME = "qualibrate-runner.toml" +DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME = "qualibrate-runner.toml" CONFIG_PATH_ENV_NAME = "QUALIBRATE_RUNNER_CONFIG_FILE" From 612fcdb9978c4fb37a50eeea3fd82f83041e57ee Mon Sep 17 00:00:00 2001 From: Maxim V4S Date: Wed, 18 Dec 2024 14:51:38 +0300 Subject: [PATCH 2/2] feat: move config to separated repo --- qualibrate_runner/__main__.py | 3 +- qualibrate_runner/api/dependencies.py | 15 +++------ qualibrate_runner/cli/config.py | 44 ++++++++++++++------------- qualibrate_runner/config/__init__.py | 3 +- qualibrate_runner/config/models.py | 15 ++------- qualibrate_runner/config/resolvers.py | 29 ++++++------------ 6 files changed, 43 insertions(+), 66 deletions(-) diff --git a/qualibrate_runner/__main__.py b/qualibrate_runner/__main__.py index 7bd6e9f..9953c96 100644 --- a/qualibrate_runner/__main__.py +++ b/qualibrate_runner/__main__.py @@ -1,6 +1,7 @@ import click +from qualibrate_config.cli import config_command -from qualibrate_runner.cli import config_command, start_command +from qualibrate_runner.cli import start_command @click.group() diff --git a/qualibrate_runner/api/dependencies.py b/qualibrate_runner/api/dependencies.py index 8b02970..06017eb 100644 --- a/qualibrate_runner/api/dependencies.py +++ b/qualibrate_runner/api/dependencies.py @@ -1,14 +1,11 @@ from collections.abc import Mapping from functools import cache -from typing import Annotated +from typing import Annotated, cast from fastapi import Depends, HTTPException +from qualibrate_config.models import CalibrationLibraryConfig -from qualibrate_runner.config import ( - QualibrateRunnerSettings, - State, - get_settings, -) +from qualibrate_runner.config import State, get_settings from qualibrate_runner.core.types import QGraphType, QLibraryType, QNodeType @@ -19,11 +16,9 @@ def get_state() -> State: @cache def get_cached_library( - settings: Annotated[QualibrateRunnerSettings, Depends(get_settings)], + config: Annotated[CalibrationLibraryConfig, Depends(get_settings)], ) -> QLibraryType: - return settings.calibration_library_resolver( - settings.calibration_library_folder - ) + return cast(QLibraryType, config.resolver(config.folder)) def get_library( diff --git a/qualibrate_runner/cli/config.py b/qualibrate_runner/cli/config.py index 6afc8ed..bbba9bc 100644 --- a/qualibrate_runner/cli/config.py +++ b/qualibrate_runner/cli/config.py @@ -8,15 +8,16 @@ import tomli_w from click.core import ParameterSource from qualibrate_config.file import get_config_file -from qualibrate_config.validation import get_config_model_or_print_error +from qualibrate_config.models.calibration_library import ( + CalibrationLibraryConfig, +) from qualibrate_config.vars import DEFAULT_CONFIG_FILENAME, QUALIBRATE_PATH -from qualibrate_runner.config import ( +from qualibrate_runner.config.vars import ( CONFIG_KEY as QUALIBRATE_RUNNER_CONFIG_KEY, ) -from qualibrate_runner.config import ( +from qualibrate_runner.config.vars import ( DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME, - QualibrateRunnerSettings, ) if sys.version_info[:2] < (3, 11): @@ -49,7 +50,7 @@ def get_config(config_path: Path) -> tuple[dict[str, Any], Path]: def _config_from_sources( ctx: click.Context, from_file: dict[str, Any] ) -> dict[str, Any]: - config_keys = QualibrateRunnerSettings.model_fields.keys() + config_keys = CalibrationLibraryConfig.get_annotations() runner_mapping: dict[str, str] = {k: k for k in config_keys} for arg_key, arg_value in ctx.params.items(): not_default_arg = not_default(ctx, arg_key) @@ -63,7 +64,7 @@ def _config_from_sources( def write_config( config_file: Path, common_config: dict[str, Any], - qrs: QualibrateRunnerSettings, + qrs: CalibrationLibraryConfig, confirm: bool = True, ) -> None: exported_data = qrs.model_dump(mode="json") @@ -153,18 +154,19 @@ def config_command( calibration_library_folder: Path, ) -> None: common_config, config_file = get_config(config_path) - runner_config = ( - common_config.get(QUALIBRATE_RUNNER_CONFIG_KEY, {}) - if not overwrite - else {} - ) - runner_config = _config_from_sources(ctx, runner_config) - - qrs = get_config_model_or_print_error( - runner_config, - QualibrateRunnerSettings, - QUALIBRATE_RUNNER_CONFIG_KEY, - ) - if qrs is None: - return - write_config(config_file, common_config, qrs, confirm=not auto_accept) + # TODO: write config + # runner_config = ( + # common_config.get(QUALIBRATE_RUNNER_CONFIG_KEY, {}) + # if not overwrite + # else {} + # ) + # runner_config = _config_from_sources(ctx, runner_config) + # + # qrs = get_config_model_or_print_error( + # runner_config, + # QualibrateRunnerSettings, + # QUALIBRATE_RUNNER_CONFIG_KEY, + # ) + # if qrs is None: + # return + # write_config(config_file, common_config, qrs, confirm=not auto_accept) diff --git a/qualibrate_runner/config/__init__.py b/qualibrate_runner/config/__init__.py index 52532cd..d58e492 100644 --- a/qualibrate_runner/config/__init__.py +++ b/qualibrate_runner/config/__init__.py @@ -1,4 +1,4 @@ -from .models import QualibrateRunnerSettings, State +from .models import State from .resolvers import get_config_path, get_settings from .vars import ( CONFIG_KEY, @@ -11,7 +11,6 @@ "DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME", "CONFIG_PATH_ENV_NAME", "State", - "QualibrateRunnerSettings", "get_config_path", "get_settings", ] diff --git a/qualibrate_runner/config/models.py b/qualibrate_runner/config/models.py index 5029f8a..0e3b385 100644 --- a/qualibrate_runner/config/models.py +++ b/qualibrate_runner/config/models.py @@ -1,11 +1,9 @@ -from pathlib import Path -from typing import Callable, Optional, Union +from typing import Optional, Union -from pydantic import BaseModel, ConfigDict, DirectoryPath, ImportString -from pydantic_settings import BaseSettings, SettingsConfigDict +from pydantic import BaseModel, ConfigDict from qualibrate_runner.core.models.last_run import LastRun, RunStatus -from qualibrate_runner.core.types import QGraphType, QLibraryType, QNodeType +from qualibrate_runner.core.types import QGraphType, QNodeType class State(BaseModel): @@ -20,10 +18,3 @@ def is_running(self) -> bool: self.last_run is not None and self.last_run.status == RunStatus.RUNNING ) - - -class QualibrateRunnerSettings(BaseSettings): - model_config = SettingsConfigDict(frozen=True) - - calibration_library_resolver: ImportString[Callable[[Path], QLibraryType]] - calibration_library_folder: DirectoryPath diff --git a/qualibrate_runner/config/resolvers.py b/qualibrate_runner/config/resolvers.py index e26e834..9dd7f44 100644 --- a/qualibrate_runner/config/resolvers.py +++ b/qualibrate_runner/config/resolvers.py @@ -5,22 +5,15 @@ from fastapi import Depends from qualibrate_config.file import get_config_file -from qualibrate_config.validation import ( - get_config_model_or_print_error, - get_config_solved_references_or_print_error, -) +from qualibrate_config.models import CalibrationLibraryConfig +from qualibrate_config.resolvers import get_qualibrate_config -from qualibrate_runner.config.models import QualibrateRunnerSettings from qualibrate_runner.config.vars import ( - CONFIG_KEY, CONFIG_PATH_ENV_NAME, DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME, ) -__all__ = [ - "get_config_path", - "get_settings", -] +__all__ = ["get_config_path", "get_settings"] @lru_cache @@ -35,13 +28,9 @@ def get_config_path() -> Path: @lru_cache def get_settings( config_path: Annotated[Path, Depends(get_config_path)], -) -> QualibrateRunnerSettings: - config = get_config_solved_references_or_print_error(config_path) - if config is None: - raise RuntimeError("Couldn't read config file") - qrs = get_config_model_or_print_error( - config.get(CONFIG_KEY, {}), QualibrateRunnerSettings, CONFIG_KEY - ) - if qrs is None: - raise RuntimeError("Couldn't read config file") - return qrs +) -> CalibrationLibraryConfig: + q_config = get_qualibrate_config(config_path) + q_lib = q_config.calibration_library + if q_lib is None: + raise ValueError("Calibration library is not specified in config") + return q_lib