sapiopycommons 2025.7.1a576__py3-none-any.whl → 2025.7.2a579__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.
Potentially problematic release.
This version of sapiopycommons might be problematic. Click here for more details.
- sapiopycommons/callbacks/callback_util.py +665 -332
- sapiopycommons/callbacks/field_builder.py +2 -0
- sapiopycommons/chem/IndigoMolecules.py +31 -1
- sapiopycommons/chem/Molecules.py +3 -3
- sapiopycommons/chem/ps_commons.py +381 -0
- sapiopycommons/customreport/auto_pagers.py +26 -1
- sapiopycommons/customreport/term_builder.py +1 -1
- sapiopycommons/datatype/pseudo_data_types.py +349 -326
- sapiopycommons/eln/experiment_cache.py +188 -0
- sapiopycommons/eln/experiment_handler.py +408 -767
- sapiopycommons/eln/experiment_report_util.py +11 -6
- sapiopycommons/eln/experiment_step_factory.py +476 -0
- sapiopycommons/eln/plate_designer.py +7 -2
- sapiopycommons/eln/step_creation.py +236 -0
- sapiopycommons/files/file_util.py +7 -5
- sapiopycommons/general/accession_service.py +2 -2
- sapiopycommons/general/aliases.py +3 -1
- sapiopycommons/general/audit_log.py +7 -0
- sapiopycommons/general/custom_report_util.py +12 -0
- sapiopycommons/general/data_structure_util.py +115 -0
- sapiopycommons/processtracking/custom_workflow_handler.py +11 -1
- sapiopycommons/processtracking/endpoints.py +27 -0
- sapiopycommons/recordmodel/record_handler.py +657 -317
- sapiopycommons/rules/eln_rule_handler.py +8 -1
- sapiopycommons/rules/on_save_rule_handler.py +8 -1
- sapiopycommons/webhook/webhook_handlers.py +3 -0
- sapiopycommons/webhook/webservice_handlers.py +2 -2
- {sapiopycommons-2025.7.1a576.dist-info → sapiopycommons-2025.7.2a579.dist-info}/METADATA +2 -2
- sapiopycommons-2025.7.2a579.dist-info/RECORD +69 -0
- sapiopycommons/ai/__init__.py +0 -0
- sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.py +0 -43
- sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.pyi +0 -31
- sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.py +0 -123
- sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.pyi +0 -598
- sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/proto/step_output_pb2.py +0 -45
- sapiopycommons/ai/api/plan/proto/step_output_pb2.pyi +0 -42
- sapiopycommons/ai/api/plan/proto/step_output_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/proto/step_pb2.py +0 -43
- sapiopycommons/ai/api/plan/proto/step_pb2.pyi +0 -43
- sapiopycommons/ai/api/plan/proto/step_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/script/proto/script_pb2.py +0 -53
- sapiopycommons/ai/api/plan/script/proto/script_pb2.pyi +0 -99
- sapiopycommons/ai/api/plan/script/proto/script_pb2_grpc.py +0 -153
- sapiopycommons/ai/api/plan/tool/proto/entry_pb2.py +0 -57
- sapiopycommons/ai/api/plan/tool/proto/entry_pb2.pyi +0 -96
- sapiopycommons/ai/api/plan/tool/proto/entry_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/tool/proto/tool_pb2.py +0 -67
- sapiopycommons/ai/api/plan/tool/proto/tool_pb2.pyi +0 -220
- sapiopycommons/ai/api/plan/tool/proto/tool_pb2_grpc.py +0 -154
- sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.py +0 -39
- sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.pyi +0 -32
- sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2_grpc.py +0 -24
- sapiopycommons/ai/protobuf_utils.py +0 -508
- sapiopycommons/ai/test_client.py +0 -216
- sapiopycommons/ai/tool_service_base.py +0 -798
- sapiopycommons-2025.7.1a576.dist-info/RECORD +0 -92
- {sapiopycommons-2025.7.1a576.dist-info → sapiopycommons-2025.7.2a579.dist-info}/WHEEL +0 -0
- {sapiopycommons-2025.7.1a576.dist-info → sapiopycommons-2025.7.2a579.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,798 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
import logging
|
|
5
|
-
import traceback
|
|
6
|
-
from abc import abstractmethod, ABC
|
|
7
|
-
from logging import Logger
|
|
8
|
-
from typing import Any, Iterable, Sequence, Mapping
|
|
9
|
-
|
|
10
|
-
from grpc import ServicerContext
|
|
11
|
-
from sapiopylib.rest.User import SapioUser, ensure_logger_initialized
|
|
12
|
-
from sapiopylib.rest.pojo.datatype.FieldDefinition import AbstractVeloxFieldDefinition
|
|
13
|
-
|
|
14
|
-
from sapiopycommons.ai.api.fielddefinitions.proto.fields_pb2 import FieldValueMapPbo, FieldValuePbo
|
|
15
|
-
from sapiopycommons.ai.api.fielddefinitions.proto.velox_field_def_pb2 import VeloxFieldDefPbo, FieldTypePbo, \
|
|
16
|
-
SelectionPropertiesPbo, IntegerPropertiesPbo, DoublePropertiesPbo, BooleanPropertiesPbo, StringPropertiesPbo
|
|
17
|
-
from sapiopycommons.ai.api.plan.tool.proto.entry_pb2 import StepOutputBatchPbo, StepItemContainerPbo, DataTypePbo, \
|
|
18
|
-
StepBinaryContainerPbo, StepCsvContainerPbo, StepCsvHeaderRowPbo, StepCsvRowPbo, StepImageContainerPbo, \
|
|
19
|
-
StepJsonContainerPbo, StepTextContainerPbo, StepInputBatchPbo
|
|
20
|
-
from sapiopycommons.ai.api.plan.tool.proto.tool_pb2 import ToolDetailsRequestPbo, ToolDetailsResponsePbo, \
|
|
21
|
-
ToolDetailsPbo, ProcessStepRequestPbo, ProcessStepResponsePbo, ToolOutputDetailsPbo, ToolIoConfigBasePbo, \
|
|
22
|
-
ToolInputDetailsPbo
|
|
23
|
-
from sapiopycommons.ai.api.plan.tool.proto.tool_pb2_grpc import ToolServiceServicer
|
|
24
|
-
from sapiopycommons.ai.api.session.proto.sapio_conn_info_pb2 import SapioUserSecretTypePbo, SapioConnectionInfoPbo
|
|
25
|
-
from sapiopycommons.ai.protobuf_utils import ProtobufUtils
|
|
26
|
-
from sapiopycommons.general.aliases import FieldMap, FieldValue
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class SapioToolResult(ABC):
|
|
30
|
-
"""
|
|
31
|
-
A class representing a result from a Sapio tool. Instantiate one of the subclasses to create a result object.
|
|
32
|
-
"""
|
|
33
|
-
|
|
34
|
-
@abstractmethod
|
|
35
|
-
def to_proto(self) -> StepOutputBatchPbo | list[FieldValueMapPbo]:
|
|
36
|
-
"""
|
|
37
|
-
Convert this SapioToolResult object to a StepOutputBatchPbo or list of FieldValueMapPbo proto objects.
|
|
38
|
-
"""
|
|
39
|
-
pass
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
class BinaryResult(SapioToolResult):
|
|
43
|
-
"""
|
|
44
|
-
A class representing binary results from a Sapio tool.
|
|
45
|
-
"""
|
|
46
|
-
binary_data: list[bytes]
|
|
47
|
-
|
|
48
|
-
def __init__(self, binary_data: list[bytes]):
|
|
49
|
-
"""
|
|
50
|
-
:param binary_data: The binary data as a list of bytes.
|
|
51
|
-
"""
|
|
52
|
-
self.binary_data = binary_data
|
|
53
|
-
|
|
54
|
-
def to_proto(self) -> StepOutputBatchPbo | list[FieldValueMapPbo]:
|
|
55
|
-
return StepOutputBatchPbo(
|
|
56
|
-
item_container=StepItemContainerPbo(
|
|
57
|
-
dataType=DataTypePbo.BINARY,
|
|
58
|
-
binary_container=StepBinaryContainerPbo(items=self.binary_data)
|
|
59
|
-
)
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
class CsvResult(SapioToolResult):
|
|
64
|
-
"""
|
|
65
|
-
A class representing CSV results from a Sapio tool.
|
|
66
|
-
"""
|
|
67
|
-
csv_data: list[dict[str, Any]]
|
|
68
|
-
|
|
69
|
-
def __init__(self, csv_data: list[dict[str, Any]]):
|
|
70
|
-
"""
|
|
71
|
-
:param csv_data: The list of CSV data results, provided as a list of dictionaries of column name to value.
|
|
72
|
-
"""
|
|
73
|
-
self.csv_data = csv_data
|
|
74
|
-
|
|
75
|
-
def to_proto(self) -> StepOutputBatchPbo | list[FieldValueMapPbo]:
|
|
76
|
-
return StepOutputBatchPbo(
|
|
77
|
-
item_container=StepItemContainerPbo(
|
|
78
|
-
dataType=DataTypePbo.CSV,
|
|
79
|
-
csv_container=StepCsvContainerPbo(
|
|
80
|
-
header=StepCsvHeaderRowPbo(cells=self.csv_data[0].keys()),
|
|
81
|
-
items=[StepCsvRowPbo(cells=[str(x) for x in row.values()]) for row in self.csv_data]
|
|
82
|
-
)
|
|
83
|
-
) if self.csv_data else None
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
class FieldMapResult(SapioToolResult):
|
|
88
|
-
"""
|
|
89
|
-
A class representing field map results from a Sapio tool.
|
|
90
|
-
"""
|
|
91
|
-
field_maps: list[FieldMap]
|
|
92
|
-
|
|
93
|
-
def __init__(self, field_maps: list[FieldMap]):
|
|
94
|
-
"""
|
|
95
|
-
:param field_maps: A list of field maps, where each map is a dictionary of field names to values. Each entry
|
|
96
|
-
will create a new data record in the system, so long as the tool definition specifies an output data type
|
|
97
|
-
name.
|
|
98
|
-
"""
|
|
99
|
-
self.field_maps = field_maps
|
|
100
|
-
|
|
101
|
-
def to_proto(self) -> StepOutputBatchPbo | list[FieldValueMapPbo]:
|
|
102
|
-
new_records: list[FieldValueMapPbo] = []
|
|
103
|
-
for field_map in self.field_maps:
|
|
104
|
-
fields: dict[str, FieldValuePbo] = {}
|
|
105
|
-
for field, value in field_map.items():
|
|
106
|
-
field_value = FieldValuePbo()
|
|
107
|
-
if isinstance(value, str):
|
|
108
|
-
field_value.string_value = value
|
|
109
|
-
elif isinstance(value, int):
|
|
110
|
-
field_value.int_value = value
|
|
111
|
-
elif isinstance(value, float):
|
|
112
|
-
field_value.double_value = value
|
|
113
|
-
elif isinstance(value, bool):
|
|
114
|
-
field_value.bool_value = value
|
|
115
|
-
fields[field] = field_value
|
|
116
|
-
new_records.append(FieldValueMapPbo(fields=fields))
|
|
117
|
-
return new_records
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
class ImageResult(SapioToolResult):
|
|
121
|
-
"""
|
|
122
|
-
A class representing image results from a Sapio tool.
|
|
123
|
-
"""
|
|
124
|
-
image_format: str
|
|
125
|
-
image_data: list[bytes]
|
|
126
|
-
|
|
127
|
-
def __init__(self, image_format: str, image_data: list[bytes]):
|
|
128
|
-
"""
|
|
129
|
-
:param image_format: The format of the image (e.g., PNG, JPEG).
|
|
130
|
-
:param image_data: The image data as a list of bytes. Each entry in the list represents a separate image.
|
|
131
|
-
"""
|
|
132
|
-
self.image_format = image_format
|
|
133
|
-
self.image_data = image_data
|
|
134
|
-
|
|
135
|
-
def to_proto(self) -> StepOutputBatchPbo | list[FieldValueMapPbo]:
|
|
136
|
-
return StepOutputBatchPbo(
|
|
137
|
-
item_container=StepItemContainerPbo(
|
|
138
|
-
dataType=DataTypePbo.IMAGE,
|
|
139
|
-
image_container=StepImageContainerPbo(
|
|
140
|
-
image_format=self.image_format,
|
|
141
|
-
items=self.image_data)
|
|
142
|
-
)
|
|
143
|
-
)
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
class JsonResult(SapioToolResult):
|
|
147
|
-
"""
|
|
148
|
-
A class representing JSON results from a Sapio tool.
|
|
149
|
-
"""
|
|
150
|
-
json_data: list[Any]
|
|
151
|
-
|
|
152
|
-
def __init__(self, json_data: list[Any]):
|
|
153
|
-
"""
|
|
154
|
-
:param json_data: The list of JSON data results. Each entry in the list represents a separate JSON object.
|
|
155
|
-
These entries must be able to be serialized to JSON using json.dumps().
|
|
156
|
-
"""
|
|
157
|
-
self.json_data = json_data
|
|
158
|
-
|
|
159
|
-
def to_proto(self) -> StepOutputBatchPbo | list[FieldValueMapPbo]:
|
|
160
|
-
return StepOutputBatchPbo(
|
|
161
|
-
item_container=StepItemContainerPbo(
|
|
162
|
-
dataType=DataTypePbo.JSON,
|
|
163
|
-
json_container=StepJsonContainerPbo(items=[json.dumps(x) for x in self.json_data])
|
|
164
|
-
)
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
class TextResult(SapioToolResult):
|
|
169
|
-
"""
|
|
170
|
-
A class representing text results from a Sapio tool.
|
|
171
|
-
"""
|
|
172
|
-
text_data: list[str]
|
|
173
|
-
|
|
174
|
-
def __init__(self, text_data: list[str]):
|
|
175
|
-
"""
|
|
176
|
-
:param text_data: The text data as a list of strings.
|
|
177
|
-
"""
|
|
178
|
-
self.text_data = text_data
|
|
179
|
-
|
|
180
|
-
def to_proto(self) -> StepOutputBatchPbo | list[FieldValueMapPbo]:
|
|
181
|
-
return StepOutputBatchPbo(
|
|
182
|
-
item_container=StepItemContainerPbo(
|
|
183
|
-
dataType=DataTypePbo.TEXT,
|
|
184
|
-
text_container=StepTextContainerPbo(items=self.text_data)
|
|
185
|
-
)
|
|
186
|
-
)
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
class ToolServiceBase(ToolServiceServicer, ABC):
|
|
190
|
-
"""
|
|
191
|
-
A base class for implementing a tool service. Subclasses should implement the register_tools method to register
|
|
192
|
-
their tools with the service.
|
|
193
|
-
"""
|
|
194
|
-
def GetToolDetails(self, request: ToolDetailsRequestPbo, context: ServicerContext) -> ToolDetailsResponsePbo:
|
|
195
|
-
try:
|
|
196
|
-
# Get the tool details from the registered tools.
|
|
197
|
-
details: list[ToolDetailsPbo] = self.get_details()
|
|
198
|
-
return ToolDetailsResponsePbo(tool_framework_version=self.tool_version(), tool_details=details)
|
|
199
|
-
except Exception:
|
|
200
|
-
# TODO: This response doesn't even allow logs. What should we do if an exception occurs in this case?
|
|
201
|
-
return ToolDetailsResponsePbo()
|
|
202
|
-
|
|
203
|
-
def ProcessData(self, request: ProcessStepRequestPbo, context: ServicerContext) -> ProcessStepResponsePbo:
|
|
204
|
-
try:
|
|
205
|
-
# Convert the SapioConnectionInfo proto object to a SapioUser object.
|
|
206
|
-
user = self.create_user(request.sapio_user)
|
|
207
|
-
# Get the tool results from the registered tool matching the request and convert them to proto objects.
|
|
208
|
-
output_data: list[StepOutputBatchPbo] = []
|
|
209
|
-
new_records: list[FieldValueMapPbo] = []
|
|
210
|
-
# TODO: Make use of the success value after the response object has a field for it.
|
|
211
|
-
success, results, logs = self.run(user, request, context)
|
|
212
|
-
for result in results:
|
|
213
|
-
data: StepOutputBatchPbo | list[FieldValueMapPbo] = result.to_proto()
|
|
214
|
-
if isinstance(data, StepOutputBatchPbo):
|
|
215
|
-
output_data.append(data)
|
|
216
|
-
else:
|
|
217
|
-
new_records.extend(data)
|
|
218
|
-
# Return a ProcessStepResponse proto object containing the output data and new records to the caller.
|
|
219
|
-
return ProcessStepResponsePbo(output=output_data, log=logs, new_records=new_records)
|
|
220
|
-
except Exception:
|
|
221
|
-
# TODO: Return a False success result after the response object has a field for it.
|
|
222
|
-
return ProcessStepResponsePbo(log=[traceback.format_exc()])
|
|
223
|
-
|
|
224
|
-
@staticmethod
|
|
225
|
-
def create_user(info: SapioConnectionInfoPbo, timeout_seconds: int = 60) -> SapioUser:
|
|
226
|
-
"""
|
|
227
|
-
Create a SapioUser object from the given SapioConnectionInfo proto object.
|
|
228
|
-
|
|
229
|
-
:param info: The SapioConnectionInfo proto object.
|
|
230
|
-
:param timeout_seconds: The request timeout for calls made from this user object.
|
|
231
|
-
"""
|
|
232
|
-
# TODO: Have a customizable request timeout? Would need to be added to the request object.
|
|
233
|
-
# TODO: How should the RMI hosts and port be used in the connection info?
|
|
234
|
-
user = SapioUser(info.webservice_url, True, timeout_seconds, guid=info.app_guid)
|
|
235
|
-
if info.secret_type == SapioUserSecretTypePbo.SESSION_TOKEN:
|
|
236
|
-
user.api_token = info.secret
|
|
237
|
-
elif info.secret_type == SapioUserSecretTypePbo.PASSWORD:
|
|
238
|
-
# TODO: Will the secret be base64 encoded if it's a password? That's how basic auth is normally handled.
|
|
239
|
-
user.password = info.secret
|
|
240
|
-
else:
|
|
241
|
-
raise Exception(f"Unexpected secret type: {info.secret_type}")
|
|
242
|
-
return user
|
|
243
|
-
|
|
244
|
-
@staticmethod
|
|
245
|
-
def tool_version() -> int:
|
|
246
|
-
"""
|
|
247
|
-
:return: The version of this tool.
|
|
248
|
-
"""
|
|
249
|
-
return 1
|
|
250
|
-
|
|
251
|
-
def _get_tools(self) -> list[ToolBase]:
|
|
252
|
-
"""
|
|
253
|
-
return: Get instances of the tools registered with this service.
|
|
254
|
-
"""
|
|
255
|
-
# This is complaining about the name and description not being filled from ToolBase,
|
|
256
|
-
# but none of the provided tools should have any init parameters.
|
|
257
|
-
# noinspection PyArgumentList
|
|
258
|
-
tools: list[ToolBase] = [x() for x in self.register_tools()]
|
|
259
|
-
if not tools:
|
|
260
|
-
raise Exception("No tools registered with this service.")
|
|
261
|
-
return tools
|
|
262
|
-
|
|
263
|
-
def _get_tool(self, name: str) -> ToolBase:
|
|
264
|
-
"""
|
|
265
|
-
Get a specific tool instance by its name.
|
|
266
|
-
|
|
267
|
-
:param name: The name of the tool to retrieve.
|
|
268
|
-
:return: The tool object corresponding to the given name.
|
|
269
|
-
"""
|
|
270
|
-
tools: dict[str, ToolBase] = {x.name: x for x in self._get_tools()}
|
|
271
|
-
if name not in tools:
|
|
272
|
-
raise Exception(f"Tool \"{name}\" not found in registered tools.")
|
|
273
|
-
return tools[name]
|
|
274
|
-
|
|
275
|
-
@abstractmethod
|
|
276
|
-
def register_tools(self) -> list[type[ToolBase]]:
|
|
277
|
-
"""
|
|
278
|
-
Register the tool types with this service. Provided tools should be subclasses of ToolBase and should not have
|
|
279
|
-
any init parameters.
|
|
280
|
-
|
|
281
|
-
:return: A list of tools to register to this service.
|
|
282
|
-
"""
|
|
283
|
-
pass
|
|
284
|
-
|
|
285
|
-
def get_details(self) -> list[ToolDetailsPbo]:
|
|
286
|
-
"""
|
|
287
|
-
Get the details of the tool.
|
|
288
|
-
|
|
289
|
-
:return: A ToolDetailsResponse object containing the tool details.
|
|
290
|
-
"""
|
|
291
|
-
tool_details: list[ToolDetailsPbo] = []
|
|
292
|
-
for tool in self._get_tools():
|
|
293
|
-
tool_details.append(tool.to_pbo())
|
|
294
|
-
return tool_details
|
|
295
|
-
|
|
296
|
-
def run(self, user: SapioUser, request: ProcessStepRequestPbo, context: ServicerContext) \
|
|
297
|
-
-> tuple[bool, list[SapioToolResult], list[str]]:
|
|
298
|
-
"""
|
|
299
|
-
Execute a tool from this service.
|
|
300
|
-
|
|
301
|
-
:param user: A user object that can be used to initialize manager classes using DataMgmtServer to query the
|
|
302
|
-
system.
|
|
303
|
-
:param request: The request object containing the input data.
|
|
304
|
-
:param context: The gRPC context.
|
|
305
|
-
:return: Whether or not the tool succeeded, the results of the tool, and any logs generated by the tool.
|
|
306
|
-
"""
|
|
307
|
-
tool = self._get_tool(request.tool_name)
|
|
308
|
-
try:
|
|
309
|
-
tool.setup(user, request, context)
|
|
310
|
-
results: list[SapioToolResult] = tool.run(user)
|
|
311
|
-
return True, results, tool.logs
|
|
312
|
-
except Exception as e:
|
|
313
|
-
tool.log_exception("Exception occurred during tool execution.", e)
|
|
314
|
-
return False, [], tool.logs
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
class ToolBase(ABC):
|
|
318
|
-
"""
|
|
319
|
-
A base class for implementing a tool.
|
|
320
|
-
"""
|
|
321
|
-
name: str
|
|
322
|
-
description: str
|
|
323
|
-
data_type_name: str | None
|
|
324
|
-
inputs: list[ToolInputDetailsPbo]
|
|
325
|
-
outputs: list[ToolOutputDetailsPbo]
|
|
326
|
-
configs: list[VeloxFieldDefPbo]
|
|
327
|
-
|
|
328
|
-
logs: list[str]
|
|
329
|
-
logger: Logger
|
|
330
|
-
verbose_logging: bool
|
|
331
|
-
|
|
332
|
-
user: SapioUser
|
|
333
|
-
request: ProcessStepRequestPbo
|
|
334
|
-
context: ServicerContext
|
|
335
|
-
|
|
336
|
-
def __init__(self, name: str, description: str, data_type_name: str | None = None):
|
|
337
|
-
"""
|
|
338
|
-
:param name: The name of the tool.
|
|
339
|
-
:param description: A description of the tool.
|
|
340
|
-
:param data_type_name: The name of the output data type of this tool, if applicable. When this tool returns
|
|
341
|
-
FieldMapResult objects in its run method, this name will be used to set the data type of the output data.
|
|
342
|
-
"""
|
|
343
|
-
self.name = name
|
|
344
|
-
self.description = description
|
|
345
|
-
self.data_type_name = data_type_name
|
|
346
|
-
self.inputs = []
|
|
347
|
-
self.outputs = []
|
|
348
|
-
self.configs = []
|
|
349
|
-
self.logs = []
|
|
350
|
-
self.logger = logging.getLogger(f"ToolBase.{self.name}")
|
|
351
|
-
ensure_logger_initialized(self.logger)
|
|
352
|
-
|
|
353
|
-
def setup(self, user: SapioUser, request: ProcessStepRequestPbo, context: ServicerContext) -> None:
|
|
354
|
-
"""
|
|
355
|
-
Setup the tool with the user, request, and context. This method can be overridden by subclasses to perform
|
|
356
|
-
additional setup.
|
|
357
|
-
|
|
358
|
-
:param user: A user object that can be used to initialize manager classes using DataMgmtServer to query the
|
|
359
|
-
system.
|
|
360
|
-
:param request: The request object containing the input data.
|
|
361
|
-
:param context: The gRPC context.
|
|
362
|
-
"""
|
|
363
|
-
self.user = user
|
|
364
|
-
self.request = request
|
|
365
|
-
self.context = context
|
|
366
|
-
# TODO: Determine verbose logging from the request.
|
|
367
|
-
self.verbose_logging = False
|
|
368
|
-
|
|
369
|
-
def add_input(self, content_type: DataTypePbo, display_name: str, description: str, example: str | None = None,
|
|
370
|
-
validation: str | None = None, input_count: tuple[int, int] | None = None, is_paged: bool = False,
|
|
371
|
-
page_size: tuple[int, int] | None = None, max_request_bytes: int | None = None) -> None:
|
|
372
|
-
"""
|
|
373
|
-
Add an input configuration to the tool. This determines how many inputs this tool will accept in the plan
|
|
374
|
-
manager, as well as what those inputs are. The IO number of the input will be set to the current number of
|
|
375
|
-
inputs. That is, the first time this is called, the IO number will be 0, the second time it is called, the IO
|
|
376
|
-
number will be 1, and so on.
|
|
377
|
-
|
|
378
|
-
:param content_type: The content type of the input.
|
|
379
|
-
:param display_name: The display name of the input.
|
|
380
|
-
:param description: The description of the input.
|
|
381
|
-
:param example: An optional example of the input.
|
|
382
|
-
:param validation: An optional validation string for the input.
|
|
383
|
-
:param input_count: A tuple of the minimum and maximum number of inputs allowed for this tool.
|
|
384
|
-
:param is_paged: If true, this input will be paged. If false, this input will not be paged.
|
|
385
|
-
:param page_size: A tuple of the minimum and maximum page size for this tool. The input must be paged in order
|
|
386
|
-
for this to have an effect.
|
|
387
|
-
:param max_request_bytes: The maximum request size in bytes for this tool.
|
|
388
|
-
"""
|
|
389
|
-
self.inputs.append(ToolInputDetailsPbo(
|
|
390
|
-
base_config=ToolIoConfigBasePbo(
|
|
391
|
-
io_number=len(self.inputs),
|
|
392
|
-
content_type=ProtobufUtils.content_type_str(content_type),
|
|
393
|
-
display_name=display_name,
|
|
394
|
-
description=description,
|
|
395
|
-
example=example
|
|
396
|
-
),
|
|
397
|
-
validation=validation,
|
|
398
|
-
min_input_count=input_count[0] if input_count else None,
|
|
399
|
-
max_input_count=input_count[1] if input_count else None,
|
|
400
|
-
paged=is_paged,
|
|
401
|
-
min_page_size=page_size[0] if page_size else None,
|
|
402
|
-
max_page_size=page_size[1] if page_size else None,
|
|
403
|
-
max_request_bytes=max_request_bytes,
|
|
404
|
-
))
|
|
405
|
-
|
|
406
|
-
def add_output(self, content_type: DataTypePbo, display_name: str, description: str, example: str | None = None) -> None:
|
|
407
|
-
"""
|
|
408
|
-
Add an output configuration to the tool. This determines how many inputs this tool will accept in the plan
|
|
409
|
-
manager, as well as what those inputs are. The IO number of the output will be set to the current number of
|
|
410
|
-
outputs. That is, the first time this is called, the IO number will be 0, the second time it is called, the IO
|
|
411
|
-
number will be 1, and so on.
|
|
412
|
-
|
|
413
|
-
:param content_type: The content type of the output.
|
|
414
|
-
:param display_name: The display name of the output.
|
|
415
|
-
:param description: The description of the output.
|
|
416
|
-
:param example: An example of the output.
|
|
417
|
-
"""
|
|
418
|
-
self.outputs.append(ToolOutputDetailsPbo(
|
|
419
|
-
base_config=ToolIoConfigBasePbo(
|
|
420
|
-
io_number=len(self.outputs),
|
|
421
|
-
content_type=ProtobufUtils.content_type_str(content_type),
|
|
422
|
-
display_name=display_name,
|
|
423
|
-
description=description,
|
|
424
|
-
example=example
|
|
425
|
-
)))
|
|
426
|
-
|
|
427
|
-
def add_config_field(self, field: VeloxFieldDefPbo) -> None:
|
|
428
|
-
"""
|
|
429
|
-
Add a configuration field to the tool. This field will be used to configure the tool in the plan manager.
|
|
430
|
-
|
|
431
|
-
:param field: The configuration field details.
|
|
432
|
-
"""
|
|
433
|
-
self.configs.append(field)
|
|
434
|
-
|
|
435
|
-
def add_config_field_def(self, field: AbstractVeloxFieldDefinition) -> None:
|
|
436
|
-
"""
|
|
437
|
-
Add a configuration field to the tool. This field will be used to configure the tool in the plan manager.
|
|
438
|
-
|
|
439
|
-
:param field: The configuration field details.
|
|
440
|
-
"""
|
|
441
|
-
self.configs.append(ProtobufUtils.field_def_to_pbo(field))
|
|
442
|
-
|
|
443
|
-
def add_boolean_config_field(self, field_name: str, display_name: str, description: str, default_value: bool,
|
|
444
|
-
optional: bool = False) -> None:
|
|
445
|
-
"""
|
|
446
|
-
Add a boolean configuration field to the tool. This field will be used to configure the tool in the plan
|
|
447
|
-
manager.
|
|
448
|
-
|
|
449
|
-
:param field_name: The name of the field.
|
|
450
|
-
:param display_name: The display name of the field.
|
|
451
|
-
:param description: The description of the field.
|
|
452
|
-
:param default_value: The default value of the field.
|
|
453
|
-
:param optional: If true, this field is optional. If false, this field is required.
|
|
454
|
-
"""
|
|
455
|
-
self.configs.append(VeloxFieldDefPbo(
|
|
456
|
-
data_field_type=FieldTypePbo.BOOLEAN,
|
|
457
|
-
data_field_name=field_name,
|
|
458
|
-
display_name=display_name,
|
|
459
|
-
description=description,
|
|
460
|
-
required=not optional,
|
|
461
|
-
editable=True,
|
|
462
|
-
boolean_properties=BooleanPropertiesPbo(
|
|
463
|
-
default_value=default_value
|
|
464
|
-
)
|
|
465
|
-
))
|
|
466
|
-
|
|
467
|
-
def add_double_config_field(self, field_name: str, display_name: str, description: str, default_value: float,
|
|
468
|
-
min_value: float = -10.**120, max_value: float = 10.**120, precision: int = 2,
|
|
469
|
-
optional: bool = False) -> None:
|
|
470
|
-
"""
|
|
471
|
-
Add a double configuration field to the tool. This field will be used to configure the tool in the plan
|
|
472
|
-
manager.
|
|
473
|
-
|
|
474
|
-
:param field_name: The name of the field.
|
|
475
|
-
:param display_name: The display name of the field.
|
|
476
|
-
:param description: The description of the field.
|
|
477
|
-
:param default_value: The default value of the field.
|
|
478
|
-
:param min_value: The minimum value of the field.
|
|
479
|
-
:param max_value: The maximum value of the field.
|
|
480
|
-
:param precision: The precision of the field.
|
|
481
|
-
:param optional: If true, this field is optional. If false, this field is required.
|
|
482
|
-
"""
|
|
483
|
-
self.configs.append(VeloxFieldDefPbo(
|
|
484
|
-
data_field_type=FieldTypePbo.DOUBLE,
|
|
485
|
-
data_field_name=field_name,
|
|
486
|
-
display_name=display_name,
|
|
487
|
-
description=description,
|
|
488
|
-
required=not optional,
|
|
489
|
-
editable=True,
|
|
490
|
-
double_properties=DoublePropertiesPbo(
|
|
491
|
-
default_value=default_value,
|
|
492
|
-
min_value=min_value,
|
|
493
|
-
max_value=max_value,
|
|
494
|
-
precision=precision
|
|
495
|
-
)
|
|
496
|
-
))
|
|
497
|
-
|
|
498
|
-
def add_integer_config_field(self, field_name: str, display_name: str, description: str,
|
|
499
|
-
default_value: int, min_value: int = -2**31, max_value: int = 2**31-1,
|
|
500
|
-
optional: bool = False) -> None:
|
|
501
|
-
"""
|
|
502
|
-
Add an integer configuration field to the tool. This field will be used to configure the tool in the plan
|
|
503
|
-
manager.
|
|
504
|
-
|
|
505
|
-
:param field_name: The name of the field.
|
|
506
|
-
:param display_name: The display name of the field.
|
|
507
|
-
:param description: The description of the field.
|
|
508
|
-
:param default_value: The default value of the field.
|
|
509
|
-
:param min_value: The minimum value of the field.
|
|
510
|
-
:param max_value: The maximum value of the field.
|
|
511
|
-
:param optional: If true, this field is optional. If false, this field is required.
|
|
512
|
-
"""
|
|
513
|
-
self.configs.append(VeloxFieldDefPbo(
|
|
514
|
-
data_field_type=FieldTypePbo.INTEGER,
|
|
515
|
-
data_field_name=field_name,
|
|
516
|
-
display_name=display_name,
|
|
517
|
-
description=description,
|
|
518
|
-
required=not optional,
|
|
519
|
-
editable=True,
|
|
520
|
-
integer_properties=IntegerPropertiesPbo(
|
|
521
|
-
default_value=default_value,
|
|
522
|
-
min_value=min_value,
|
|
523
|
-
max_value=max_value
|
|
524
|
-
)
|
|
525
|
-
))
|
|
526
|
-
|
|
527
|
-
def add_string_config_field(self, field_name: str, display_name: str, description: str,
|
|
528
|
-
default_value: str, max_length: int = 1000, optional: bool = False) -> None:
|
|
529
|
-
"""
|
|
530
|
-
Add a string configuration field to the tool. This field will be used to configure the tool in the plan
|
|
531
|
-
manager.
|
|
532
|
-
|
|
533
|
-
:param field_name: The name of the field.
|
|
534
|
-
:param display_name: The display name of the field.
|
|
535
|
-
:param description: The description of the field.
|
|
536
|
-
:param default_value: The default value of the field.
|
|
537
|
-
:param max_length: The maximum length of the field.
|
|
538
|
-
:param optional: If true, this field is optional. If false, this field is required.
|
|
539
|
-
"""
|
|
540
|
-
self.configs.append(VeloxFieldDefPbo(
|
|
541
|
-
data_field_type=FieldTypePbo.STRING,
|
|
542
|
-
data_field_name=field_name,
|
|
543
|
-
display_name=display_name,
|
|
544
|
-
description=description,
|
|
545
|
-
required=not optional,
|
|
546
|
-
editable=True,
|
|
547
|
-
string_properties=StringPropertiesPbo(
|
|
548
|
-
default_value=default_value,
|
|
549
|
-
max_length=max_length
|
|
550
|
-
)
|
|
551
|
-
))
|
|
552
|
-
|
|
553
|
-
def add_list_config_field(self, field_name: str, display_name: str, description: str, default_value: str,
|
|
554
|
-
allowed_values: list[str], direct_edit: bool = False, optional: bool = False) -> None:
|
|
555
|
-
"""
|
|
556
|
-
Add a list configuration field to the tool. This field will be used to configure the tool in the plan
|
|
557
|
-
manager.
|
|
558
|
-
|
|
559
|
-
:param field_name: The name of the field.
|
|
560
|
-
:param display_name: The display name of the field.
|
|
561
|
-
:param description: The description of the field.
|
|
562
|
-
:param default_value: The default value of the field.
|
|
563
|
-
:param allowed_values: The list of allowed values for the field.
|
|
564
|
-
:param direct_edit: If true, the user can enter a value that is not in the list of allowed values. If false,
|
|
565
|
-
the user can only select from the list of allowed values.
|
|
566
|
-
:param optional: If true, this field is optional. If false, this field is required.
|
|
567
|
-
"""
|
|
568
|
-
self.configs.append(VeloxFieldDefPbo(
|
|
569
|
-
data_field_type=FieldTypePbo.SELECTION,
|
|
570
|
-
data_field_name=field_name,
|
|
571
|
-
display_name=display_name,
|
|
572
|
-
description=description,
|
|
573
|
-
required=not optional,
|
|
574
|
-
editable=True,
|
|
575
|
-
selection_properties=SelectionPropertiesPbo(
|
|
576
|
-
default_value=default_value,
|
|
577
|
-
static_list_values=allowed_values,
|
|
578
|
-
direct_edit=direct_edit,
|
|
579
|
-
)
|
|
580
|
-
))
|
|
581
|
-
|
|
582
|
-
def add_multi_list_config_field(self, field_name: str, display_name: str, description: str,
|
|
583
|
-
default_value: list[str], allowed_values: list[str], direct_edit: bool = False,
|
|
584
|
-
optional: bool = False) -> None:
|
|
585
|
-
"""
|
|
586
|
-
Add a multi-select list configuration field to the tool. This field will be used to configure the tool in the
|
|
587
|
-
plan manager.
|
|
588
|
-
|
|
589
|
-
:param field_name: The name of the field.
|
|
590
|
-
:param display_name: The display name of the field.
|
|
591
|
-
:param description: The description of the field.
|
|
592
|
-
:param default_value: The default value of the field.
|
|
593
|
-
:param allowed_values: The list of allowed values for the field.
|
|
594
|
-
:param direct_edit: If true, the user can enter a value that is not in the list of allowed values. If false,
|
|
595
|
-
the user can only select from the list of allowed values.
|
|
596
|
-
:param optional: If true, this field is optional. If false, this field is required.
|
|
597
|
-
"""
|
|
598
|
-
self.configs.append(VeloxFieldDefPbo(
|
|
599
|
-
data_field_type=FieldTypePbo.SELECTION,
|
|
600
|
-
data_field_name=field_name,
|
|
601
|
-
display_name=display_name,
|
|
602
|
-
description=description,
|
|
603
|
-
required=not optional,
|
|
604
|
-
editable=True,
|
|
605
|
-
selection_properties=SelectionPropertiesPbo(
|
|
606
|
-
default_value=",".join(default_value),
|
|
607
|
-
static_list_values=allowed_values,
|
|
608
|
-
multi_select=True,
|
|
609
|
-
direct_edit=direct_edit,
|
|
610
|
-
)
|
|
611
|
-
))
|
|
612
|
-
|
|
613
|
-
def to_pbo(self) -> ToolDetailsPbo:
|
|
614
|
-
"""
|
|
615
|
-
:return: The ToolDetailsPbo proto object representing this tool.
|
|
616
|
-
"""
|
|
617
|
-
return ToolDetailsPbo(
|
|
618
|
-
name=self.name,
|
|
619
|
-
description=self.description,
|
|
620
|
-
input_configs=self.inputs,
|
|
621
|
-
output_configs=self.outputs,
|
|
622
|
-
output_data_type_name=self.data_type_name,
|
|
623
|
-
config_fields=self.configs
|
|
624
|
-
)
|
|
625
|
-
|
|
626
|
-
@abstractmethod
|
|
627
|
-
def run(self, user: SapioUser) -> list[SapioToolResult]:
|
|
628
|
-
"""
|
|
629
|
-
Execute this tool.
|
|
630
|
-
|
|
631
|
-
The request inputs can be accessed using the self.get_input_*() methods.
|
|
632
|
-
The request settings can be accessed using the self.get_config_fields() method.
|
|
633
|
-
The request itself can be accessed using self.request.
|
|
634
|
-
|
|
635
|
-
:param user: A user object that can be used to initialize manager classes using DataMgmtServer to query the
|
|
636
|
-
system.
|
|
637
|
-
:return: A SapioToolResults object containing the response data. Each result in the list corresponds to a
|
|
638
|
-
separate output from the tool. Field map results do not appear as tool output in the plan manager, instead
|
|
639
|
-
appearing as records related to the plan step during the run.
|
|
640
|
-
"""
|
|
641
|
-
pass
|
|
642
|
-
|
|
643
|
-
def log_info(self, message: str) -> None:
|
|
644
|
-
"""
|
|
645
|
-
Log an info message for this tool. If verbose logging is enabled, this message will be included in the logs
|
|
646
|
-
returned to the caller. Empty/None inputs will not be logged.
|
|
647
|
-
|
|
648
|
-
:param message: The message to log.
|
|
649
|
-
"""
|
|
650
|
-
if not message:
|
|
651
|
-
return
|
|
652
|
-
if self.verbose_logging:
|
|
653
|
-
self.logs.append(f"INFO: {self.name}: {message}")
|
|
654
|
-
self.logger.info(message)
|
|
655
|
-
|
|
656
|
-
def log_warning(self, message: str) -> None:
|
|
657
|
-
"""
|
|
658
|
-
Log a warning message for this tool. This message will be included in the logs returned to the caller.
|
|
659
|
-
Empty/None inputs will not be logged.
|
|
660
|
-
|
|
661
|
-
:param message: The message to log.
|
|
662
|
-
"""
|
|
663
|
-
if not message:
|
|
664
|
-
return
|
|
665
|
-
self.logs.append(f"WARNING: {self.name}: {message}")
|
|
666
|
-
self.logger.warning(message)
|
|
667
|
-
|
|
668
|
-
def log_error(self, message: str) -> None:
|
|
669
|
-
"""
|
|
670
|
-
Log an error message for this tool. This message will be included in the logs returned to the caller.
|
|
671
|
-
Empty/None inputs will not be logged.
|
|
672
|
-
|
|
673
|
-
:param message: The message to log.
|
|
674
|
-
"""
|
|
675
|
-
if not message:
|
|
676
|
-
return
|
|
677
|
-
self.logs.append(f"ERROR: {self.name}: {message}")
|
|
678
|
-
self.logger.error(message)
|
|
679
|
-
|
|
680
|
-
def log_exception(self, message: str, e: Exception) -> None:
|
|
681
|
-
"""
|
|
682
|
-
Log an exception for this tool. This message will be included in the logs returned to the caller.
|
|
683
|
-
Empty/None inputs will not be logged.
|
|
684
|
-
|
|
685
|
-
:param message: The message to log.
|
|
686
|
-
:param e: The exception to log.
|
|
687
|
-
"""
|
|
688
|
-
if not message and not e:
|
|
689
|
-
return
|
|
690
|
-
self.logs.append(f"EXCEPTION: {self.name}: {message} - {e}")
|
|
691
|
-
self.logger.error(f"{message}\n{traceback.format_exc()}")
|
|
692
|
-
|
|
693
|
-
def get_input_binary(self, index: int = 0) -> list[bytes]:
|
|
694
|
-
"""
|
|
695
|
-
Get the binary data from the request object.
|
|
696
|
-
|
|
697
|
-
:param index: The index of the input to parse. Defaults to 0. Used for tools that accept multiple inputs.
|
|
698
|
-
:return: The binary data from the request object.
|
|
699
|
-
"""
|
|
700
|
-
return list(self.request.input[index].item_container.binary_container.items)
|
|
701
|
-
|
|
702
|
-
def get_input_csv(self, index: int = 0) -> tuple[list[str], list[dict[str, str]]]:
|
|
703
|
-
"""
|
|
704
|
-
Parse the CSV data from the request object.
|
|
705
|
-
|
|
706
|
-
:param index: The index of the input to parse. Defaults to 0. Used for tools that accept multiple inputs.
|
|
707
|
-
:return: A tuple containing the header row and the data rows. The header row is a list of strings representing
|
|
708
|
-
the column names, and the data rows are a list of dictionaries where each dictionary represents a row in the
|
|
709
|
-
CSV with the column names as keys and the corresponding values as strings.
|
|
710
|
-
"""
|
|
711
|
-
input_data: Sequence[StepInputBatchPbo] = self.request.input
|
|
712
|
-
ret_val: list[dict[str, str]] = []
|
|
713
|
-
headers: Iterable[str] = input_data[index].item_container.csv_container.header.cells
|
|
714
|
-
for row in input_data[index].item_container.csv_container.items:
|
|
715
|
-
row_dict: dict[str, str] = {}
|
|
716
|
-
for header, value in zip(headers, row.cells):
|
|
717
|
-
row_dict[header] = value
|
|
718
|
-
ret_val.append(row_dict)
|
|
719
|
-
return list(headers), ret_val
|
|
720
|
-
|
|
721
|
-
def get_input_images(self, index: int = 0) -> tuple[str, list[bytes]]:
|
|
722
|
-
"""
|
|
723
|
-
Parse the image data from the request object.
|
|
724
|
-
|
|
725
|
-
:param index: The index of the input to parse. Defaults to 0. Used for tools that accept multiple inputs.
|
|
726
|
-
:return: A tuple containing the image format and the image data. The image format is a string representing the
|
|
727
|
-
format of the image (e.g., PNG, JPEG), and the image data is a list of bytes representing the image data.
|
|
728
|
-
Each entry in the list represents a separate image.
|
|
729
|
-
"""
|
|
730
|
-
image_data: StepImageContainerPbo = self.request.input[index].item_container.image_container
|
|
731
|
-
return image_data.image_format, list(image_data.items)
|
|
732
|
-
|
|
733
|
-
def get_input_json(self, index: int = 0) -> list[list[Any]] | list[dict[str, Any]]:
|
|
734
|
-
"""
|
|
735
|
-
Parse the JSON data from the request object.
|
|
736
|
-
|
|
737
|
-
:param index: The index of the input to parse. Defaults to 0. Used for tools that accept multiple inputs.
|
|
738
|
-
:return: A list of parsed JSON objects. Each entry in the list represents a separate JSON entry from the input.
|
|
739
|
-
Depending on this tool, this may be a list of dictionaries or a list of lists.
|
|
740
|
-
"""
|
|
741
|
-
return [json.loads(x) for x in self.request.input[index].item_container.json_container.items]
|
|
742
|
-
|
|
743
|
-
def get_input_text(self, index: int = 0) -> list[str]:
|
|
744
|
-
"""
|
|
745
|
-
Parse the text data from the request object.
|
|
746
|
-
|
|
747
|
-
:param index: The index of the input to parse. Defaults to 0. Used for tools that accept multiple inputs.
|
|
748
|
-
:return: A list of text data as strings.
|
|
749
|
-
"""
|
|
750
|
-
return list(self.request.input[index].item_container.text_container.items)
|
|
751
|
-
|
|
752
|
-
def get_config_defs(self) -> dict[str, VeloxFieldDefPbo]:
|
|
753
|
-
"""
|
|
754
|
-
Get the config field definitions for this tool.
|
|
755
|
-
|
|
756
|
-
:return: A dictionary of field definitions, where the keys are the field names and the values are the
|
|
757
|
-
VeloxFieldDefPbo objects representing the field definitions.
|
|
758
|
-
"""
|
|
759
|
-
field_defs: dict[str, VeloxFieldDefPbo] = {}
|
|
760
|
-
for field_def in self.to_pbo().config_fields:
|
|
761
|
-
field_defs[field_def.data_field_name] = field_def
|
|
762
|
-
return field_defs
|
|
763
|
-
|
|
764
|
-
def get_config_fields(self) -> dict[str, FieldValue]:
|
|
765
|
-
"""
|
|
766
|
-
Get the configuration field values from the request object. If a field is not present in the request,
|
|
767
|
-
the default value from the config definition will be returned.
|
|
768
|
-
|
|
769
|
-
:return: A dictionary of configuration field names and their values.
|
|
770
|
-
"""
|
|
771
|
-
config_fields: dict[str, Any] = {}
|
|
772
|
-
raw_configs: Mapping[str, FieldValuePbo] = self.request.config_field_values
|
|
773
|
-
for field_name, field_def in self.get_config_defs().items():
|
|
774
|
-
# Use the default value if the field is not present in the request.
|
|
775
|
-
if field_name not in raw_configs:
|
|
776
|
-
config_fields[field_name] = ProtobufUtils.field_def_pbo_to_default_value(field_def)
|
|
777
|
-
else:
|
|
778
|
-
config_fields[field_name] = ProtobufUtils.field_pbo_to_value(field_def, raw_configs[field_name])
|
|
779
|
-
return config_fields
|
|
780
|
-
|
|
781
|
-
@staticmethod
|
|
782
|
-
def read_from_json(json_data: list[dict[str, Any]], key: str) -> list[Any]:
|
|
783
|
-
"""
|
|
784
|
-
From a list of dictionaries, return a list of values for the given key from each dictionary. Skips null values.
|
|
785
|
-
|
|
786
|
-
:param json_data: The JSON data to read from.
|
|
787
|
-
:param key: The key to read the values from.
|
|
788
|
-
:return: A list of values corresponding to the given key in the JSON data.
|
|
789
|
-
"""
|
|
790
|
-
ret_val: list[Any] = []
|
|
791
|
-
for entry in json_data:
|
|
792
|
-
if key in entry:
|
|
793
|
-
value = entry[key]
|
|
794
|
-
if isinstance(value, list):
|
|
795
|
-
ret_val.extend(value)
|
|
796
|
-
elif value is not None:
|
|
797
|
-
ret_val.append(value)
|
|
798
|
-
return ret_val
|