Fix imports and use of six

This commit is contained in:
Andrew Godwin 2018-02-01 21:02:37 -08:00
parent 51c2de3f8c
commit db68c43de1
4 changed files with 9 additions and 17 deletions

View File

@ -17,6 +17,6 @@ install:
script: script:
- pytest - pytest
- flake8 - flake8
- isort --check-only --recursive daphne tests - isort --check-only --diff --recursive daphne tests
- unify --check-only --recursive --quote \" daphne tests - unify --check-only --recursive --quote \" daphne tests

View File

@ -1,15 +1,13 @@
import logging import logging
import time import time
import traceback import traceback
from urllib.parse import unquote
import six
from twisted.internet.interfaces import IProtocolNegotiationFactory from twisted.internet.interfaces import IProtocolNegotiationFactory
from twisted.protocols.policies import ProtocolWrapper from twisted.protocols.policies import ProtocolWrapper
from twisted.web import http from twisted.web import http
from zope.interface import implementer from zope.interface import implementer
from six.moves.urllib_parse import unquote
from .utils import parse_x_forwarded_for from .utils import parse_x_forwarded_for
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -68,8 +66,8 @@ class WebRequest(http.Request):
if hasattr(self.client, "host") and hasattr(self.client, "port"): if hasattr(self.client, "host") and hasattr(self.client, "port"):
# client.host and host.host are byte strings in Python 2, but spec # client.host and host.host are byte strings in Python 2, but spec
# requires unicode string. # requires unicode string.
self.client_addr = [six.text_type(self.client.host), self.client.port] self.client_addr = [str(self.client.host), self.client.port]
self.server_addr = [six.text_type(self.host.host), self.host.port] self.server_addr = [str(self.host.host), self.host.port]
else: else:
self.client_addr = None self.client_addr = None
self.server_addr = None self.server_addr = None
@ -202,9 +200,6 @@ class WebRequest(http.Request):
self.setResponseCode(message["status"]) self.setResponseCode(message["status"])
# Write headers # Write headers
for header, value in message.get("headers", {}): for header, value in message.get("headers", {}):
# Shim code from old ASGI version, can be removed after a while
if isinstance(header, six.text_type):
header = header.encode("latin1")
self.responseHeaders.addRawHeader(header, value) self.responseHeaders.addRawHeader(header, value)
logger.debug("HTTP %s response started for %s", message["status"], self.client_addr) logger.debug("HTTP %s response started for %s", message["status"], self.client_addr)
elif message["type"] == "http.response.body": elif message["type"] == "http.response.body":
@ -283,7 +278,7 @@ class WebRequest(http.Request):
self.handle_reply({ self.handle_reply({
"type": "http.response.body", "type": "http.response.body",
"body": (self.error_template % { "body": (self.error_template % {
"title": six.text_type(status) + " " + status_text.decode("ascii"), "title": str(status) + " " + status_text.decode("ascii"),
"body": body, "body": body,
}).encode("utf8"), }).encode("utf8"),
}) })

View File

@ -1,13 +1,11 @@
import logging import logging
import time import time
import traceback import traceback
from urllib.parse import unquote
import six
from autobahn.twisted.websocket import ConnectionDeny, WebSocketServerFactory, WebSocketServerProtocol from autobahn.twisted.websocket import ConnectionDeny, WebSocketServerFactory, WebSocketServerProtocol
from twisted.internet import defer from twisted.internet import defer
from six.moves.urllib_parse import unquote
from .utils import parse_x_forwarded_for from .utils import parse_x_forwarded_for
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -44,8 +42,8 @@ class WebSocketProtocol(WebSocketServerProtocol):
peer = self.transport.getPeer() peer = self.transport.getPeer()
host = self.transport.getHost() host = self.transport.getHost()
if hasattr(peer, "host") and hasattr(peer, "port"): if hasattr(peer, "host") and hasattr(peer, "port"):
self.client_addr = [six.text_type(peer.host), peer.port] self.client_addr = [str(peer.host), peer.port]
self.server_addr = [six.text_type(host.host), host.port] self.server_addr = [str(host.host), host.port]
else: else:
self.client_addr = None self.client_addr = None
self.server_addr = None self.server_addr = None

View File

@ -1,9 +1,8 @@
import string import string
from urllib import parse
from hypothesis import strategies from hypothesis import strategies
from six.moves.urllib import parse
HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT"] HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT"]
# Unicode characters of the "Letter" category # Unicode characters of the "Letter" category