-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise config loading to avoid using a singleton (#604)
* Do not create Config as a singleton This allows the config to be copied and passed around without being updated in-place whenever a new config is loaded * Make (re)create independent of global config * Remove dependency on global cfg from duqduq cli tools * Update path always
- Loading branch information
1 parent
94dabec
commit 6524a4a
Showing
10 changed files
with
84 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
from ._config import Config, cfg | ||
from ._config import Config, cfg, load_config | ||
from ._variables import lookup_vars, var_lookup | ||
|
||
__all__ = [ | ||
'cfg', | ||
'Config', | ||
'load_config', | ||
'lookup_vars', | ||
'var_lookup', | ||
] |
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 |
---|---|---|
@@ -1,32 +1,41 @@ | ||
"""Config class containing all configs, can be used with: | ||
from duqtools.config import cfg | ||
cfg.<variable you want> | ||
To update the config: | ||
load_config('duqtools.yaml') | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import Union | ||
|
||
from ..schema.cli import ConfigModel | ||
|
||
|
||
class Config(ConfigModel): | ||
"""Config class containing all configs, can be used with: | ||
... | ||
|
||
|
||
def load_config(path: Union[str, Path]) -> Config: | ||
global cfg | ||
|
||
new_cfg = Config.parse_file(path) | ||
|
||
from duqtools.config import cfg | ||
cfg.<variable you want> | ||
from ._variables import var_lookup | ||
|
||
To update the config: | ||
if new_cfg.extra_variables: | ||
var_lookup.update(new_cfg.extra_variables.to_variable_dict()) | ||
|
||
cfg.parse_file('duqtools.yaml') | ||
""" | ||
_instance = None | ||
cfg.__dict__.update(new_cfg.__dict__) | ||
|
||
def __new__(cls, *args, **kwargs): | ||
# Make it a singleton | ||
if not Config._instance: | ||
Config._instance = object.__new__(cls) | ||
return Config._instance | ||
for obj in (cfg, new_cfg): | ||
obj._path = path | ||
|
||
def parse_file(self, *args, **kwargs): | ||
"""Add extra variables to variable lookup table.""" | ||
super().parse_file(*args, **kwargs) | ||
from ._variables import var_lookup | ||
if self.extra_variables: | ||
var_lookup.update(self.extra_variables.to_variable_dict()) | ||
return new_cfg | ||
|
||
|
||
cfg = Config.construct() |
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
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
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
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