From 40ff849228c218ca8ca3ce5eacc3faafdd76a252 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 3 Apr 2018 21:25:07 +0200 Subject: [PATCH] flake8: make flake8 better and more strict this removes some unused variables and also catches a couple bugs --- .flake8 | 8 +--- .travis.yml | 2 +- tools/signtest.py | 74 ++++++++++++++++----------------- trezorctl | 2 +- trezorlib/client.py | 8 +--- trezorlib/transport/__init__.py | 2 +- trezorlib/transport/udp.py | 1 - 7 files changed, 44 insertions(+), 53 deletions(-) diff --git a/.flake8 b/.flake8 index 52c02f4bfe..a89b939558 100644 --- a/.flake8 +++ b/.flake8 @@ -1,17 +1,13 @@ [flake8] filename = *.py, - trezorctl + ./trezorctl exclude = .tox/, build/, dist/, - trezorlib/*_pb2.py + vendor/, ignore = - # F821 undefined name 'unicode' - F821, - # F841 local variable is assigned to but never used - F841, # F401: module imported but unused F401, # F403: used import * diff --git a/.travis.yml b/.travis.yml index ff9a6ccfae..84d548e0f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ install: script: - python setup.py install - - flake8 . trezorctl --exclude=vendor + - flake8 - tox notifications: diff --git a/tools/signtest.py b/tools/signtest.py index 7ec2f4f5c7..547c3c6953 100755 --- a/tools/signtest.py +++ b/tools/signtest.py @@ -111,7 +111,7 @@ class MyTxApiBitcoin(object): txser = self.serialize_tx(t) txhash = tools.Hash(txser)[::-1] - outi = self.inputs.append( + self.inputs.append( proto_types.TxInputType( address_n=self.client.expand_path("44'/0'/0'/0/%d" % idx), script_type=( @@ -178,42 +178,42 @@ def main(): # Get the first address of first BIP44 account # (should be the same address as shown in wallet.trezor.io) - outputs = [ - proto_types.TxOutputType( - amount=0, - script_type=proto_types.PAYTOADDRESS, - address='p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm' - # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') - # address_n=client.expand_path("44'/0'/0'/0/35"), - # address='3PUxV6Cc4udQZQsJhArVUzvvVoKC8ohkAj', - ), - # proto_types.TxOutputType( - # amount=0, - # script_type=proto_types.PAYTOOPRETURN, - # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') - # ), - # proto_types.TxOutputType( - # amount= 8120, - # script_type=proto_types.PAYTOADDRESS, - # address_n=client.expand_path("44'/1'/0'/1/0"), - # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', - # address='14KRxYgFc7Se8j7MDdrK5PTNv8meq4GivK', - # ), - # proto_types.TxOutputType( - # amount= 18684 - 2000, - # script_type=proto_types.PAYTOADDRESS, - # address_n=client.expand_path("44'/0'/0'/0/7"), - # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', - # # address='1s9TSqr3PHZdXGrYws59Uaf5SPqavH43z', - # ), - # proto_types.TxOutputType( - # amount= 1000, - # script_type=proto_types.PAYTOADDRESS, - # # address_n=client.expand_path("44'/0'/0'/0/18"), - # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', - # # address='1NcMqUvyWv1K3Zxwmx5sqfj7ZEmPCSdJFM', - # ), - ] + # outputs = [ + # proto_types.TxOutputType( + # amount=0, + # script_type=proto_types.PAYTOADDRESS, + # address='p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm' + # # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') + # # address_n=client.expand_path("44'/0'/0'/0/35"), + # # address='3PUxV6Cc4udQZQsJhArVUzvvVoKC8ohkAj', + # ), + # proto_types.TxOutputType( + # amount=0, + # script_type=proto_types.PAYTOOPRETURN, + # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') + # ), + # proto_types.TxOutputType( + # amount= 8120, + # script_type=proto_types.PAYTOADDRESS, + # address_n=client.expand_path("44'/1'/0'/1/0"), + # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', + # address='14KRxYgFc7Se8j7MDdrK5PTNv8meq4GivK', + # ), + # proto_types.TxOutputType( + # amount= 18684 - 2000, + # script_type=proto_types.PAYTOADDRESS, + # address_n=client.expand_path("44'/0'/0'/0/7"), + # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', + # # address='1s9TSqr3PHZdXGrYws59Uaf5SPqavH43z', + # ), + # proto_types.TxOutputType( + # amount= 1000, + # script_type=proto_types.PAYTOADDRESS, + # # address_n=client.expand_path("44'/0'/0'/0/18"), + # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', + # # address='1NcMqUvyWv1K3Zxwmx5sqfj7ZEmPCSdJFM', + # ), + # ] # (signatures, serialized_tx) = client.sign_tx('Testnet', inputs, outputs) (signatures, serialized_tx) = client.sign_tx('Bitcoin', txstore.get_inputs(), txstore.get_outputs()) diff --git a/trezorctl b/trezorctl index b9ad180c26..d59a6c91fb 100755 --- a/trezorctl +++ b/trezorctl @@ -233,7 +233,7 @@ def set_homescreen(connect, filename): elif filename.endswith('.toif'): img = open(filename, 'rb').read() if img[:8] != b'TOIf\x90\x00\x90\x00': - raise CallException(types.Failure_DataError, 'File is not a TOIF file with size of 144x144') + raise CallException(proto.FailureType.DataError, 'File is not a TOIF file with size of 144x144') else: from PIL import Image im = Image.open(filename) diff --git a/trezorlib/client.py b/trezorlib/client.py index 398f94b3ee..ffded24395 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -42,11 +42,6 @@ from .protobuf import MessageType if sys.version_info.major < 3: raise Exception("Trezorlib does not support Python 2 anymore.") -# try: -# from PIL import Image -# SCREENSHOT = True -# except: -# SCREENSHOT = False SCREENSHOT = False @@ -424,6 +419,7 @@ class DebugLinkMixin(object): def call_raw(self, msg): if SCREENSHOT and self.debug: + from PIL import Image layout = self.debug.read_layout() im = Image.new("RGB", (128, 64)) pix = im.load() @@ -804,7 +800,7 @@ class ProtocolMixin(object): @session def sign_tx(self, coin_name, inputs, outputs, version=None, lock_time=None, debug_processor=None): - start = time.time() + # start = time.time() txes = self._prepare_sign_tx(inputs, outputs) # Prepare and send initial message diff --git a/trezorlib/transport/__init__.py b/trezorlib/transport/__init__.py index b720c2e3d4..e6a4bca13e 100644 --- a/trezorlib/transport/__init__.py +++ b/trezorlib/transport/__init__.py @@ -115,4 +115,4 @@ def get_transport(path=None, prefix_search=False): if transports: return transports[0].find_by_path(path, prefix_search=prefix_search) - raise Exception("Unknown path prefix '%s'" % prefix) + raise Exception("Could not find device by path: {}".format(path)) diff --git a/trezorlib/transport/udp.py b/trezorlib/transport/udp.py index 7bbad169c1..ffcfb250f6 100644 --- a/trezorlib/transport/udp.py +++ b/trezorlib/transport/udp.py @@ -67,7 +67,6 @@ class UdpTransport(Transport): @classmethod def enumerate(cls): - devices = [] default_path = '{}:{}'.format(cls.DEFAULT_HOST, cls.DEFAULT_PORT) try: return [cls._try_path(default_path)]