Skip to content

Commit ae13a40

Browse files
callum-oakleyCallum Oakley
authored andcommitted
finished making everything a consistent style (PEP 8 compliant)
1 parent 0f58ff0 commit ae13a40

File tree

12 files changed

+122
-55
lines changed

12 files changed

+122
-55
lines changed

pusher/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
from .pusher import Pusher
3+
from pusher.pusher import Pusher
44

5-
__all__ = [
6-
'Pusher',
7-
]
5+
__all__ = ['Pusher']

pusher/aiohttp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pusher.http import process_response
77

8+
89
class AsyncIOBackend:
910
def __init__(self, client):
1011
"""Adapter for the requests module.
@@ -14,6 +15,7 @@ def __init__(self, client):
1415
self.client = client
1516
self.conn = aiohttp.TCPConnector()
1617

18+
1719
def send_request(self, request):
1820
method = request.method
1921
url = "%s%s" % (request.base_url, request.path)
@@ -22,8 +24,10 @@ def send_request(self, request):
2224
headers = request.headers
2325

2426
response = yield from asyncio.wait_for(
25-
aiohttp.request(method, url, params=params, data=data, headers=headers, connector=self.conn),
26-
timeout=self.client.timeout
27-
)
27+
aiohttp.request(
28+
method, url, params=params, data=data, headers=headers,
29+
connector=self.conn),
30+
timeout=self.client.timeout)
31+
2832
body = yield from response.read_and_close()
2933
return process_response(response.status, body.decode('utf8'))

pusher/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import six
1212

13+
1314
class Client(object):
1415
def __init__(
1516
self, app_id, key, secret, ssl=True, host=None, port=None,

pusher/errors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
class PusherError(Exception):
44
pass
55

6+
67
class PusherBadRequest(PusherError):
78
pass
89

10+
911
class PusherBadAuth(PusherError):
1012
pass
1113

14+
1215
class PusherForbidden(PusherError):
1316
pass
1417

18+
1519
class PusherBadStatus(PusherError):
16-
pass
20+
pass

pusher/gae.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
# -*- coding: utf-8 -*-
22

3-
from __future__ import (print_function, unicode_literals, absolute_import,
4-
division)
3+
from __future__ import (
4+
print_function,
5+
unicode_literals,
6+
absolute_import,
7+
division)
58

69
from google.appengine.api import urlfetch
710
from pusher.http import process_response
811

12+
913
class GAEBackend(object):
10-
"""Adapter for the URLFetch Module. Necessary for using this library with Google
11-
App Engine"""
14+
"""
15+
Adapter for the URLFetch Module. Necessary for using this library with
16+
Google App Engine
17+
"""
18+
def __init__(self, client, **options):
19+
self.client = client
20+
self.options = options
21+
22+
23+
def send_request(self, request):
24+
resp = urlfetch.fetch(
25+
url=request.url,
26+
headers=request.headers,
27+
method=request.method,
28+
payload=request.body,
29+
deadline=self.client.timeout,
30+
**self.options)
1231

13-
def __init__(self, client, **options):
14-
self.client = client
15-
self.options = options
16-
17-
def send_request(self, request):
18-
resp = urlfetch.fetch(
19-
url=request.url,
20-
headers=request.headers,
21-
method=request.method,
22-
payload=request.body,
23-
deadline=self.client.timeout,
24-
**self.options
25-
)
26-
return process_response(resp.status_code, resp.content)
32+
return process_response(resp.status_code, resp.content)

pusher/http.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# -*- coding: utf-8 -*-
22

3-
from __future__ import (print_function, unicode_literals, absolute_import,
4-
division)
3+
from __future__ import (
4+
print_function,
5+
unicode_literals,
6+
absolute_import,
7+
division)
8+
59
from pusher.errors import *
610
from pusher.signature import sign
711
from pusher.version import VERSION
@@ -12,47 +16,62 @@
1216
import six
1317
import time
1418

19+
1520
GET, POST, PUT, DELETE = "GET", "POST", "PUT", "DELETE"
1621

22+
1723
class RequestMethod(object):
1824
def __init__(self, client, f):
1925
self.client = client
2026
self.f = f
2127

28+
2229
def __call__(self, *args, **kwargs):
2330
return self.client.http.send_request(self.make_request(*args, **kwargs))
2431

32+
2533
def make_request(self, *args, **kwargs):
2634
return self.f(self.client, *args, **kwargs)
2735

36+
2837
def doc_string(doc):
2938
def decorator(f):
3039
f.__doc__ = doc
3140
return f
41+
3242
return decorator
3343

44+
3445
def request_method(f):
3546
@property
3647
@doc_string(f.__doc__)
3748
def wrapped(self):
3849
return RequestMethod(self, f)
50+
3951
return wrapped
4052

53+
4154
def make_query_string(params):
4255
return '&'.join(map('='.join, sorted(params.items(), key=lambda x: x[0])))
4356

57+
4458
def process_response(status, body):
4559
if status == 200 or status == 202:
4660
return json.loads(body)
61+
4762
elif status == 400:
4863
raise PusherBadRequest(body)
64+
4965
elif status == 401:
5066
raise PusherBadAuth(body)
67+
5168
elif status == 403:
5269
raise PusherForbidden(body)
70+
5371
else:
5472
raise PusherBadStatus("%s: %s" % (status, body))
5573

74+
5675
class Request(object):
5776
"""Represents the request to be made to the Pusher API.
5877
@@ -67,56 +86,68 @@ class Request(object):
6786
def __init__(self, client, method, path, params=None):
6887
if params is None:
6988
params = {}
89+
7090
self.client = client
7191
self.method = method
7292
self.path = path
7393
self.params = copy.copy(params)
7494
if method == POST:
7595
self.body = six.text_type(json.dumps(params)).encode('utf8')
7696
self.query_params = {}
97+
7798
elif method == GET:
7899
self.body = bytes()
79100
self.query_params = params
101+
80102
else:
81103
raise NotImplementedError("Only GET and POST supported")
104+
82105
self._generate_auth()
83106

