From 27826553200f113cf40606d8395856589030a57a Mon Sep 17 00:00:00 2001 From: slush0 Date: Sat, 1 Feb 2014 11:29:44 +0100 Subject: [PATCH] Monkeypatching of google.protobuf.text_format --- trezorlib/client.py | 17 ++--------------- trezorlib/tools.py | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/trezorlib/client.py b/trezorlib/client.py index 97e9e79a84..2943e48df2 100755 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -9,21 +9,8 @@ import messages_pb2 as proto import types_pb2 as types from api_blockchain import BlockchainApi -# === start monkeypatching: text formatting of protobuf messages -import google.protobuf.text_format -import google.protobuf.descriptor - -_oldPrintFieldValue = google.protobuf.text_format.PrintFieldValue - -def _customPrintFieldValue(field, value, out, indent=0, as_utf8=False, as_one_line=False): - if field.cpp_type == google.protobuf.descriptor.FieldDescriptor.CPPTYPE_STRING: - if str(field.GetOptions()).strip() == '[binary]:': # binary option set - _oldPrintFieldValue(field, 'hex(%s)' % binascii.hexlify(value), out, indent, as_utf8, as_one_line) - else: - _oldPrintFieldValue(field, value, out, indent, as_utf8, as_one_line) - -google.protobuf.text_format.PrintFieldValue = _customPrintFieldValue -# === end of monkeypatching +# monkeypatching: text formatting of protobuf messages +tools.monkeypatch_google_protobuf_text_format() def show_message(message): print "MESSAGE FROM DEVICE:", message diff --git a/trezorlib/tools.py b/trezorlib/tools.py index cb264a4a52..ac389d652f 100644 --- a/trezorlib/tools.py +++ b/trezorlib/tools.py @@ -1,4 +1,5 @@ import hashlib +import binascii Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest() @@ -54,7 +55,6 @@ def b58encode(v): return (__b58chars[0] * nPad) + result - def b58decode(v, length): """ decode v into a string of len bytes.""" long_value = 0L @@ -80,3 +80,21 @@ def b58decode(v, length): return None return result + +def monkeypatch_google_protobuf_text_format(): + # monkeypatching: text formatting of protobuf messages + import google.protobuf.text_format + import google.protobuf.descriptor + + _oldPrintFieldValue = google.protobuf.text_format.PrintFieldValue + + def _customPrintFieldValue(field, value, out, indent=0, as_utf8=False, as_one_line=False): + if field.cpp_type == google.protobuf.descriptor.FieldDescriptor.CPPTYPE_STRING and \ + str(field.GetOptions()).strip() == '[binary]:': # binary option set + _oldPrintFieldValue(field, 'hex(%s) str(%s)' % (binascii.hexlify(value), value), out, indent, as_utf8, as_one_line) + + else: + _oldPrintFieldValue(field, value, out, indent, as_utf8, as_one_line) + + google.protobuf.text_format.PrintFieldValue = _customPrintFieldValue +