Add debugger_protocol.messages.look_up().

This commit is contained in:
Eric Snow 2018-02-07 01:48:56 +00:00
parent cabbc318f1
commit 8912574f74
2 changed files with 17 additions and 6 deletions

View file

@ -46,6 +46,14 @@ def register(cls, msgtype=None, typekey=None, key=None):
return cls
def look_up(data):
"""Return the message class for the given raw message data."""
msgtype = data['type']
typekey = MESSAGE_TYPE_KEYS[msgtype]
key = data[typekey]
return MESSAGE_TYPES[msgtype][key]
class Message(object):
"""The API for register-able message types."""
@ -63,5 +71,6 @@ class Message(object):
# Force registration.
from .message import ProtocolMessage, Request, Response, Event
from .requests import * # noqa
from .events import * # noqa

View file

@ -1,6 +1,6 @@
import json
from . import MESSAGE_TYPES, MESSAGE_TYPE_KEYS
from . import look_up
def read(stream):
@ -22,14 +22,16 @@ def read(stream):
data = json.loads(body.decode('utf-8'))
msgtype = data['type']
typekey = MESSAGE_TYPE_KEYS[msgtype]
key = data[typekey]
cls = MESSAGE_TYPES[msgtype][key]
cls = look_up(data)
return cls.from_data(**data)
def write(stream, msg):
"""Serialize the message and write it to the stream."""
raw = as_bytes(msg)
stream.write(raw)
def as_bytes(msg):
"""Return the raw bytes for the message."""
headers, body = _as_http_data(msg)