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

config.py improvements based on code from dfint/search_offsets #82

Merged
merged 3 commits into from
Nov 7, 2024
Merged
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
16 changes: 11 additions & 5 deletions dfint64_patch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
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[T], *config_files: str) -> Callable[[Callable[[T], None]], Callable[[], None]]:
"""
A decorator to load the config file and merge it with the CLI options.

:param config_class: A class, which describes the possible configuration parameters.
Should be inherited from `omegaconf.DictConfig`.
:param config_files: Names of configs. The closer a file is to the end of the list, the higher its priority.
Default config should be the first in the list. CLI options have the highest priority.
"""

def decorator(func: Callable[[T], 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")
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")

config.merge_with(OmegaConf.from_cli())

Expand Down