Skip to content

Commit

Permalink
Copypaste config.py code from dfint/search_offsets
Browse files Browse the repository at this point in the history
insolor committed Nov 7, 2024
1 parent fc65440 commit 794720e
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions dfint64_patch/config.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import functools
from collections.abc import Callable
from typing import TypeVar

from loguru import logger
from omegaconf import DictConfig, OmegaConf

T = TypeVar("T", bound=DictConfig)


def with_config(config_class: type[T], config_file: str) -> Callable[[Callable[[T], None]], Callable[[], None]]:
def with_config(config_class: type, *config_files: str) -> Callable[[Callable[[DictConfig], None]], Callable[[], None]]:
"""
A decorator to load the config file and merge it with the CLI options.
"""

def decorator(func: Callable[[T], None]) -> Callable[[], None]:
def decorator(func: Callable[[DictConfig], None]) -> Callable[[], None]:
@functools.wraps(func)
def wrapper() -> None:
config = OmegaConf.structured(config_class)

try:
config.merge_with(OmegaConf.load(config_file))
except FileNotFoundError:
logger.info(f"Config {config_file} not found, using CLI options only")

config.merge_with(OmegaConf.from_cli())

for config_file in config_files:
try:
config.merge_with(OmegaConf.load(config_file))
except FileNotFoundError: # noqa: PERF203
logger.info(f"Config {config_file} not found, using CLI options only")

missing = ", ".join(OmegaConf.missing_keys(config))
if missing:
logger.error(f"Missing configuration keys: {missing}")

0 comments on commit 794720e

Please sign in to comment.