Skip to content

Commit

Permalink
Don't add duplicate sources when getting available signals
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedThree committed Aug 23, 2021
1 parent 81ae091 commit 52dc66a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pyxpad/pyxpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
11 changes: 11 additions & 0 deletions tests/test_pyxpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 52dc66a

Please sign in to comment.