def labware_from_paths(paths: List[str]) -> Dict[str, Dict[str, Any]]: labware_defs: Dict[str, Dict[str, Any]] = {} for strpath in paths: log.info(f"local labware: checking path {strpath}") purepath = pathlib.PurePath(strpath) if purepath.is_absolute(): path = pathlib.Path(purepath) else: path = pathlib.Path.cwd() / purepath if not path.is_dir(): raise RuntimeError(f'{path} is not a directory') for child in path.iterdir(): if child.is_file() and child.suffix.endswith('json'): try: defn = labware.verify_definition(child.read_bytes()) except (ValidationError, JSONDecodeError) as e: print(f'{child}: invalid ({str(e)})') log.info(f"{child}: invalid ({str(e)})") else: uri = labware.uri_from_definition(defn) labware_defs[uri] = defn log.info(f'loaded labware {uri} from {child}') else: log.info(f'ignoring {child} in labware path') return labware_defs
def test_add_index_file(labware_name, index_file_dir): deck = Deck() parent = deck.position_for(1) definition = labware.get_labware_definition(labware_name) lw = labware.Labware(definition, parent) labware_hash = labware._hash_labware_def(lw._definition) labware._add_to_index_offset_file(lw, labware_hash) lw_uri = labware.uri_from_definition(definition) str_parent = labware._get_parent_identifier(lw.parent) slot = '1' if str_parent: mod_dict = {str_parent: f'{slot}-{str_parent}'} else: mod_dict = {} blob = { "id": f'{labware_hash}', "slot": f'{labware_hash}{str_parent}', "module": mod_dict } lw_path = index_file_dir / 'index.json' info = labware._read_file(lw_path) assert info[lw_uri] == blob
def test_uris(): details = ('opentrons', 'opentrons_96_tiprack_300ul', '1') uri = 'opentrons/opentrons_96_tiprack_300ul/1' assert labware.uri_from_details(*details) == uri defn = labware.get_labware_definition(details[1], details[0], details[2]) assert labware.uri_from_definition(defn) == uri lw = labware.Labware(defn, Location(Point(0, 0, 0), 'Test Slot')) assert lw.uri == uri
def build_and_prep( cls, name, contents, hardware, loop, broker, motion_lock, extra_labware ): protocol = parse(contents, filename=name, extra_labware={labware.uri_from_definition(defn): defn for defn in extra_labware}) sess = cls(name, protocol, hardware, loop, broker, motion_lock) sess.prepare() return sess
def create_bundle(contents: BundleContents, into_file: BinaryIO): """ Create a bundle from assumed-good contents """ with ZipFile(into_file, mode='w') as zf: zf.writestr(MAIN_PROTOCOL_FILENAME, contents.protocol) for dataname, datafile in contents.bundled_data.items(): name = PurePath(dataname).name zf.writestr(f'{DATA_DIR}/{name}', datafile) for lwdef in contents.bundled_labware.values(): zipsafe = uri_from_definition(lwdef, '-') zf.writestr(f'{LABWARE_DIR}/{zipsafe}.json', json.dumps(lwdef)) zf.writestr('.bundle_beta', str(date.today()))
def extract_bundle(bundle: ZipFile) -> BundleContents: # noqa(C901) """ Extract a bundle and verify its contents and structure. """ if not _has_files_at_root(bundle): raise RuntimeError( 'No files found in ZIP file\'s root directory. When selecting ' 'files to zip, make sure to directly select the files ' 'themselves. Do not select their parent directory, which would ' 'result in nesting all files inside that directory in the ZIP.') try: with bundle.open(MAIN_PROTOCOL_FILENAME, 'r') as protocol_file: py_protocol = protocol_file.read().decode('utf-8') except KeyError: raise RuntimeError( f'Bundled protocol should have a {MAIN_PROTOCOL_FILENAME} ' + 'file in the root directory') bundled_labware: Dict[str, 'LabwareDefinition'] = {} bundled_data = {} bundled_python = {} for zipInfo in bundle.infolist(): filepath = PurePosixPath(zipInfo.filename) rootpath = filepath.parts[0] # skip directories and weird OS-added directories # (note: the __MACOSX dir would contain '__MACOSX/foo.py' # and other files. This would break our inferences, so we need # to exclude all contents of that directory) if rootpath == '__MACOSX' or zipInfo.is_dir(): continue with bundle.open(zipInfo) as f: if rootpath == LABWARE_DIR and filepath.suffix == '.json': labware_def = json.loads(f.read().decode('utf-8')) labware_key = uri_from_definition(labware_def) if labware_key in bundled_labware: raise RuntimeError( f'Conflicting labware in bundle: {labware_key}') bundled_labware[labware_key] = labware_def elif rootpath == DATA_DIR: # note: data files are read as binary bundled_data[str(filepath.relative_to(DATA_DIR))] = f.read() elif (filepath.suffix == '.py' and str(filepath) != MAIN_PROTOCOL_FILENAME): bundled_python[str(filepath)] = f.read().decode('utf-8') if not bundled_labware: raise RuntimeError('No labware definitions found in bundle.') return BundleContents(py_protocol, bundled_labware, bundled_data, bundled_python)