mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 07:28:10 +00:00
refactor pprint (add new inspection fields, but comment them)
This commit is contained in:
parent
69dda6c61c
commit
2d2e31fa50
1
tests/.gitignore
vendored
Normal file
1
tests/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.out
|
@ -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)
|
||||
|
@ -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 <dpp@dpp.su>'
|
||||
__version__='0.0.5'
|
||||
__author__='Paul Dovbush <dpp@dpp.su>'
|
||||
|
||||
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user