def location_and_station_set_to(location: int, work_station: int): cfg = qc.Config() old_cfg = deepcopy(cfg.current_config) cfg['GUID_components']['location'] = location cfg['GUID_components']['work_station'] = work_station cfg.save_to_home() yield cfg.current_config = old_cfg cfg.save_to_home()
def update_GUIDs(conn: ConnectionPlus) -> None: """ Update all GUIDs in this database where either the location code or the work_station code is zero to use the location and work_station code from the qcodesrc.json file in home. Runs where it is not true that both codes are zero are skipped. """ log.info('Commencing update of all GUIDs in database') cfg = qc.Config() location = cfg['GUID_components']['location'] work_station = cfg['GUID_components']['work_station'] if location == 0: log.warning('The location is still set to the default (0). Can not ' 'proceed. Please configure the location before updating ' 'the GUIDs.') return if work_station == 0: log.warning('The work_station is still set to the default (0). Can not' ' proceed. Please configure the location before updating ' 'the GUIDs.') return query = f"select MAX(run_id) from runs" c = atomic_transaction(conn, query) no_of_runs = c.fetchall()[0][0] # now, there are four actions we can take def _both_nonzero(run_id: int, *args) -> None: log.info(f'Run number {run_id} already has a valid GUID, skipping.') def _location_only_zero(run_id: int, *args) -> None: log.warning(f'Run number {run_id} has a zero (default) location ' 'code, but a non-zero work station code. Please manually ' 'resolve this, skipping the run now.') def _workstation_only_zero(run_id: int, *args) -> None: log.warning(f'Run number {run_id} has a zero (default) work station' ' code, but a non-zero location code. Please manually ' 'resolve this, skipping the run now.') def _both_zero(run_id: int, conn, guid_comps) -> None: guid_str = generate_guid(timeint=guid_comps['time'], sampleint=guid_comps['sample']) with atomic(conn) as conn: sql = f""" UPDATE runs SET guid = ? where run_id == {run_id} """ cur = conn.cursor() cur.execute(sql, (guid_str, )) log.info(f'Succesfully updated run number {run_id}.') actions: Dict[Tuple[bool, bool], Callable] actions = { (True, True): _both_zero, (False, True): _workstation_only_zero, (True, False): _location_only_zero, (False, False): _both_nonzero } for run_id in range(1, no_of_runs + 1): guid_str = get_guid_from_run_id(conn, run_id) guid_comps = parse_guid(guid_str) loc = guid_comps['location'] ws = guid_comps['work_station'] log.info(f'Updating run number {run_id}...') actions[(loc == 0, ws == 0)](run_id, conn, guid_comps)