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
Provenance
- Commit
d8fb6d2- Tree
bb713b1- Author
Luca De Vitis <luca@monkeython.com>- Committer
Luca De Vitis <luca@monkeython.com>- Parent
c243d09
1 file changed+52 added−17 removed
modified:requests/hooks/__init__.py+52 added-17 removed
23 unmodified lines24 25 import warnings26 from collections import Iterable27 from .. import config28-from response import unicode_response, decode_response28+from . import args29+from . import pre_request30+from . import post_request31+from . import response32 30-def setup_hooks(hooks):31- """Setup hooks as a dictionary. Each value is a set of hooks."""33+def setup_hooks(supplied):34+ """Setup the supplied dictionary of hooks.35+ Each value is a list of hooks and will extend **default_hooks**.36 33- 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+ """43+44+ # Copy the default hooks settings.45+ dispatching = dict([(k, v[:]) for k, v in config.settings.default_hooks])46+47+ # 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)53 37- # 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)58 59+ 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)67+68+ return dispatching69+44-def dispatch_hooks(hooks, hook_data):70+def dispatch_hooks(hooks, data):71 """Dispatches multiple hooks on a given piece of data.72 73 :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)85+86 except Exception, why:87+88+ # 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: raise93+94 warnings.warn(str(why))95+61- return hook_data96+ return data