simpleconfigfinder
This is a small package with utility functions to find and handle configuration files that are stored upstream from the code.
configfinder
ConfigNotFound
Bases: Exception
will be raised when the given keys for the sub-configuration do not exist in the configuration file
Source code in src\simpleconfigfinder\configfinder.py
13 14 15 16 |
|
combine_dictionaries
combine_dictionaries(dict_a, dict_b)
combine two dictionaries on a granular level. The entries of dict_a
always have priority over entries of dict_b
.
Caution
this function modifies the original dicitionaries. If this matters, use:
from copy import deepcopy
combine_dictionaries(dict_a, deepcopy(dict_b))
Parameters: |
|
---|
Returns: |
|
---|
Examples:
>>> combine_dictionaries({"a" : 1}, {"b" : 2})
{'b': 2, 'a': 1}
>>> combine_dictionaries({"a" : {"c" : 3}}, {"a" : {"c" : 4}})
{'a': {'c': 3}}
>>> combine_dictionaries({"a" : {"c" : 3}}, {"a" : {"e" : 5}})
{'a': {'e': 5, 'c': 3}}
>>> some_dictionary = {"a" : 1} # to show changes
>>> combine_dictionaries({"b" : 2}, some_dictionary)
{'a': 1, 'b': 2}
>>> some_dictionary # ATTENTION: modified
{'a': 1, 'b': 2}
>>> from copy import deepcopy
>>> some_dictionary = {"a" : 1}
>>> combine_dictionaries({"b" : 2}, deepcopy(some_dictionary))
{'a': 1, 'b': 2}
>>> some_dictionary
{'a': 1}
Source code in src\simpleconfigfinder\configfinder.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
config_finder
config_finder(
config_fname,
sub_config_keys=None,
raise_error=True,
additional_readers=None,
strategy="__main__",
)
goes upstream from the currently executed file and finds the file config_fname
and returns the sub_config_keys
Starts with the directory of the currently executed file (__main__.__file__
) and searches upstream.
For Jupyter Notebooks, os.path.abspath("")
will be returned instead.
Examples:
When configurations to the pyproject.toml like
[tool.some_tool.default_config]
some_key = "some_value"
Then you can get these values via
>>> config_finder("pyproject.toml", ["tool", "some_tool", "default_config"])
{'some_key': 'some_value'}
Parameters: |
|
---|
Returns: |
|
---|
Source code in src\simpleconfigfinder\configfinder.py
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
config_walker
config_walker(configuration_dictionary, sub_config_keys)
goes upstream from the currently executed file and finds the file config_fname
and returns the sub_config_keys
Parameters: |
|
---|
Example:
>>> config_walker({"a": {"b" : {"b1" : 1, "b2" : 2}}}, ["a", "b"])
{'b1': 1, 'b2': 2}
Source code in src\simpleconfigfinder\configfinder.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
configparser_to_dict
configparser_to_dict(configuration)
converts a configparser element (handling ini files) to a dictionary
Example:
>>> from configparser import ConfigParser
>>> cfg = ConfigParser()
>>> cfg.read_dict({ "a": {"a1": "1", "a2": "2"}})
>>> configparser_to_dict(cfg)
{'DEFAULT': {}, 'a': {'a1': '1', 'a2': '2'}}
Parameters: |
|
---|
Returns: |
|
---|
Source code in src\simpleconfigfinder\configfinder.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
find_file
find_file(config_fname, strategy='__main__')
finds the configuration file by checking every parent directory.
Strategy main:
Starts with the directory of the currently executed file (__main__.__file__
) and searches upstream.
For Jupyter Notebooks, os.path.abspath("")
will be returned instead.
Strategy cwd: starts with os.cwd()
Parameters: |
|
---|
Source code in src\simpleconfigfinder\configfinder.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
multi_config_finder
multi_config_finder(
config_fname,
sub_config_keys=None,
raise_error=True,
additional_readers=None,
strategy="__main__",
)
goes upstream from the currently executed file and finds the file config_fname
and returns the sub_config_keys
Starts with the directory of the currently executed file (__main__.__file__
) and searches upstream.
In case there are multiple configuration files provided and keys are in multiple of them, the first occurence will be returned. This function first combines all files and afterwards applies the sub_config_keys
Parameters: |
|
---|
Source code in src\simpleconfigfinder\configfinder.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|