wsdump, test_fuzzingclient, websocket.*, test_websocket: Organise imports

Previously, because of the multiple instances of `from foo import *`, names were
imported from modules that had themselves imported them, instead of from the
place of definition. This commit therefore does the following:

- Declares `__all__` in every module that imports anything, so that
`from foo import *` is sane.
- Sorts the imports based on conventions, similar to the output of `isort`.
- Places all conditional imports after unconditional imports, except where that
isn't valid.
- Imports local names from the modules where they are defined, except when
importing the package itself.
This commit is contained in:
Allan Lewis 2016-04-27 15:00:56 +01:00
parent 9f138c94e7
commit 525fcfc0fb
14 changed files with 83 additions and 68 deletions

@ -2,12 +2,15 @@
import argparse import argparse
import code import code
import six
import sys import sys
import threading import threading
import time import time
import websocket
import six
from six.moves.urllib.parse import urlparse from six.moves.urllib.parse import urlparse
import websocket
try: try:
import readline import readline
except ImportError: except ImportError:

@ -1,13 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
import websocket
import json import json
import traceback import traceback
import six
import websocket
SERVER = 'ws://127.0.0.1:8642' SERVER = 'ws://127.0.0.1:8642'
AGENT = 'py-websockets-client' AGENT = 'py-websockets-client'

@ -19,7 +19,10 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
from ._core import *
from ._app import WebSocketApp from ._app import WebSocketApp
from ._core import *
from ._exceptions import *
from ._logging import *
from ._socket import *
__version__ = "0.37.0" __version__ = "0.37.0"

@ -19,10 +19,12 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
import six
import array import array
import struct
import os import os
import struct
import six
from ._exceptions import * from ._exceptions import *
from ._utils import validate_utf8 from ._utils import validate_utf8
@ -44,6 +46,22 @@ except ImportError:
else: else:
return _d.tostring() return _d.tostring()
__all__ = [
'ABNF', 'continuous_frame', 'frame_buffer',
'STATUS_NORMAL',
'STATUS_GOING_AWAY',
'STATUS_PROTOCOL_ERROR',
'STATUS_UNSUPPORTED_DATA_TYPE',
'STATUS_STATUS_NOT_AVAILABLE',
'STATUS_ABNORMAL_CLOSED',
'STATUS_INVALID_PAYLOAD',
'STATUS_POLICY_VIOLATION',
'STATUS_MESSAGE_TOO_BIG',
'STATUS_INVALID_EXTENSION',
'STATUS_UNEXPECTED_CONDITION',
'STATUS_TLS_HANDSHAKE_ERROR',
]
# closing frame status codes. # closing frame status codes.
STATUS_NORMAL = 1000 STATUS_NORMAL = 1000
STATUS_GOING_AWAY = 1001 STATUS_GOING_AWAY = 1001

@ -23,17 +23,18 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
""" """
WebSocketApp provides higher level APIs. WebSocketApp provides higher level APIs.
""" """
import select
import sys
import threading import threading
import time import time
import traceback import traceback
import sys
import select
import six import six
from ._abnf import ABNF
from ._core import WebSocket, getdefaulttimeout from ._core import WebSocket, getdefaulttimeout
from ._exceptions import * from ._exceptions import *
from ._logging import * from ._logging import *
from ._abnf import ABNF
__all__ = ["WebSocketApp"] __all__ = ["WebSocketApp"]

@ -21,24 +21,22 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
""" """
from __future__ import print_function from __future__ import print_function
import socket
import struct
import threading
import six import six
import socket
if six.PY3:
from base64 import encodebytes as base64encode
else:
from base64 import encodestring as base64encode
import threading
# websocket modules # websocket modules
from ._abnf import * from ._abnf import *
from ._exceptions import *
from ._handshake import *
from ._http import *
from ._logging import *
from ._socket import * from ._socket import *
from ._utils import * from ._utils import *
from ._logging import *
from ._http import * __all__ = ['WebSocket', 'create_connection']
from ._handshake import *
""" """
websocket python client. websocket python client.

