Skip to content

Commit 232b536

Browse files
committed
Merge pull request Lawouach#74 from KLab/cleanup
Cleanup indent and trailing spaces.
2 parents ddf73a5 + 88fe146 commit 232b536

File tree

9 files changed

+153
-171
lines changed

9 files changed

+153
-171
lines changed

ws4py/compat.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
unicode = str
2121
basestring = (bytes, str)
2222
_ord = ord
23-
23+
2424
def enc(b, encoding='utf-8'):
2525
if isinstance(b, str):
2626
return b.encode(encoding)
2727
elif isinstance(b, bytearray):
2828
return bytes(b)
2929
return b
30-
30+
3131
def dec(b, encoding='utf-8'):
3232
if isinstance(b, (bytes, bytearray)):
3333
return b.decode(encoding)
@@ -50,14 +50,14 @@ def ord(c):
5050
unicode = unicode
5151
basestring = basestring
5252
ord = ord
53-
53+
5454
def enc(b, encoding='utf-8'):
5555
if isinstance(b, unicode):
5656
return b.encode(encoding)
5757
elif isinstance(b, bytearray):
5858
return str(b)
5959
return b
60-
60+
6161
def dec(b, encoding='utf-8'):
6262
if isinstance(b, (str, bytearray)):
6363
return b.decode(encoding)
@@ -68,4 +68,3 @@ def get_connection(fileobj):
6868

6969
def detach_connection(fileobj):
7070
fileobj._sock = None
71-

ws4py/framing.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
import array
3-
import os
42
from struct import pack, unpack
53

