Skip to content

Commit

Permalink
Rename Message -> WSMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jul 31, 2016
1 parent b0ecd21 commit 20442dc
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ CHANGES

- Introduce aiohttp.WSMessage officially

- Rename Message -> WSMessage

0.22.3 (07-26-2016)
-------------------

Expand Down
4 changes: 2 additions & 2 deletions aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .streams import * # noqa
from .multipart import * # noqa
from .client_ws import ClientWebSocketResponse # noqa
from ._ws_impl import WSMsgType, WSCloseCode, Message, WebSocketError # noqa
from ._ws_impl import WSMsgType, WSCloseCode, WSMessage, WebSocketError # noqa
from .file_sender import FileSender # noqa


Expand All @@ -35,5 +35,5 @@
multidict.__all__ + # noqa
multipart.__all__ + # noqa
('hdrs', 'FileSender', 'WSMsgType', 'MsgType', 'WSCloseCode',
'WebSocketError', 'Message',
'WebSocketError', 'WSMessage',
'ClientWebSocketResponse'))
32 changes: 16 additions & 16 deletions aiohttp/_ws_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


__all__ = ('WebSocketParser', 'WebSocketWriter', 'do_handshake',
'Message', 'WebSocketError', 'WSMsgType', 'WSCloseCode')
'WSMessage', 'WebSocketError', 'WSMsgType', 'WSCloseCode')


class WSCloseCode(IntEnum):
Expand Down Expand Up @@ -61,10 +61,11 @@ class WSMsgType(IntEnum):
MSG_SIZE = 2 ** 14


_MessageBase = collections.namedtuple('Message', ['tp', 'data', 'extra'])
_WSMessageBase = collections.namedtuple('_WSMessageBase',
['tp', 'data', 'extra'])


