benchling-sdk 1.10.0a4__py3-none-any.whl → 1.10.0a6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,190 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from abc import ABC, abstractmethod
4
- from datetime import date, datetime
5
- import json
6
- from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
7
-
8
- from benchling_sdk.models import (
9
- BooleanAppConfigItemType,
10
- DateAppConfigItemType,
11
- DatetimeAppConfigItemType,
12
- FloatAppConfigItemType,
13
- IntegerAppConfigItemType,
14
- JsonAppConfigItemType,
15
- SecureTextAppConfigItemType,
16
- TextAppConfigItemType,
17
- )
18
-
19
- JsonType = Union[Dict[str, Any], List[Any], str, int, float, bool]
20
- ScalarType = TypeVar("ScalarType", bool, date, datetime, float, int, JsonType, str)
21
- # JsonType support requires object to be unioned. Currently we do it inline.
22
- ScalarModelType = Union[bool, date, datetime, float, int, str]
23
- # Enum values cannot be used in literals, so copy the strings
24
- ScalarConfigItemType = Union[
25
- BooleanAppConfigItemType,
26
- DateAppConfigItemType,
27
- DatetimeAppConfigItemType,
28
- FloatAppConfigItemType,
29
- IntegerAppConfigItemType,
30
- JsonAppConfigItemType,
31
- SecureTextAppConfigItemType,
32
- TextAppConfigItemType,
33
- ]
34
-
35
-
36
- # TODO BNCH-52772 We should probably consider moving all of these to a weakly _ internal scoped usage
37
-
38
-
39
- class ScalarDefinition(ABC, Generic[ScalarType]):
40
- """
41
- Scalar definition.
42
-
43
- Map how ScalarConfigTypes values can be converted into corresponding Python types.
44
- """
45
-
46
- @classmethod
47
- def init(cls):
48
- """Init."""
49
- return cls()
50
-
51
- @classmethod
52
- @abstractmethod
53
- def from_str(cls, value: Optional[str]) -> Optional[ScalarType]:
54
- """
55
- From string.
56
-
57
- Given an optional string value of scalar configuration, produce an Optional instance of the
58
- specific ScalarType. For instance, converting str to int.
59
-
60
- Used when coercing Python types from string values in API responses.
61
- """
62
- pass
63
-
64
-
65
- class BoolScalar(ScalarDefinition[bool]):
66
- """
67
- Bool Scalar.
68
-
69
- Turn a Boolean-like string value into bool. Any permutation of "true" - case insensitive - is interpreted
70
- as True. Any other non-empty string is False.
71
- """
72
-
73
- @classmethod
74
- def from_str(cls, value: Optional[str]) -> Optional[bool]:
75
- """Convert optional str to optional bool."""
76
- # Though the spec declares str, this is actually being sent in JSON as a real Boolean
77
- # So runtime check defensively
78
- if value is not None:
79
- if isinstance(value, bool):
80
- return value
81
- if value.lower() == "true":
82
- return True
83
- return False
84
- return None
85
-
86
-
87
- class DateScalar(ScalarDefinition[date]):
88
- """
89
- Date Scalar.
90
-
91
- Turn an ISO formatted date like YYYY-MM-dd into a date.
92
- """
93
-
94
- @classmethod
95
- def from_str(cls, value: Optional[str]) -> Optional[date]:
96
- """Convert optional str to optional date."""
97
- return date.fromisoformat(value) if value is not None else None
98
-
99
-
100
- class DateTimeScalar(ScalarDefinition[datetime]):
101
- """
102
- Date Time Scalar.
103
-
104
- Turn a date time string into datetime.
105
- """
106
-
107
- @classmethod
108
- def from_str(cls, value: Optional[str]) -> Optional[datetime]:
109
- """Convert optional str to optional datetime."""
110
- return datetime.strptime(value, cls.expected_format()) if value is not None else None
111
-
112
- @staticmethod
113
- def expected_format() -> str:
114
- """Return the expected date mask for parsing string to datetime."""
115
- return "%Y-%m-%d %I:%M:%S %p"
116
-
117
-
118
- class IsoDateTimeScalar(ScalarDefinition[datetime]):
119
- """
120
- Iso Date Time Scalar.
121
-
122
- Turn a ISO 8601 date time string into datetime. Benchling fields use RFC 3339, unlike app config date times.
123
- """
124
-
125
- @classmethod
126
- def from_str(cls, value: Optional[str]) -> Optional[datetime]:
127
- """Convert optional str to optional datetime."""
128
- return datetime.fromisoformat(value) if value is not None else None
129
-
130
-
131
- class FloatScalar(ScalarDefinition[float]):
132
- """
133
- Float Scalar.
134
-
135
- Turn a string into float. Assumes the string, if not empty, is a valid floating point.
136
- """
137
-
138
- @classmethod
139
- def from_str(cls, value: Optional[str]) -> Optional[float]:
140
- """Convert optional str to optional float."""
141
- return float(value) if value is not None else None
142
-
143
-
144
- class IntScalar(ScalarDefinition[int]):
145
- """
146
- Int Scalar.
147
-
148
- Turn a string into int. Assumes the string, if not empty, is a valid integer.
149
- """
150
-
151
- @classmethod
152
- def from_str(cls, value: Optional[str]) -> Optional[int]:
153
- """Convert optional str to optional int."""
154
- return int(value) if value is not None else None
155
-
156
-
157
- class JsonScalar(ScalarDefinition[JsonType]):
158
- """
159
- Json Scalar.
160
-
161
- Turn a string into JSON. Assumes the string is a valid JSON string.
162
- """
163
-
164
- @classmethod
165
- def from_str(cls, value: Optional[str]) -> Optional[JsonType]:
166
- """Convert optional str to optional JsonType."""
167
- return json.loads(value) if value is not None else None
168
-
169
-
170
- class TextScalar(ScalarDefinition[str]):
171
- """
172
- Text Scalar.
173
-
174
- Text is already a string, so no conversion is performed.
175
- """
176
-
177
- @classmethod
178
- def from_str(cls, value: Optional[str]) -> Optional[str]:
179
- """Convert optional str to optional str. Implemented to appease ScalarDefinition contract."""
180
- return value
181
-
182
-
183
- class SecureTextScalar(TextScalar):
184
- """
185
- Secure Text Scalar.
186
-
187
- Text is already a string, so no conversion is performed.
188
- """
189
-
190
- pass