1
1
# noqa: D100
2
2
3
- from typing import Iterator
3
+ from datetime import datetime
4
+ from dataclasses import dataclass
5
+ from typing import Iterator , Sequence
4
6
from typing_extensions import assert_never
5
7
6
8
from opentrons .protocol_engine .types import (
9
11
OnAddressableAreaOffsetLocationSequenceComponent ,
10
12
OnModuleOffsetLocationSequenceComponent ,
11
13
OnLabwareOffsetLocationSequenceComponent ,
12
- LabwareOffsetLocationSequenceComponents ,
13
- LabwareOffsetLocationSequence ,
14
14
)
15
15
16
16
from robot_server .persistence .tables import (
17
17
labware_offset_table ,
18
18
labware_offset_location_sequence_components_table ,
19
19
)
20
- from .models import StoredLabwareOffset , DoNotFilterType , DO_NOT_FILTER
20
+ from .models import (
21
+ StoredLabwareOffset ,
22
+ DoNotFilterType ,
23
+ DO_NOT_FILTER ,
24
+ StoredLabwareOffsetLocationSequenceComponents ,
25
+ ReturnedLabwareOffsetLocationSequenceComponents ,
26
+ UnknownLabwareOffsetLocationSequenceComponent ,
27
+ )
21
28
22
29
import sqlalchemy
23
30
import sqlalchemy .exc
24
31
25
32
from ._search_query_builder import SearchQueryBuilder
26
33
34
+ ReturnedLabwareOffsetLocationSequence = Sequence [
35
+ ReturnedLabwareOffsetLocationSequenceComponents
36
+ ]
37
+
38
+
39
+ @dataclass
40
+ class IncomingStoredLabwareOffset :
41
+ """Internal class for representing valid incoming offsets."""
42
+
43
+ id : str
44
+ createdAt : datetime
45
+ definitionUri : str
46
+ locationSequence : Sequence [StoredLabwareOffsetLocationSequenceComponents ]
47
+ vector : LabwareOffsetVector
48
+
27
49
28
50
class LabwareOffsetStore :
29
51
"""A persistent store for labware offsets, to support the `/labwareOffsets` endpoints."""
@@ -37,7 +59,10 @@ def __init__(self, sql_engine: sqlalchemy.engine.Engine) -> None:
37
59
"""
38
60
self ._sql_engine = sql_engine
39
61
40
- def add (self , offset : StoredLabwareOffset ) -> None :
62
+ def add (
63
+ self ,
64
+ offset : IncomingStoredLabwareOffset ,
65
+ ) -> None :
41
66
"""Store a new labware offset."""
42
67
with self ._sql_engine .begin () as transaction :
43
68
offset_row_id = transaction .execute (
@@ -131,7 +156,7 @@ def __init__(self, bad_offset_id: str) -> None:
131
156
132
157
def _sql_sequence_component_to_pydantic_sequence_component (
133
158
component_row : sqlalchemy .engine .Row ,
134
- ) -> LabwareOffsetLocationSequenceComponents :
159
+ ) -> ReturnedLabwareOffsetLocationSequenceComponents :
135
160
if component_row .component_kind == "onLabware" :
136
161
return OnLabwareOffsetLocationSequenceComponent (
137
162
labwareUri = component_row .primary_component_value
@@ -145,14 +170,19 @@ def _sql_sequence_component_to_pydantic_sequence_component(
145
170
addressableAreaName = component_row .primary_component_value
146
171
)
147
172
else :
148
- raise KeyError (component_row .component_kind )
173
+ return UnknownLabwareOffsetLocationSequenceComponent (
174
+ storedKind = component_row .component_kind ,
175
+ primaryValue = component_row .primary_component_value ,
176
+ )
149
177
150
178
151
179
def _collate_sql_locations (
152
180
first_row : sqlalchemy .engine .Row , rest_rows : Iterator [sqlalchemy .engine .Row ]
153
- ) -> tuple [LabwareOffsetLocationSequence , sqlalchemy .engine .Row | None ]:
181
+ ) -> tuple [
182
+ list [ReturnedLabwareOffsetLocationSequenceComponents ], sqlalchemy .engine .Row | None
183
+ ]:
154
184
offset_id = first_row .offset_id
155
- location_sequence : list [LabwareOffsetLocationSequenceComponents ] = [
185
+ location_sequence : list [ReturnedLabwareOffsetLocationSequenceComponents ] = [
156
186
_sql_sequence_component_to_pydantic_sequence_component (first_row )
157
187
]
158
188
while True :
@@ -197,7 +227,9 @@ def _collate_sql_to_pydantic(
197
227
yield result
198
228
199
229
200
- def _pydantic_to_sql_offset (labware_offset : StoredLabwareOffset ) -> dict [str , object ]:
230
+ def _pydantic_to_sql_offset (
231
+ labware_offset : IncomingStoredLabwareOffset ,
232
+ ) -> dict [str , object ]:
201
233
return dict (
202
234
offset_id = labware_offset .id ,
203
235
definition_uri = labware_offset .definitionUri ,
@@ -210,7 +242,7 @@ def _pydantic_to_sql_offset(labware_offset: StoredLabwareOffset) -> dict[str, ob
210
242
211
243
212
244
def _pydantic_to_sql_location_sequence_iterator (
213
- labware_offset : StoredLabwareOffset , offset_row_id : int
245
+ labware_offset : IncomingStoredLabwareOffset , offset_row_id : int
214
246
) -> Iterator [dict [str , object ]]:
215
247
for index , component in enumerate (labware_offset .locationSequence ):
216
248
if isinstance (component , OnLabwareOffsetLocationSequenceComponent ):
0 commit comments