dyanconf API¶
dynaconf.DynaconfFormatError
¶
Bases: Exception
Error to raise when formatting a lazy variable fails
Source code in dynaconf/utils/parse_conf.py
35 36 |
|
dynaconf.DynaconfParseError
¶
Bases: Exception
Error to raise when parsing @casts
Source code in dynaconf/utils/parse_conf.py
39 40 |
|
dynaconf.FlaskDynaconf
¶
The arguments are. app = The created app dynaconf_args = Extra args to be passed to Dynaconf (validator for example)
All other values are stored as config vars specially::
ENVVAR_PREFIX_FOR_DYNACONF = env prefix for your envvars to be loaded
example:
if you set to `MYSITE` then
export MYSITE_SQL_PORT='@int 5445'
with that exported to env you access using:
app.config.SQL_PORT
app.config.get('SQL_PORT')
app.config.get('sql_port')
# get is case insensitive
app.config['SQL_PORT']
Dynaconf uses `@int, @bool, @float, @json` to cast
env vars
SETTINGS_FILE_FOR_DYNACONF = The name of the module or file to use as
default to load settings. If nothing is
passed it will be `settings.*` or value
found in `ENVVAR_FOR_DYNACONF`
Dynaconf supports
.py, .yml, .toml, ini, json
Take a look at settings.yml
and .secrets.yml
to know the
required settings format.
Settings load order in Dynaconf:
- Load all defaults and Flask defaults
- Load all passed variables when applying FlaskDynaconf
- Update with data in settings files
- Update with data in environment vars
ENVVAR_FOR_DYNACONF_
TOML files are very useful to have envd
settings, lets say,
production
and development
.
You can also achieve the same using multiple .py
files naming as
settings.py
, production_settings.py
and development_settings.py
(see examples/validator)
Example::
app = Flask(__name__)
FlaskDynaconf(
app,
ENV='MYSITE',
SETTINGS_FILE='settings.yml',
EXTRA_VALUE='You can add additional config vars here'
)
Take a look at examples/flask in Dynaconf repository
Source code in dynaconf/contrib/flask_dynaconf.py
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 |
|
dynaconf.FlaskDynaconf.__init__(app=None, instance_relative_config=False, dynaconf_instance=None, extensions_list=False, **kwargs)
¶
kwargs holds initial dynaconf configuration
Source code in dynaconf/contrib/flask_dynaconf.py
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 |
|
dynaconf.FlaskDynaconf.init_app(app, **kwargs)
¶
kwargs holds initial dynaconf configuration
Source code in dynaconf/contrib/flask_dynaconf.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
dynaconf.LazySettings
¶
Bases: LazyObject
Loads settings lazily from multiple sources:
settings = Dynaconf(
settings_files=["settings.toml"], # path/glob
environments=True, # activate layered environments
envvar_prefix="MYAPP", # `export MYAPP_FOO=bar`
env_switcher="MYAPP_MODE", # `export MYAPP_MODE=production`
load_dotenv=True, # read a .env file
)
More options available on https://www.dynaconf.com/configuration/
Source code in dynaconf/base.py
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 |
|
dynaconf.LazySettings.configured
property
¶
If wrapped is configured
dynaconf.LazySettings.__call__(*args, **kwargs)
¶
Allow direct call of settings('val') in place of settings.get('val')
Source code in dynaconf/base.py
150 151 152 153 154 |
|
dynaconf.LazySettings.__getattr__(name)
¶
Allow getting keys from self.store using dot notation
Source code in dynaconf/base.py
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 |
|
dynaconf.LazySettings.__init__(wrapped=None, **kwargs)
¶
handle initialization for the customization cases
:param wrapped: a deepcopy of this object will be wrapped (issue #596) :param kwargs: values that overrides default_settings
Source code in dynaconf/base.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
dynaconf.LazySettings.__resolve_config_aliases(kwargs)
¶
takes aliases for _FOR_DYNACONF configurations
e.g: ROOT_PATH='/' is transformed into ROOT_PATH_FOR_DYNACONF
Source code in dynaconf/base.py
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 |
|
dynaconf.LazySettings.configure(settings_module=None, **kwargs)
¶
Allows user to reconfigure settings object passing a new settings module or separated kwargs
:param settings_module: defines the settings file :param kwargs: override default settings
Source code in dynaconf/base.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
dynaconf.ValidationError
¶
Bases: Exception
Raised when a validation fails
Source code in dynaconf/validator.py
28 29 30 31 32 33 34 |
|
dynaconf.Validator
¶
Validators are conditions attached to settings variables names or patterns::
Validator('MESSAGE', must_exist=True, eq='Hello World')
The above ensure MESSAGE is available in default env and is equal to 'Hello World'
names
are a one (or more) names or patterns::
Validator('NAME')
Validator('NAME', 'OTHER_NAME', 'EVEN_OTHER')
Validator(r'^NAME', r'OTHER./*')
The operations
are::
eq: value == other
ne: value != other
gt: value > other
lt: value < other
gte: value >= other
lte: value <= other
is_type_of: isinstance(value, type)
is_in: value in sequence
is_not_in: value not in sequence
identity: value is other
cont: contain value in
len_eq: len(value) == other
len_ne: len(value) != other
len_min: len(value) > other
len_max: len(value) < other
env
is which env to be checked, can be a list or
default is used.
when
holds a validator and its return decides if validator runs or not::
Validator('NAME', must_exist=True, when=Validator('OTHER', eq=2))
# NAME is required only if OTHER eq to 2
# When the very first thing to be performed when passed.
# if no env is passed to `when` it is inherited
must_exist
is alias to required
requirement. (executed after when)::
settings.get(value, empty) returns non empty
condition is a callable to be executed and return boolean::
Validator('NAME', condition=lambda x: x == 1) # it is executed before operations.
Source code in dynaconf/validator.py
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 |
|
dynaconf.Validator.validate(settings, only=None, exclude=None, only_current_env=False)
¶
Raise ValidationError if invalid
Source code in dynaconf/validator.py
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 |
|
dynaconf.add_converter(converter_key, func)
¶
Adds a new converter to the converters dict
Source code in dynaconf/utils/parse_conf.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
dynaconf.get_history(obj, key=None, *, filter_callable=None, include_internal=False, history_limit=None)
¶
Gets data from settings.loaded_by_loaders
in order of loading with
optional filtering options.
Returns a list of dict in new-first order, where the dict contains the data and it's source metadata.
:param obj: Setting object which contain the data :param key: Key path to desired key. Use all if not provided :param filter_callable: Takes SourceMetadata and returns a boolean :param include_internal: If True, include internal loaders (e.g. defaults). This has effect only if key is not provided. history_limit: limits how many entries are shown
Example
settings = Dynaconf(...) _get_history(settings) [ { "loader": "yaml" "identifier": "path/to/file.yml" "env": "default" "data": {"foo": 123, "spam": "eggs"} }, ... ]
Source code in dynaconf/utils/inspect.py
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 |
|
dynaconf.inspect_settings(settings, key=None, env=None, *, new_first=True, history_limit=None, include_internal=False, to_file=None, print_report=False, dumper=None, report_builder=None)
¶
Print and return the loading history of a settings object.
Optional arguments must be provided as kwargs.
:param settings: A Dynaconf instance :param key: String dotted path. E.g "path.to.key" :param env: Filter by this env
:param new_first: If True, uses newest to oldest loading order
:param history_limit: Limits how many entries are shown
:param include_internal: If True, include internal loaders (e.g. defaults).
This has effect only if key is not provided.
:param to_file: If specified, write to this filename
:param print_report: If true, prints the dumped report to stdout
:param dumper: Accepts preset strings (e.g. "yaml", "json") or custom
dumper callable (dict, TextIO) -> None
. Defaults to "yaml"
:param report_builder: if provided, it is used to generate the report
:return: Dict with a dict containing report data :rtype: dict
Source code in dynaconf/utils/inspect.py
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 |
|