mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-06-07 00:28:45 +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 binascii
|
||||||
import hashlib
|
import hashlib
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
import mapping
|
||||||
|
import json
|
||||||
|
|
||||||
import tools
|
import tools
|
||||||
import messages_pb2 as proto
|
import messages_pb2 as proto
|
||||||
import types_pb2 as types
|
import types_pb2 as types
|
||||||
|
import protobuf_json
|
||||||
from trezorlib.debuglink import DebugLink
|
from trezorlib.debuglink import DebugLink
|
||||||
from mnemonic import Mnemonic
|
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]
|
return [ k for k, v in types.ButtonRequestType.items() if v == code][0]
|
||||||
|
|
||||||
def pprint(msg):
|
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):
|
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:
|
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):
|
def log(msg):
|
||||||
sys.stderr.write("%s\n" % msg)
|
sys.stderr.write("%s\n" % msg)
|
||||||
|
@ -43,6 +43,7 @@ __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
|
from google.protobuf.descriptor import FieldDescriptor as FD
|
||||||
|
import binascii
|
||||||
|
|
||||||
|
|
||||||
class ParseError(Exception): pass
|
class ParseError(Exception): pass
|
||||||
@ -76,8 +77,9 @@ def json2pb(pb, js):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def pb2json(pb, js={}):
|
def pb2json(pb):
|
||||||
''' convert google.protobuf.descriptor instance to JSON string '''
|
''' convert google.protobuf.descriptor instance to JSON string '''
|
||||||
|
js = {}
|
||||||
# fields = pb.DESCRIPTOR.fields #all fields
|
# fields = pb.DESCRIPTOR.fields #all fields
|
||||||
fields = pb.ListFields() #only filled (including extensions)
|
fields = pb.ListFields() #only filled (including extensions)
|
||||||
for field,value in fields:
|
for field,value in fields:
|
||||||
@ -108,7 +110,7 @@ _ftype2js = {
|
|||||||
FD.TYPE_BOOL: bool,
|
FD.TYPE_BOOL: bool,
|
||||||
FD.TYPE_STRING: unicode,
|
FD.TYPE_STRING: unicode,
|
||||||
#FD.TYPE_MESSAGE: pb2json, #handled specially
|
#FD.TYPE_MESSAGE: pb2json, #handled specially
|
||||||
FD.TYPE_BYTES: lambda x: x.encode('string_escape'),
|
FD.TYPE_BYTES: lambda x: binascii.hexlify(x),
|
||||||
FD.TYPE_UINT32: int,
|
FD.TYPE_UINT32: int,
|
||||||
FD.TYPE_ENUM: int,
|
FD.TYPE_ENUM: int,
|
||||||
FD.TYPE_SFIXED32: float,
|
FD.TYPE_SFIXED32: float,
|
||||||
@ -128,7 +130,7 @@ _js2ftype = {
|
|||||||
FD.TYPE_BOOL: bool,
|
FD.TYPE_BOOL: bool,
|
||||||
FD.TYPE_STRING: unicode,
|
FD.TYPE_STRING: unicode,
|
||||||
# FD.TYPE_MESSAGE: json2pb, #handled specially
|
# 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_UINT32: int,
|
||||||
FD.TYPE_ENUM: int,
|
FD.TYPE_ENUM: int,
|
||||||
FD.TYPE_SFIXED32: float,
|
FD.TYPE_SFIXED32: float,
|
||||||
|
Loading…
Reference in New Issue
Block a user