Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use qualibrate config package #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion qualibrate_runner/__main__.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
15 changes: 5 additions & 10 deletions qualibrate_runner/api/dependencies.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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(
Expand Down
52 changes: 31 additions & 21 deletions qualibrate_runner/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
import click
import tomli_w
from click.core import ParameterSource
from qualibrate_config.file import get_config_file
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 (
DEFAULT_CONFIG_FILENAME,
QUALIBRATE_PATH,
QualibrateRunnerSettings,
get_config_file,
from qualibrate_runner.config.vars import (
DEFAULT_QUALIBRATE_RUNNER_CONFIG_FILENAME,
)
from qualibrate_runner.config.validation import get_config_model_or_print_error

if sys.version_info[:2] < (3, 11):
import tomli as tomllib
Expand All @@ -36,7 +37,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
Expand All @@ -45,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)
Expand All @@ -59,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")
Expand Down Expand Up @@ -149,14 +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)
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)
7 changes: 2 additions & 5 deletions qualibrate_runner/cli/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 3 additions & 11 deletions qualibrate_runner/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
from .file import get_config_file, read_config_file
from .models import QualibrateRunnerSettings, State
from .models import 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",
]
58 changes: 0 additions & 58 deletions qualibrate_runner/config/file.py

This file was deleted.

15 changes: 3 additions & 12 deletions qualibrate_runner/config/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Empty file.
19 changes: 0 additions & 19 deletions qualibrate_runner/config/references/models.py

This file was deleted.

Loading
Loading