Skip to content
Myr

No more sets in default hooks, code cleanup, more docs and comments.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Luca De Vitis · committed by Luca De Vitis · parent c243d09 · Browse files · 1 file+52 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+52 added−17 removed

  • other:+52 added−17 removed

1 file+52 added−17 removed

modified:requests/hooks/__init__.py+52 added-17 removed
23 unmodified lines
2425import warnings26from collections import Iterable27from .. import config28from response import unicode_response, decode_response28from . import args29from . import pre_request30from . import post_request31from . import response3230def setup_hooks(hooks):31 """Setup hooks as a dictionary. Each value is a set of hooks."""33def setup_hooks(supplied):34 """Setup the supplied dictionary of hooks.35 Each value is a list of hooks and will extend **default_hooks**.3633 for key, values in hooks.items():37 :param supplied: a dictionary of hooks. Each value can either be a callable38 or a list of callables.39 :type supplied: dict40 :returns: a dictionary of hooks that extends the **default_hooks** dictionary.41 :rtype: dict42 """4344 # Copy the default hooks settings.45 dispatching = dict([(k, v[:]) for k, v in config.settings.default_hooks])4647 # I abandoned the idea of a dictionary of sets because sets may not keep48 # insertion order, while it may be important. Also, there is no real reason49 # to force hooks to run once.50 for hooks, values in supplied.items():51 hook_list = values if isinstance(values, Iterable) else [values]35 hooks[key] = set(hook_list) 52 dispatching[hooks].extends(hook_list)5337 # Also, based on settings, 38 if config.settings.unicode_response:39 hooks.setdefault('response', set()).add(unicode_response)40 if config.settings.decode_response:41 hooks.setdefault('response', set()).add(decode_response)42 return hooks54 # If header is set, maybe response is encoded. Whatever hook you want to55 # run on response, content decoding should be first.56 if config.settings.base_headers.get('Accept-Encoding', ''):57 dispatching['response'].insert(0, response.decode_encoding)5859 if config.settings.decode_unicode:60 try:61 # Try unicode encoding just after content decoding...62 index = dispatching['response'].index(response.decode_encoding) + 163 except ValueError:64 # ... Or as first hook65 index = 066 dispatching['response'].insert(index, response.decode_unicode)6768 return dispatching6944def dispatch_hooks(hooks, hook_data):70def dispatch_hooks(hooks, data):71 """Dispatches multiple hooks on a given piece of data.7273 :param key: the hooks group to lookup74 :type key: str75 :param hooks: the hooks dictionary. The value of each key can be a callable76 object, or a list of callable objects.77 :type hooks: dict52 :param hook_data: the object on witch the hooks should be applied53 :type hook_data: object78 :param data: the object on witch the hooks should be applied79 :type data: object80 """81 for hook in hooks:82 try:57 # hook must be a callable58 hook_data = hook(hook_data)83 # hook must be a callable.84 data = hook(data)8586 except Exception, why:8788 # Letting users to choose a policy may be an idea. It can be as89 # simple as "be gracefull, or not":90 #91 # config.settings.gracefull_hooks = True | False92 if not config.settings.gracefull_hooks: raise9394 warnings.warn(str(why))9561 return hook_data96 return data