68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 | class DeSerializeOperationType(OperationType[DeSerializeDetails]):
"""
An operation that takes a value, and serializes it into the format suitable to the [`serialized_value`][kiara.data_types.included_core_types.SeriailzedValue] value type.
For a module profile to be picked up by this operation type, it needs to have:
- exactly one output field of type `serialized_value`
- either one of (in this order):
- exactly one input field
- one input field where the field name equals the type name
- an input field called 'value'
"""
_operation_type_name = "deserialize"
def retrieve_included_operation_configs(
self,
) -> Iterable[Union[Mapping, OperationConfig]]:
result = []
for name, module_cls in self._kiara.module_type_classes.items():
if not hasattr(module_cls, "retrieve_serialized_value_type"):
continue
if not hasattr(module_cls, "retrieve_supported_target_profiles"):
continue
if not hasattr(module_cls, "retrieve_supported_serialization_profile"):
continue
try:
value_type = module_cls.retrieve_serialized_value_type() # type: ignore
except TypeError:
raise Exception(
f"Can't retrieve source value type for deserialization module '{module_cls.__name__}'. This is most likely a bug, maybe you are missing a '@classmethod' annotation on the 'retrieve_source_value_type' method?"
)
try:
serialization_profile = module_cls.retrieve_supported_serialization_profile() # type: ignore
except TypeError:
raise Exception(
f"Can't retrieve supported serialization profiles for deserialization module '{module_cls.__name__}'. This is most likely a bug, maybe you are missing a '@classmethod' annotation on the 'retrieve_supported_serialization_profile' method?"
)
try:
target_profiles = module_cls.retrieve_supported_target_profiles() # type: ignore
except TypeError:
raise Exception(
f"Can't retrieve supported target profile for deserialization module '{module_cls.__name__}'. This is most likely a bug, maybe you are missing a '@classmethod' annotation on the 'retrieve_supported_target_profile' method?"
)
for _profile_name, cls in target_profiles.items():
func_name = f"to__{_profile_name}"
attr = getattr(module_cls, func_name)
doc = DocumentationMetadataModel.from_function(attr)
mc = {
"value_type": value_type,
"target_profile": _profile_name,
"serialization_profile": serialization_profile
# "target_class": PythonClass.from_class(cls),
}
oc = ManifestOperationConfig(
module_type=name, module_config=mc, doc=doc
)
result.append(oc)
return result
def check_matching_operation(
self, module: "KiaraModule"
) -> Union[DeSerializeDetails, None]:
details = self.extract_details(module)
if details is None:
return None
else:
return details
def extract_details(self, module: "KiaraModule") -> Union[DeSerializeDetails, None]:
result_field_name = None
for field_name, schema in module.outputs_schema.items():
if schema.type != "python_object":
continue
else:
if result_field_name is not None:
log_message(
"ignore.operation",
reason=f"found more than one potential result value field: {result_field_name} -- {field_name}'",
module_type=module.module_type_name,
)
continue
else:
result_field_name = field_name
if not result_field_name:
return None
input_field_name = None
for field_name, schema in module.inputs_schema.items():
if field_name != schema.type:
continue
if input_field_name is not None:
log_message(
"ignore.operation",
reason=f"found more than one potential result value field: {result_field_name} -- {field_name}'",
module_type=module.module_type_name,
)
continue
else:
input_field_name = field_name
if not input_field_name:
return None
try:
value_type = module.config.get("value_type")
target_profile = module.config.get("target_profile")
serialization_profile = module.config.get("serialization_profile")
# target_class = module.config.get("target_class")
except Exception as e:
log_message(
"ignore.operation",
reason=str(e),
module_type=module.module_type_name,
)
return None
if value_type not in self._kiara.type_registry.data_type_names:
log_message(
"ignore.operation",
reason=f"Invalid value type: {value_type}",
module_type=module.module_type_name,
)
return None
if input_field_name == "any":
operation_id = "deserialize.value"
else:
operation_id = f"deserialize.{input_field_name}.as.{target_profile}"
details: Dict[str, Any] = {
"module_inputs_schema": module.inputs_schema,
"module_outputs_schema": module.outputs_schema,
"operation_id": operation_id,
"value_type": input_field_name,
"value_input_field": input_field_name,
"object_output_field": result_field_name,
"target_profile": target_profile,
"serialization_profile": serialization_profile,
# "target_class": target_class,
"is_internal_operation": True,
}
result = DeSerializeDetails.construct(**details)
return result
def find_deserialization_operations_for_type(
self, type_name: str
) -> List[Operation]:
lineage = self._kiara.type_registry.get_type_lineage(type_name)
result = []
for data_type in lineage:
match = []
for op in self.operations.values():
details = self.retrieve_operation_details(op)
if details.value_type == data_type:
match.append(op)
result.extend(match)
return result
def find_deserialzation_operation_for_type_and_profile(
self, type_name: str, serialization_profile: str
) -> List[Operation]:
lineage = self._kiara.type_registry.get_type_lineage(type_name)
serialize_ops: List[Operation] = []
for data_type in lineage:
match = []
op = None
for op in self.operations.values():
details = self.retrieve_operation_details(op)
if (
details.value_type == data_type
and details.serialization_profile == serialization_profile
):
match.append(op)
if match:
if len(match) > 1:
assert op is not None
raise Exception(
f"Multiple deserialization operations found for data type '{type_name}' and serialization profile '{serialization_profile}'. This is not supported (yet)."
)
serialize_ops.append(match[0])
return serialize_ops
|