class Message(_MessageBase):
class WSMessage(_WSMessageBase):
def json(self, *, loads=json.loads):
"""Return parsed JSON data.
Expand All @@ -73,7 +74,7 @@ def json(self, *, loads=json.loads):
return loads(self.data)


CLOSED_MESSAGE = Message(WSMsgType.closed, None, None)
CLOSED_MESSAGE = WSMessage(WSMsgType.closed, None, None)


class WebSocketError(Exception):
Expand Down Expand Up @@ -101,22 +102,22 @@ def WebSocketParser(out, buf):
raise WebSocketError(
WSCloseCode.invalid_text,
'Invalid UTF-8 text message') from exc
msg = Message(WSMsgType.close, close_code, close_message)
msg = WSMessage(WSMsgType.close, close_code, close_message)
elif payload:
raise WebSocketError(
WSCloseCode.protocol_error,
'Invalid close frame: {} {} {!r}'.format(
fin, opcode, payload))
else:
msg = Message(WSMsgType.close, 0, '')
msg = WSMessage(WSMsgType.close, 0, '')

out.feed_data(msg, 0)

elif opcode == WSMsgType.ping:
out.feed_data(Message(WSMsgType.ping, payload, ''), len(payload))
out.feed_data(WSMessage(WSMsgType.ping, payload, ''), len(payload))

elif opcode == WSMsgType.pong:
out.feed_data(Message(WSMsgType.pong, payload, ''), len(payload))
out.feed_data(WSMessage(WSMsgType.pong, payload, ''), len(payload))

elif opcode not in (WSMsgType.text, WSMsgType.binary):
raise WebSocketError(
Expand All @@ -133,7 +134,7 @@ def WebSocketParser(out, buf):
# text message, Case 5.*
if _opcode == WSMsgType.ping:
out.feed_data(
Message(WSMsgType.ping, payload, ''), len(payload))
WSMessage(WSMsgType.ping, payload, ''), len(payload))
fin, _opcode, payload = yield from parse_frame(buf, True)
elif _opcode == WSMsgType.close:
if len(payload) >= 2:
Expand All @@ -149,15 +150,15 @@ def WebSocketParser(out, buf):
raise WebSocketError(
WSCloseCode.invalid_text,
'Invalid UTF-8 text message') from exc
msg = Message(WSMsgType.close, close_code,
close_message)
msg = WSMessage(WSMsgType.close, close_code,
close_message)
elif payload:
raise WebSocketError(
WSCloseCode.protocol_error,
'Invalid close frame: {} {} {!r}'.format(
fin, opcode, payload))
else:
msg = Message(WSMsgType.close, 0, '')
msg = WSMessage(WSMsgType.close, 0, '')

out.feed_data(msg, 0)
fin, _opcode, payload = yield from parse_frame(buf, True)
Expand All @@ -173,17 +174,16 @@ def WebSocketParser(out, buf):
if opcode == WSMsgType.text:
try:
text = b''.join(data).decode('utf-8')
out.feed_data(
Message(
WSMsgType.text, text, ''), len(text))
out.feed_data(WSMessage(WSMsgType.text, text, ''),
len(text))
except UnicodeDecodeError as exc:
raise WebSocketError(
WSCloseCode.invalid_text,
'Invalid UTF-8 text message') from exc
else:
data = b''.join(data)
out.feed_data(
Message(WSMsgType.binary, data, ''), len(data))
WSMessage(WSMsgType.binary, data, ''), len(data))


native_byteorder = sys.byteorder
Expand Down
6 changes: 3 additions & 3 deletions aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
import json

from ._ws_impl import Message, WebSocketError, WSMsgType, CLOSED_MESSAGE
from ._ws_impl import WSMessage, WebSocketError, WSMsgType, CLOSED_MESSAGE

PY_35 = sys.version_info >= (3, 5)

Expand Down Expand Up @@ -132,13 +132,13 @@ def receive(self):
except WebSocketError as exc:
self._close_code = exc.code
yield from self.close(code=exc.code)
return Message(WSMsgType.error, exc, None)
return WSMessage(WSMsgType.error, exc, None)
except Exception as exc:
self._exception = exc
self._closing = True
self._close_code = 1006
yield from self.close()
return Message(WSMsgType.error, exc, None)
return WSMessage(WSMsgType.error, exc, None)

if msg.tp == WSMsgType.close:
self._closing = True
Expand Down
8 changes: 4 additions & 4 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from . import hdrs, Timeout
from .errors import HttpProcessingError, ClientDisconnectedError
from ._ws_impl import (do_handshake, Message, WebSocketError,
from ._ws_impl import (do_handshake, WSMessage, WebSocketError,
WSMsgType, CLOSED_MESSAGE)
from .web_exceptions import (
HTTPBadRequest, HTTPMethodNotAllowed, HTTPInternalServerError)
Expand Down Expand Up @@ -229,17 +229,17 @@ def receive(self):
except WebSocketError as exc:
self._close_code = exc.code
yield from self.close(code=exc.code)
return Message(WSMsgType.error, exc, None)
return WSMessage(WSMsgType.error, exc, None)
except ClientDisconnectedError:
self._closed = True
self._close_code = 1006
return Message(WSMsgType.close, None, None)
return WSMessage(WSMsgType.close, None, None)
except Exception as exc:
self._exception = exc
self._closing = True
self._close_code = 1006
yield from self.close()
return Message(WSMsgType.error, exc, None)
return WSMessage(WSMsgType.error, exc, None)

if msg.tp == WSMsgType.close:
self._closing = True
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_close(self, m_req, m_os, WebSocketWriter):
aiohttp.ws_connect('http://test.org', loop=self.loop))
self.assertFalse(resp.closed)

msg = aiohttp.Message(aiohttp.MsgType.close, b'', b'')
msg = aiohttp.WSMessage(aiohttp.MsgType.close, b'', b'')
reader.read.return_value = helpers.create_future(self.loop)
reader.read.return_value.set_result(msg)

Expand Down

0 comments on commit 20442dc

Please sign in to comment.