-
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.
* Load toml configuration files * Masked configuration back in init
- Loading branch information
Showing
5 changed files
with
64 additions
and
17 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
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,34 @@ | ||
import datetime | ||
from typing import Any, Tuple | ||
|
||
from omegaconf import OmegaConf | ||
|
||
SUPPORTED_EXTENSIONS = ['yaml', 'json'] | ||
|
||
try: | ||
# since python 3.11 (tomllib is available) | ||
import toml | ||
|
||
class TomlDecoderPrimitive(toml.TomlDecoder): | ||
def load_value(self, v: str, strictly_valid: bool = True) -> Tuple[Any, str]: | ||
value, itype = super().load_value(v, strictly_valid) | ||
# convert date, datetime, time using isoformat() | ||
if itype in ('date', 'time'): | ||
itype = 'str' | ||
if isinstance(value, datetime.datetime): | ||
value = value.isoformat(' ') | ||
else: | ||
value = value.isoformat() | ||
return value, itype | ||
|
||
SUPPORTED_EXTENSIONS.append('toml') | ||
except ImportError: | ||
toml = None # type: ignore | ||
|
||
|
||
def read_configuration_file(path: str): | ||
"""Read a configuration file and return a configuration""" | ||
if path.endswith('.toml') and toml: | ||
config = toml.load(path, decoder=TomlDecoderPrimitive()) | ||
return OmegaConf.create(config) | ||
return OmegaConf.load(path) |
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 |
---|---|---|
|
@@ -5,9 +5,11 @@ black | |
flake8 | ||
isort | ||
mypy | ||
types-toml | ||
|
||
# Libraries | ||
omegaconf>=2 | ||
colorama | ||
python-dotenv | ||
invoke | ||
toml |
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 |
---|---|---|
|
@@ -41,5 +41,6 @@ | |
'color': ['colorama'], | ||
'dotenv': ['python-dotenv'], | ||
'invoke': ['invoke'], | ||
'toml': ['toml'], | ||
}, | ||
) |