@@ -97,6 +97,24 @@ def find_class_in_module(module_name, class_name):
97
97
return None
98
98
99
99
100
+ def search_class_in_module_from_partial_name (module_name : str , class_partial_name : str ) -> Optional [List [type ]]:
101
+ """
102
+ Search a class in a module using a partial name.
103
+ :param module_name: The name of the module to search in.
104
+ :param class_partial_name: The partial name of the class to search for.
105
+ :return: A list of classes that match the partial name.
106
+
107
+ """
108
+ try :
109
+ module = import_module (module_name )
110
+ classes = get_module_classes_from_name (module_name )
111
+ matching_classes = [cls for cls_name , cls in classes if class_partial_name .lower () in cls_name .lower ()]
112
+ return matching_classes
113
+ except ImportError as e :
114
+ logging .error (f"Module '{ module_name } ' not found: { e } " )
115
+ return None
116
+
117
+
100
118
def get_class_methods (cls : Union [type , Any ]) -> List [str ]:
101
119
"""
102
120
Returns the list of the methods names for a specific class.
@@ -196,44 +214,32 @@ def get_energyml_class_in_related_dev_pkg(cls: type):
196
214
return res
197
215
198
216
199
- def get_class_from_qualified_type (qualified_type : str ) -> Optional [type ]:
200
- return get_class_from_content_type (qualified_type )
201
-
202
-
203
- def get_class_from_content_type (content_type : str ) -> Optional [type ]:
217
+ def get_module_name_and_type_from_content_or_qualified_type (cqt : str ) -> Tuple [str , str ]:
204
218
"""
205
- Return a :class:`type` object matching with the content-type :param:`content_type`.
206
- :param content_type:
207
- :return:
219
+ Return a tuple (module_name, type) from a content-type or qualified-type string.
208
220
"""
209
221
ct = None
210
222
try :
211
- ct = parse_content_type (content_type )
223
+ ct = parse_content_type (cqt )
212
224
except AttributeError :
213
225
pass
214
226
if ct is None :
215
227
try :
216
- ct = parse_qualified_type (content_type )
228
+ ct = parse_qualified_type (cqt )
217
229
except AttributeError :
218
230
pass
219
231
220
232
domain = ct .group ("domain" )
221
233
if domain is None :
222
234
# logging.debug(f"\tdomain {domain} xmlDomain {ct.group('xmlDomain')} ")
223
235
domain = "opc"
236
+
224
237
if domain == "opc" :
225
238
xml_domain = ct .group ("xmlDomain" )
226
239
if "." in xml_domain :
227
240
xml_domain = xml_domain [xml_domain .rindex ("." ) + 1 :]
228
- # Don't know what to do with http://schemas.f2i-consulting.com/package/2014/metadata/extended-core-properties
229
- # if "extended" in xml_domain:
230
- # xml_domain = xml_domain.replace("extended", "")
231
- # if xml_domain.startswith("-"):
232
- # xml_domain = xml_domain[1:]
233
- #
234
241
opc_type = pascal_case (xml_domain ).replace ("-" , "" )
235
- # logging.debug("\tenergyml.opc.opc." + opc_type)
236
- return get_class_from_name ("energyml.opc.opc." + opc_type )
242
+ return ("energyml.opc.opc" , opc_type )
237
243
else :
238
244
domain = ct .group ("domain" )
239
245
obj_type = ct .group ("type" )
@@ -246,10 +252,21 @@ def get_class_from_content_type(content_type: str) -> Optional[type]:
246
252
if domain .lower () == "resqml" and version_num .startswith ("2_0" ):
247
253
version_num = "2_0_1"
248
254
249
- # logging.debug(get_module_name(domain, version_num)
250
- # + "."
251
- # + obj_type)
252
- return get_class_from_name (get_module_name (domain , version_num ) + "." + obj_type )
255
+ return (get_module_name (domain , version_num ), obj_type )
256
+
257
+
258
+ def get_class_from_qualified_type (qualified_type : str ) -> Optional [type ]:
259
+ return get_class_from_content_type (qualified_type )
260
+
261
+
262
+ def get_class_from_content_type (content_type : str ) -> Optional [type ]:
263
+ """
264
+ Return a :class:`type` object matching with the content-type :param:`content_type`.
265
+ :param content_type:
266
+ :return:
267
+ """
268
+ module_name , object_type = get_module_name_and_type_from_content_or_qualified_type (content_type )
269
+ return get_class_from_name (module_name + "." + object_type )
253
270
254
271
255
272
def get_module_name (domain : str , domain_version : str ):
@@ -1078,7 +1095,7 @@ def get_obj_pkg_pkgv_type_uuid_version(
1078
1095
1079
1096
if ct is not None :
1080
1097
ct_match = parse_content_type (ct )
1081
- logging .debug ("ct : " , ct_match )
1098
+ logging .debug ("ct : %S " , ct_match )
1082
1099
if ct_match is not None :
1083
1100
pkg = ct_match .group ("domain" )
1084
1101
pkg_v = ct_match .group ("domainVersion" )
@@ -1087,7 +1104,7 @@ def get_obj_pkg_pkgv_type_uuid_version(
1087
1104
try :
1088
1105
qt = get_object_attribute_no_verif (obj , "qualified_type" )
1089
1106
qt_match = parse_qualified_type (qt )
1090
- logging .debug ("qt : " , qt , obj .__dict__ , qt_match )
1107
+ logging .debug ("qt : %s %s " , qt , obj .__dict__ , qt_match )
1091
1108
if qt_match is not None :
1092
1109
pkg = qt_match .group ("domain" )
1093
1110
pkg_v = qt_match .group ("domainVersion" )
@@ -1392,11 +1409,14 @@ def random_value_from_class(cls: type):
1392
1409
if not is_primitive (cls ):
1393
1410
# import_related_module(cls.__module__)
1394
1411
energyml_module_context = get_related_energyml_modules_name (cls )
1395
- return _random_value_from_class (
1396
- cls = cls ,
1397
- energyml_module_context = energyml_module_context ,
1398
- attribute_name = None ,
1399
- )
1412
+ if cls is not None :
1413
+ return _random_value_from_class (
1414
+ cls = cls ,
1415
+ energyml_module_context = energyml_module_context ,
1416
+ attribute_name = None ,
1417
+ )
1418
+ else :
1419
+ return None
1400
1420
1401
1421
1402
1422
def _random_value_from_class (
0 commit comments