config
darts.utils.config
¶
Utility functions for parsing and handling configuration files.
ConfigParser
¶
Parser for cyclopts config.
An own implementation is needed to select our own toml structure and source. Implemented as a class to be able to provide the config-file as a parameter of the CLI.
Initialize the ConfigParser (no-op).
Source code in darts/src/darts/utils/config.py
__call__
¶
__call__(
apps: list[cyclopts.App],
commands: tuple[str, ...],
arguments: cyclopts.ArgumentCollection,
)
Parser for cyclopts config. An own implementation is needed to select our own toml structure.
First, the configuration file at "config.toml" is loaded. Then, this config is flattened and then mapped to the input arguments of the called function. Hence parent keys are not considered.
Parameters:
-
apps
(list[cyclopts.App]
) –The cyclopts apps. Unused, but must be provided for the cyclopts hook.
-
commands
(tuple[str, ...]
) –The commands. Unused, but must be provided for the cyclopts hook.
-
arguments
(cyclopts.ArgumentCollection
) –The arguments to apply the config to.
Examples:
Setup the cyclopts App¶
import cyclopts
from darts.utils.config import ConfigParser
config_parser = ConfigParser()
app = cyclopts.App(config=config_parser)
# Intercept the logging behavior to add a file handler
@app.meta.default
def launcher(
*tokens: Annotated[str, cyclopts.Parameter(show=False, allow_leading_hyphen=True)],
log_dir: Path = Path("logs"),
config_file: Path = Path("config.toml"),
):
command, bound, _ = app.parse_args(tokens)
add_logging_handlers(command.__name__, console, log_dir)
return command(*bound.args, **bound.kwargs)
if __name__ == "__main__":
app.meta()
Usage¶
Config file ./config.toml
:
Function signature which is called:
Calling the function from CLI:
Source code in darts/src/darts/utils/config.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
apply_config
¶
Apply the loaded config to the cyclopts mapping.
Parameters:
-
arguments
(cyclopts.ArgumentCollection
) –The arguments to apply the config to.
Source code in darts/src/darts/utils/config.py
open_config
¶
Open the config file, takes the 'darts' key, flattens the resulting dict and saves as config.
Parameters:
Source code in darts/src/darts/utils/config.py
flatten_dict
¶
Flatten a nested dictionary.
Parameters:
-
d
(dict
) –The dictionary to flatten.
-
parent_key
(str
, default:''
) –The parent key. Defaults to "".
-
sep
(str
, default:'.'
) –The separator. Defaults to ".".
Returns:
-
dict[str, dict[str, str]]
–dict[str, dict[str, str]]: The flattened dictionary. Key is the original key, value is a dictionary with the value and a concatenated key to save parents.
Examples:
>>> d = {
>>> "a": 1,
>>> "b": {
>>> "c": 2,
>>> },
>>> }
>>> print(flatten_dict(d))
{
"a": {"value": 1, "key": "a"},
"c": {"value": 2, "key": "b.c"},
}