64
from ws4py.exc import FrameTooLargeException, ProtocolException
@@ -25,7 +23,7 @@ def __init__(self, opcode=None, body='', masking_key=None, fin=0, rsv1=0, rsv2=0
2523
:linenos:
2624
2725
>>> test_mask = 'XXXXXX' # perhaps from os.urandom(4)
28-
>>> f = Frame(OPCODE_TEXT, 'hello world', masking_key=test_mask, fin=1)
26+
>>> f = Frame(OPCODE_TEXT, 'hello world', masking_key=test_mask, fin=1)
2927
>>> bytes = f.build()
3028
>>> bytes.encode('hex')
3129
'818bbe04e66ad6618a06d1249105cc6882'
@@ -55,7 +53,7 @@ def parser(self):
5553
# Python generators must be initialized once.
5654
next(self.parser)
5755
return self._parser
58-
56+
5957
def _cleanup(self):
6058
if self._parser:
6159
self._parser.close()
@@ -73,7 +71,7 @@ def build(self):
7371

7472
if 0x3 <= self.opcode <= 0x7 or 0xB <= self.opcode:
7573
raise ValueError('Opcode cannot be a reserved opcode')
76-
74+
7775
## +-+-+-+-+-------+
7876
## |F|R|R|R| opcode|
7977
## |I|S|S|S| (4) |
@@ -165,7 +163,7 @@ def _parsing(self):
165163
# Yield until we get the second header's byte
166164
while not bytes:
167165
bytes = (yield 1)
168-
166+
169167
second_byte = ord(bytes[0])
170168
mask = (second_byte >> 7) & 1
171169
self.payload_length = second_byte & 0x7f
@@ -217,7 +215,7 @@ def _parsing(self):
217215
extended_payload_length = bytes
218216
self.payload_length = unpack(
219217
'!H', extended_payload_length)[0]
220-
218+
221219
if mask:
222220
if len(buf) < 4:
223221
nxt_buf_size = 4 - len(buf)
@@ -248,7 +246,7 @@ def _parsing(self):
248246
bytes = buf
249247
else:
250248
bytes = buf[:self.payload_length]
251-
249+
252250
self.body = bytes
253251
yield
254252

@@ -257,10 +255,10 @@ def mask(self, data):
257255
Performs the masking or unmasking operation on data
258256
using the simple masking algorithm:
259257
260-
..
258+
..
261259
j = i MOD 4
262260
transformed-octet-i = original-octet-i XOR masking-key-octet-j
263-
261+
264262
"""
265263
masked = bytearray(data)
266264
if py3k: key = self.masking_key

ws4py/messaging.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import os
33
import struct
4-
import copy
54

65
from ws4py.framing import Frame, OPCODE_CONTINUATION, OPCODE_TEXT, \
76
OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG
@@ -33,7 +32,7 @@ def __init__(self, opcode, data='', encoding='utf-8'):
3332
self._completed = False
3433
self.encoding = encoding
3534
self.data = enc(data, encoding)
36-
35+
3736
def single(self, mask=False):
3837
"""
3938
Returns a frame bytes with the fin bit set and a random mask.
@@ -77,19 +76,19 @@ def completed(self, state):
7776
set by the stream's parser.
7877
"""
7978
self._completed = state
80-
79+
8180
def extend(self, data):
8281
"""
8382
Add more ``data`` to the message.
8483
"""
8584
self.data += enc(data, self.encoding)
86-
85+
8786
def __len__(self):
8887
return len(self.__unicode__())
8988

9089
def __str__(self):
9190
return self.data
92-
91+
9392
def __unicode__(self):
9493
return dec(self.data, self.encoding)
9594

@@ -124,21 +123,21 @@ def __init__(self, code=1000, reason=''):
124123
data += struct.pack("!H", code)
125124
if reason:
126125
data += enc(reason, 'utf-8')
127-
126+
128127
Message.__init__(self, OPCODE_CLOSE, data, 'utf-8')
129128
self.code = code
130129
self.reason = enc(reason, self.encoding)
131130

132131
def __str__(self):
133132
return self.reason
134-
133+
135134
def __unicode__(self):
136135
return dec(self.reason, self.encoding)
137136

138137
class PingControlMessage(Message):
139138
def __init__(self, data=None):
140139
Message.__init__(self, OPCODE_PING, data)
141-
140+
142141
class PongControlMessage(Message):
143142
def __init__(self, data):
144143
Message.__init__(self, OPCODE_PONG, data)

ws4py/server/cherrypyserver.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ def ws(self):
6161
meaning you could also dynamically change the class based
6262
on other envrionmental settings (is the user authenticated for ex).
6363
"""
64-
import sys
6564
import base64
6665
from hashlib import sha1
6766
import inspect
68-
import socket
6967
import threading
7068

7169
import cherrypy

ws4py/server/geventserver.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,45 @@
1010

1111
class UpgradableWSGIHandler(gevent.pywsgi.WSGIHandler):
1212
"""Upgradable version of gevent.pywsgi.WSGIHandler class
13-
13+
1414
This is a drop-in replacement for gevent.pywsgi.WSGIHandler that supports
1515
protocol upgrades via WSGI environment. This means you can create upgraders
1616
as WSGI apps or WSGI middleware.
17-
17+
1818
If an HTTP request comes in that includes the Upgrade header, it will add
1919
to the environment two items:
20-
20+
2121
``upgrade.protocol``
2222
The protocol to upgrade to. Checking for this lets you know the request
23-
wants to be upgraded and the WSGI server supports this interface.
24-
23+
wants to be upgraded and the WSGI server supports this interface.
24+
2525
``upgrade.socket``
2626
The raw Python socket object for the connection. From this you can do any
2727
upgrade negotiation and hand it off to the proper protocol handler.
28-
28+
2929
The upgrade must be signalled by starting a response using the 101 status
3030
code. This will inform the server to flush the headers and response status
31-
immediately, not to expect the normal WSGI app return value, and not to
32-
look for more HTTP requests on this connection.
33-
31+
immediately, not to expect the normal WSGI app return value, and not to
32+
look for more HTTP requests on this connection.
33+
3434
To use this handler with gevent.pywsgi.WSGIServer, you can pass it to the
3535
constructor:
36-
36+
3737
.. code-block:: python
3838
:linenos:
3939
40-
server = WSGIServer(('127.0.0.1', 80), app,
40+
server = WSGIServer(('127.0.0.1', 80), app,
4141
handler_class=UpgradableWSGIHandler)
42-
43-
Alternatively, you can specify it as a class variable for a WSGIServer
42+
43+
Alternatively, you can specify it as a class variable for a WSGIServer
4444
subclass:
45-
45+
4646
.. code-block:: python
4747
:linenos:
4848
4949
class UpgradableWSGIServer(gevent.pywsgi.WSGIServer):
5050
handler_class = UpgradableWSGIHandler
51-
51+
5252
"""
5353
def run_application(self):
5454
upgrade_header = self.environ.get('HTTP_UPGRADE', '').lower()
@@ -65,12 +65,12 @@ def start_response_for_upgrade(status, headers, exc_info=None):
6565
sline = '%s %s\r\n' % (self.request_version, self.status)
6666
write(sline)
6767
self.response_length += len(sline)
68-
68+
6969
for header in headers:
7070
hline = '%s: %s\r\n' % header
7171
write(hline)
7272
self.response_length += len(hline)
73-
73+
7474
write('\r\n')
7575
self.response_length += 2
7676
else:
@@ -93,17 +93,17 @@ def start_response_for_upgrade(status, headers, exc_info=None):
9393

9494
class WebSocketServer(gevent.pywsgi.WSGIServer):
9595
handler_class = UpgradableWSGIHandler
96-
96+
9797
def __init__(self, address, *args, **kwargs):
9898
protocols = kwargs.pop('websocket_protocols', [])
9999
extensions = kwargs.pop('websocket_extensions', [])
100100
websocket = kwargs.pop('websocket_class', WebSocket)
101-
101+
102102
gevent.pywsgi.WSGIServer.__init__(self, address, *args, **kwargs)
103103
self.application = WebSocketUpgradeMiddleware(app=self.handler,
104104
protocols=protocols,
105105
extensions=extensions,
106-
websocket_class=websocket)
106+
websocket_class=websocket)
107107

108108
def handler(self, websocket):
109109
g = gevent.spawn(websocket.run)
@@ -112,7 +112,6 @@ def handler(self, websocket):
112112

113113
if __name__ == '__main__':
114114
import logging
115-
import sys
116115
logging.basicConfig(format='%(asctime)s %(message)s')
117116
logger = logging.getLogger()
118117
logger.setLevel(logging.DEBUG)
@@ -123,4 +122,3 @@ def handler(self, websocket):
123122
from ws4py.websocket import EchoWebSocket
124123
server = WebSocketServer(('127.0.0.1', 9001), websocket_class=EchoWebSocket)
125124
server.serve_forever()
126-

0 commit comments

Comments
 (0)