Skip to content

Commit 7d72ac1

Browse files
committed
PEP8: Wrap 80+ column lines.
1 parent f81d183 commit 7d72ac1

File tree

5 files changed

+290
-121
lines changed

5 files changed

+290
-121
lines changed

example/client.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,55 +41,64 @@
4141
CALLBACK_URL = 'http://printer.example.com/request_token_ready'
4242
RESOURCE_URL = 'http://photos.example.net/photos'
4343

44-
# key and secret granted by the service provider for this consumer application - same as the MockOAuthDataStore
44+
# key and secret granted by the service provider for this consumer
45+
# application - same as the MockOAuthDataStore
4546
CONSUMER_KEY = 'key'
4647
CONSUMER_SECRET = 'secret'
4748

4849
# example client using httplib with headers
4950
class SimpleOAuthClient(oauth.OAuthClient):
5051

51-
def __init__(self, server, port=httplib.HTTP_PORT, request_token_url='', access_token_url='', authorization_url=''):
52+
def __init__(self, server, port=httplib.HTTP_PORT, request_token_url='',
53+
access_token_url='', authorization_url=''):
5254
self.server = server
5355
self.port = port
5456
self.request_token_url = request_token_url
5557
self.access_token_url = access_token_url
5658
self.authorization_url = authorization_url
57-
self.connection = httplib.HTTPConnection("%s:%d" % (self.server, self.port))
59+
self.connection = httplib.HTTPConnection(
60+
"%s:%d" % (self.server, self.port))
5861

5962
def fetch_request_token(self, oauth_request):
6063
# via headers
6164
# -> OAuthToken
62-
self.connection.request(oauth_request.http_method, self.request_token_url, headers=oauth_request.to_header())
65+
self.connection.request(oauth_request.http_method,
66+
self.request_token_url, headers=oauth_request.to_header())
6367
response = self.connection.getresponse()
6468
return oauth.OAuthToken.from_string(response.read())
6569

6670
def fetch_access_token(self, oauth_request):
6771
# via headers
6872
# -> OAuthToken
69-
self.connection.request(oauth_request.http_method, self.access_token_url, headers=oauth_request.to_header())
73+
self.connection.request(oauth_request.http_method,
74+
self.access_token_url, headers=oauth_request.to_header())
7075
response = self.connection.getresponse()
7176
return oauth.OAuthToken.from_string(response.read())
7277

7378
def authorize_token(self, oauth_request):
7479
# via url
7580
# -> typically just some okay response
76-
self.connection.request(oauth_request.http_method, oauth_request.to_url())
81+
self.connection.request(oauth_request.http_method,
82+
oauth_request.to_url())
7783
response = self.connection.getresponse()
7884
return response.read()
7985

8086
def access_resource(self, oauth_request):
8187
# via post body
8288
# -> some protected resources
8389
headers = {'Content-Type' :'application/x-www-form-urlencoded'}
84-
self.connection.request('POST', RESOURCE_URL, body=oauth_request.to_postdata(), headers=headers)
90+
self.connection.request('POST', RESOURCE_URL,
91+
body=oauth_request.to_postdata(),
92+
headers=headers)
8593
response = self.connection.getresponse()
8694
return response.read()
8795

8896
def run_example():
8997

9098
# setup
9199
print('** OAuth Python Library Example **')
92-
client = SimpleOAuthClient(SERVER, PORT, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL)
100+
client = SimpleOAuthClient(SERVER, PORT, REQUEST_TOKEN_URL,
101+
ACCESS_TOKEN_URL, AUTHORIZATION_URL)
93102
consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
94103
signature_method_plaintext = oauth.OAuthSignatureMethod_PLAINTEXT()
95104
signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
@@ -98,7 +107,8 @@ def run_example():
98107
# get request token
99108
print('* Obtain a request token ...')
100109
pause()
101-
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, callback=CALLBACK_URL, http_url=client.request_token_url)
110+
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
111+
consumer, callback=CALLBACK_URL, http_url=client.request_token_url)
102112
oauth_request.sign_request(signature_method_plaintext, consumer, None)
103113
print('REQUEST (via headers)')
104114
print('parameters: %s' % str(oauth_request.parameters))
@@ -112,7 +122,8 @@ def run_example():
112122

