From: Locker537 Date: Thu, 16 Aug 2012 21:33:27 +0000 (-0400) Subject: Whitespace fixes following PEP8. X-Git-Tag: v0.13.7~6^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=42d0a2169eb894df0fc3c3788925bfd107765597;p=services%2Fpython-requests.git Whitespace fixes following PEP8. --- diff --git a/requests/api.py b/requests/api.py index f192b8f..ded7935 100644 --- a/requests/api.py +++ b/requests/api.py @@ -14,6 +14,7 @@ This module implements the Requests API. from . import sessions from .safe_mode import catch_exceptions_if_in_safe_mode + @catch_exceptions_if_in_safe_mode def request(method, url, **kwargs): """Constructs and sends a :class:`Request `. @@ -52,6 +53,7 @@ def request(method, url, **kwargs): if adhoc_session: session.close() + def get(url, **kwargs): """Sends a GET request. Returns :class:`Response` object. diff --git a/requests/auth.py b/requests/auth.py index 6aee69b..38dd874 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -34,6 +34,7 @@ log = logging.getLogger(__name__) CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded' + def _basic_auth_str(username, password): """Returns a Basic Auth string.""" @@ -239,6 +240,7 @@ class HTTPDigestAuth(AuthBase): r.register_hook('response', self.handle_401) return r + def _negotiate_value(r): """Extracts the gssapi authentication token from the appropriate header""" @@ -252,6 +254,7 @@ def _negotiate_value(r): return None + class HTTPKerberosAuth(AuthBase): """Attaches HTTP GSSAPI/Kerberos Authentication to the given Request object.""" def __init__(self, require_mutual_auth=True): diff --git a/requests/compat.py b/requests/compat.py index 201da3a..d701203 100644 --- a/requests/compat.py +++ b/requests/compat.py @@ -112,4 +112,3 @@ elif is_py3: bytes = bytes basestring = (str,bytes) numeric_types = (int, float) - diff --git a/requests/cookies.py b/requests/cookies.py index 0415856..bd2d665 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -14,6 +14,7 @@ try: except ImportError: import dummy_threading as threading + class MockRequest(object): """Wraps a `requests.Request` to mimic a `urllib2.Request`. @@ -66,6 +67,7 @@ class MockRequest(object): def get_new_headers(self): return self._new_headers + class MockResponse(object): """Wraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`. @@ -86,6 +88,7 @@ class MockResponse(object): def getheaders(self, name): self._headers.getheaders(name) + def extract_cookies_to_jar(jar, request, response): """Extract the cookies from the response into a CookieJar. @@ -99,12 +102,14 @@ def extract_cookies_to_jar(jar, request, response): res = MockResponse(response._original_response.msg) jar.extract_cookies(res, req) + def get_cookie_header(jar, request): """Produce an appropriate Cookie header string to be sent with `request`, or None.""" r = MockRequest(request) jar.add_cookie_header(r) return r.get_new_headers().get('Cookie') + def remove_cookie_by_name(cookiejar, name, domain=None, path=None): """Unsets a cookie by name, by default over all domains and paths. @@ -120,10 +125,12 @@ def remove_cookie_by_name(cookiejar, name, domain=None, path=None): for domain, path, name in clearables: cookiejar.clear(domain, path, name) + class CookieConflictError(RuntimeError): - """There are two cookies that meet the criteria specified in the cookie jar. + """There are two cookies that meet the criteria specified in the cookie jar. Use .get and .set and include domain and path args in order to be more specific.""" + class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): """Compatibility class; is a cookielib.CookieJar, but exposes a dict interface. @@ -181,7 +188,7 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): for cookie in iter(self): values.append(cookie.value) return values - + def items(self): """Dict-like items() that returns a list of name-value tuples from the jar. See keys() and values(). Allows client-code to call "dict(RequestsCookieJar) @@ -215,14 +222,14 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): if cookie.domain is not None and cookie.domain in domains: return True domains.append(cookie.domain) - return False # there is only one domain in jar + return False # there is only one domain in jar def get_dict(self, domain=None, path=None): """Takes as an argument an optional domain and path and returns a plain old Python dict of name-value pairs of cookies that meet the requirements.""" dictionary = {} for cookie in iter(self): - if (domain == None or cookie.domain == domain) and (path == None + if (domain == None or cookie.domain == domain) and (path == None or cookie.path == path): dictionary[cookie.name] = cookie.value return dictionary @@ -244,7 +251,7 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): remove_cookie_by_name(self, name) def _find(self, name, domain=None, path=None): - """Requests uses this method internally to get cookie values. Takes as args name + """Requests uses this method internally to get cookie values. Takes as args name and optional domain and path. Returns a cookie.value. If there are conflicting cookies, _find arbitrarily chooses one. See _find_no_duplicates if you want an exception thrown if there are conflicting cookies.""" @@ -257,18 +264,18 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path)) def _find_no_duplicates(self, name, domain=None, path=None): - """__get_item__ and get call _find_no_duplicates -- never used in Requests internally. - Takes as args name and optional domain and path. Returns a cookie.value. - Throws KeyError if cookie is not found and CookieConflictError if there are + """__get_item__ and get call _find_no_duplicates -- never used in Requests internally. + Takes as args name and optional domain and path. Returns a cookie.value. + Throws KeyError if cookie is not found and CookieConflictError if there are multiple cookies that match name and optionally domain and path.""" toReturn = None for cookie in iter(self): if cookie.name == name: if domain is None or cookie.domain == domain: if path is None or cookie.path == path: - if toReturn != None: # if there are multiple cookies that meet passed in criteria + if toReturn != None: # if there are multiple cookies that meet passed in criteria raise CookieConflictError('There are multiple cookies with name, %r' % (name)) - toReturn = cookie.value # we will eventually return this as long as no cookie conflict + toReturn = cookie.value # we will eventually return this as long as no cookie conflict if toReturn: return toReturn @@ -291,6 +298,7 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): """This is not implemented. Calling this will throw an exception.""" raise NotImplementedError + def create_cookie(name, value, **kwargs): """Make a cookie from underspecified parameters. @@ -326,6 +334,7 @@ def create_cookie(name, value, **kwargs): return cookielib.Cookie(**result) + def morsel_to_cookie(morsel): """Convert a Morsel object into a Cookie containing the one k/v pair.""" c = create_cookie( @@ -349,6 +358,7 @@ def morsel_to_cookie(morsel): ) return c + def cookiejar_from_dict(cookie_dict, cookiejar=None): """Returns a CookieJar from a key/value dictionary. diff --git a/requests/exceptions.py b/requests/exceptions.py index 57f7b82..6759af5 100644 --- a/requests/exceptions.py +++ b/requests/exceptions.py @@ -8,34 +8,44 @@ This module contains the set of Requests' exceptions. """ + class RequestException(RuntimeError): """There was an ambiguous exception that occurred while handling your request.""" + class HTTPError(RequestException): """An HTTP error occurred.""" response = None + class ConnectionError(RequestException): """A Connection error occurred.""" + class SSLError(ConnectionError): """An SSL error occurred.""" + class Timeout(RequestException): """The request timed out.""" + class URLRequired(RequestException): """A valid URL is required to make a request.""" + class TooManyRedirects(RequestException): """Too many redirects.""" + class MissingSchema(RequestException, ValueError): """The URL schema (e.g. http or https) is missing.""" + class InvalidSchema(RequestException, ValueError): """See defaults.py for valid schemas.""" + class InvalidURL(RequestException, ValueError): """ The URL provided was somehow invalid. """ diff --git a/requests/hooks.py b/requests/hooks.py index 272abb7..55bd9ac 100644 --- a/requests/hooks.py +++ b/requests/hooks.py @@ -30,6 +30,7 @@ import traceback HOOKS = ('args', 'pre_request', 'pre_send', 'post_request', 'response') + def dispatch_hook(key, hooks, hook_data): """Dispatches a hook dictionary on a given piece of data.""" diff --git a/requests/models.py b/requests/models.py index ae3c1be..87d795c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -39,6 +39,7 @@ from .compat import ( REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved) CONTENT_CHUNK_SIZE = 10 * 1024 + class Request(object): """The :class:`Request ` object. It carries out all functionality of Requests. Recommended interface is with the Requests functions. @@ -553,7 +554,7 @@ class Request(object): self.__dict__.update(r.__dict__) _p = urlparse(url) - no_proxy = filter(lambda x:x.strip(), self.proxies.get('no', '').split(',')) + no_proxy = filter(lambda x: x.strip(), self.proxies.get('no', '').split(',')) proxy = self.proxies.get(_p.scheme) if proxy and not any(map(_p.netloc.endswith, no_proxy)): diff --git a/requests/safe_mode.py b/requests/safe_mode.py index cd171f7..0fb8d70 100644 --- a/requests/safe_mode.py +++ b/requests/safe_mode.py @@ -16,15 +16,16 @@ from .packages.urllib3.response import HTTPResponse from .exceptions import RequestException, ConnectionError, HTTPError import socket + def catch_exceptions_if_in_safe_mode(function): """New implementation of safe_mode. We catch all exceptions at the API level and then return a blank Response object with the error field filled. This decorator wraps request() in api.py. """ - + def wrapped(method, url, **kwargs): # if save_mode, we catch exceptions and fill error field - if (kwargs.get('config') and kwargs.get('config').get('safe_mode')) or (kwargs.get('session') + if (kwargs.get('config') and kwargs.get('config').get('safe_mode')) or (kwargs.get('session') and kwargs.get('session').config.get('safe_mode')): try: return function(method, url, **kwargs) @@ -32,8 +33,8 @@ def catch_exceptions_if_in_safe_mode(function): socket.timeout, socket.gaierror) as e: r = Response() r.error = e - r.raw = HTTPResponse() # otherwise, tests fail - r.status_code = 0 # with this status_code, content returns None + r.raw = HTTPResponse() # otherwise, tests fail + r.status_code = 0 # with this status_code, content returns None return r return function(method, url, **kwargs) return wrapped diff --git a/requests/sessions.py b/requests/sessions.py index 73c7b17..1201498 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -18,6 +18,7 @@ from .hooks import dispatch_hook from .utils import header_expand from .packages.urllib3.poolmanager import PoolManager + def merge_kwargs(local_kwarg, default_kwarg): """Merges kwarg dictionaries. @@ -56,7 +57,6 @@ class Session(object): 'headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks', 'params', 'config', 'verify', 'cert', 'prefetch'] - def __init__(self, headers=None, cookies=None, @@ -240,7 +240,6 @@ class Session(object): # Return the response. return r.response - def get(self, url, **kwargs): """Sends a GET request. Returns :class:`Response` object. @@ -251,7 +250,6 @@ class Session(object): kwargs.setdefault('allow_redirects', True) return self.request('get', url, **kwargs) - def options(self, url, **kwargs): """Sends a OPTIONS request. Returns :class:`Response` object. @@ -262,7 +260,6 @@ class Session(object): kwargs.setdefault('allow_redirects', True) return self.request('options', url, **kwargs) - def head(self, url, **kwargs): """Sends a HEAD request. Returns :class:`Response` object. @@ -273,7 +270,6 @@ class Session(object): kwargs.setdefault('allow_redirects', False) return self.request('head', url, **kwargs) - def post(self, url, data=None, **kwargs): """Sends a POST request. Returns :class:`Response` object. @@ -284,7 +280,6 @@ class Session(object): return self.request('post', url, data=data, **kwargs) - def put(self, url, data=None, **kwargs): """Sends a PUT request. Returns :class:`Response` object. @@ -295,7 +290,6 @@ class Session(object): return self.request('put', url, data=data, **kwargs) - def patch(self, url, data=None, **kwargs): """Sends a PATCH request. Returns :class:`Response` object. @@ -306,7 +300,6 @@ class Session(object): return self.request('patch', url, data=data, **kwargs) - def delete(self, url, **kwargs): """Sends a DELETE request. Returns :class:`Response` object. diff --git a/requests/status_codes.py b/requests/status_codes.py index da74286..e25ecdb 100644 --- a/requests/status_codes.py +++ b/requests/status_codes.py @@ -83,4 +83,4 @@ for (code, titles) in list(_codes.items()): for title in titles: setattr(codes, title, code) if not title.startswith('\\'): - setattr(codes, title.upper(), code) \ No newline at end of file + setattr(codes, title.upper(), code) diff --git a/requests/structures.py b/requests/structures.py index fd1051a..3fda984 100644 --- a/requests/structures.py +++ b/requests/structures.py @@ -47,6 +47,7 @@ class CaseInsensitiveDict(dict): else: return default + class LookupDict(dict): """Dictionary lookup object.""" diff --git a/requests/utils.py b/requests/utils.py index ce08ba7..864760c 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -49,6 +49,7 @@ POSSIBLE_CA_BUNDLE_PATHS = [ '/etc/ssl/ca-bundle.pem', ] + def get_os_ca_bundle_path(): """Try to pick an available CA certificate bundle provided by the OS.""" for path in POSSIBLE_CA_BUNDLE_PATHS: @@ -60,6 +61,7 @@ def get_os_ca_bundle_path(): # otherwise, try and use the OS bundle DEFAULT_CA_BUNDLE_PATH = CERTIFI_BUNDLE_PATH or get_os_ca_bundle_path() + def dict_to_sequence(d): """Returns an internal sequence dictionary update.""" @@ -445,6 +447,7 @@ def requote_uri(uri): # or '%') return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~") + def get_environ_proxies(): """Return a dict of environment proxies.""" diff --git a/tests/informal/test_leaked_connections.py b/tests/informal/test_leaked_connections.py index 5357bf2..438a6ce 100644 --- a/tests/informal/test_leaked_connections.py +++ b/tests/informal/test_leaked_connections.py @@ -6,6 +6,7 @@ the body of the request is not read. import gc, os, subprocess, requests, sys + def main(): gc.disable() diff --git a/tests/test_cookies.py b/tests/test_cookies.py index c6f71b4..e1c4203 100755 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -16,6 +16,7 @@ from requests.compat import cookielib sys.path.append('.') from test_requests import httpbin, TestBaseMixin + class CookieTests(TestBaseMixin, unittest.TestCase): def test_cookies_from_response(self): @@ -106,22 +107,22 @@ class CookieTests(TestBaseMixin, unittest.TestCase): def test_disabled_cookie_persistence(self): """Test that cookies are not persisted when configured accordingly.""" - config = {'store_cookies' : False} + config = {'store_cookies': False} # Check the case when no cookie is passed as part of the request and the one in response is ignored - cookies = requests.get(httpbin('cookies', 'set', 'key', 'value'), config = config).cookies + cookies = requests.get(httpbin('cookies', 'set', 'key', 'value'), config=config).cookies self.assertTrue(cookies.get("key") is None) # Test that the cookies passed while making the request still gets used and is available in response object. # only the ones received from server is not saved - cookies_2 = requests.get(httpbin('cookies', 'set', 'key', 'value'), config = config,\ - cookies = {"key_2" : "value_2"}).cookies + cookies_2 = requests.get(httpbin('cookies', 'set', 'key', 'value'), config=config,\ + cookies={"key_2": "value_2"}).cookies self.assertEqual(len(cookies_2), 1) self.assertEqual(cookies_2.get("key_2"), "value_2") # Use the session and make sure that the received cookie is not used in subsequent calls s = requests.session() - s.get(httpbin('cookies', 'set', 'key', 'value'), config = config) + s.get(httpbin('cookies', 'set', 'key', 'value'), config=config) r = s.get(httpbin('cookies')) self.assertEqual(json.loads(r.text)['cookies'], {}) @@ -134,7 +135,7 @@ class CookieTests(TestBaseMixin, unittest.TestCase): self.assertEqual(len(c), len(r.cookies.keys())) self.assertEqual(len(c), len(r.cookies.values())) self.assertEqual(len(c), len(r.cookies.items())) - + # domain and path utility functions domain = r.cookies.list_domains()[0] path = r.cookies.list_paths()[0] @@ -151,13 +152,14 @@ class CookieTests(TestBaseMixin, unittest.TestCase): # test keys, values, and items self.assertEqual(r.cookies.keys(), ['myname']) self.assertEqual(r.cookies.values(), ['myvalue']) - self.assertEqual(r.cookies.items(), [('myname','myvalue')]) - + self.assertEqual(r.cookies.items(), [('myname', 'myvalue')]) + # test if we can convert jar to dict dictOfCookies = dict(r.cookies) - self.assertEqual(dictOfCookies, {'myname':'myvalue'}) + self.assertEqual(dictOfCookies, {'myname': 'myvalue'}) self.assertEqual(dictOfCookies, r.cookies.get_dict()) + class LWPCookieJarTest(TestBaseMixin, unittest.TestCase): """Check store/load of cookies to FileCookieJar's, specifically LWPCookieJar's.""" @@ -254,6 +256,7 @@ class LWPCookieJarTest(TestBaseMixin, unittest.TestCase): self.assertEqual(len(cookiejar_2), 1) self.assertCookieHas(list(cookiejar_2)[0], name='Persistent', value='CookiesAreScary') + class MozCookieJarTest(LWPCookieJarTest): """Same test, but substitute MozillaCookieJar.""" diff --git a/tests/test_requests.py b/tests/test_requests.py index 3bbcfdf..25e6bbd 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -52,6 +52,7 @@ class TestSetup(object): # time.sleep(1) _httpbin = True + class TestBaseMixin(object): def assertCookieHas(self, cookie, **kwargs): @@ -61,6 +62,7 @@ class TestBaseMixin(object): message = 'Failed comparison for %s: %s != %s' % (attr, cookie_attr, expected_value) self.assertEqual(cookie_attr, expected_value, message) + class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): """Requests test cases.""" @@ -349,10 +351,10 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): post1 = post(url, files={'fname.txt': 'fdata'}) self.assertEqual(post1.status_code, 200) - post2 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':'more fdata'}) + post2 = post(url, files={'fname.txt': 'fdata', 'fname2.txt': 'more fdata'}) self.assertEqual(post2.status_code, 200) - post3 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':open(__file__,'rb')}) + post3 = post(url, files={'fname.txt': 'fdata', 'fname2.txt': open(__file__, 'rb')}) self.assertEqual(post3.status_code, 200) post4 = post(url, files={'fname.txt': 'fdata'}) @@ -377,7 +379,6 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): self.assertTrue(rbody['files'].get('fname.txt'), None) self.assertEqual(rbody['files']['fname.txt'], 'fdata to verify') - def test_nonzero_evaluation(self): for service in SERVICES: @@ -824,7 +825,6 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): r = get('http://localhost:1/nope', allow_redirects=False, config=config) assert r.content == None - # def test_invalid_content(self): # # WARNING: if you're using a terrible DNS provider (comcast), # # this will fail. @@ -940,7 +940,6 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): s.config['danger_mode'] = True s.get(httpbin('redirect', '4')) - def test_empty_response(self): r = requests.get(httpbin('status', '404')) r.text diff --git a/tests/test_requests_ext.py b/tests/test_requests_ext.py index 7645d8c..3e0d5b7 100644 --- a/tests/test_requests_ext.py +++ b/tests/test_requests_ext.py @@ -25,17 +25,14 @@ class RequestsTestSuite(unittest.TestCase): def test_addition(self): assert (1 + 1) == 2 - def test_ssl_hostname_ok(self): requests.get('https://github.com', verify=True) - def test_ssl_hostname_not_ok(self): requests.get('https://kennethreitz.com', verify=False) self.assertRaises(requests.exceptions.SSLError, requests.get, 'https://kennethreitz.com') - def test_ssl_hostname_session_not_ok(self): s = requests.session() @@ -44,7 +41,6 @@ class RequestsTestSuite(unittest.TestCase): s.get('https://kennethreitz.com', verify=False) - def test_binary_post(self): '''We need to be careful how we build the utf-8 string since unicode literals are a syntax error in python3 @@ -59,13 +55,10 @@ class RequestsTestSuite(unittest.TestCase): raise EnvironmentError('Flesh out this test for your environment.') requests.post('http://www.google.com/', data=utf8_string) - - def test_unicode_error(self): url = 'http://blip.fm/~1abvfu' requests.get(url) - def test_chunked_head_redirect(self): url = "http://t.co/NFrx0zLG" r = requests.head(url, allow_redirects=True) @@ -128,4 +121,3 @@ class RequestsTestSuite(unittest.TestCase): if __name__ == '__main__': unittest.main() - diff --git a/tests/test_requests_https.py b/tests/test_requests_https.py index c6ea8f3..1691a8c 100755 --- a/tests/test_requests_https.py +++ b/tests/test_requests_https.py @@ -9,6 +9,7 @@ import unittest sys.path.insert(0, os.path.abspath('..')) import requests + class HTTPSTest(unittest.TestCase): """Smoke test for https functionality."""