@ -19,21 +19,21 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
import six
if six.PY3:
from base64 import encodebytes as base64encode
else:
from base64 import encodestring as base64encode
import hashlib import hashlib
import hmac import hmac
import os import os
from ._logging import * import six
from ._socket import*
from ._http import *
from ._exceptions import * from ._exceptions import *
from ._http import *
from ._logging import *
from ._socket import *
if six.PY3:
from base64 import encodebytes as base64encode
else:
from base64 import encodestring as base64encode
__all__ = ["handshake_response", "handshake"] __all__ = ["handshake_response", "handshake"]

@ -19,24 +19,24 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
import six
import socket
import errno import errno
import os import os
import socket
import sys import sys
import six
from ._exceptions import *
from ._logging import *
from ._socket import*
from ._ssl_compat import *
from ._url import *
if six.PY3: if six.PY3:
from base64 import encodebytes as base64encode from base64 import encodebytes as base64encode
else: else:
from base64 import encodestring as base64encode from base64 import encodestring as base64encode
from ._logging import *
from ._url import *
from ._socket import*
from ._exceptions import *
from ._ssl_compat import *
__all__ = ["proxy_info", "connect", "read_headers"] __all__ = ["proxy_info", "connect", "read_headers"]

@ -19,7 +19,6 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
import logging import logging
_logger = logging.getLogger('websocket') _logger = logging.getLogger('websocket')

@ -19,13 +19,13 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
import socket import socket
import six import six
from ._exceptions import * from ._exceptions import *
from ._utils import *
from ._ssl_compat import * from ._ssl_compat import *
from ._utils import *
DEFAULT_SOCKET_OPTION = [(socket.SOL_TCP, socket.TCP_NODELAY, 1)] DEFAULT_SOCKET_OPTION = [(socket.SOL_TCP, socket.TCP_NODELAY, 1)]
if hasattr(socket, "SO_KEEPALIVE"): if hasattr(socket, "SO_KEEPALIVE"):

@ -19,7 +19,6 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
__all__ = ["HAVE_SSL", "ssl", "SSLError"] __all__ = ["HAVE_SSL", "ssl", "SSLError"]
try: try:

@ -19,9 +19,9 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
import os
from six.moves.urllib.parse import urlparse from six.moves.urllib.parse import urlparse
import os
__all__ = ["parse_url", "get_proxy_info"] __all__ = ["parse_url", "get_proxy_info"]

@ -19,7 +19,6 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
Boston, MA 02110-1335 USA Boston, MA 02110-1335 USA
""" """
import six import six
__all__ = ["NoLock", "validate_utf8", "extract_err_message"] __all__ = ["NoLock", "validate_utf8", "extract_err_message"]

@ -1,40 +1,39 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import six
import sys import sys
sys.path[0:0] = [""] sys.path[0:0] = [""]
import os import os
import os.path import os.path
import socket import socket
try:
from ssl import SSLError
except ImportError:
# dummy class of SSLError for ssl none-support environment.
class SSLError(Exception):
pass
if sys.version_info[0] == 2 and sys.version_info[1] < 7: import six
import unittest2 as unittest
else:
import unittest
# websocket-client
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: if six.PY3:
from base64 import decodebytes as base64decode from base64 import decodebytes as base64decode
else: else:
from base64 import decodestring as base64decode from base64 import decodestring as base64decode
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
import unittest2 as unittest
else:
import unittest
# websocket-client try:
import websocket as ws from ssl import SSLError
from websocket._handshake import _create_sec_websocket_key except ImportError:
from websocket._url import parse_url, get_proxy_info # dummy class of SSLError for ssl none-support environment.
from websocket._utils import validate_utf8 class SSLError(Exception):
from websocket._handshake import _validate as _validate_header pass
from websocket._http import read_headers
# Skip test to access the internet. # Skip test to access the internet.
TEST_WITH_INTERNET = os.environ.get('TEST_WITH_INTERNET', '0') == '1' TEST_WITH_INTERNET = os.environ.get('TEST_WITH_INTERNET', '0') == '1'