summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorengn33r <engn33r@users.noreply.github.com>2021-05-04 17:11:04 -0700
committerengn33r <engn33r@users.noreply.github.com>2021-05-04 17:11:04 -0700
commitdc3f5c469c6874c02544b64d2e1aa1c29370c16f (patch)
tree7054cd4ce9a2c866f6fad719549a65706d75f50d
parent70739568cf922c34d4707b86d5f6dfc367f6e12d (diff)
downloadwebsocket-client-dc3f5c469c6874c02544b64d2e1aa1c29370c16f.tar.gz
PEP8 style compliance fixes
-rw-r--r--.github/workflows/lint.yml9
-rwxr-xr-xbin/wsdump.py2
-rw-r--r--compliance/test_fuzzingclient.py5
-rw-r--r--docs/source/conf.py9
-rw-r--r--setup.py2
-rw-r--r--websocket/_abnf.py8
-rw-r--r--websocket/_app.py10
-rw-r--r--websocket/_cookiejar.py6
-rw-r--r--websocket/_core.py8
-rw-r--r--websocket/_exceptions.py1
-rw-r--r--websocket/_handshake.py7
-rw-r--r--websocket/_http.py19
-rw-r--r--websocket/_logging.py4
-rw-r--r--websocket/_socket.py1
-rw-r--r--websocket/_ssl_compat.py2
-rw-r--r--websocket/tests/test_abnf.py8
-rw-r--r--websocket/tests/test_app.py6
-rw-r--r--websocket/tests/test_cookiejar.py5
-rw-r--r--websocket/tests/test_http.py13
-rw-r--r--websocket/tests/test_websocket.py4
20 files changed, 63 insertions, 66 deletions
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
index b767753..75c5d88 100644
--- a/.github/workflows/lint.yml
+++ b/.github/workflows/lint.yml
@@ -13,8 +13,13 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: "3.9"
- - name: Perform flake8 linting
+
+ - name: Perform mandatory flake8 linting
run: |
pip install flake8
flake8 --version
- flake8 . --exit-zero --statistics
+ flake8 . --count --select=E11,E12,E22,E25,E26,E3,E71,E9,F63,F7,F82,F84,W3,W503 --ignore=F821 --show-source --statistics
+
+ - name: Perform optional exit-zero flake8 linting
+ run: |
+ flake8 . --exit-zero --statistics --count
diff --git a/bin/wsdump.py b/bin/wsdump.py
index 9a7b5bc..b270790 100755
--- a/bin/wsdump.py
+++ b/bin/wsdump.py
@@ -187,7 +187,7 @@ def main():
msg = None
if six.PY3 and opcode == websocket.ABNF.OPCODE_TEXT and isinstance(data, bytes):
data = str(data, "utf-8")
- if isinstance(data, bytes) and len(data)>2 and data[:2] == b'\037\213': # gzip magick
+ if isinstance(data, bytes) and len(data) > 2 and data[:2] == b'\037\213': # gzip magick
try:
data = "[gzip] " + str(gzip.decompress(data), "utf-8")
except:
diff --git a/compliance/test_fuzzingclient.py b/compliance/test_fuzzingclient.py
index 07be3aa..424ca48 100644
--- a/compliance/test_fuzzingclient.py
+++ b/compliance/test_fuzzingclient.py
@@ -36,7 +36,7 @@ count = json.loads(ws.recv())
ws.close()
-for case in range(1, count+1):
+for case in range(1, count + 1):
url = SERVER + '/runCase?case={0}&agent={1}'.format(case, AGENT)
status = websocket.STATUS_NORMAL
try:
@@ -45,7 +45,7 @@ for case in range(1, count+1):
opcode, msg = ws.recv_data()
if opcode == websocket.ABNF.OPCODE_TEXT:
msg.decode("utf-8")
- if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY):
+ if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY):
ws.send(msg, opcode)
except UnicodeDecodeError:
# this case is ok.
@@ -57,6 +57,7 @@ for case in range(1, count+1):
except Exception as e:
# status = websocket.STATUS_PROTOCOL_ERROR
print(traceback.format_exc())
+ print(e)
finally:
ws.close(status)
diff --git a/docs/source/conf.py b/docs/source/conf.py
index e55c109..9f44b01 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -12,21 +12,23 @@
#
import os
import sys
-#sys.path.insert(0, os.path.abspath('../..'))
+
sys.path.append(os.path.abspath('../..'))
sys.path.append(os.path.abspath('websocket'))
+
# Don't skip __init__, which is skipped by default
def skip(app, what, name, obj, would_skip, options):
if name == "__init__":
return False
return would_skip
+
def setup(app):
app.connect("autodoc-skip-member", skip)
-# -- Project information -----------------------------------------------------
+# -- Project information -----------------------------------------------------
project = 'websocket-client'
copyright = '2021'
author = 'liris'
@@ -41,7 +43,8 @@ release = '0.58.0'
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.coverage',
-'sphinx.ext.napoleon', 'sphinx.ext.viewcode', 'sphinx.ext.autosectionlabel']
+ 'sphinx.ext.napoleon', 'sphinx.ext.viewcode',
+ 'sphinx.ext.autosectionlabel']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
diff --git a/setup.py b/setup.py
index b7c07e9..8a666f1 100644
--- a/setup.py
+++ b/setup.py
@@ -33,7 +33,7 @@ install_requires = ["six"]
tests_require = []
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
- tests_require.append('unittest2==0.8.0')
+ tests_require.append('unittest2==0.8.0')
insecure_pythons = '2.6, ' + ', '.join("2.7.{pv}".format(pv=pv) for pv in range(10))
diff --git a/websocket/_abnf.py b/websocket/_abnf.py
index cc66704..80fbe1f 100644
--- a/websocket/_abnf.py
+++ b/websocket/_abnf.py
@@ -228,9 +228,9 @@ class ABNF(object):
if length >= ABNF.LENGTH_63:
raise ValueError("data is too long")
- frame_header = chr(self.fin << 7
- | self.rsv1 << 6 | self.rsv2 << 5 | self.rsv3 << 4
- | self.opcode)
+ frame_header = chr(self.fin << 7 |
+ self.rsv1 << 6 | self.rsv2 << 5 | self.rsv3 << 4 |
+ self.opcode)
if length < ABNF.LENGTH_7:
frame_header += chr(self.mask << 7 | length)
frame_header = six.b(frame_header)
@@ -287,7 +287,7 @@ class ABNF(object):
a = numpy.frombuffer(data, dtype="uint32")
masked = numpy.bitwise_xor(a, [_mask_key]).astype("uint32")
if len(data) > origlen:
- return masked.tobytes()[:origlen]
+ return masked.tobytes()[:origlen]
return masked.tobytes()
else:
_m = array.array("B", mask_key)
diff --git a/websocket/_app.py b/websocket/_app.py
index babea1d..a3f91a3 100644
--- a/websocket/_app.py
+++ b/websocket/_app.py
@@ -39,6 +39,7 @@ from . import _logging
__all__ = ["WebSocketApp"]
+
class Dispatcher:
"""
Dispatcher
@@ -50,12 +51,13 @@ class Dispatcher:
def read(self, sock, read_callback, check_callback):
while self.app.keep_running:
r, w, e = select.select(
- (self.app.sock.sock, ), (), (), self.ping_timeout)
+ (self.app.sock.sock, ), (), (), self.ping_timeout)
if r:
if not read_callback():
break
check_callback()
+
class SSLDispatcher:
"""
SSLDispatcher
@@ -343,9 +345,9 @@ class WebSocketApp(object):
has_pong_not_arrived_after_last_ping = self.last_pong_tm - self.last_ping_tm < 0
has_pong_arrived_too_late = self.last_pong_tm - self.last_ping_tm > ping_timeout
- if (self.last_ping_tm
- and has_timeout_expired
- and (has_pong_not_arrived_after_last_ping or has_pong_arrived_too_late)):
+ if (self.last_ping_tm and
+ has_timeout_expired and
+ (has_pong_not_arrived_after_last_ping or has_pong_arrived_too_late)):
raise WebSocketTimeoutException("ping/pong timed out")
return True
diff --git a/websocket/_cookiejar.py b/websocket/_cookiejar.py
index d0e049e..bc2891a 100644
--- a/websocket/_cookiejar.py
+++ b/websocket/_cookiejar.py
@@ -72,5 +72,7 @@ class SimpleCookieJar(object):
if host.endswith(domain) or host == domain[1:]:
cookies.append(self.jar.get(domain))
- return "; ".join(filter(None, sorted(["%s=%s" % (k, v.value) for cookie in filter(None, cookies) for k, v in
- cookie.items()])))
+ return "; ".join(filter(
+ None, sorted(
+ ["%s=%s" % (k, v.value) for cookie in filter(None, cookies) for k, v in cookie.items()]
+ )))
diff --git a/websocket/_core.py b/websocket/_core.py
index a3e484b..1ff80f0 100644
--- a/websocket/_core.py
+++ b/websocket/_core.py
@@ -44,6 +44,7 @@ from ._utils import *
__all__ = ['WebSocket', 'create_connection']
+
class WebSocket(object):
"""
Low level WebSocket interface.
@@ -254,8 +255,8 @@ class WebSocket(object):
if self.handshake_response.status in SUPPORTED_REDIRECT_STATUSES:
url = self.handshake_response.headers['location']
self.sock.close()
- self.sock, addrs = connect(url, self.sock_opt, proxy_info(**options),
- options.pop('socket', None))
+ self.sock, addrs = connect(url, self.sock_opt, proxy_info(**options),
+ options.pop('socket', None))
self.handshake_response = handshake(self.sock, *addrs, **options)
self.connected = True
except:
@@ -466,8 +467,7 @@ class WebSocket(object):
try:
self.connected = False
- self.send(struct.pack('!H', status) +
- reason, ABNF.OPCODE_CLOSE)
+ self.send(struct.pack('!H', status) + reason, ABNF.OPCODE_CLOSE)
sock_timeout = self.sock.gettimeout()
self.sock.settimeout(timeout)
start_time = time.time()
diff --git a/websocket/_exceptions.py b/websocket/_exceptions.py
index be56b6e..83c6e42 100644
--- a/websocket/_exceptions.py
+++ b/websocket/_exceptions.py
@@ -23,6 +23,7 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
"""
+
class WebSocketException(Exception):
"""
WebSocket exception class.
diff --git a/websocket/_handshake.py b/websocket/_handshake.py
index 1876919..c4d9d16 100644
--- a/websocket/_handshake.py
+++ b/websocket/_handshake.py
@@ -93,6 +93,7 @@ def _pack_hostname(hostname):
return hostname
+
def _get_handshake_headers(resource, host, port, options):
headers = [
"GET %s HTTP/1.1" % resource,
@@ -116,16 +117,16 @@ def _get_handshake_headers(resource, host, port, options):
key = _create_sec_websocket_key()
# Append Sec-WebSocket-Key & Sec-WebSocket-Version if not manually specified
- if not 'header' in options or 'Sec-WebSocket-Key' not in options['header']:
+ if 'header' not in options or 'Sec-WebSocket-Key' not in options['header']:
key = _create_sec_websocket_key()
headers.append("Sec-WebSocket-Key: %s" % key)
else:
key = options['header']['Sec-WebSocket-Key']
- if not 'header' in options or 'Sec-WebSocket-Version' not in options['header']:
+ if 'header' not in options or 'Sec-WebSocket-Version' not in options['header']:
headers.append("Sec-WebSocket-Version: %s" % VERSION)
- if not 'connection' in options or options['connection'] is None:
+ if 'connection' not in options or options['connection'] is None:
headers.append('Connection: Upgrade')
else:
headers.append(options['connection'])
diff --git a/websocket/_http.py b/websocket/_http.py
index 51dd39f..b0dad48 100644
--- a/websocket/_http.py
+++ b/websocket/_http.py
@@ -47,6 +47,7 @@ except:
pass
HAS_PYSOCKS = False
+
class proxy_info(object):
def __init__(self, **options):
@@ -80,15 +81,15 @@ def _open_proxied_socket(url, options, proxy):
rdns = True
sock = socks.create_connection(
- (hostname, port),
- proxy_type = ptype,
- proxy_addr = proxy.host,
- proxy_port = proxy.port,
- proxy_rdns = rdns,
- proxy_username = proxy.auth[0] if proxy.auth else None,
- proxy_password = proxy.auth[1] if proxy.auth else None,
- timeout = options.timeout,
- socket_options = DEFAULT_SOCKET_OPTION + options.sockopt
+ (hostname, port),
+ proxy_type=ptype,
+ proxy_addr=proxy.host,
+ proxy_port=proxy.port,
+ proxy_rdns=rdns,
+ proxy_username=proxy.auth[0] if proxy.auth else None,
+ proxy_password=proxy.auth[1] if proxy.auth else None,
+ timeout=options.timeout,
+ socket_options=DEFAULT_SOCKET_OPTION + options.sockopt
)
if is_secure:
diff --git a/websocket/_logging.py b/websocket/_logging.py
index 562af14..07d9009 100644
--- a/websocket/_logging.py
+++ b/websocket/_logging.py
@@ -40,7 +40,7 @@ __all__ = ["enableTrace", "dump", "error", "warning", "debug", "trace",
"isEnabledForError", "isEnabledForDebug", "isEnabledForTrace"]
-def enableTrace(traceable, handler = logging.StreamHandler()):
+def enableTrace(traceable, handler=logging.StreamHandler()):
"""
Turn on/off the traceability.
@@ -55,6 +55,7 @@ def enableTrace(traceable, handler = logging.StreamHandler()):
_logger.addHandler(handler)
_logger.setLevel(logging.DEBUG)
+
def dump(title, message):
if _traceEnabled:
_logger.debug("--- " + title + " ---")
@@ -86,5 +87,6 @@ def isEnabledForError():
def isEnabledForDebug():
return _logger.isEnabledFor(logging.DEBUG)
+
def isEnabledForTrace():
return _traceEnabled
diff --git a/websocket/_socket.py b/websocket/_socket.py
index f4f4ed9..2c383ed 100644
--- a/websocket/_socket.py
+++ b/websocket/_socket.py
@@ -27,7 +27,6 @@ import select
import socket
import six
-import sys
from ._exceptions import *
from ._ssl_compat import *
diff --git a/websocket/_ssl_compat.py b/websocket/_ssl_compat.py
index b8df0b3..9e201dd 100644
--- a/websocket/_ssl_compat.py
+++ b/websocket/_ssl_compat.py
@@ -48,6 +48,6 @@ except ImportError:
class SSLWantWriteError(Exception):
pass
- ssl = lambda: None
+ ssl = None
HAVE_SSL = False
diff --git a/websocket/tests/test_abnf.py b/websocket/tests/test_abnf.py
index 78c00aa..acce020 100644
--- a/websocket/tests/test_abnf.py
+++ b/websocket/tests/test_abnf.py
@@ -22,18 +22,11 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
"""
import os
-import os.path
-import socket
import websocket as ws
from websocket._abnf import *
import sys
sys.path[0:0] = [""]
-try:
- import socks
-except:
- HAS_PYSOCKS = False
-
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
import unittest2 as unittest
else:
@@ -82,4 +75,3 @@ class ABNFTest(unittest.TestCase):
if __name__ == "__main__":
unittest.main()
-
diff --git a/websocket/tests/test_app.py b/websocket/tests/test_app.py
index e711489..e5a7390 100644
--- a/websocket/tests/test_app.py
+++ b/websocket/tests/test_app.py
@@ -23,9 +23,7 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
import os
import os.path
-import socket
import websocket as ws
-from websocket._app import WebSocketApp
import sys
sys.path[0:0] = [""]
@@ -43,6 +41,7 @@ else:
TEST_WITH_INTERNET = os.environ.get('TEST_WITH_INTERNET', '0') == '1'
TRACEABLE = True
+
class WebSocketAppTest(unittest.TestCase):
class NotSetYet(object):
@@ -130,10 +129,9 @@ class WebSocketAppTest(unittest.TestCase):
app.close()
app = ws.WebSocketApp('wss://api-pub.bitfinex.com/ws/1', on_ping=on_ping, on_pong=on_pong)
- app.run_forever(ping_interval=2, ping_timeout=1) #, sslopt={"cert_reqs": ssl.CERT_NONE}
+ app.run_forever(ping_interval=2, ping_timeout=1) # , sslopt={"cert_reqs": ssl.CERT_NONE}
self.assertRaises(ws.WebSocketException, app.run_forever, ping_interval=2, ping_timeout=3, sslopt={"cert_reqs": ssl.CERT_NONE})
if __name__ == "__main__":
unittest.main()
-
diff --git a/websocket/tests/test_cookiejar.py b/websocket/tests/test_cookiejar.py
index 190c115..fc66e58 100644
--- a/websocket/tests/test_cookiejar.py
+++ b/websocket/tests/test_cookiejar.py
@@ -26,11 +26,6 @@ import unittest
from websocket._cookiejar import SimpleCookieJar
-try:
- import Cookie
-except:
- import http.cookies as Cookie
-
class CookieJarTest(unittest.TestCase):
def testAdd(self):
diff --git a/websocket/tests/test_http.py b/websocket/tests/test_http.py
index acad83d..0336ff7 100644
--- a/websocket/tests/test_http.py
+++ b/websocket/tests/test_http.py
@@ -23,22 +23,17 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
import os
import os.path
-import socket
import websocket as ws
-from websocket._http import proxy_info, connect, read_headers, _open_proxied_socket, _tunnel, _open_socket, _get_addrinfo_list
+from websocket._http import proxy_info, read_headers, _open_proxied_socket, _tunnel
import sys
sys.path[0:0] = [""]
-try:
- import socks
-except:
- HAS_PYSOCKS = False
-
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
import unittest2 as unittest
else:
import unittest
+
class SockMock(object):
def __init__(self):
self.data = []
@@ -75,12 +70,14 @@ class HeaderSockMock(SockMock):
with open(path, "rb") as f:
self.add_packet(f.read())
+
class OptsList():
def __init__(self):
self.timeout = 0
self.sockopt = []
+
class HttpTest(unittest.TestCase):
def testReadHeader(self):
@@ -107,6 +104,6 @@ class HttpTest(unittest.TestCase):
self.assertEqual(proxy_info(http_proxy_host="127.0.0.1", http_proxy_port="8080", proxy_type="http").port, "8080")
self.assertEqual(proxy_info(http_proxy_host="127.0.0.1", http_proxy_port="8080", proxy_type="http").auth, None)
+
if __name__ == "__main__":
unittest.main()
-
diff --git a/websocket/tests/test_websocket.py b/websocket/tests/test_websocket.py
index b6722b9..0d1d639 100644
--- a/websocket/tests/test_websocket.py
+++ b/websocket/tests/test_websocket.py
@@ -39,7 +39,6 @@ import websocket as ws
from websocket._handshake import _create_sec_websocket_key, \
_validate as _validate_header
from websocket._http import read_headers
-from websocket._url import get_proxy_info, parse_url
from websocket._utils import validate_utf8
if six.PY3:
@@ -135,8 +134,7 @@ class WebSocketTest(unittest.TestCase):
required_header = {
"upgrade": "websocket",
"connection": "upgrade",
- "sec-websocket-accept": "Kxep+hNu9n51529fGidYu7a3wO0=",
- }
+ "sec-websocket-accept": "Kxep+hNu9n51529fGidYu7a3wO0="}
self.assertEqual(_validate_header(required_header, key, None), (True, None))
header = required_header.copy()