diff --git a/thriftpy2/_compat.py b/thriftpy2/_compat.py index 7229d2a..84d77c9 100644 --- a/thriftpy2/_compat.py +++ b/thriftpy2/_compat.py @@ -11,6 +11,8 @@ import platform import sys +from urllib.parse import urlparse +from urllib.request import urlopen PY3 = sys.version_info[0] == 3 PY35 = sys.version_info >= (3, 5) @@ -19,24 +21,12 @@ UNIX = platform.system() in ("Linux", "Darwin") CYTHON = UNIX and not PYPY # Cython always disabled in pypy and windows -if PY3: - text_type = str - string_types = (str,) - from urllib.request import urlopen - from urllib.parse import urlparse - - def u(s): - return s -else: - text_type = unicode # noqa - string_types = (str, unicode) # noqa - from urllib2 import urlopen # noqa - from urlparse import urlparse # noqa - - def u(s): - if not isinstance(s, text_type): - s = s.decode("utf-8") - return s +text_type = str +string_types = (str,) + + +def u(s): + return s def with_metaclass(meta, *bases): diff --git a/thriftpy2/contrib/aio/rpc.py b/thriftpy2/contrib/aio/rpc.py index a6e4254..6ee8445 100644 --- a/thriftpy2/contrib/aio/rpc.py +++ b/thriftpy2/contrib/aio/rpc.py @@ -1,20 +1,13 @@ # -*- coding: utf-8 -*- +import urllib import warnings -from thriftpy2._compat import PY3 -if PY3: - import urllib -else: - import urllib2 as urllib - import urlparse - urllib.parse = urlparse - -from .processor import TAsyncProcessor from .client import TAsyncClient +from .processor import TAsyncProcessor from .protocol.binary import TAsyncBinaryProtocolFactory -from .transport.buffered import TAsyncBufferedTransportFactory -from .socket import TAsyncSocket, TAsyncServerSocket from .server import TAsyncServer +from .socket import TAsyncServerSocket, TAsyncSocket +from .transport.buffered import TAsyncBufferedTransportFactory async def make_client(service, host='localhost', port=9090, unix_socket=None, diff --git a/thriftpy2/http.py b/thriftpy2/http.py index ef0a678..ae18c3a 100644 --- a/thriftpy2/http.py +++ b/thriftpy2/http.py @@ -32,33 +32,20 @@ from __future__ import absolute_import +import http.client as http_client +import http.server as http_server import os import socket import sys +import urllib from contextlib import contextmanager from io import BytesIO -from thriftpy2._compat import PY3 -if PY3: - import http.client as http_client - import http.server as http_server - import urllib -else: - import httplib as http_client - import BaseHTTPServer as http_server - import urllib2 as urllib - import urlparse - urllib.parse = urlparse - urllib.parse.quote = urllib.quote - - -from thriftpy2.thrift import TProcessor, TClient -from thriftpy2.server import TServer -from thriftpy2.transport import TTransportBase, TMemoryBuffer - from thriftpy2.protocol import TBinaryProtocolFactory -from thriftpy2.transport import TBufferedTransportFactory - +from thriftpy2.server import TServer +from thriftpy2.thrift import TClient, TProcessor +from thriftpy2.transport import (TBufferedTransportFactory, TMemoryBuffer, + TTransportBase) HTTP_URI = '{scheme}://{host}:{port}{path}' DEFAULT_HTTP_CLIENT_TIMEOUT_MS = 30000 # 30 seconds diff --git a/thriftpy2/rpc.py b/thriftpy2/rpc.py index dbc75dd..c9e7664 100644 --- a/thriftpy2/rpc.py +++ b/thriftpy2/rpc.py @@ -4,27 +4,15 @@ import contextlib import socket +import urllib import warnings -from thriftpy2._compat import PY3, PY35 -if PY3: - import urllib -else: - import urllib2 as urllib - import urlparse - urllib.parse = urlparse - urllib.parse.quote = urllib.quote - +from thriftpy2._compat import PY35 from thriftpy2.protocol import TBinaryProtocolFactory from thriftpy2.server import TThreadedServer -from thriftpy2.thrift import TProcessor, TClient -from thriftpy2.transport import ( - TBufferedTransportFactory, - TServerSocket, - TSSLServerSocket, - TSocket, - TSSLSocket, -) +from thriftpy2.thrift import TClient, TProcessor +from thriftpy2.transport import (TBufferedTransportFactory, TServerSocket, + TSocket, TSSLServerSocket, TSSLSocket) def make_client(service, host="localhost", port=9090, unix_socket=None, @@ -137,7 +125,6 @@ def client_context(service, host="localhost", port=9090, unix_socket=None, if PY35: - from thriftpy2.contrib.aio.rpc import ( # noqa - make_server as make_aio_server, - make_client as make_aio_client - ) + from thriftpy2.contrib.aio.rpc import make_client as make_aio_client + from thriftpy2.contrib.aio.rpc import \ + make_server as make_aio_server # noqa diff --git a/thriftpy2/tornado.py b/thriftpy2/tornado.py index 323e1b5..95e3804 100644 --- a/thriftpy2/tornado.py +++ b/thriftpy2/tornado.py @@ -17,32 +17,22 @@ from __future__ import absolute_import +import logging +import socket +import struct +import urllib from contextlib import contextmanager -from tornado import tcpserver, iostream, gen -from tornado import version as tornado_version -from io import BytesIO from datetime import timedelta +from io import BytesIO -from .transport import TTransportException, TTransportBase -from .transport.memory import TMemoryBuffer -from .thrift import TApplicationException, TProcessor, TClient +from tornado import gen, iostream, tcpserver +from tornado import version as tornado_version # TODO need TCyTornadoStreamTransport to work with cython binary protocol from .protocol.binary import TBinaryProtocolFactory -from ._compat import PY3 - -if PY3: - import urllib -else: - import urllib2 as urllib - import urlparse - - urllib.parse = urlparse - urllib.parse.quote = urllib.quote - -import logging -import socket -import struct +from .thrift import TApplicationException, TClient, TProcessor +from .transport import TTransportBase, TTransportException +from .transport.memory import TMemoryBuffer try: from tornado.locks import Lock