You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dynaconf allows changing the wrapped class to allow method hooking.
fromdynaconfimportDynaconffromdynaconf.hookingimportAction, Hook, HookableSettingssettings=Dynaconf(_wrapper_class=HookableSettings)
deflookup_for_key_in_cache_or_database(temp_settings, value, key, default):
ifkey.upper() notin [listofkeysIaminterested]:
returnvalue.valueprint(f"doing things to lookup key {key!r} on cache or db")
new_value=find_in_cache(key) orquery_db(key) orvaluereturnnew_value# NOTE: if value is a mergeable data structure, then the temp_settings might be used.# temp_settings.set(key, new_value)# return temp_settings.get(key, value.value) settings["_registered_hooks"][Action.AFTER_GET] = [lookup_for_key_in_cache_or_database]
When settings["_registered_hooks"] is set as a dictionary, keyed by Action.AFTER_GET|BEFORE_GET|etc
at any access to settings.* dynaconf after performing its usual get process, will call the hook functions, chained in sequence, passing down the value from one hook to another, this way the latest hook in the list is the final value.
Each hook function may do a conditional check (that is why we do if key in list_of_interesting_keys) and if the key is not interesting for the hook it may return value to be passed to the next hook unaltered.
If key is the key that the hook wants, then it can alter it and return the altered value (which will be passed to the next hook in the pipeline) or can return a EagerValue(thing) that will avoid repassing to the next and just return as the final value.
In our case, I think we want to maintain a list of enabled hooks, and if there are none registered, we can just bypass its configuration.
This all could be registered on app/settings.py directly, or in special cases using dynaconf_hooks.py
The text was updated successfully, but these errors were encountered:
Add documentation for the feature added on #975
Dynaconf allows changing the wrapped class to allow method hooking.
When
settings["_registered_hooks"]
is set as a dictionary, keyed byAction.AFTER_GET|BEFORE_GET|etc
at any access to
settings.*
dynaconf after performing its usual get process, will call the hook functions, chained in sequence, passing down the value from one hook to another, this way the latest hook in the list is the final value.alter_settings_from_request(lookup_for_key_in_cache_or_database(do_another_thing(value, **kwargs)))
Each hook function may do a conditional check (that is why we do if key in list_of_interesting_keys) and if the key is not interesting for the hook it may return value to be passed to the next hook unaltered.
If key is the key that the hook wants, then it can alter it and return the altered value (which will be passed to the next hook in the pipeline) or can return a EagerValue(thing) that will avoid repassing to the next and just return as the final value.
In our case, I think we want to maintain a list of enabled hooks, and if there are none registered, we can just bypass its configuration.
This all could be registered on app/settings.py directly, or in special cases using dynaconf_hooks.py
The text was updated successfully, but these errors were encountered: