From c39a08703629fce815a5f7627fe4d8b2dc03a9e2 Mon Sep 17 00:00:00 2001 From: Simon Hengel Date: Tue, 30 Aug 2022 02:29:45 +0700 Subject: [PATCH] plover/machine/*: DRY up / remove code duplication --- plover/machine/base.py | 5 +++++ plover/machine/geminipr.py | 4 +--- plover/machine/passport.py | 4 +--- plover/machine/procat.py | 4 +--- plover/machine/stentura.py | 7 +------ 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/plover/machine/base.py b/plover/machine/base.py index 11fb20c31..265af86b1 100644 --- a/plover/machine/base.py +++ b/plover/machine/base.py @@ -90,6 +90,11 @@ def _notify(self, steno_keys): for callback in self.stroke_subscribers: callback(steno_keys) + def _notify_keys(self, steno_keys): + steno_keys = self.keymap.keys_to_actions(steno_keys) + if steno_keys: + self._notify(steno_keys) + def set_suppression(self, enabled): '''Enable keyboard suppression. diff --git a/plover/machine/geminipr.py b/plover/machine/geminipr.py index 19e6d23bf..024a603c3 100644 --- a/plover/machine/geminipr.py +++ b/plover/machine/geminipr.py @@ -53,6 +53,4 @@ def run(self): for j in range(1, 8): if (b & (0x80 >> j)): steno_keys.append(STENO_KEY_CHART[i * 7 + j - 1]) - steno_keys = self.keymap.keys_to_actions(steno_keys) - if steno_keys: - self._notify(steno_keys) + self._notify_keys(steno_keys) diff --git a/plover/machine/passport.py b/plover/machine/passport.py index 184c58039..c7d78139d 100644 --- a/plover/machine/passport.py +++ b/plover/machine/passport.py @@ -42,9 +42,7 @@ def _handle_packet(self, packet): shadow = int(shadow, base=16) if shadow >= 8: steno_keys.append(key) - steno_keys = self.keymap.keys_to_actions(steno_keys) - if steno_keys: - self._notify(steno_keys) + self._notify_keys(steno_keys) def run(self): """Overrides base class run method. Do not call directly.""" diff --git a/plover/machine/procat.py b/plover/machine/procat.py index 3dc645557..893815379 100644 --- a/plover/machine/procat.py +++ b/plover/machine/procat.py @@ -40,11 +40,9 @@ def run(self): log.error('discarding invalid packet: %s', binascii.hexlify(packet)) continue - steno_keys = self.keymap.keys_to_actions( + self._notify_keys( self.process_steno_packet(packet) ) - if steno_keys: - self._notify(steno_keys) @staticmethod def process_steno_packet(raw): diff --git a/plover/machine/stentura.py b/plover/machine/stentura.py index 82de4e45a..32d47bb9a 100644 --- a/plover/machine/stentura.py +++ b/plover/machine/stentura.py @@ -658,15 +658,10 @@ class Stentura(plover.machine.base.SerialStenotypeBase): ^ ''' - def _on_stroke(self, keys): - steno_keys = self.keymap.keys_to_actions(keys) - if steno_keys: - self._notify(steno_keys) - def run(self): """Overrides base class run method. Do not call directly.""" try: - _loop(self.serial_port, self.finished, self._on_stroke, self._ready) + _loop(self.serial_port, self.finished, self._notify_keys, self._ready) except _StopException: pass except Exception: