From 77f4b8a75e804b83cca7c917a16bec93fdd6b302 Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 21 Apr 2011 17:56:25 +0200 Subject: [PATCH] Implement settings context manager --- requests/__init__.py | 25 +++++++++++++++++++++++++ requests/core.py | 33 +++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/requests/__init__.py b/requests/__init__.py index d44b266..cf171f5 100644 --- a/requests/__init__.py +++ b/requests/__init__.py @@ -1,6 +1,31 @@ # -*- coding: utf-8 -*- +import inspect + import packages from core import * from core import __version__ + +timeout = None + +class settings: + """Context manager for settings.""" + + cache = {} + + def __init__(self, timeout): + self.module = inspect.getmodule(self) + + # Cache settings + self.cache['timeout'] = self.module.timeout + + self.module.timeout = timeout + + def __enter__(self): + pass + + def __exit__(self, type, value, traceback): + # Restore settings + for key in self.cache: + setattr(self.module, key, self.cache[key]) diff --git a/requests/core.py b/requests/core.py index 6408afc..25becc4 100644 --- a/requests/core.py +++ b/requests/core.py @@ -12,6 +12,7 @@ from __future__ import absolute_import +import requests import urllib import urllib2 import socket @@ -24,7 +25,6 @@ from .packages.poster.encode import multipart_encode from .packages.poster.streaminghttp import register_openers, get_handlers - __title__ = 'requests' __version__ = '0.3.2' __build__ = 0x000302 @@ -39,7 +39,6 @@ __all__ = [ ] - class _Request(urllib2.Request): """Hidden wrapper around the urllib2.Request object. Allows for manual setting of HTTP methods. @@ -446,19 +445,20 @@ def request(method, url, **kwargs): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload. :param auth: (optional) AuthObject to enable Basic HTTP Auth. + :param timeout: (optional) Float describing the timeout of the request. """ data = kwargs.pop('data', dict()) or kwargs.pop('params', dict()) - + r = Request(method=method, url=url, data=data, headers=kwargs.pop('headers', {}), cookiejar=kwargs.pop('cookies', None), files=kwargs.pop('files', None), auth=kwargs.pop('auth', auth_manager.get_auth(url)), - timeout=kwargs.pop('timeout', None)) + timeout=kwargs.pop('timeout', requests.timeout)) r.send() return r.response -def get(url, params={}, headers={}, cookies=None, auth=None, timeout=None): +def get(url, params={}, headers={}, cookies=None, auth=None, **kwargs): """Sends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -466,12 +466,13 @@ def get(url, params={}, headers={}, cookies=None, auth=None, timeout=None): :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. + :param timeout: (optional) Float describing the timeout of the request. """ - return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout) + return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs) -def head(url, params={}, headers={}, cookies=None, auth=None, timeout=None): +def head(url, params={}, headers={}, cookies=None, auth=None, **kwargs): """Sends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -479,12 +480,13 @@ def head(url, params={}, headers={}, cookies=None, auth=None, timeout=None): :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. + :param timeout: (optional) Float describing the timeout of the request. """ - return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout) + return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs) -def post(url, data={}, headers={}, files=None, cookies=None, auth=None, timeout=None): +def post(url, data={}, headers={}, files=None, cookies=None, auth=None, **kwargs): """Sends a POST request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -493,12 +495,13 @@ def post(url, data={}, headers={}, files=None, cookies=None, auth=None, timeout= :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload. :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. + :param timeout: (optional) Float describing the timeout of the request. """ - return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout) + return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs) -def put(url, data='', headers={}, files={}, cookies=None, auth=None, timeout=None): +def put(url, data='', headers={}, files={}, cookies=None, auth=None, **kwargs): """Sends a PUT request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -507,12 +510,13 @@ def put(url, data='', headers={}, files={}, cookies=None, auth=None, timeout=Non :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload. :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. + :param timeout: (optional) Float describing the timeout of the request. """ - return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout) + return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs) -def delete(url, params={}, headers={}, cookies=None, auth=None, timeout=None): +def delete(url, params={}, headers={}, cookies=None, auth=None, **kwargs): """Sends a DELETE request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -520,9 +524,10 @@ def delete(url, params={}, headers={}, cookies=None, auth=None, timeout=None): :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`. :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. + :param timeout: (optional) Float describing the timeout of the request. """ - return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout) + return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs) -- 2.7.4