From 98e82e3cd5b8e836d7e6c20ba17525119764a2b5 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 12 Jun 2017 18:16:06 +0200 Subject: [PATCH] code style --- src/apps/management/recovery_device.py | 4 ++-- src/lib/typing.py | 4 ++++ src/lib/unittest.py | 4 ++-- src/trezor/crypto/aes.py | 10 ++++++++++ src/trezor/crypto/base58.py | 4 +++- src/trezor/crypto/hmac.py | 1 + src/trezor/log.py | 10 +++++++++- src/trezor/loop.py | 1 + src/trezor/res/__init__.py | 2 ++ src/trezor/ui/button.py | 10 ++++++++-- src/trezor/ui/confirm.py | 2 ++ src/trezor/ui/container.py | 4 ++-- src/trezor/ui/keyboard.py | 4 ++-- src/trezor/ui/loader.py | 2 +- src/trezor/ui/pin.py | 8 ++++---- src/trezor/ui/qr.py | 5 +---- src/trezor/ui/swipe.py | 24 +++++++++--------------- src/trezor/wire/codec_v1.py | 2 +- 18 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/apps/management/recovery_device.py b/src/apps/management/recovery_device.py index 37e8a0bb..e9fdb9f2 100644 --- a/src/apps/management/recovery_device.py +++ b/src/apps/management/recovery_device.py @@ -11,9 +11,9 @@ def nth(n): @unimport -async def layout_recovery_device(session_id, message): +async def layout_recovery_device(session_id, msg): - msg = 'Please enter ' + nth(message.word_count) + ' word' + msg = 'Please enter ' + nth(msg.word_count) + ' word' ui.display.clear() ui.header('Recovery device', ui.ICON_RECOVERY, ui.BLACK, ui.LIGHT_GREEN) diff --git a/src/lib/typing.py b/src/lib/typing.py index d0217b47..003d29ee 100644 --- a/src/lib/typing.py +++ b/src/lib/typing.py @@ -42,7 +42,9 @@ __names_obj = [ 'Text', ] + class __dummy: + def __getitem__(self, *args): return object @@ -54,9 +56,11 @@ for __n in __names_get: for __n in __names_obj: globals()[__n] = object + def TypeVar(*args): return object + def NewType(*args): return lambda x: x diff --git a/src/lib/unittest.py b/src/lib/unittest.py index 9b8f544b..5b47fb52 100644 --- a/src/lib/unittest.py +++ b/src/lib/unittest.py @@ -47,7 +47,7 @@ class TestCase: else: if places is None: places = 7 - if round(abs(y-x), places) == 0: + if round(abs(y - x), places) == 0: return if not msg: msg = '%r != %r within %r places' % (x, y, places) @@ -66,7 +66,7 @@ class TestCase: else: if places is None: places = 7 - if not (x == y) and round(abs(y-x), places) != 0: + if not (x == y) and round(abs(y - x), places) != 0: return if not msg: msg = '%r == %r within %r places' % (x, y, places) diff --git a/src/trezor/crypto/aes.py b/src/trezor/crypto/aes.py index f5fc43c9..1a42f532 100644 --- a/src/trezor/crypto/aes.py +++ b/src/trezor/crypto/aes.py @@ -1,59 +1,69 @@ from TrezorCrypto import AES + def AES_ECB_Encrypt(key: bytes) -> AES: ''' Create AES encryption context in ECB mode ''' return AES(AES.ECB | AES.Encrypt, key) + def AES_ECB_Decrypt(key: bytes) -> AES: ''' Create AES decryption context in ECB mode ''' return AES(AES.ECB | AES.Decrypt, key) + def AES_CBC_Encrypt(key: bytes, iv: bytes) -> AES: ''' Create AES encryption context in CBC mode ''' return AES(AES.CBC | AES.Encrypt, key, iv) + def AES_CBC_Decrypt(key: bytes, iv: bytes) -> AES: ''' Create AES decryption context in CBC mode ''' return AES(AES.CBC | AES.Decrypt, key, iv) + def AES_CFB_Encrypt(key: bytes, iv: bytes) -> AES: ''' Create AES encryption context in CFB mode ''' return AES(AES.CFB | AES.Encrypt, key, iv) + def AES_CFB_Decrypt(key: bytes, iv: bytes) -> AES: ''' Create AES decryption context in CFB mode ''' return AES(AES.CFB | AES.Decrypt, key, iv) + def AES_OFB_Encrypt(key: bytes, iv: bytes) -> AES: ''' Create AES encryption context in OFB mode ''' return AES(AES.OFB | AES.Encrypt, key, iv) + def AES_OFB_Decrypt(key: bytes, iv: bytes) -> AES: ''' Create AES decryption context in OFB mode ''' return AES(AES.OFB | AES.Decrypt, key, iv) + def AES_CTR_Encrypt(key: bytes) -> AES: ''' Create AES encryption context in CTR mode ''' return AES(AES.CTR | AES.Encrypt, key) + def AES_CTR_Decrypt(key: bytes) -> AES: ''' Create AES decryption context in CTR mode diff --git a/src/trezor/crypto/base58.py b/src/trezor/crypto/base58.py index 7e36e9f1..62b784b3 100644 --- a/src/trezor/crypto/base58.py +++ b/src/trezor/crypto/base58.py @@ -16,6 +16,7 @@ # 58 character alphabet used _alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' + def encode(data: bytes) -> str: ''' Convert bytes to base58 encoded string. @@ -55,7 +56,7 @@ def decode(string: str) -> bytes: acc, mod = divmod(acc, 256) result.append(mod) - return bytes((b for b in reversed(result +[0] * (origlen - newlen)))) + return bytes((b for b in reversed(result + [0] * (origlen - newlen)))) def encode_check(data: bytes, digestfunc=None) -> str: @@ -67,6 +68,7 @@ def encode_check(data: bytes, digestfunc=None) -> str: digestfunc = lambda x: sha256(sha256(x).digest()).digest()[:4] return encode(data + digestfunc(data)) + def decode_check(string: str, digestfunc=None) -> bytes: ''' Convert base58 encoded string to bytes and verify checksum. diff --git a/src/trezor/crypto/hmac.py b/src/trezor/crypto/hmac.py index db1b9916..7722e00f 100644 --- a/src/trezor/crypto/hmac.py +++ b/src/trezor/crypto/hmac.py @@ -1,5 +1,6 @@ class Hmac: + def __init__(self, key, msg, digestmod): self.__digestmod = digestmod self.__inner = digestmod() diff --git a/src/trezor/log.py b/src/trezor/log.py index 42da7a9d..6a2b73fe 100644 --- a/src/trezor/log.py +++ b/src/trezor/log.py @@ -20,29 +20,37 @@ _leveldict = { level = NOTSET color = True + def _log(name, mlevel, msg, *args): if __debug__ and mlevel >= level: if color: - fmt = '%d \x1b[35m%s\x1b[0m %s \x1b[' + _leveldict[mlevel][1] + 'm' + msg + '\x1b[0m' + fmt = '%d \x1b[35m%s\x1b[0m %s \x1b[' + \ + _leveldict[mlevel][1] + 'm' + msg + '\x1b[0m' else: fmt = '%d %s %s ' + msg print(fmt % ((utime.ticks_us(), name, _leveldict[mlevel][0]) + args)) + def debug(name, msg, *args): _log(name, DEBUG, msg, *args) + def info(name, msg, *args): _log(name, INFO, msg, *args) + def warning(name, msg, *args): _log(name, WARNING, msg, *args) + def error(name, msg, *args): _log(name, ERROR, msg, *args) + def exception(name, exc): _log(name, ERROR, 'exception:') sys.print_exception(exc) + def critical(name, msg, *args): _log(name, CRITICAL, msg, *args) diff --git a/src/trezor/loop.py b/src/trezor/loop.py index 57bbec06..a3b31a35 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -287,6 +287,7 @@ class Wait(Syscall): self.exit() raise + select = Select sleep = Sleep wait = Wait diff --git a/src/trezor/res/__init__.py b/src/trezor/res/__init__.py index 32f3bc8d..30482720 100644 --- a/src/trezor/res/__init__.py +++ b/src/trezor/res/__init__.py @@ -3,6 +3,7 @@ try: except ImportError: resdata = None + def load(name): ''' Loads resource of a given name as bytes. @@ -12,6 +13,7 @@ def load(name): with open(name, 'rb') as f: return f.read() + def gettext(message): ''' Returns localized string. This function is aliased to _. diff --git a/src/trezor/ui/button.py b/src/trezor/ui/button.py index c0b8a9e1..e909e39c 100644 --- a/src/trezor/ui/button.py +++ b/src/trezor/ui/button.py @@ -116,8 +116,14 @@ class Button(Widget): ax, ay, aw, ah = self.area tx = ax + aw // 2 ty = ay + ah // 2 + 8 - display.bar_radius(ax, ay, aw, ah, style['border-color'], ui.BLACK, style['radius']) - display.bar_radius(ax + 1, ay + 1, aw - 2, ah - 2, style['bg-color'], style['border-color'], style['radius']) + display.bar_radius(ax, ay, aw, ah, + style['border-color'], + ui.BLACK, + style['radius']) + display.bar_radius(ax + 1, ay + 1, aw - 2, ah - 2, + style['bg-color'], + style['border-color'], + style['radius']) if isinstance(self.content, str): display.text_center(tx, ty, self.content, diff --git a/src/trezor/ui/confirm.py b/src/trezor/ui/confirm.py index 93b23f89..364c0777 100644 --- a/src/trezor/ui/confirm.py +++ b/src/trezor/ui/confirm.py @@ -11,6 +11,7 @@ CANCELLED = const(2) class ConfirmDialog(Widget): + def __init__(self, content=None, confirm='Confirm', cancel='Cancel'): self.content = content if cancel is not None: @@ -44,6 +45,7 @@ class ConfirmDialog(Widget): class HoldToConfirmDialog(Widget): + def __init__(self, content=None, hold='Hold to confirm', *args, **kwargs): self.button = Button((0, 240 - 48, 240, 48), hold, normal_style=CONFIRM_BUTTON, diff --git a/src/trezor/ui/container.py b/src/trezor/ui/container.py index 1bc77fbb..6f53ac4e 100644 --- a/src/trezor/ui/container.py +++ b/src/trezor/ui/container.py @@ -10,8 +10,8 @@ class Container(Widget): for child in self.children: child.render() - def send(self, event, pos): + def touch(self, event, pos): for child in self.children: - result = child.send(event, pos) + result = child.touch(event, pos) if result is not None: return result diff --git a/src/trezor/ui/keyboard.py b/src/trezor/ui/keyboard.py index 9a9e1bb9..aa131030 100644 --- a/src/trezor/ui/keyboard.py +++ b/src/trezor/ui/keyboard.py @@ -43,7 +43,7 @@ def compute_mask(text): return mask -class KeyboardMultiTap: +class KeyboardMultiTap(ui.Widget): def __init__(self, content=''): self.content = content @@ -162,7 +162,7 @@ def zoom_buttons(keys, upper=False): return [Button(cell_area(i, n_x, n_y), key) for i, key in enumerate(keys)] -class KeyboardZooming: +class KeyboardZooming(ui.Widget): def __init__(self, content='', uppercase=True): self.content = content diff --git a/src/trezor/ui/loader.py b/src/trezor/ui/loader.py index 1c53718c..9e8fef00 100644 --- a/src/trezor/ui/loader.py +++ b/src/trezor/ui/loader.py @@ -19,7 +19,7 @@ DEFAULT_LOADER_ACTIVE = { _LOADER_MSEC = const(1000) -class Loader: +class Loader(ui.Widget): def __init__(self, normal_style=None, active_style=None): self.start_ticks_ms = None diff --git a/src/trezor/ui/pin.py b/src/trezor/ui/pin.py index 967df753..f1e8c0e7 100644 --- a/src/trezor/ui/pin.py +++ b/src/trezor/ui/pin.py @@ -21,7 +21,7 @@ def generate_digits(): return digits -class PinMatrix(): +class PinMatrix(ui.Widget): def __init__(self, label, pin=''): self.label = label @@ -67,10 +67,10 @@ class PinMatrix(): # display.bar(0, 95, 240, 2, ui.blend(ui.BLACK, ui.WHITE, 0.25)) # display.bar(0, 142, 240, 2, ui.blend(ui.BLACK, ui.WHITE, 0.25)) - def send(self, event, pos): - if self.clear_button.send(event, pos) == BTN_CLICKED: + def touch(self, event, pos): + if self.clear_button.touch(event, pos) == BTN_CLICKED: self.pin = '' for btn in self.pin_buttons: - if btn.send(event, pos) == BTN_CLICKED: + if btn.touch(event, pos) == BTN_CLICKED: if len(self.pin) < 9: self.pin += btn.content diff --git a/src/trezor/ui/qr.py b/src/trezor/ui/qr.py index 09acb870..b23960e5 100644 --- a/src/trezor/ui/qr.py +++ b/src/trezor/ui/qr.py @@ -1,7 +1,7 @@ from trezor import ui -class Qr: +class Qr(ui.Widget): def __init__(self, data, pos, scale): self.data = data @@ -10,6 +10,3 @@ class Qr: def render(self): ui.display.qrcode(self.pos[0], self.pos[1], self.data, self.scale) - - def send(self, event, pos): - pass diff --git a/src/trezor/ui/swipe.py b/src/trezor/ui/swipe.py index 23fff82e..ae0e0145 100644 --- a/src/trezor/ui/swipe.py +++ b/src/trezor/ui/swipe.py @@ -13,7 +13,8 @@ SWIPE_LEFT = const(90) SWIPE_RIGHT = const(270) -class Swipe(): +class Swipe(ui.Widget): + def __init__(self, area=None, absolute=False): self.area = area or (0, 0, ui.SCREEN, ui.SCREEN) self.absolute = absolute @@ -22,7 +23,7 @@ class Swipe(): self.light_origin = None self.light_target = ui.BACKLIGHT_NONE - def send(self, event, pos): + def touch(self, event, pos): if not self.absolute: pos = rotate_coords(pos) @@ -42,8 +43,8 @@ class Swipe(): velya = abs(pdy / td) if td > 0 else 1 ratio = int(pdxa / pdya * 100) if pdya > 0 else 100 if (velya >= _SWIPE_VELOCITY_THRESHOLD - and pdya >= _SWIPE_DISTANCE_THRESHOLD - and ratio <= _SWIPE_RATIO_THRESHOLD): + and pdya >= _SWIPE_DISTANCE_THRESHOLD + and ratio <= _SWIPE_RATIO_THRESHOLD): light = ui.display.backlight() if light > self.light_target: light -= 5 @@ -68,16 +69,16 @@ class Swipe(): velxa = abs(pdx / td) ratio = int(pdya / pdxa * 100) if pdxa > 0 else 100 if (velxa >= _SWIPE_VELOCITY_THRESHOLD - and pdxa >= _SWIPE_DISTANCE_THRESHOLD - and ratio <= _SWIPE_RATIO_THRESHOLD): + and pdxa >= _SWIPE_DISTANCE_THRESHOLD + and ratio <= _SWIPE_RATIO_THRESHOLD): return SWIPE_RIGHT if pdx > 0 else SWIPE_LEFT else: # Vertical direction velya = abs(pdy / td) ratio = int(pdxa / pdya * 100) if pdya > 0 else 100 if (velya >= _SWIPE_VELOCITY_THRESHOLD - and pdya >= _SWIPE_DISTANCE_THRESHOLD - and ratio <= _SWIPE_RATIO_THRESHOLD): + and pdya >= _SWIPE_DISTANCE_THRESHOLD + and ratio <= _SWIPE_RATIO_THRESHOLD): if pdy < 0: ui.display.backlight(self.light_origin) return SWIPE_DOWN if pdy > 0 else SWIPE_UP @@ -85,10 +86,3 @@ class Swipe(): self.start_pos = None self.start_time = 0 ui.display.backlight(self.light_origin) - - def __iter__(self): - while True: - event, *pos = yield loop.Select(loop.TOUCH) - result = self.send(event, pos) - if result is not None: - return result diff --git a/src/trezor/wire/codec_v1.py b/src/trezor/wire/codec_v1.py index 55684ffd..6657f60a 100644 --- a/src/trezor/wire/codec_v1.py +++ b/src/trezor/wire/codec_v1.py @@ -111,4 +111,4 @@ def encode_session_open(session_id, callback): def encode_session_close(session_id, callback): # v1 codec does not have explicit session support - pass \ No newline at end of file + pass