def test_update(lab): ''' Updates information on hosts, benches or instruments''' new_host2 = Host(name="Host2", hostname='foo') old_host2 = lab.hosts["Host2"] lab.hosts["Host2"] = new_host2 assert lab.hosts["Host2"] == new_host2 assert lab.hosts["Host2"] != old_host2 # old_host2 got de-referenced # WARNING! non-trivial effect true for hosts, benches and instruments: lab.hosts["Host3"] = new_host2 # this will rename new_host2 to "Host3" assert lab.hosts["Host3"] == new_host2 assert new_host2.name == "Host3" assert lab.hosts["Host3"].name == "Host3" assert "Host2" not in lab.hosts.keys() del lab.hosts["Host3"] assert new_host2 not in lab.hosts
def test_overwriting(lab): ''' Special cases when there are instrumentation_servers ''' old_remote = Host2 updated_remote = Host(name="Host2", foo=1) lab.updateHost(updated_remote) assert lab.hosts["Host2"] != old_remote assert lab.hosts["Host2"] == updated_remote old_server = Host1 updated_server = LocalHost(name="Host1") updated_server.mac_address = 'test_overwriting' lab.updateHost(updated_server) # should replace entry for 'Host1' assert lab.hosts["Host1"] != old_server assert lab.hosts["Host1"] == updated_server second_server = LocalHost(name="Another Host") lab.updateHost(second_server) assert lab.hosts["Host1"] == updated_server with pytest.raises(KeyError): lab.hosts["Another Host"]
def patch_labstate(from_version, old_lab): """ This takes the loaded JSON version of labstate (old_lab) and applies a patch to the current version of labstate. """ if from_version == 1: assert old_lab.__version__ == from_version # In labstate version 1, instruments are stored in lists called # in lab.benches[x].instruments and/or lab.hosts[x].instruments, # with potential name duplicates # We need to transport them into a single list that will reside # lab.instruments, with no name duplicates. old_benches = old_lab.benches old_hosts = old_lab.hosts old_connections = old_lab.connections instruments = TypedList(Instrument) benches = TypedList(Bench) devices = TypedList(Device) hosts = TypedList(Host) for old_bench in old_benches.values(): # restarting new bench afresh (only name matters so far) new_bench = Bench(name=old_bench.name) benches.append(new_bench) # bench.instruments is now a property descriptor, # can't access directly. Need to use __dict__ # here we move bench.instruments into a global instruments for instrument in old_bench.__dict__['instruments']: instrument.bench = new_bench # if there is a duplicate name, update instrument if instrument.name in instruments.dict.keys(): instruments[instrument.name].__dict__.update(instrument.__dict__) else: instruments.append(instrument) # same for devices for device in old_bench.__dict__['devices']: device.bench = new_bench if device.name in devices.dict.keys(): devices[device.name].__dict__.update(device.__dict__) else: devices.append(device) # Same code as above for old_host in old_hosts.values(): new_host = Host(name=old_host.name, mac_address=old_host.mac_address, hostname=old_host.hostname, os=old_host.os) hosts.append(new_host) for instrument in old_host.__dict__['instruments']: instrument.host = new_host if instrument.name in instruments.dict.keys(): instruments[instrument.name].__dict__.update(instrument.__dict__) else: instruments.append(instrument) # instantiating new labstate from scratch. patched_lab = LabState() patched_lab.instruments.extend(instruments) patched_lab.benches.extend(benches) patched_lab.devices.extend(devices) patched_lab.hosts.extend(hosts) patched_lab.hosts['cassander'] = LocalHost(name='cassander') patched_lab.connections = old_connections patched_lab.__sha256__ = old_lab.__sha256__ patched_lab.__version__ = LabState.__version__ patched_lab.filename = old_lab.filename patched_lab.__user__ = old_lab.__user__ patched_lab.__datetime__ = old_lab.__datetime__ return patched_lab raise NotImplementedError("Patch not found")
from lightlab.laboratory.devices import Device from lightlab.laboratory.experiments import Experiment from lightlab.laboratory.virtualization import DualFunction from lightlab.equipment.lab_instruments import Keithley_2400_SM import json import time import os import logging logging.disable(logging.CRITICAL) filename = 'test_{}.json'.format(int(time.time())) labstate._filename = filename # Shared objects Host1 = LocalHost(name="Host1") Host2 = Host(name="Host2") Bench1 = Bench(name="Bench1") Bench2 = Bench(name="Bench2") def open_error(self): raise RuntimeError( "self.open() function being called upon initialization.") __GETCURRENTTEST = 0.123 Keithley_2400_SM.startup = lambda self: True Keithley_2400_SM.getCurrent = lambda self: __GETCURRENTTEST Keithley_2400_SM.open = open_error instrument1 = Keithley(name="keithley1",