Simple TOML Configurator Docs¶
ConfigObject
¶
Represents a configuration object that wraps a dictionary and provides attribute access.
Any key in the dictionary can be accessed as an attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table | dict | The dictionary representing the configuration. | required |
Attributes:
Name | Type | Description |
---|---|---|
_table | dict | The internal dictionary representing the configuration. |
Source code in src/simple_toml_configurator/toml_configurator.py
__setattr__(__name, __value)
¶
Update the table value when an attribute is set
Source code in src/simple_toml_configurator/toml_configurator.py
Configuration
¶
Class to set our configuration values we can use around in our app. The configuration is stored in a toml file as well as on the instance.
Examples:
>>> default_config = {
"app": {
"ip": "0.0.0.0",
"host": "",
"port": 5000,
"upload_folder": "uploads"
}
}
>>> import os
>>> from simple_toml_configurator import Configuration
>>> settings = Configuration("config", default_config, "app_config")
{'app': {'ip': '0.0.0.0', 'host': '', 'port': 5000, 'upload_folder': 'uploads'}}
# Update the config dict directly
>>> settings.app.ip = "1.1.1.1"
>>> settings.update()
>>> settings.app.ip
'1.1.1.1'
>>> settings.get_settings()
{'app_ip': '1.1.1.1', 'app_host': '', 'app_port': 5000, 'app_upload_folder': 'uploads'}
>>> settings.update_config({"app_ip":"1.2.3.4"})
>>> settings.app_ip
'1.2.3.4'
>>> settings.config.get("app").get("ip")
'1.2.3.4'
>>> settings.config["app"]["ip"] = "0.0.0.0"
>>> settings.update()
>>> settings.app_ip
'0.0.0.0'
>>> print(os.environ.get("APP_UPLOAD_FOLDER"))
'uploads'
Attributes:
Name | Type | Description |
---|---|---|
config | TOMLDocument | TOMLDocument with all config values |
config_path | str | Path | Path to config folder |
config_file_name | str | Name of the config file |
defaults | dict[str, dict] | Dictionary with all default values for your application |
Info
Changing a table name in your defaults
will remove the old table in config including all keys and values.
Note
If updating an attribute needs extra logic, make a custom class that inherits from this class and add property attributes with a getter and setter.
Example:
from simple_toml_configurator import Configuration
from utils import configure_logging
class CustomConfiguration(Configuration):
def __init__(self):
super().__init__()
@property
def logging_debug(self):
return getattr(self, "_logging_debug")
@logging_debug.setter
def logging_debug(self, value: bool):
if not isinstance(value, bool):
raise ValueError(f"value must be of type bool not {type(value)}")
self._logging_debug = value
log_level = "DEBUG" if value else "INFO"
configure_logging(log_level)
logger.debug(f"Debug logging set to {value}")
defaults = {
"logging": {
"debug": False
...
}
config_path = os.environ.get("CONFIG_PATH", "config")
settings = CustomConfiguration()
settings.init_config(config_path, defaults, "app_config"))
Source code in src/simple_toml_configurator/toml_configurator.py
18 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 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 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 242 243 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
|
get_envs()
¶
Get all the environment variables we set as a dictionary.
Examples:
```pycon
defaults = {...} settings = Configuration() settings.init_config("config", defaults, "app_config")) settings.get_envs() {'PREFIX_APP_IP': '0.0.0.0'}
Returns:
Type | Description |
---|---|
dict[str, Any] | dict[str, Any]: Dictionary with all environment variables name as keys and their value. |
Source code in src/simple_toml_configurator/toml_configurator.py
get_settings()
¶
Get all config key values as a dictionary.
Dict keys are formatted as: table_key
:
Examples:
>>> defaults = {...}
>>> settings = Configuration()
>>> settings.init_config("config", defaults, "app_config"))
>>> settings.get_settings()
{'app_ip': '0.0.0.0', 'app_host': '', 'app_port': 5000, 'app_upload_folder': 'uploads'}
Returns:
Type | Description |
---|---|
dict[str, Any] | dict[str, Any]: Dictionary with config key values. |
Source code in src/simple_toml_configurator/toml_configurator.py
init_config(config_path, defaults, config_file_name='config', env_prefix='')
¶
Creates the config folder and toml file if needed.
Upon init it will add any new/missing values/tables from defaults
into the existing TOML config. Removes any old values/tables from self.config
that are not in self.defaults
.
Sets all config keys as attributes on the class. e.g. self.table.key
, self.table_key
and self._table_key
Env variables of all config keys are set as uppercase. e.g. APP_HOST
and APP_PORT
or APP_CONFIG_APP_HOST
and APP_CONFIG_APP_PORT
if env_prefix
is set to "app_config".
Nested tables are set as nested environment variables. e.g. APP_CONFIG_APP_DATABASES_PROD
and APP_CONFIG_APP_DATABASES_DEV
.
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path | str | Path | Path to config folder | required |
defaults | dict[str, dict] | Dictionary with all default values for your application | required |
config_file_name | str | Name of the config file. Defaults to "config". | 'config' |
env_prefix | str | Prefix for environment variables. Defaults to "". | '' |
Returns:
Type | Description |
---|---|
TOMLDocument | dict[str,Any]: Returns a TOMLDocument. |
Source code in src/simple_toml_configurator/toml_configurator.py
update()
¶
Write the current config to file.
Examples:
>>> from simple_toml_configurator import Configuration
>>> settings = Configuration()
>>> defaults = {"mysql": {"databases": {"prod":"prod_db1", "dev":"dev_db1"}}}
>>> settings.init_config("config", defaults, "app_config")
>>> settings.mysql.databases.prod = "prod_db2"
>>> settings.update()
>>> settings.config["mysql"]["databases"]["prod"]
'prod_db2'
>>> settings.config["mysql"]["databases"]["prod"] = "prod_db3"
>>> settings.update()
>>> settings.mysql_databases["prod"]
'prod_db3'
Source code in src/simple_toml_configurator/toml_configurator.py
update_config(settings)
¶
Update all config values from a dictionary, set new attribute values and write the config to file. Use the same format as self.get_settings()
returns to update the config.
Examples:
>>> defaults = {"mysql": {"databases": {"prod":"prod_db1", "dev":"dev_db1"}}}
>>> settings = Configuration()
>>> settings.init_config("config", defaults, "app_config")
>>> settings.update_config({"mysql_databases": {"prod":"prod_db1", "dev":"dev_db2"}})
print(settings.mysql_databases["dev"])
'dev_db2'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings | dict | Dict with key values | required |