From bf7c876f8e34ce442c3a1132df4d93f522838ae6 Mon Sep 17 00:00:00 2001 From: Jean Do Date: Sat, 3 Aug 2024 10:40:05 -0400 Subject: [PATCH 1/4] Uses grid_type to label TinySeed page ui --- src/krux/pages/login.py | 4 ++-- src/krux/pages/tiny_seed.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/krux/pages/login.py b/src/krux/pages/login.py index d789f852..dfd2bb7d 100644 --- a/src/krux/pages/login.py +++ b/src/krux/pages/login.py @@ -88,7 +88,7 @@ def load_key_from_camera(self): self.ctx, [ (t("QR Code"), self.load_key_from_qr_code), - ("Tiny Seed", lambda: self.load_key_from_tiny_seed_image("TinySeed")), + ("Tiny Seed", lambda: self.load_key_from_tiny_seed_image("Tiny Seed")), ( "OneKey KeyTag", lambda: self.load_key_from_tiny_seed_image("OneKey KeyTag"), @@ -681,7 +681,7 @@ def load_key_from_tiny_seed(self): return self._load_key_from_words(words) return MENU_CONTINUE - def load_key_from_tiny_seed_image(self, grid_type="TinySeed"): + def load_key_from_tiny_seed_image(self, grid_type="Tiny Seed"): """Menu handler to scan key from Tiny Seed sheet metal storage method""" from .tiny_seed import TinyScanner diff --git a/src/krux/pages/tiny_seed.py b/src/krux/pages/tiny_seed.py index 584b2b69..b447d3f6 100644 --- a/src/krux/pages/tiny_seed.py +++ b/src/krux/pages/tiny_seed.py @@ -56,7 +56,7 @@ class TinySeed(Page): """Class for handling Tinyseed fomat""" - def __init__(self, ctx): + def __init__(self, ctx, label=None): super().__init__(ctx, None) self.ctx = ctx self.x_offset = MINIMAL_PADDING + 2 * FONT_WIDTH @@ -71,6 +71,10 @@ def __init__(self, ctx): self.y_offset = DEFAULT_PADDING + 3 * FONT_HEIGHT else: self.y_offset = 2 * FONT_HEIGHT + if label == "Binary Grid": + self.label = t("Binary Grid") + else: + self.label = label def _draw_grid(self): """Draws grid for import and export Tinyseed UI""" @@ -96,7 +100,7 @@ def _draw_grid(self): def _draw_labels(self, page): """Draws labels for import and export Tinyseed UI""" - self.ctx.display.draw_hcentered_text("Tiny Seed") + self.ctx.display.draw_hcentered_text(self.label) # case for non m5stickv, cube if not MINIMAL_DISPLAY: @@ -582,7 +586,7 @@ class TinyScanner(Page): # Settings for different binary grid types binary_grid_settings = { - "TinySeed": { + "Tiny Seed": { "xpad_factor": (240 / (12 * 345)), "ypad_factor": (210 / (12 * 272)), "x_offset_factor_amigo_p0": 39 / 345, @@ -628,7 +632,7 @@ class TinyScanner(Page): grid_settings = None - def __init__(self, ctx, grid_type="TinySeed"): + def __init__(self, ctx, grid_type="Tiny Seed"): super().__init__(ctx, None) self.ctx = ctx # Capturing flag used for first page of 24 words seed @@ -638,7 +642,7 @@ def __init__(self, ctx, grid_type="TinySeed"): self.y_regions = [] self.time_frame = time.ticks_ms() self.previous_seed_numbers = [1] * 12 - self.tiny_seed = TinySeed(self.ctx) + self.tiny_seed = TinySeed(self.ctx, label=grid_type) self.grid_settings = self.binary_grid_settings[grid_type] def _map_punches_region(self, rect_size, page=0): From 95a9e5854cd7f1f3a50243df4860370dc64d9d03 Mon Sep 17 00:00:00 2001 From: Jean Do Date: Sat, 3 Aug 2024 11:17:11 -0400 Subject: [PATCH 2/4] refactor to put custom label logic closer to where it's defined --- src/krux/pages/tiny_seed.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/krux/pages/tiny_seed.py b/src/krux/pages/tiny_seed.py index b447d3f6..836af42a 100644 --- a/src/krux/pages/tiny_seed.py +++ b/src/krux/pages/tiny_seed.py @@ -59,6 +59,7 @@ class TinySeed(Page): def __init__(self, ctx, label=None): super().__init__(ctx, None) self.ctx = ctx + self.label = label self.x_offset = MINIMAL_PADDING + 2 * FONT_WIDTH self.printer = None if self.ctx.display.width() > SMALLEST_WIDTH: @@ -71,10 +72,6 @@ def __init__(self, ctx, label=None): self.y_offset = DEFAULT_PADDING + 3 * FONT_HEIGHT else: self.y_offset = 2 * FONT_HEIGHT - if label == "Binary Grid": - self.label = t("Binary Grid") - else: - self.label = label def _draw_grid(self): """Draws grid for import and export Tinyseed UI""" @@ -642,8 +639,12 @@ def __init__(self, ctx, grid_type="Tiny Seed"): self.y_regions = [] self.time_frame = time.ticks_ms() self.previous_seed_numbers = [1] * 12 - self.tiny_seed = TinySeed(self.ctx, label=grid_type) self.grid_settings = self.binary_grid_settings[grid_type] + if grid_type == "Binary Grid": + label = t("Binary Grid") + else: + label = grid_type + self.tiny_seed = TinySeed(self.ctx, label=label) def _map_punches_region(self, rect_size, page=0): # Think in portrait mode, with Tiny Seed tilted 90 degrees From 73dec13f0c51e8e447f458c676f9b29fa5249740 Mon Sep 17 00:00:00 2001 From: Jean Do Date: Sat, 3 Aug 2024 12:48:54 -0400 Subject: [PATCH 3/4] added test to cover passing grid_type thru TinyScanner to TinySeed.label --- tests/pages/test_tiny_seed.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/pages/test_tiny_seed.py b/tests/pages/test_tiny_seed.py index efeccb97..dccd5987 100644 --- a/tests/pages/test_tiny_seed.py +++ b/tests/pages/test_tiny_seed.py @@ -329,3 +329,32 @@ def test_scan_tiny_seed_24w(all_devices, mocker): assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE) assert " ".join(words) == TEST_24_WORDS + + +def test_tinyscanner_initializes_tinyseed_with_label(all_devices, mocker): + import pytest + from krux.pages.tiny_seed import TinyScanner + from krux.input import BUTTON_PAGE_PREV, BUTTON_ENTER + + test_cases = [ + # TinyScanner grid_type param, expected exception, expected TinySeed label + (None, False, "Tiny Seed"), + ("Tiny Seed", None, "Tiny Seed"), + ("OneKey KeyTag", None, "OneKey KeyTag"), + ("Binary Grid", None, "Binary Grid"), + ("Unsupported Format", KeyError, None), + ] + BTN_SEQUENCE = [BUTTON_ENTER, BUTTON_PAGE_PREV] # enter and leave + + for i, (grid_type, expected_exception, expected_label) in enumerate(test_cases): + print(i, (grid_type, expected_exception, expected_label)) + ctx = create_ctx(mocker, BTN_SEQUENCE) + if not expected_exception: + if grid_type: + tiny_scanner = TinyScanner(ctx, grid_type=grid_type) + else: + tiny_scanner = TinyScanner(ctx) + assert tiny_scanner.tiny_seed.label == expected_label + else: + with pytest.raises(expected_exception): + tiny_scanner = TinyScanner(ctx, grid_type=grid_type) From ed4b4d6e620134bc3fc58b21d611dd7a93953990 Mon Sep 17 00:00:00 2001 From: Jean Do Date: Sat, 3 Aug 2024 15:13:07 -0400 Subject: [PATCH 4/4] simplified test --- tests/pages/test_tiny_seed.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/pages/test_tiny_seed.py b/tests/pages/test_tiny_seed.py index dccd5987..cf237239 100644 --- a/tests/pages/test_tiny_seed.py +++ b/tests/pages/test_tiny_seed.py @@ -334,21 +334,19 @@ def test_scan_tiny_seed_24w(all_devices, mocker): def test_tinyscanner_initializes_tinyseed_with_label(all_devices, mocker): import pytest from krux.pages.tiny_seed import TinyScanner - from krux.input import BUTTON_PAGE_PREV, BUTTON_ENTER test_cases = [ # TinyScanner grid_type param, expected exception, expected TinySeed label - (None, False, "Tiny Seed"), + (None, None, "Tiny Seed"), ("Tiny Seed", None, "Tiny Seed"), ("OneKey KeyTag", None, "OneKey KeyTag"), ("Binary Grid", None, "Binary Grid"), ("Unsupported Format", KeyError, None), ] - BTN_SEQUENCE = [BUTTON_ENTER, BUTTON_PAGE_PREV] # enter and leave for i, (grid_type, expected_exception, expected_label) in enumerate(test_cases): print(i, (grid_type, expected_exception, expected_label)) - ctx = create_ctx(mocker, BTN_SEQUENCE) + ctx = create_ctx(mocker, []) if not expected_exception: if grid_type: tiny_scanner = TinyScanner(ctx, grid_type=grid_type)