113123
print('* Authorize the request token ...')
114124
pause()
115-
oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token, http_url=client.authorization_url)
125+
oauth_request = oauth.OAuthRequest.from_token_and_callback(
126+
token=token, http_url=client.authorization_url)
116127
print('REQUEST (via url query string)')
117128
print('parameters: %s' % str(oauth_request.parameters))
118129
pause()
@@ -131,7 +142,9 @@ def run_example():
131142
# get access token
132143
print('* Obtain an access token ...')
133144
pause()
134-
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=client.access_token_url)
145+
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
146+
consumer, token=token, verifier=verifier,
147+
http_url=client.access_token_url)
135148
oauth_request.sign_request(signature_method_plaintext, consumer, token)
136149
print('REQUEST (via headers)')
137150
print('parameters: %s' % str(oauth_request.parameters))
@@ -145,8 +158,11 @@ def run_example():
145158
# access some protected resources
146159
print('* Access protected resources ...')
147160
pause()
148-
parameters = {'file': 'vacation.jpg', 'size': 'original'} # resource specific params
149-
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='POST', http_url=RESOURCE_URL, parameters=parameters)
161+
parameters = {'file': 'vacation.jpg',
162+
'size': 'original'} # resource specific params
163+
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
164+
token=token, http_method='POST', http_url=RESOURCE_URL,
165+
parameters=parameters)
150166
oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
151167
print('REQUEST (via post body)')
152168
print('parameters: %s' % str(oauth_request.parameters))

example/server.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ def lookup_token(self, token_type, token):
6060
return None
6161

6262
def lookup_nonce(self, oauth_consumer, oauth_token, nonce):
63-
if oauth_token and oauth_consumer.key == self.consumer.key and (oauth_token.key == self.request_token.key or oauth_token.key == self.access_token.key) and nonce == self.nonce:
63+
if (oauth_token and
64+
oauth_consumer.key == self.consumer.key and
65+
(oauth_token.key == self.request_token.key or
66+
oauth_token.key == self.access_token.key) and
67+
nonce == self.nonce):
6468
return self.nonce
6569
return None
6670

@@ -74,7 +78,9 @@ def fetch_request_token(self, oauth_consumer, oauth_callback):
7478
return None
7579

7680
def fetch_access_token(self, oauth_consumer, oauth_token, oauth_verifier):
77-
if oauth_consumer.key == self.consumer.key and oauth_token.key == self.request_token.key and oauth_verifier == self.verifier:
81+
if (oauth_consumer.key == self.consumer.key and
82+
oauth_token.key == self.request_token.key and
83+
oauth_verifier == self.verifier):
7884
# want to check here if token is authorized
7985
# for mock store, we assume it is
8086
return self.access_token
@@ -91,8 +97,10 @@ class RequestHandler(BaseHTTPRequestHandler):
9197

9298
def __init__(self, *args, **kwargs):
9399
self.oauth_server = oauth.OAuthServer(MockOAuthDataStore())
94-
self.oauth_server.add_signature_method(oauth.OAuthSignatureMethod_PLAINTEXT())
95-
self.oauth_server.add_signature_method(oauth.OAuthSignatureMethod_HMAC_SHA1())
100+
self.oauth_server.add_signature_method(
101+
oauth.OAuthSignatureMethod_PLAINTEXT())
102+
self.oauth_server.add_signature_method(
103+
oauth.OAuthSignatureMethod_HMAC_SHA1())
96104
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
97105

98106
# example way to send an oauth error
@@ -119,7 +127,8 @@ def do_GET(self):
119127
pass
120128

121129
# construct the oauth request from the request parameters
122-
oauth_request = oauth.OAuthRequest.from_request(self.command, self.path, headers=self.headers, query_string=postdata)
130+
oauth_request = oauth.OAuthRequest.from_request(self.command,
131+
self.path, headers=self.headers, query_string=postdata)
123132

124133
# request token
125134
if self.path.startswith(REQUEST_TOKEN_URL):
@@ -170,7 +179,8 @@ def do_GET(self):
170179
if self.path.startswith(RESOURCE_URL):
171180
try:
172181
# verify the request has been oauth authorized
173-
consumer, token, params = self.oauth_server.verify_request(oauth_request)
182+
consumer, token, params = self.oauth_server.verify_request(
183+
oauth_request)
174184
# send okay response
175185
self.send_response(200, 'OK')
176186
self.end_headers()

