* hooks is a package now.
authorLuca De Vitis <luca@monkeython.com>
Tue, 23 Aug 2011 21:30:50 +0000 (23:30 +0200)
committerLuca De Vitis <luca@monkeython.com>
Tue, 23 Aug 2011 21:30:50 +0000 (23:30 +0200)
* Added hooks.setup_hooks to setup multiple and default hooks
* Added hooks.response hooks to unicode encoding, and uncompress response.
* Modified hooks.dispatch_hooks api: don't need a key any more.

requests/api.py
requests/config.py
requests/hooks.py [deleted file]
requests/hooks/__init__.py [new file with mode: 0644]
requests/models.py

index 9acc1e5f8d0fbad6bbde139d09ce67e1bae5c1e7..9c78c17f04f42511c8377ada550d76abaebd431a 100644 (file)
@@ -14,7 +14,7 @@ This module impliments the Requests API.
 import config
 from .models import Request, Response, AuthObject
 from .status_codes import codes
-from .hooks import dispatch_hooks
+from hooks import setup_hooks, dispatch_hooks
 from .utils import cookiejar_from_dict
 
 from urlparse import urlparse
@@ -43,10 +43,9 @@ def request(method, url,
 
     method = str(method).upper()
 
-    if cookies is None:
-        cookies = {}
+    cookies = cookiejar_from_dict(cookies or dict())
 
-    cookies = cookiejar_from_dict(cookies)
+    hooks = setup_hooks(hooks or dict())
 
     args = dict(
         method = method,
@@ -61,23 +60,24 @@ def request(method, url,
         allow_redirects = allow_redirects,
         proxies = proxies or config.settings.proxies,
     )
+    
 
     # Arguments manipulation hook.
-    args = dispatch_hooks('args', hooks, args)
+    args = dispatch_hooks(hooks.get('args', []), args)
 
     r = Request(**args)
 
     # Pre-request hook.
-    r = dispatch_hooks('pre_request', hooks, r)
+    r = dispatch_hooks(hooks.get('pre_request', []), r)
 
     # Send the HTTP Request.
     r.send()
 
     # Post-request hook.
-    r = dispatch_hooks('post_request', hooks, r)
+    r = dispatch_hooks(hooks.get('post_request', []), hooks, r)
 
     # Response manipulation hook.
-    r.response = dispatch_hooks('response', hooks, r.response)
+    r.response = dispatch_hooks(hooks.get('response', []), r.response)
 
     return r.response
 
index 794109c5303f9b6fa5b315f054ffd4cbeba7df17..a92e1f577d5b8214a9aed58b35485d2d4b352b0c 100644 (file)
@@ -62,7 +62,9 @@ settings.proxies = None
 settings.verbose = None
 settings.timeout = None
 settings.max_redirects = 30
-settings.decode_unicode = True
+# settings.decode_unicode = True
+settings.unicode_response = True
+settings.decode_response = True
 
 #: Use socket.setdefaulttimeout() as fallback?
 settings.timeout_fallback = True
diff --git a/requests/hooks.py b/requests/hooks.py
deleted file mode 100644 (file)
index 2fa97d0..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-# -*- coding: utf-8 -*-
-
-"""
-requests.hooks
-~~~~~~~~~~~~~~
-
-This module provides the capabilities for the Requests hooks system.
-
-Available hooks:
-
-``args``:
-    A dictionary of the arguments being sent to Request().
-
-``pre_request``:
-    The Request object, directly before being sent.
-
-``post_request``:
-    The Request object, directly after being sent.
-
-``response``:
-    The response generated from a Request.
-
-"""
-
-import warnings
-from collections import Iterable
-
-def dispatch_hooks(key, hooks, hook_data):
-    """Dispatches multiple hooks on a given piece of data.
-
-    :param key: the hooks group to lookup
-    :type key: str
-    :param hooks: the hooks dictionary. The value of each key can be a callable
-                  object, or a list of callable objects.
-    :type hooks: dict
-    :param hook_data: the object on witch the hooks should be applied
-    :type hook_data: object
-    """
-    hook_list = hooks.get(key, []) if hooks else []
-    dispatching = hook_list if isinstance(hook_list, Iterable) else [hook_list]
-    for hook in dispatching:
-        try:
-            # hook must be a callable
-            hook_data = hook(hook_data)
-        except Exception, why:
-            warnings.warn(str(why))
-    return hook_data
-
diff --git a/requests/hooks/__init__.py b/requests/hooks/__init__.py
new file mode 100644 (file)
index 0000000..8f7c6b1
--- /dev/null
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+
+"""
+requests.hooks
+~~~~~~~~~~~~~~
+
+This module provides the capabilities for the Requests hooks system.
+
+Available hooks:
+
+``args``:
+    A dictionary of the arguments being sent to Request().
+
+``pre_request``:
+    The Request object, directly before being sent.
+
+``post_request``:
+    The Request object, directly after being sent.
+
+``response``:
+    The response generated from a Request.
+
+"""
+
+import warnings
+from collections import Iterable
+from .. import config
+from response import unicode_response, decode_response
+
+def setup_hooks(hooks):
+    """Setup hooks as a dictionary. Each value is a set of hooks."""
+
+    for key, values in hooks.items():
+        hook_list = values if isinstance(values, Iterable) else [values]
+        hooks[key] = set(hook_list) 
+
+    # Also, based on settings, 
+    if config.settings.unicode_response:
+        hooks.setdefault('response', set()).add(unicode_response)
+    if config.settings.decode_response:
+        hooks.setdefault('response', set()).add(decode_response)
+    return hooks
+
+def dispatch_hooks(hooks, hook_data):
+    """Dispatches multiple hooks on a given piece of data.
+
+    :param key: the hooks group to lookup
+    :type key: str
+    :param hooks: the hooks dictionary. The value of each key can be a callable
+                  object, or a list of callable objects.
+    :type hooks: dict
+    :param hook_data: the object on witch the hooks should be applied
+    :type hook_data: object
+    """
+    for hook in hooks:
+        try:
+            # hook must be a callable
+            hook_data = hook(hook_data)
+        except Exception, why:
+            warnings.warn(str(why))
+    return hook_data
index 5983c237d2b0f6ee1a813774b0d09849e5106b55..e199b3ca2d35a79710165d1005ef94e696bc2415 100644 (file)
@@ -444,22 +444,20 @@ class Response(object):
         (if available).
         """
 
-        if self._content is not None:
-            return self._content
-
-        # Read the contents.
-        self._content = self.fo.read()
-
-        # Decode GZip'd content.
-        if 'gzip' in self.headers.get('content-encoding', ''):
-            try:
-                self._content = decode_gzip(self._content)
-            except zlib.error:
-                pass
-
-        # Decode unicode content.
-        if settings.decode_unicode:
-            self._content = get_unicode_from_response(self)
+        if self._content is None:
+            # Read the contents.
+            self._content = self.fo.read()
+
+        # # Decode GZip'd content.
+        # if 'gzip' in self.headers.get('content-encoding', ''):
+        #     try:
+        #         self._content = decode_gzip(self._content)
+        #     except zlib.error:
+        #         pass
+
+        # # Decode unicode content.
+        # if settings.decode_unicode:
+        #     self._content = get_unicode_from_response(self)
 
         return self._content