From 2d2e31fa50b19b9bc4f301eb1fe0ea1d4167fa91 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 27 Jun 2014 17:02:44 +0200 Subject: [PATCH] refactor pprint (add new inspection fields, but comment them) --- tests/.gitignore | 1 + trezorlib/client.py | 14 ++++++++++++-- trezorlib/protobuf_json.py | 24 +++++++++++++----------- 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 tests/.gitignore diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..f47cb20 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +*.out diff --git a/trezorlib/client.py b/trezorlib/client.py index 34ecefd..495a6a1 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -4,10 +4,13 @@ import time import binascii import hashlib import unicodedata +import mapping +import json import tools import messages_pb2 as proto import types_pb2 as types +import protobuf_json from trezorlib.debuglink import DebugLink from mnemonic import Mnemonic @@ -19,10 +22,17 @@ def get_buttonrequest_value(code): return [ k for k, v in types.ButtonRequestType.items() if v == code][0] def pprint(msg): + msg_class = msg.__class__.__name__ + msg_size = msg.ByteSize() + """ + msg_ser = msg.SerializeToString() + msg_id = mapping.get_type(msg) + msg_json = json.dumps(protobuf_json.pb2json(msg)) + """ if isinstance(msg, proto.FirmwareUpload): - return "<%s> (%d bytes):\n" % (msg.__class__.__name__, msg.ByteSize()) + return "<%s> (%d bytes):\n" % (msg_class, msg_size) else: - return "<%s> (%d bytes):\n%s" % (msg.__class__.__name__, msg.ByteSize(), msg) + return "<%s> (%d bytes):\n%s" % (msg_class, msg_size, msg) def log(msg): sys.stderr.write("%s\n" % msg) diff --git a/trezorlib/protobuf_json.py b/trezorlib/protobuf_json.py index 2704fef..0ba7d26 100644 --- a/trezorlib/protobuf_json.py +++ b/trezorlib/protobuf_json.py @@ -37,12 +37,13 @@ Provide serialization and de-serialization of Google's protobuf Messages into/fr # Note that preservation of unknown fields is currently not available for Python (c) google docs # extensions is not supported from 0.0.5 (due to gpb2.3 changes) -__version__ = '0.0.5' -__author__ = 'Paul Dovbush ' +__version__='0.0.5' +__author__='Paul Dovbush ' -import json # py2.6+ TODO: add support for other JSON serialization modules +import json # py2.6+ TODO: add support for other JSON serialization modules from google.protobuf.descriptor import FieldDescriptor as FD +import binascii class ParseError(Exception): pass @@ -58,7 +59,7 @@ def json2pb(pb, js): elif field.type in _js2ftype: ftype = _js2ftype[field.type] else: - raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,)) + raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type, )) value = js[field.name] if field.label == FD.LABEL_REPEATED: pb_value = getattr(pb, field.name, None) @@ -76,17 +77,18 @@ def json2pb(pb, js): -def pb2json(pb, js={}): +def pb2json(pb): ''' convert google.protobuf.descriptor instance to JSON string ''' + js = {} # fields = pb.DESCRIPTOR.fields #all fields - fields = pb.ListFields() # only filled (including extensions) - for field, value in fields: + fields = pb.ListFields() #only filled (including extensions) + for field,value in fields: if field.type == FD.TYPE_MESSAGE: ftype = pb2json elif field.type in _ftype2js: ftype = _ftype2js[field.type] else: - raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,)) + raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type, )) if field.label == FD.LABEL_REPEATED: js_value = [] for v in value: @@ -107,8 +109,8 @@ _ftype2js = { FD.TYPE_FIXED32: float, FD.TYPE_BOOL: bool, FD.TYPE_STRING: unicode, - # FD.TYPE_MESSAGE: pb2json, #handled specially - FD.TYPE_BYTES: lambda x: x.encode('string_escape'), + #FD.TYPE_MESSAGE: pb2json, #handled specially + FD.TYPE_BYTES: lambda x: binascii.hexlify(x), FD.TYPE_UINT32: int, FD.TYPE_ENUM: int, FD.TYPE_SFIXED32: float, @@ -128,7 +130,7 @@ _js2ftype = { FD.TYPE_BOOL: bool, FD.TYPE_STRING: unicode, # FD.TYPE_MESSAGE: json2pb, #handled specially - FD.TYPE_BYTES: lambda x: x.decode('string_escape'), + FD.TYPE_BYTES: lambda x: binascii.unhexlify(x), FD.TYPE_UINT32: int, FD.TYPE_ENUM: int, FD.TYPE_SFIXED32: float,