Skip to content

Commit

Permalink
feat(api): provide labware schema 3 definitions if possible (#16148)
Browse files Browse the repository at this point in the history
  • Loading branch information
caila-marashaj authored Aug 28, 2024
1 parent f2ff007 commit ce66744
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 42 deletions.
1 change: 0 additions & 1 deletion api/src/opentrons/protocol_api/labware.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
# remove when their usage is no longer needed
from opentrons.protocols.labware import ( # noqa: F401
get_labware_definition as get_labware_definition,
get_all_labware_definitions as get_all_labware_definitions,
verify_definition as verify_definition,
save_definition as save_definition,
)
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocols/api_support/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

OPENTRONS_NAMESPACE = "opentrons"
CUSTOM_NAMESPACE = "custom_beta"
STANDARD_DEFS_PATH = Path("labware/definitions/2")
STANDARD_DEFS_PATH = Path("labware/definitions")
USER_DEFS_PATH = get_opentrons_path("labware_user_definitions_dir_v2")
10 changes: 0 additions & 10 deletions api/src/opentrons/protocols/api_support/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,3 @@ def _check_version_wrapper(*args: Any, **kwargs: Any) -> Any:
return cast(FuncT, _check_version_wrapper)

return _set_version


class ModifiedList(list[str]):
def __contains__(self, item: object) -> bool:
if not isinstance(item, str):
return False
for name in self:
if name == item.replace("-", "_").lower():
return True
return False
45 changes: 15 additions & 30 deletions api/src/opentrons/protocols/labware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import logging
import json
import os

from pathlib import Path
from typing import Any, AnyStr, List, Dict, Optional, Union
from typing import Any, AnyStr, Dict, Optional, Union

import jsonschema # type: ignore

from opentrons.protocols.api_support.util import ModifiedList
from opentrons_shared_data import load_shared_data, get_shared_data_root
from opentrons.protocols.api_support.constants import (
OPENTRONS_NAMESPACE,
Expand Down Expand Up @@ -63,29 +61,6 @@ def get_labware_definition(
return _get_standard_labware_definition(load_name, namespace, version)


def get_all_labware_definitions() -> List[str]:
"""
Return a list of standard and custom labware definitions with load_name +
name_space + version existing on the robot
"""
labware_list = ModifiedList()

def _check_for_subdirectories(path: Union[str, Path, os.DirEntry[str]]) -> None:
with os.scandir(path) as top_path:
for sub_dir in top_path:
if sub_dir.is_dir():
labware_list.append(sub_dir.name)

# check for standard labware
_check_for_subdirectories(get_shared_data_root() / STANDARD_DEFS_PATH)

# check for custom labware
for namespace in os.scandir(USER_DEFS_PATH):
_check_for_subdirectories(namespace)

return labware_list


def save_definition(
labware_def: LabwareDefinition, force: bool = False, location: Optional[Path] = None
) -> None:
Expand Down Expand Up @@ -114,7 +89,6 @@ def save_definition(
f'Saving definitions to the "{OPENTRONS_NAMESPACE}" namespace '
+ "is not permitted"
)

def_path = _get_path_to_labware(load_name, namespace, version, location)

if not force and def_path.is_file():
Expand Down Expand Up @@ -219,7 +193,6 @@ def _get_standard_labware_definition(
Definitions Folder from the Opentrons App before
uploading your protocol.
"""

if namespace is None:
for fallback_namespace in [OPENTRONS_NAMESPACE, CUSTOM_NAMESPACE]:
try:
Expand Down Expand Up @@ -252,9 +225,21 @@ def _get_path_to_labware(
) -> Path:
if namespace == OPENTRONS_NAMESPACE:
# all labware in OPENTRONS_NAMESPACE is stored in shared data
return (
get_shared_data_root() / STANDARD_DEFS_PATH / load_name / f"{version}.json"
schema_3_path = (
get_shared_data_root()
/ STANDARD_DEFS_PATH
/ "3"
/ load_name
/ f"{version}.json"
)
schema_2_path = (
get_shared_data_root()
/ STANDARD_DEFS_PATH
/ "2"
/ load_name
/ f"{version}.json"
)
return schema_3_path if schema_3_path.exists() else schema_2_path
if not base_path:
base_path = USER_DEFS_PATH
def_path = base_path / namespace / load_name / f"{version}.json"
Expand Down

0 comments on commit ce66744

Please sign in to comment.