A python library for handling code-declared and file-defined settings.
Supports saving to JSON and YAML files.
the file handlers have native support for these types and are used as is, no conversion is done on these values
- string
- int
- float
- bool
- None / null
converted using
- serialize:
{VAR}.to_dict()
- de-serialize:
Badger_Config_Section.from_dict({VAR})
converted using
- serialize:
{VAR}.isoformat()
- de-serialize:
datetime.fromisoformat({VAR})
converted using
- serialize:
str({VAR})
- de-serialize:
pathlib.Path({VAR})
NOTE:
It is recommended to use a Config Section instead of Collections.
If collections are used items should be of native type only, if they are not of native type they are serialized but can NOT be de-serialize.
Code using these values must handle these cases.
path to the config file
all allowed file extensions
Save config to file
Load config from file
Sync config with file
runs: load()
- save()
- load()
this adds new config fields to the file or removes old ones
name of the current section
by default the project root path or overridden by the parent section
reference to the parent section (if it exists)
Replacement for __init__()
should be used to set default values
NOTE: the property parent_section is NOT available during this
Pre process values before save()
useful for:
- converting unsupported data type to a native or supported type
- converting absolute paths to relative (keeps them short in the config file)
post process values after load()
useful for:
- creating unsupported data type from native or supported type
- converting relative paths to absolute (keeps them short in the config file)
converts all values to a dictionary
Parameters:
param | type | required | default |
---|---|---|---|
convert_to_native | bool | True |
Reconstruct Config Section from dictionary
Parameters:
param | type | description | required | default |
---|---|---|---|---|
data | dict[str, native] | the dict representation of this section (as generated from to_dict(True) ) | x | |
safe_load | bool | ! UNTESTED ! True -> Only variables that already exist in the class are set (uses hasattr ) False -> New variables can be set from config file |
True | |
danger_convert | bool | ! UNTESTED ! For details see docs of _native_to_var() |
False |