Skip to content

Commit febeeea

Browse files
committed
fix(shared-data): handle bad files in pipette defs
We are iterating through pipette definitions on disk but not really handlign the possibility that you might have random files or dirs in there. Now we do, and you can drop .DS_Store to your heart's content. Closes RQA-2726
1 parent 36a7501 commit febeeea

File tree

1 file changed

+23
-11
lines changed
  • shared-data/python/opentrons_shared_data/pipette

1 file changed

+23
-11
lines changed

shared-data/python/opentrons_shared_data/pipette/load_data.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import json
22
import os
3+
from pathlib import Path
4+
from logging import getLogger
35

4-
from typing import Dict, Any, Union, Optional, List
6+
from typing import Dict, Any, Union, Optional, List, Iterator
57
from typing_extensions import Literal
68
from functools import lru_cache
79

@@ -26,6 +28,7 @@
2628

2729
LoadedConfiguration = Dict[str, Union[str, Dict[str, Any]]]
2830

31+
LOG = getLogger(__name__)
2932

3033
def _get_configuration_dictionary(
3134
config_type: Literal["general", "geometry", "liquid"],
@@ -95,6 +98,11 @@ def _physical(
9598
) -> LoadedConfiguration:
9699
return _get_configuration_dictionary("general", channels, model, version)
97100

101+
def _dirs_in(path: Path) -> Iterator[Path]:
102+
for child in path.iterdir():
103+
if child.is_dir():
104+
yield child
105+
98106

99107
@lru_cache(maxsize=None)
100108
def load_serial_lookup_table() -> Dict[str, str]:
@@ -112,23 +120,27 @@ def load_serial_lookup_table() -> Dict[str, str]:
112120
"eight_channel": "multi",
113121
}
114122
_model_shorthand = {"p1000": "p1k", "p300": "p3h"}
115-
for channel_dir in os.listdir(config_path):
116-
for model_dir in os.listdir(config_path / channel_dir):
117-
for version_file in os.listdir(config_path / channel_dir / model_dir):
118-
version_list = version_file.split(".json")[0].split("_")
119-
built_model = f"{model_dir}_{_channel_model_str[channel_dir]}_v{version_list[0]}.{version_list[1]}"
120-
121-
model_shorthand = _model_shorthand.get(model_dir, model_dir)
122-
123+
for channel_dir in _dirs_in(config_path):
124+
for model_dir in _dirs_in(channel_dir):
125+
for version_file in model_dir.iterdir():
126+
if version_file.suffix != '.json':
127+
continue
128+
try:
129+
version_list = version_file.stem.split("_")
130+
built_model = f"{model_dir.stem}_{_channel_model_str[channel_dir.stem]}_v{version_list[0]}.{version_list[1]}"
131+
except IndexError:
132+
LOG.warning(f'Pipette def with bad name {version_file} ignored')
133+
continue
134+
model_shorthand = _model_shorthand.get(model_dir.stem, model_dir.stem)
123135
if (
124136
model_dir == "p300"
125137
and int(version_list[0]) == 1
126138
and int(version_list[1]) == 0
127139
):
128140
# Well apparently, we decided to switch the shorthand of the p300 depending
129141
# on whether it's a "V1" model or not...so...here is the lovely workaround.
130-
model_shorthand = model_dir
131-
serial_shorthand = f"{model_shorthand.upper()}{_channel_shorthand[channel_dir]}V{version_list[0]}{version_list[1]}"
142+
model_shorthand = model_dir.stem
143+
serial_shorthand = f"{model_shorthand.upper()}{_channel_shorthand[channel_dir.stem]}V{version_list[0]}{version_list[1]}"
132144
_lookup_table[serial_shorthand] = built_model
133145
return _lookup_table
134146

0 commit comments

Comments
 (0)