mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-23 06:48:16 +00:00
protobuf: move formatter function to protobuf where it belongs better
This commit is contained in:
parent
579adb1871
commit
325312d11c
@ -27,7 +27,7 @@ import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
from trezorlib.client import TrezorClient, TrezorClientVerbose, CallException, format_protobuf
|
||||
from trezorlib.client import TrezorClient, TrezorClientVerbose, CallException
|
||||
from trezorlib.transport import get_transport, enumerate_devices
|
||||
from trezorlib import coins
|
||||
from trezorlib import messages as proto
|
||||
@ -111,7 +111,7 @@ def print_result(res, path, verbose, is_json):
|
||||
else:
|
||||
click.echo('%s: %s' % (k, v))
|
||||
elif isinstance(res, protobuf.MessageType):
|
||||
click.echo(format_protobuf(res))
|
||||
click.echo(protobuf.format_message(res))
|
||||
else:
|
||||
click.echo(res)
|
||||
|
||||
|
@ -34,10 +34,9 @@ from . import messages as proto
|
||||
from . import tools
|
||||
from . import mapping
|
||||
from . import nem
|
||||
from . import protobuf
|
||||
from . import stellar
|
||||
from .debuglink import DebugLink
|
||||
from .protobuf import MessageType
|
||||
|
||||
|
||||
if sys.version_info.major < 3:
|
||||
raise Exception("Trezorlib does not support Python 2 anymore.")
|
||||
@ -83,38 +82,6 @@ def get_buttonrequest_value(code):
|
||||
return [k for k in dir(proto.ButtonRequestType) if getattr(proto.ButtonRequestType, k) == code][0]
|
||||
|
||||
|
||||
def format_protobuf(pb, indent=0, sep=' ' * 4):
|
||||
def pformat_value(value, indent):
|
||||
level = sep * indent
|
||||
leadin = sep * (indent + 1)
|
||||
if isinstance(value, MessageType):
|
||||
return format_protobuf(value, indent, sep)
|
||||
if isinstance(value, list):
|
||||
lines = []
|
||||
lines.append('[')
|
||||
lines += [leadin + pformat_value(x, indent + 1) + ',' for x in value]
|
||||
lines.append(level + ']')
|
||||
return '\n'.join(lines)
|
||||
if isinstance(value, dict):
|
||||
lines = []
|
||||
lines.append('{')
|
||||
for key, val in sorted(value.items()):
|
||||
if val is None or val == []:
|
||||
continue
|
||||
if key == 'address_n' and isinstance(val, list):
|
||||
lines.append(leadin + key + ': ' + repr(val) + ',')
|
||||
else:
|
||||
lines.append(leadin + key + ': ' + pformat_value(val, indent + 1) + ',')
|
||||
lines.append(level + '}')
|
||||
return '\n'.join(lines)
|
||||
if isinstance(value, bytearray):
|
||||
return 'bytearray(0x{})'.format(binascii.hexlify(value).decode('ascii'))
|
||||
|
||||
return repr(value)
|
||||
|
||||
return pb.__class__.__name__ + ' ' + pformat_value(pb.__dict__, indent)
|
||||
|
||||
|
||||
def pprint(msg):
|
||||
msg_class = msg.__class__.__name__
|
||||
msg_size = msg.ByteSize()
|
||||
@ -122,7 +89,7 @@ def pprint(msg):
|
||||
or isinstance(msg, proto.Features):
|
||||
return "<%s> (%d bytes)" % (msg_class, msg_size)
|
||||
else:
|
||||
return "<%s> (%d bytes):\n%s" % (msg_class, msg_size, format_protobuf(msg))
|
||||
return "<%s> (%d bytes):\n%s" % (msg_class, msg_size, protobuf.format_message(msg))
|
||||
|
||||
|
||||
def log(msg):
|
||||
|
@ -39,7 +39,9 @@ required:
|
||||
>>> """
|
||||
'''
|
||||
|
||||
import binascii
|
||||
from io import BytesIO
|
||||
from typing import Any
|
||||
|
||||
_UVARINT_BUFFER = bytearray(1)
|
||||
|
||||
@ -344,3 +346,35 @@ def dump_message(writer, msg):
|
||||
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
|
||||
def format_message(pb: MessageType, indent: int=0, sep: str= ' ' * 4) -> str:
|
||||
def pformat_value(value: Any, indent: int) -> str:
|
||||
level = sep * indent
|
||||
leadin = sep * (indent + 1)
|
||||
if isinstance(value, MessageType):
|
||||
return format_message(value, indent, sep)
|
||||
if isinstance(value, list):
|
||||
lines = []
|
||||
lines.append('[')
|
||||
lines += [leadin + pformat_value(x, indent + 1) + ',' for x in value]
|
||||
lines.append(level + ']')
|
||||
return '\n'.join(lines)
|
||||
if isinstance(value, dict):
|
||||
lines = []
|
||||
lines.append('{')
|
||||
for key, val in sorted(value.items()):
|
||||
if val is None or val == []:
|
||||
continue
|
||||
if key == 'address_n' and isinstance(val, list):
|
||||
lines.append(leadin + key + ': ' + repr(val) + ',')
|
||||
else:
|
||||
lines.append(leadin + key + ': ' + pformat_value(val, indent + 1) + ',')
|
||||
lines.append(level + '}')
|
||||
return '\n'.join(lines)
|
||||
if isinstance(value, bytearray):
|
||||
return 'bytearray(0x{})'.format(binascii.hexlify(value).decode('ascii'))
|
||||
|
||||
return repr(value)
|
||||
|
||||
return pb.__class__.__name__ + ' ' + pformat_value(pb.__dict__, indent)
|
||||
|
Loading…
Reference in New Issue
Block a user