From 81ae091b11f53127d52f3fd51ca2c67cd389df72 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Mon, 23 Aug 2021 17:44:52 +0100 Subject: [PATCH] Allow last shot to work without an existing source --- pyxpad/pyxpad.py | 22 +++++++++++----------- tests/test_pyxpad.py | 8 ++++++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pyxpad/pyxpad.py b/pyxpad/pyxpad.py index 3e86e26..eb7f1f3 100755 --- a/pyxpad/pyxpad.py +++ b/pyxpad/pyxpad.py @@ -728,20 +728,20 @@ def lastShot(self): """ Get the latest shot number """ - - # Find an XPadSource in the list of sources from pyxpad.xpadsource import XPadSource - source = None - for _source in self.sources.sources: - if isinstance(_source, XPadSource): - source = _source - if source is None: - # None available - return + try: + source = self.sources.sources[0] + except IndexError: + source = XPadSource() - # Now ask for the lastshot - last_shot_number = source.last_shot_number + try: + last_shot_number = source.last_shot_number + except AttributeError: + self.statusbar.showMessage( + "Last shot number not available for {source.label}", 1500 + ) + return # Append the shot number to any existing shot numbers current_text = self.shotInput.text() diff --git a/tests/test_pyxpad.py b/tests/test_pyxpad.py index a1df5b6..6391e7d 100644 --- a/tests/test_pyxpad.py +++ b/tests/test_pyxpad.py @@ -29,6 +29,14 @@ def test_get_last_shot(mock_uda, copy_tree_data, pyxpadbot, qtbot): assert str(main.shotInput.text()).strip() == MOCK_LAST_SHOT +def test_get_last_shot_no_source(mock_uda, pyxpadbot, qtbot): + main = pyxpadbot + + main.lastShotButton.setEnabled(True) + qtbot.mouseClick(main.lastShotButton, QtCore.Qt.LeftButton) + assert str(main.shotInput.text()).strip() == MOCK_LAST_SHOT + + def test_get_available_signals(mock_uda, pyxpadbot, qtbot): main = pyxpadbot