def test_caching(): # Create a datetime device d = TimeDevice(name='Test', prefix='Tst:This:2', beamline='TST', device_class='datetime.timedelta', args=list(), days=10, kwargs={ 'days': '{{days}}', 'seconds': 30 }) td = from_container(d) assert d.prefix in cache assert id(td) == id(from_container(d)) assert id(td) != id(from_container(d, use_cache=False))
def test_add_md(): d = HappiItem(name='test', prefix='Tst:This:3', beamline="TST", args=list(), device_class="happi.HappiItem") obj = from_container(d, attach_md=True) assert obj.md.extraneous['beamline'] == 'TST' assert obj.md.name == 'test'
def test_add_md(): d = Device(name='Test', prefix='Tst:This:3', beamline="TST", args=list(), device_class="happi.Device") obj = from_container(d, attach_md=True) assert obj.md.beamline == 'TST' assert obj.md.name == 'Test'
def test_caching(): # Create a datetime device d = TimeDevice(name='Test', prefix='Tst:This:2', beamline='TST', device_class='types.SimpleNamespace', args=list(), days=10, kwargs={ 'days': '{{days}}', 'seconds': 30 }) td = from_container(d) assert d.prefix in cache assert id(td) == id(from_container(d)) assert id(td) != id(from_container(d, use_cache=False)) # Modify md and check we see a reload d.days = 12 assert id(td) != id(from_container(d, use_cache=True)) # Check with a device where metadata is unavailable d = TimeDevice(name='Test', prefix='Tst:Delta:3', beamline='TST', device_class='datetime.timedelta', args=list(), days=10, kwargs={ 'days': '{{days}}', 'seconds': 30 }) td = from_container(d) assert id(td) == id(from_container(d)) assert id(td) != id(from_container(d, use_cache=False))
def test_from_container(): # Create a datetime device d = TimeDevice(name='Test', prefix='Tst:This:1', beamline='TST', device_class='datetime.timedelta', args=list(), days=10, kwargs={'days': '{{days}}', 'seconds': 30}) td = from_container(d) # Now import datetime and check that we constructed ours correctly import datetime assert td == datetime.timedelta(days=10, seconds=30)
def add_listener(self, channel): """Add a new channel to the existing connection""" super().add_listener(channel) # Connect our channel to the signal self.tx.connect(channel.tx_slot) logger.debug("Loading %r from happi Client", channel) # Load the device from the Client md = _client.find_device(name=self.address) obj = from_container(md) # Send the device and metdata to all of our subscribers self.tx.emit({'obj': obj, 'md': md.post()})
def load_beamline(self, endstation): """ Load a beamline from the provided happi client Parameters ---------- endstation : str Name of endstation to load Returns ------- path: BeamPath """ try: path = beamlines[endstation] path[endstation] = dict() except KeyError: logger.warning( "Unable to find %s as a configured endstation, " "assuming this is an independent path", endstation) path = {endstation: {}} # Load the devices specified in the configuration devices = list() for line, info in path.items(): # Find the happi containers for this section of beamlines start = info.get('start', 0.0) end = info.get('end', math.inf) logger.debug("Searching for devices on line %s between %s and %s", line, start, end) containers = self.client.search(beamline=line, active=True, start=start, end=end) # Ensure we actually found valid devices if not containers: logger.error("No valid beamline devices found for %s", line) continue # Load all the devices we found logger.debug("Found %s devices along %s", len(containers), line) for c in containers: try: dev = from_container(c) devices.append(dev) except Exception: logger.exception("Failure loading %s ...", c.name) self.containers.append(c) # Create the beamline from the loaded devices bp = BeamPath(*devices, name=line) self.beamlines[line] = bp # Set as attribute for easy access setattr(self, bp.name.replace(' ', '_').lower(), bp) return bp
def test_one_alarm_happi_ch(alarm, qtbot, metadata, response, fake_client): name = 'happi_test_device_' + str(uuid4()).replace('-', '_') item = fake_client.find_device(name=name) device = from_container(item) with qtbot.wait_signal(alarm.alarm_changed, timeout=1000): alarm.channel = 'happi://' + name with qtbot.wait_signal(alarm.alarm_changed, timeout=1000): device.hint_sig.update_metadata(metadata) assert alarm.alarm_summary == response
def test_add_device(qapp, qtbot): # Create a device and attach metadata md = happi.Device(name='Test This', prefix='Tst:This:1', beamline='TST', device_class='types.SimpleNamespace', args=list(), kwargs={'here': 'very unique text'}) device = from_container(md) # Add the device to the Console tc = TyphonConsole.from_device(device) qtbot.addWidget(tc) # Check that we created the object in the shell tc.kernel.kernel_client.execute('print(test_this.here)', silent=False) while md.kwargs['here'] not in tc.kernel._control.toPlainText(): qapp.processEvents() # Smoke test not happi Device tc.add_device(types.SimpleNamespace(hi=3)) return tc
def add_listener(self, channel): """Add a new channel to the existing connection.""" super().add_listener(channel) # Connect our channel to the signal self.tx.connect(channel.tx_slot, QtCore.Qt.QueuedConnection) logger.debug("Loading %r from happi Client", channel) if '.' in self.address: device, child = self.address.split('.', 1) else: device, child = self.address, None # Load the device from the Client md = HappiClientState.client.find_device(name=device) obj = from_container(md) md = md.post() # If we have a child grab it if child: logger.debug("Retrieving child %r from %r", child, obj.name) obj = getattr(obj, child) md = {'name': obj.name} # Send the device and metdata to all of our subscribers self.tx.emit({'obj': obj, 'md': md})