107+
84108
def _generate_auth(self):
85109
self.body_md5 = hashlib.md5(self.body).hexdigest()
86110
self.query_params.update({
87111
'auth_key': self.client.key,
88112
'body_md5': six.text_type(self.body_md5),
89113
'auth_version': '1.0',
90-
'auth_timestamp': '%.0f' % time.time()
91-
})
114+
'auth_timestamp': '%.0f' % time.time()})
92115

93116
auth_string = '\n'.join([
94117
self.method,
95118
self.path,
96-
make_query_string(self.query_params)
97-
])
119+
make_query_string(self.query_params)])
120+
121+
self.query_params['auth_signature'] = sign(
122+
self.client.secret, auth_string)
98123

99-
self.query_params['auth_signature'] = sign(self.client.secret, auth_string)
100124

101125
@property
102126
def query_string(self):
103127
return make_query_string(self.query_params)
104128

129+
105130
@property
106131
def signed_path(self):
107132
return "%s?%s" % (self.path, self.query_string)
108133

134+
109135
@property
110136
def url(self):
111137
return "%s%s" % (self.base_url, self.signed_path)
112138

139+
113140
@property
114141
def base_url(self):
115-
return "%s://%s:%s" % (self.client.scheme, self.client.host, self.client.port)
142+
return (
143+
"%s://%s:%s" %
144+
(self.client.scheme, self.client.host, self.client.port))
145+
116146

117147
@property
118148
def headers(self):
119149
hdrs = {"X-Pusher-Library": "pusher-http-python " + VERSION}
120150
if self.method == POST:
121151
hdrs["Content-Type"] = "application/json"
152+
122153
return hdrs

pusher/notification_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def notify(self, interests, notification):
5050
'interests': interests}
5151

5252
params.update(notification)
53-
path = "/%s/%s/apps/%s/notifications" %
54-
(API_PREFIX, API_VERSION, self.app_id)
53+
path = (
54+
"/%s/%s/apps/%s/notifications" %
55+
(API_PREFIX, API_VERSION, self.app_id))
5556

5657
return Request(self, POST, path, params)

pusher/pusher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def validate_webhook(self, key, signature, body):
213213

214214
try:
215215
body_data = json.loads(body, cls=self._json_decoder)
216+
216217
except ValueError:
217218
return None
218219

pusher/pusher_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def __init__(
3939
self._host = ensure_text(host, "host")
4040

4141
elif cluster:
42-
self._host = six.text_type("api-%s.pusher.com") %
43-
ensure_text(cluster, "cluster")
44-
42+
self._host = (
43+
six.text_type("api-%s.pusher.com") %
44+
ensure_text(cluster, "cluster"))
4545
else:
4646
self._host = six.text_type("api.pusherapp.com")
4747

pusher/requests.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
# -*- coding: utf-8 -*-
22

3-
from __future__ import (print_function, unicode_literals, absolute_import,
4-
division)
3+
from __future__ import (
4+
print_function,
5+
unicode_literals,
6+
absolute_import,
7+
division)
8+
59
from pusher.http import process_response
610

711
import requests
812
import sys
913
import os
1014

15+
1116
if sys.version_info < (3,):
1217
import urllib3.contrib.pyopenssl
1318
urllib3.contrib.pyopenssl.inject_into_urllib3()
1419

1520
CERT_PATH = os.path.dirname(os.path.abspath(__file__)) + '/cacert.pem'
1621

22+
1723
class RequestsBackend(object):
1824
"""Adapter for the requests module.
1925
@@ -24,16 +30,17 @@ def __init__(self, client, **options):
2430
self.client = client
2531
self.options = options
2632
if self.client.ssl:
27-
self.options.update({'verify': CERT_PATH})
33+
self.options.update({'verify': CERT_PATH})
2834
self.session = requests.Session()
2935

36+
3037
def send_request(self, request):
3138
resp = self.session.request(
3239
request.method,
3340
request.url,
3441
headers=request.headers,
3542
data=request.body,
3643
timeout=self.client.timeout,
37-
**self.options
38-
)
44+
**self.options)
45+
3946
return process_response(resp.status_code, resp.text)

0 commit comments

Comments
 (0)