Skip to content

Commit

Permalink
Merge pull request #1560 from sol/update-machine-state-on-disconnect
Browse files Browse the repository at this point in the history
Update machine state on unhandled exceptions
  • Loading branch information
sammdot committed Sep 27, 2023
2 parents fa52336 + 2c44a27 commit 8a8d360
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions news.d/bugfix/1560.ui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update the tray icon to "disconnected" when a serial-over-USB machine is unplugged.
8 changes: 8 additions & 0 deletions plover/machine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,18 @@ class ThreadedStenotypeBase(StenotypeBase, threading.Thread):
"""
def __init__(self):
threading.Thread.__init__(self)
self._on_unhandled_exception(self._error)
self.name += '-machine'
StenotypeBase.__init__(self)
self.finished = threading.Event()

def _on_unhandled_exception(self, action):
super_invoke_excepthook = self._invoke_excepthook
def invoke_excepthook(self):
action()
super_invoke_excepthook(self)
self._invoke_excepthook = invoke_excepthook

def run(self):
"""This method should be overridden by a subclass."""
pass
Expand Down
14 changes: 14 additions & 0 deletions test/test_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from unittest.mock import Mock
from plover.machine.base import ThreadedStenotypeBase

class MyMachine(ThreadedStenotypeBase):
def run(self):
raise "some unexpected error"

def test_update_machine_staten_on_unhandled_exception():
machine = MyMachine()
callback = Mock()
machine.add_state_callback(callback)
machine.start_capture()
machine.join()
callback.assert_called_with('disconnected')

0 comments on commit 8a8d360

Please sign in to comment.