Skip to content

Commit

Permalink
Rename msg.tp -> msg.type (#1078)
Browse files Browse the repository at this point in the history
fix #1019 (1)

Signed-off-by: Young Ho Cha <ganadist@gmail.com>
  • Loading branch information
ganadist authored and asvetlov committed Aug 15, 2016
1 parent 1cf6c18 commit 0f7cc5e
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 85 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ This is simple usage example:
await ws.prepare(request)
async for msg in ws:
if msg.tp == web.MsgType.text:
if msg.type == web.MsgType.text:
ws.send_str("Hello, {}".format(msg.data))
elif msg.tp == web.MsgType.binary:
elif msg.type == web.MsgType.binary:
ws.send_bytes(msg.data)
elif msg.tp == web.MsgType.close:
elif msg.type == web.MsgType.close:
break
return ws
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/_ws_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WSMsgType(IntEnum):


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


class WSMessage(_WSMessageBase):
Expand All @@ -79,6 +79,10 @@ def json(self, *, loads=json.loads):
"""
return loads(self.data)

@property
def tp(self):
return self.type


CLOSED_MESSAGE = WSMessage(WSMsgType.CLOSED, None, None)

Expand Down
19 changes: 10 additions & 9 deletions aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def close(self, *, code=1000, message=b''):
self._response.close()
return True

if msg.tp == WSMsgType.CLOSE:
if msg.type == WSMsgType.CLOSE:
self._close_code = msg.data
self._response.close()
return True
Expand Down Expand Up @@ -139,16 +139,16 @@ def receive(self):
yield from self.close()
return WSMessage(WSMsgType.ERROR, exc, None)

if msg.tp == WSMsgType.CLOSE:
if msg.type == WSMsgType.CLOSE:
self._closing = True
self._close_code = msg.data
if not self._closed and self._autoclose:
yield from self.close()
return msg
elif not self._closed:
if msg.tp == WSMsgType.PING and self._autoping:
if msg.type == WSMsgType.PING and self._autoping:
self.pong(msg.data)
elif msg.tp == WSMsgType.PONG and self._autoping:
elif msg.type == WSMsgType.PONG and self._autoping:
continue
else:
return msg
Expand All @@ -158,17 +158,18 @@ def receive(self):
@asyncio.coroutine
def receive_str(self):
msg = yield from self.receive()
if msg.tp != WSMsgType.TEXT:
if msg.type != WSMsgType.TEXT:
raise TypeError(
"Received message {}:{!r} is not str".format(msg.tp, msg.data))
"Received message {}:{!r} is not str".format(msg.type,
msg.data))
return msg.data

@asyncio.coroutine
def receive_bytes(self):
msg = yield from self.receive()
if msg.tp != WSMsgType.BINARY:
if msg.type != WSMsgType.BINARY:
raise TypeError(
"Received message {}:{!r} is not bytes".format(msg.tp,
"Received message {}:{!r} is not bytes".format(msg.type,
msg.data))
return msg.data

Expand All @@ -185,6 +186,6 @@ def __aiter__(self):
@asyncio.coroutine
def __anext__(self):
msg = yield from self.receive()
if msg.tp == WSMsgType.CLOSE:
if msg.type == WSMsgType.CLOSE:
raise StopAsyncIteration # NOQA
return msg
19 changes: 10 additions & 9 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def close(self, *, code=1000, message=b''):
self._exception = exc
return True

if msg.tp == WSMsgType.CLOSE:
if msg.type == WSMsgType.CLOSE:
self._close_code = msg.data
return True
else:
Expand Down Expand Up @@ -241,16 +241,16 @@ def receive(self):
yield from self.close()
return WSMessage(WSMsgType.ERROR, exc, None)

if msg.tp == WSMsgType.CLOSE:
if msg.type == WSMsgType.CLOSE:
self._closing = True
self._close_code = msg.data
if not self._closed and self._autoclose:
yield from self.close()
return msg
elif not self._closed:
if msg.tp == WSMsgType.PING and self._autoping:
if msg.type == WSMsgType.PING and self._autoping:
self.pong(msg.data)
elif msg.tp == WSMsgType.PONG and self._autoping:
elif msg.type == WSMsgType.PONG and self._autoping:
continue
else:
return msg
Expand All @@ -267,17 +267,18 @@ def receive_msg(self):
@asyncio.coroutine
def receive_str(self):
msg = yield from self.receive()
if msg.tp != WSMsgType.TEXT:
if msg.type != WSMsgType.TEXT:
raise TypeError(
"Received message {}:{!r} is not str".format(msg.tp, msg.data))
"Received message {}:{!r} is not str".format(msg.type,
msg.data))
return msg.data

@asyncio.coroutine
def receive_bytes(self):
msg = yield from self.receive()
if msg.tp != WSMsgType.BINARY:
if msg.type != WSMsgType.BINARY:
raise TypeError(
"Received message {}:{!r} is not bytes".format(msg.tp,
"Received message {}:{!r} is not bytes".format(msg.type,
msg.data))
return msg.data

Expand All @@ -297,6 +298,6 @@ def __aiter__(self):
@asyncio.coroutine
def __anext__(self):
msg = yield from self.receive()
if msg.tp == WSMsgType.CLOSE:
if msg.type == WSMsgType.CLOSE:
raise StopAsyncIteration # NOQA
return msg
2 changes: 1 addition & 1 deletion demos/chat/aiohttpdemo_chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def index(request):
while True:
msg = await resp.receive()

if msg.tp == web.MsgType.text:
if msg.type == web.MsgType.text:
for ws in request.app['sockets'].values():
if ws is not resp:
ws.send_str(json.dumps({'action': 'sent',
Expand Down
8 changes: 7 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ WebSocket utilities

Websocket message, returned by ``.receive()`` calls.

.. attribute:: tp
.. attribute:: type

Message type, :class:`WSMsgType` instance.

Expand Down Expand Up @@ -167,6 +167,12 @@ WebSocket utilities

:param loads: optional JSON decoder function.

.. attribute:: tp

Deprecated alias for :attr:`type`.

.. deprecated:: 1.0


aiohttp.errors module
---------------------
Expand Down
6 changes: 3 additions & 3 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -650,15 +650,15 @@ methods::
async with session.ws_connect('http://example.org/websocket') as ws:

async for msg in ws:
if msg.tp == aiohttp.WSMsgType.TEXT:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close cmd':
await ws.close()
break
else:
ws.send_str(msg.data + '/answer')
elif msg.tp == aiohttp.WSMsgType.CLOSED:
elif msg.type == aiohttp.WSMsgType.CLOSED:
break
elif msg.tp == aiohttp.WSMsgType.ERROR:
elif msg.type == aiohttp.WSMsgType.ERROR:
break


Expand Down
4 changes: 2 additions & 2 deletions docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,12 @@ with the peer::
await ws.prepare(request)

async for msg in ws:
if msg.tp == aiohttp.WSMsgType.TEXT:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
else:
ws.send_str(msg.data + '/answer')
elif msg.tp == aiohttp.WSMsgType.ERROR:
elif msg.type == aiohttp.WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())

Expand Down
14 changes: 7 additions & 7 deletions examples/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ def dispatch():
while True:
msg = yield from ws.receive()

if msg.tp == aiohttp.WSMsgType.TEXT:
if msg.type == aiohttp.WSMsgType.TEXT:
print('Text: ', msg.data.strip())
elif msg.tp == aiohttp.WSMsgType.BINARY:
elif msg.type == aiohttp.WSMsgType.BINARY:
print('Binary: ', msg.data)
elif msg.tp == aiohttp.WSMsgType.PING:
elif msg.type == aiohttp.WSMsgType.PING:
ws.pong()
elif msg.tp == aiohttp.WSMsgType.PONG:
elif msg.type == aiohttp.WSMsgType.PONG:
print('Pong received')
else:
if msg.tp == aiohttp.WSMsgType.CLOSE:
if msg.type == aiohttp.WSMsgType.CLOSE:
yield from ws.close()
elif msg.tp == aiohttp.WSMsgType.ERROR:
elif msg.type == aiohttp.WSMsgType.ERROR:
print('Error during receive %s' % ws.exception())
elif msg.tp == aiohttp.WSMsgType.CLOSED:
elif msg.type == aiohttp.WSMsgType.CLOSED:
pass

break
Expand Down
10 changes: 5 additions & 5 deletions examples/legacy/tcp_protocol_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def dispatch(self):

print('Message received: {}'.format(msg))

if msg.tp == MSG_PING:
if msg.type == MSG_PING:
writer.pong()
elif msg.tp == MSG_TEXT:
elif msg.type == MSG_TEXT:
writer.send_text('Re: ' + msg.data)
elif msg.tp == MSG_STOP:
elif msg.type == MSG_STOP:
self.transport.close()
break

Expand All @@ -123,10 +123,10 @@ def start_client(loop, host, port):
break

print('Message received: {}'.format(msg))
if msg.tp == MSG_PONG:
if msg.type == MSG_PONG:
writer.send_text(message)
print('data sent:', message)
elif msg.tp == MSG_TEXT:
elif msg.type == MSG_TEXT:
writer.stop()
print('stop sent')
break
Expand Down
2 changes: 1 addition & 1 deletion examples/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def wshandler(request):
request.app['sockets'].append(resp)

async for msg in resp:
if msg.tp == WSMsgType.TEXT:
if msg.type == WSMsgType.TEXT:
for ws in request.app['sockets']:
if ws is not resp:
ws.send_str(msg.data)
Expand Down
6 changes: 3 additions & 3 deletions tests/autobahn/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def client(loop, url, name):
while True:
msg = yield from ws.receive()

if msg.tp == aiohttp.MsgType.text:
if msg.type == aiohttp.MsgType.text:
ws.send_str(msg.data)
elif msg.tp == aiohttp.MsgType.binary:
elif msg.type == aiohttp.MsgType.binary:
ws.send_bytes(msg.data)
elif msg.tp == aiohttp.MsgType.close:
elif msg.type == aiohttp.MsgType.close:
yield from ws.close()
break
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/autobahn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def wshandler(request):
while True:
msg = yield from ws.receive()

if msg.tp == web.MsgType.text:
if msg.type == web.MsgType.text:
ws.send_str(msg.data)
elif msg.tp == web.MsgType.binary:
elif msg.type == web.MsgType.binary:
ws.send_bytes(msg.data)
elif msg.tp == web.MsgType.close:
elif msg.type == web.MsgType.close:
yield from ws.close()
break
else:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ def test_reader_read_exception(self, m_req, m_os, WebSocketWriter):
reader.read.return_value.set_exception(exc)

msg = self.loop.run_until_complete(resp.receive())
self.assertEqual(msg.tp, aiohttp.MsgType.ERROR)
self.assertEqual(msg.type, aiohttp.MsgType.ERROR)
self.assertIs(msg.type, msg.tp)
self.assertIs(resp.exception(), exc)

def test_receive_runtime_err(self):
Expand Down
Loading

0 comments on commit 0f7cc5e

Please sign in to comment.