From 52dc66a9db2d2007d3b3d61c108eaf0f6185483c Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Mon, 23 Aug 2021 17:45:26 +0100 Subject: [PATCH] Don't add duplicate sources when getting available signals --- pyxpad/pyxpad.py | 23 +++++++++++++++++++++-- tests/test_pyxpad.py | 11 +++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/pyxpad/pyxpad.py b/pyxpad/pyxpad.py index eb7f1f3..ebec6d5 100755 --- a/pyxpad/pyxpad.py +++ b/pyxpad/pyxpad.py @@ -113,6 +113,10 @@ def __init__(self, mainwindow): ) self.actionConfig.triggered.connect(self.configureSource) + @property + def names(self): + return [source.label for source in self.sources] + def saveState(self, f): pickle.dump(self.sources, f) @@ -757,8 +761,23 @@ def getAvailableSignals(self): """Add the available signals for listed shots as new sources""" from pyxpad.xpadsource import XPadSource - shots = self.shotInput.text().split(",") - for shot in shots: + shot_text = self.shotInput.text().split(",") + shots = set(map(str.strip, shot_text)) + + if shots == {""}: + shots = {str(self.lastShot())} + + existing_sources = shots.intersection(self.sources.names) + new_shots = shots.difference(self.sources.names) + if existing_sources: + plural = "s" if len(existing_sources) > 1 else "" + grammar = "" if len(existing_sources) > 1 else "s" + self.statusbar.showMessage( + f"Skipping shot{plural} {', '.join(existing_sources)}; already exist{grammar} as source{plural}", + 1000, + ) + + for shot in new_shots: self.sources.addSource(XPadSource.from_signals(shot)) self.sources.updateDisplay() diff --git a/tests/test_pyxpad.py b/tests/test_pyxpad.py index 6391e7d..9ea5045 100644 --- a/tests/test_pyxpad.py +++ b/tests/test_pyxpad.py @@ -44,7 +44,18 @@ def test_get_available_signals(mock_uda, pyxpadbot, qtbot): qtbot.mouseClick(main.getAvailableSignalsButton, QtCore.Qt.LeftButton) assert main.treeView.topLevelItemCount() == 1 + assert main.shotInput.text() == MOCK_LAST_SHOT main.treeView.topLevelItem(0).setSelected(True) assert main.sourceTable.rowCount() == 3 assert main.sourceTable.item(0, 0).text() == "first_signal" + + # Clicking again shouldn't add more sources + qtbot.mouseClick(main.getAvailableSignalsButton, QtCore.Qt.LeftButton) + assert main.treeView.topLevelItemCount() == 1 + + qtbot.keyClicks(main.shotInput, ", 66") + qtbot.mouseClick(main.getAvailableSignalsButton, QtCore.Qt.LeftButton) + assert main.treeView.topLevelItemCount() == 2 + qtbot.mouseClick(main.getAvailableSignalsButton, QtCore.Qt.LeftButton) + assert main.treeView.topLevelItemCount() == 2