def dummy_load(labware_name, namespace=None, version=None, bundled_defs=None, extra_defs=None, api_level=None): # TODO: Ian 2019-05-30 use fixtures not real defs labware_def = json.loads( load_shared_data(f'labware/definitions/2/{labware_name}/1.json')) return labware_def
def validate_json( protocol_json: Dict[Any, Any]) -> Tuple[int, 'JsonProtocolDef']: """ Validates a json protocol and returns its schema version """ # Check if this is actually a labware labware_schema_v2 = json.loads( load_shared_data('labware/schemas/2.json').decode('utf-8')) try: jsonschema.validate(protocol_json, labware_schema_v2) except jsonschema.ValidationError: pass else: MODULE_LOG.error("labware uploaded instead of protocol") raise RuntimeError( 'The file you are trying to open is a JSON labware definition, ' 'and therefore can not be opened here. Please try ' 'uploading a JSON protocol file instead.') # this is now either a protocol or something corrupt version_num = _get_protocol_schema_version(protocol_json) if version_num <= 2: raise RuntimeError( f'JSON protocol version {version_num} is ' 'deprecated. Please upload your protocol into Protocol ' 'Designer and save it to migrate the protocol to a later ' 'version. This error might mean a labware ' 'definition was specified instead of a protocol.') if version_num > MAX_SUPPORTED_JSON_SCHEMA_VERSION: raise RuntimeError( f'The protocol you are trying to open is a JSONv{version_num} ' 'protocol and is not supported by your current robot server ' 'version. Please update your OT-2 App and robot server to the ' 'latest version and try again.') protocol_schema = _get_schema_for_protocol(version_num) # instruct schema how to resolve all $ref's used in protocol schemas resolver = jsonschema.RefResolver( protocol_schema.get('$id', ''), protocol_schema, store={"opentronsLabwareSchemaV2": labware_schema_v2}) # do the validation try: jsonschema.validate(protocol_json, protocol_schema, resolver=resolver) except jsonschema.ValidationError: MODULE_LOG.exception("JSON protocol validation failed") raise RuntimeError( 'This may be a corrupted file or a JSON file that is not an ' 'Opentrons JSON protocol.') else: return version_num, protocol_json # type: ignore
def _get_schema_for_protocol(version_num: int) -> protocol.Schema: """ Retrieve the json schema for a protocol schema version """ # TODO(IL, 2020/03/05): use $otSharedSchema, but maybe wait until # deprecating v1/v2 JSON protocols? if version_num > MAX_SUPPORTED_JSON_SCHEMA_VERSION: raise RuntimeError(f'JSON Protocol version {version_num} is not yet ' + 'supported in this version of the API') try: schema = load_shared_data(f'protocol/schemas/{version_num}.json') except FileNotFoundError: schema = None # type: ignore if not schema: raise RuntimeError( 'JSON Protocol schema "{}" does not exist'.format(version_num)) return json.loads(schema.decode('utf-8'))
def test_module_load_v1(v1_module_name): module_defs = json.loads(load_shared_data('module/definitions/1.json')) model = module_geometry.resolve_module_model(v1_module_name) mod = module_geometry.load_module(model, Location(Point(0, 0, 0), 'test')) mod_def = module_defs[v1_module_name] offset = Point(mod_def['labwareOffset']['x'], mod_def['labwareOffset']['y'], mod_def['labwareOffset']['z']) high_z = mod_def['dimensions']['bareOverallHeight'] assert mod.highest_z == high_z assert mod.location.point == offset mod = module_geometry.load_module(model, Location(Point(1, 2, 3), 'test')) assert mod.highest_z == high_z + 3 assert mod.location.point == (offset + Point(1, 2, 3)) mod2 = module_geometry.load_module_from_definition( module_defs[v1_module_name], Location(Point(3, 2, 1), 'test2'), module_geometry.ThermocyclerConfiguration.FULL) assert mod2.highest_z == high_z + 1 assert mod2.location.point == (offset + Point(3, 2, 1))
def test_module_load_labware(loop): ctx = papi.ProtocolContext(loop) labware_name = 'corning_96_wellplate_360ul_flat' # TODO Ian 2019-05-29 load fixtures, not real defs labware_def = json.loads( load_shared_data(f'labware/definitions/2/{labware_name}/1.json')) ctx._hw_manager.hardware._backend._attached_modules = [('mod0', 'tempdeck') ] mod = ctx.load_module('Temperature Module', 1) assert mod.labware is None lw = mod.load_labware(labware_name) lw_offset = Point(labware_def['cornerOffsetFromSlot']['x'], labware_def['cornerOffsetFromSlot']['y'], labware_def['cornerOffsetFromSlot']['z']) assert lw._offset == lw_offset + mod._geometry.location.point assert lw.name == labware_name # Test load with old name mod2 = ctx.load_module('tempdeck', 2) lw2 = mod2.load_labware(labware_name) assert lw2._offset == lw_offset + mod2._geometry.location.point
def verify_definition(contents: Union[ AnyStr, 'LabwareDefinition', Dict[str, Any]])\ -> 'LabwareDefinition': """ Verify that an input string is a labware definition and return it. If the definition is invalid, an exception is raised; otherwise parse the json and return the valid definition. :raises json.JsonDecodeError: If the definition is not valid json :raises jsonschema.ValidationError: If the definition is not valid. :returns: The parsed definition """ schema_body = load_shared_data('labware/schemas/2.json').decode('utf-8') labware_schema_v2 = json.loads(schema_body) if isinstance(contents, dict): to_return = contents else: to_return = json.loads(contents) jsonschema.validate(to_return, labware_schema_v2) # we can type ignore this because if it passes the jsonschema it has # the correct structure return to_return # type: ignore
def test_magdeck_gen1_labware_props(loop): ctx = papi.ProtocolContext(loop) # TODO Ian 2019-05-29 load fixtures, not real defs labware_name = 'biorad_96_wellplate_200ul_pcr' labware_def = json.loads( load_shared_data(f'labware/definitions/2/{labware_name}/1.json')) mod = ctx.load_module('magdeck', 1) assert mod.labware is None mod.engage(height=45) assert mod._module.current_height == 45 with pytest.raises(ValueError): mod.engage(height=45.1) # max engage height for gen1 is 45 mm mod.load_labware(labware_name) mod.engage() lw_offset = labware_def['parameters']['magneticModuleEngageHeight'] assert mod._module._driver.plate_height == lw_offset mod.disengage() mod.engage(offset=2) assert mod._module._driver.plate_height == lw_offset + 2 mod.disengage() mod.engage(height=3) assert mod._module._driver.plate_height == 3 mod._geometry.reset_labware() labware_name = 'corning_96_wellplate_360ul_flat' mod.load_labware(labware_name) with pytest.raises(ValueError): mod.engage() with pytest.raises(ValueError): mod.engage(offset=1) mod.engage(height=2) assert mod._module._driver.plate_height == 2 mod.engage(height=0) assert mod._module._driver.plate_height == 0 mod.engage(height_from_base=2) assert mod._module._driver.plate_height == 2 +\ OFFSET_TO_LABWARE_BOTTOM[mod._module.model()]
def test_load_schema(schemaname): assert load_schema(schemaname)\ == json.loads(load_shared_data(f'module/schemas/{schemaname}.json'))
def test_load_v1_defs(defname): assert load_definition('1', defname)\ == json.loads(load_shared_data(f'module/definitions/1.json'))[defname]
def test_load_v2_defs(defname): assert load_definition('2', defname)\ == json.loads(load_shared_data(f'module/definitions/2/{defname}.json'))
import json from unittest.mock import patch from numpy import isclose import pytest from opentrons import types from opentrons.config import pipette_config, feature_flags as ff, CONFIG from opentrons_shared_data import load_shared_data defs = json.loads( load_shared_data("pipette/definitions/pipetteModelSpecs.json")) def check_sequences_close(first, second): """ Check two ul/mm sequences are the same (replaces pytest.approx nested ) """ assert len(first) == len(second) for f, s in zip(first, second): assert f == pytest.approx(s) @pytest.mark.parametrize('pipette_model', [ c for c in pipette_config.config_models if not (c.startswith('p1000') or c.startswith('p300_multi') or c.endswith('1.5') or c.endswith('1.6') or 'v2' in c) ]) def test_versioned_aspiration(pipette_model, monkeypatch): monkeypatch.setattr(ff, 'use_old_aspiration_functions', lambda: True)
def test_v3_types(defpath): defn = json.loads(load_shared_data(defpath)) typeguard.check_type('defn', defn, JsonProtocolV3)