From 325312d11c23372743fb4dd541bd67672000d923 Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 20 Apr 2018 15:52:47 +0200 Subject: [PATCH] protobuf: move formatter function to protobuf where it belongs better --- trezorctl | 4 ++-- trezorlib/client.py | 37 ++----------------------------------- trezorlib/protobuf.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/trezorctl b/trezorctl index 1fc7394cc4..b12499efb8 100755 --- a/trezorctl +++ b/trezorctl @@ -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) diff --git a/trezorlib/client.py b/trezorlib/client.py index eedc6225c4..f5bdd973d1 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -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): diff --git a/trezorlib/protobuf.py b/trezorlib/protobuf.py index 287d7dabf6..b42347c3d8 100644 --- a/trezorlib/protobuf.py +++ b/trezorlib/protobuf.py @@ -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)