def test_clear_tip_length_calibration_data(monkeypatch): calpath = config.get_tip_length_cal_path() with open(calpath / f'{PIPETTE_ID}.json', 'w') as offset_file: test_offset = {MOCK_HASH: {'tipLength': 22.0, 'lastModified': 1}} json.dump(test_offset, offset_file) assert len(os.listdir(calpath)) > 0 delete.clear_tip_length_calibration() assert len(os.listdir(calpath)) == 0
def clear_tip_length_calibration(): """ Delete all tip length calibration files. """ tip_length_path = config.get_tip_length_cal_path() try: targets = (f for f in tip_length_path.iterdir() if f.suffix == '.json') for target in targets: target.unlink() except FileNotFoundError: pass
def _get_tip_length_data(pip_id: str, labware_hash: str, labware_load_name: str) -> 'TipLengthCalibration': try: pip_tip_length_path = config.get_tip_length_cal_path( ) / f'{pip_id}.json' tip_length_data =\ io.read_cal_file(str(pip_tip_length_path)) return tip_length_data[labware_hash] except (FileNotFoundError, KeyError): raise local_types.TipLengthCalNotFound( f'Tip length of {labware_load_name} has not been ' f'calibrated for this pipette: {pip_id} and cannot' 'be loaded')
def _append_to_index_tip_length_file(pip_id: str, lw_hash: str): index_file = config.get_tip_length_cal_path() / 'index.json' try: index_data = io.read_cal_file(str(index_file)) except FileNotFoundError: index_data = {} if lw_hash not in index_data: index_data[lw_hash] = [pip_id] elif pip_id not in index_data[lw_hash]: index_data[lw_hash].append(pip_id) io.save_to_file(index_file, index_data)
def test_load_nonexistent_tip_length_calibration_data(monkeypatch, clear_tlc_calibration): assert not os.path.exists(tlc_path(PIPETTE_ID)) # file does not exist (FileNotFoundError) with pytest.raises(cs_types.TipLengthCalNotFound): result = get.load_tip_length_calibration(PIPETTE_ID, minimalLabwareDef, '') # labware hash not in calibration file (KeyError) calpath = config.get_tip_length_cal_path() with open(calpath / f'{PIPETTE_ID}.json', 'w') as offset_file: test_offset = {'FAKE_HASH': {'tipLength': 22.0, 'lastModified': 1}} json.dump(test_offset, offset_file) with pytest.raises(cs_types.TipLengthCalNotFound): result = get.load_tip_length_calibration(PIPETTE_ID, minimalLabwareDef, '')
def save_tip_length_calibration(pip_id: str, tip_length_cal: 'PipTipLengthCalibration'): """ Function used to save tip length calibration to file. :param pip_id: pipette id to associate with this tip length :param tip_length_cal: results of the data created using :meth:`create_tip_length_data` """ tip_length_dir_path = config.get_tip_length_cal_path() tip_length_dir_path.mkdir(parents=True, exist_ok=True) pip_tip_length_path = tip_length_dir_path / f'{pip_id}.json' for lw_hash in tip_length_cal.keys(): _append_to_index_tip_length_file(pip_id, lw_hash) try: tip_length_data = io.read_cal_file(str(pip_tip_length_path)) except FileNotFoundError: tip_length_data = {} tip_length_data.update(tip_length_cal) io.save_to_file(pip_tip_length_path, tip_length_data)
def tlc_path(pip_id): return config.get_tip_length_cal_path() \ / '{}.json'.format(pip_id)