oauth2/__init__.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,17 @@ def to_unicode(s):
9999
message if s is not unicode, ascii, or utf-8. """
100100
if not isinstance(s, unicode):
101101
if not isinstance(s, str):
102-
raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s))
102+
raise TypeError('You are required to pass either unicode or '
103+
'string here, not: %r (%s)' % (type(s), s))
103104
try:
104105
s = s.decode('utf-8')
105106
except UnicodeDecodeError as le:
106-
raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,))
107+
raise TypeError('You are required to pass either a unicode '
108+
'object or a utf-8 string here. You passed a '
109+
'Python string object which contained non-utf-8: '
110+
'%r. The UnicodeDecodeError that resulted from '
111+
'attempting to interpret it as utf-8 was: %s'
112+
% (s, le,))
107113
return s
108114

109115
def to_utf8(s):
@@ -357,7 +363,8 @@ def __init__(self, method=HTTP_METHOD, url=None, parameters=None,
357363
def url(self, value):
358364
self.__dict__['url'] = value
359365
if value is not None:
360-
scheme, netloc, path, params, query, fragment = urlparse.urlparse(value)
366+
scheme, netloc, path, params, query, fragment = urlparse.urlparse(
367+
value)
361368

362369
# Exclude default port numbers.
363370
if scheme == 'http' and netloc[-3:] == ':80':
@@ -368,7 +375,8 @@ def url(self, value):
368375
raise ValueError("Unsupported URL %s (%s)." % (value, scheme))
369376

370377
# Normalized URL excludes params, query, and fragment.
371-
self.normalized_url = urlparse.urlunparse((scheme, netloc, path, None, None, None))
378+
self.normalized_url = urlparse.urlunparse(
379+
(scheme, netloc, path, None, None, None))
372380
else:
373381
self.normalized_url = None
374382
self.__dict__['url'] = None
@@ -462,15 +470,19 @@ def get_normalized_parameters(self):
462470
value = list(value)
463471
except TypeError as e:
464472
assert 'is not iterable' in str(e)
465-
items.append((to_utf8_if_string(key), to_utf8_if_string(value)))
473+
items.append(
474+
(to_utf8_if_string(key), to_utf8_if_string(value)))
466475
else:
467-
items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value)
476+
items.extend(
477+
(to_utf8_if_string(key), to_utf8_if_string(item))
478+
for item in value)
468479

469480
# Include any query string parameters from the provided URL
470481
query = urlparse.urlparse(self.url)[4]
471482

472483
url_items = self._split_url_string(query).items()
473-
url_items = [(to_utf8(k), to_utf8(v)) for k, v in url_items if k != 'oauth_signature' ]
484+
url_items = [(to_utf8(k), to_utf8(v))
485+
for k, v in url_items if k != 'oauth_signature' ]
474486
items.extend(url_items)
475487

476488
items.sort()
@@ -486,7 +498,8 @@ def sign_request(self, signature_method, consumer, token):
486498

487499
if not self.is_form_encoded:
488500
# according to
489-
# http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html
501+
# http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/
502+
# oauth-bodyhash.html
490503
# section 4.1.1 "OAuth Consumers MUST NOT include an
491504
# oauth_body_hash parameter on requests with form-encoded
492505
# request bodies."
@@ -606,7 +619,8 @@ def _split_header(header):
606619
@staticmethod
607620
def _split_url_string(param_str):
608621
"""Turn URL string into parameters."""
609-
parameters = parse_qs(param_str.encode('utf-8'), keep_blank_values=True)
622+
parameters = parse_qs(param_str.encode('utf-8'),
623+
keep_blank_values=True)
610624
for k, v in parameters.iteritems():
611625
parameters[k] = urllib.unquote(v[0])
612626
return parameters
@@ -628,7 +642,8 @@ def __init__(self, consumer, token=None, cache=None, timeout=None,
628642
self.token = token
629643
self.method = SignatureMethod_HMAC_SHA1()
630644

631-
httplib2.Http.__init__(self, cache=cache, timeout=timeout, proxy_info=proxy_info)
645+
httplib2.Http.__init__(self, cache=cache, timeout=timeout,
646+
proxy_info=proxy_info)
632647

633648
def set_signature_method(self, method):
634649
if not isinstance(method, SignatureMethod):
@@ -734,7 +749,9 @@ def _get_signature_method(self, request):
734749
return self.signature_methods[signature_method]
735750
except KeyError:
736751
signature_method_names = ', '.join(self.signature_methods.keys())
737-
raise Error('Signature method %s not supported try one of the following: %s' % (signature_method, signature_method_names))
752+
raise Error('Signature method %s not supported try one of the '
753+
'following: %s'
754+
% (signature_method, signature_method_names))
738755

739756
def _check_signature(self, request, consumer, token):
740757
timestamp, nonce = request._get_timestamp_nonce()
@@ -805,7 +822,8 @@ class SignatureMethod_HMAC_SHA1(SignatureMethod):
805822
name = 'HMAC-SHA1'
806823

807824
def signing_base(self, request, consumer, token):
808-
if not hasattr(request, 'normalized_url') or request.normalized_url is None:
825+
if (not hasattr(request, 'normalized_url') or
826+
request.normalized_url is None):
809827
raise ValueError("Base URL for request is not set.")
810828

811829
sig = (

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
mverstr = mo.group(1)
1717
else:
1818
print("unable to find version in %s" % (VERSIONFILE,))
19-
raise RuntimeError("if %s.py exists, it must be well-formed" % (VERSIONFILE,))
19+
raise RuntimeError("if %s.py exists, it must be well-formed"
20+
% (VERSIONFILE,))
2021
AVSRE = r"^auto_build_num *= *['\"]([^'\"]*)['\"]"
2122
mo = re.search(AVSRE, verstrline, re.M)
2223
if mo:

0 commit comments

Comments
 (0)