-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use omegaconf to configure the utility
- Loading branch information
Showing
6 changed files
with
164 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import functools | ||
from collections.abc import Callable | ||
|
||
from loguru import logger | ||
from omegaconf import DictConfig, OmegaConf | ||
|
||
|
||
def with_config(config_class: type, config_file: 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[[DictConfig], None]) -> Callable[[], None]: | ||
@functools.wraps(func) | ||
def wrapper() -> None: | ||
config = OmegaConf.structured(config_class) | ||
|
||
config.merge_with(OmegaConf.from_cli()) | ||
try: | ||
config.merge_with(OmegaConf.load(config_file)) | ||
except FileNotFoundError: | ||
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}") | ||
return | ||
func(config) | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters