Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Document the new method hooks #1095

Open
rochacbruno opened this issue May 7, 2024 · 0 comments
Open

[docs] Document the new method hooks #1095

rochacbruno opened this issue May 7, 2024 · 0 comments
Assignees
Labels
Docs Not a Bug Not a Problem, expected behavior

Comments

@rochacbruno
Copy link
Member

Add documentation for the feature added on #975

Dynaconf allows changing the wrapped class to allow method hooking.

from dynaconf import Dynaconf
from dynaconf.hooking import Action, Hook, HookableSettings

settings = Dynaconf(_wrapper_class=HookableSettings)

def lookup_for_key_in_cache_or_database(temp_settings, value,  key, default):
    if key.upper() not in [list of keys I am interested]:
        return value.value
    print(f"doing things to lookup key {key!r} on cache or db")
    new_value = find_in_cache(key) or query_db(key) or value 
    return new_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.

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

@rochacbruno rochacbruno added Not a Bug Not a Problem, expected behavior RFC Docs and removed RFC labels May 7, 2024
@rochacbruno rochacbruno self-assigned this May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Not a Bug Not a Problem, expected behavior
Projects
None yet
Development

No branches or pull requests

1 participant