From 8912574f74df7e1927f4e3bb5da0ab4de37cc39a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 7 Feb 2018 01:48:56 +0000 Subject: [PATCH] Add debugger_protocol.messages.look_up(). --- debugger_protocol/messages/__init__.py | 9 +++++++++ debugger_protocol/messages/wireformat.py | 14 ++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/debugger_protocol/messages/__init__.py b/debugger_protocol/messages/__init__.py index a6dfa91d..ccc56c04 100644 --- a/debugger_protocol/messages/__init__.py +++ b/debugger_protocol/messages/__init__.py @@ -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 diff --git a/debugger_protocol/messages/wireformat.py b/debugger_protocol/messages/wireformat.py index c58778ef..a31a2e82 100644 --- a/debugger_protocol/messages/wireformat.py +++ b/debugger_protocol/messages/wireformat.py @@ -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)