1313import lxml .etree as ET
1414from lxml .etree import ParseError as xmlER
1515
16+ from . import xml_utils
17+
1618
1719def decode_or_not (elem , encoding : str = "utf-8" , decode : bool = True ):
1820 """
@@ -52,10 +54,9 @@ def decode_or_not(elem, encoding: str = "utf-8", decode: bool = True):
5254 return elem
5355
5456
55- def remove_namespace_from_tag (tag ):
56- """Helper function to remove the namespace from an XML tag."""
57-
58- return tag .split ("}" )[- 1 ]
57+ def get_nxdl_element_type (element ):
58+ type = xml_utils .get_local_name (element )
59+ return "field" if type == "link" else type
5960
6061
6162class NxdlAttributeNotFoundError (Exception ):
@@ -87,20 +88,13 @@ def get_app_defs_names():
8788
8889 files = sorted (glob (str (app_def_path_glob )))
8990 for nexus_file in sorted (glob (str (contrib_def_path_glob ))):
90- root = get_xml_root (nexus_file )
91+ root = xml_utils . read_xml_file (nexus_file )
9192 if root .attrib ["category" ] == "application" :
9293 files .append (nexus_file )
9394
9495 return [Path (file ).name [:- 9 ] for file in files ] + ["NXroot" ]
9596
9697
97- @lru_cache (maxsize = None )
98- def get_xml_root (file_path ):
99- """Reducing I/O time by caching technique"""
100-
101- return ET .parse (file_path ).getroot ()
102-
103-
10498def get_hdf_root (hdf_node ):
10599 """Get the root HDF5 node"""
106100 node = hdf_node
@@ -243,7 +237,7 @@ def get_nx_classes():
243237 nx_class = []
244238 for nexus_file in base_classes + applications + contributed :
245239 try :
246- root = get_xml_root (nexus_file )
240+ root = xml_utils . read_xml_file (nexus_file )
247241 except xmlER as e :
248242 raise ValueError (f"Getting an issue while parsing file { nexus_file } " ) from e
249243 if root .attrib ["category" ] == "base" :
@@ -254,7 +248,7 @@ def get_nx_classes():
254248def get_nx_units ():
255249 """Read unit kinds from the NeXus definition/nxdlTypes.xsd file"""
256250 filepath = nexus_def_path / "nxdlTypes.xsd"
257- root = get_xml_root (filepath )
251+ root = xml_utils . read_xml_file (filepath )
258252 units_and_type_list = []
259253 for child in root :
260254 units_and_type_list .extend (child .attrib .values ())
@@ -275,7 +269,7 @@ def get_nx_attribute_type():
275269 """Read attribute types from the NeXus definition/nxdlTypes.xsd file"""
276270 filepath = nexus_def_path / "nxdlTypes.xsd"
277271
278- root = get_xml_root (filepath )
272+ root = xml_utils . read_xml_file (filepath )
279273 units_and_type_list = []
280274 for child in root :
281275 units_and_type_list .extend (child .attrib .values ())
@@ -321,7 +315,7 @@ def is_name_type(child, name_type_value: str) -> bool:
321315 return True
322316
323317 if name_type_value == "any" and (
324- get_local_name_from_xml (child ) == "group"
318+ get_nxdl_element_type (child ) == "group"
325319 and "nameType" not in child .attrib
326320 and "name" not in child .attrib
327321 ):
@@ -355,7 +349,7 @@ def belongs_to(nxdl_elem, child, name, class_type=None, hdf_name=None):
355349 if not isinstance (child2 .tag , str ):
356350 continue
357351 if (
358- get_local_name_from_xml (child ) != get_local_name_from_xml (child2 )
352+ get_nxdl_element_type (child ) != get_nxdl_element_type (child2 )
359353 or get_node_name (child2 ) == act_htmlname
360354 ):
361355 continue
@@ -376,15 +370,9 @@ def belongs_to(nxdl_elem, child, name, class_type=None, hdf_name=None):
376370 return False
377371
378372
379- def get_local_name_from_xml (element ):
380- """Helper function to extract the element tag without the namespace."""
381- type = remove_namespace_from_tag (element .tag )
382- return "field" if type == "link" else type
383-
384-
385373def get_own_nxdl_child_reserved_elements (child , name , nxdl_elem ):
386374 """checking reserved elements, like doc, enumeration"""
387- local_name = get_local_name_from_xml (child )
375+ local_name = get_nxdl_element_type (child )
388376 if local_name == "doc" and name == "doc" :
389377 return set_nxdlpath (child , nxdl_elem , tag_name = name )
390378
@@ -395,16 +383,16 @@ def get_own_nxdl_child_reserved_elements(child, name, nxdl_elem):
395383
396384def get_own_nxdl_child_base_types (child , class_type , nxdl_elem , name , hdf_name ):
397385 """checking base types of group, field, attribute"""
398- if get_local_name_from_xml (child ) == "group" :
386+ if get_nxdl_element_type (child ) == "group" :
399387 if (
400388 class_type is None or (class_type and get_nx_class (child ) == class_type )
401389 ) and belongs_to (nxdl_elem , child , name , class_type , hdf_name ):
402390 return set_nxdlpath (child , nxdl_elem )
403- if get_local_name_from_xml (child ) == "field" and belongs_to (
391+ if get_nxdl_element_type (child ) == "field" and belongs_to (
404392 nxdl_elem , child , name , None , hdf_name
405393 ):
406394 return set_nxdlpath (child , nxdl_elem )
407- if get_local_name_from_xml (child ) == "attribute" and belongs_to (
395+ if get_nxdl_element_type (child ) == "attribute" and belongs_to (
408396 nxdl_elem , child , name , None , hdf_name
409397 ):
410398 return set_nxdlpath (child , nxdl_elem )
@@ -424,7 +412,7 @@ def get_own_nxdl_child(
424412 result = get_own_nxdl_child_reserved_elements (child , name , nxdl_elem )
425413 if result is not False :
426414 return result
427- if nexus_type and get_local_name_from_xml (child ) != nexus_type :
415+ if nexus_type and get_nxdl_element_type (child ) != nexus_type :
428416 continue
429417 result = get_own_nxdl_child_base_types (
430418 child , class_type , nxdl_elem , name , hdf_name
@@ -470,7 +458,7 @@ def get_nxdl_child(
470458 bc_filename = find_definition_file (bc_name )
471459 if not bc_filename :
472460 raise ValueError ("nxdl file not found in definitions folder!" )
473- bc_obj = ET . parse (bc_filename ). getroot ( )
461+ bc_obj = xml_utils . read_xml_file (bc_filename )
474462 bc_obj .set ("nxdlbase" , bc_filename )
475463 if "category" in bc_obj .attrib :
476464 bc_obj .set ("nxdlbase_class" , bc_obj .attrib ["category" ])
@@ -692,11 +680,6 @@ def print_doc(node, ntype, level, nxhtml, nxpath):
692680 print (wrapper .fill (par ))
693681
694682
695- def get_namespace (element ):
696- """Extracts the namespace for elements in the NXDL"""
697- return element .tag [element .tag .index ("{" ) : element .tag .rindex ("}" ) + 1 ]
698-
699-
700683def get_enums (node : ET ._Element ) -> Optional [List [str ]]:
701684 """
702685 Makes list of enumerations, if node contains any.
@@ -709,7 +692,7 @@ def get_enums(node: ET._Element) -> Optional[List[str]]:
709692 Returns a list of the enumeration values if an enumeration was found.
710693 If no enumeration was found it returns None.
711694 """
712- namespace = get_namespace (node )
695+ namespace = xml_utils . get_namespace (node )
713696 enums = []
714697 for enumeration in node .findall (f"{ namespace } enumeration" ):
715698 for item in enumeration .findall (f"{ namespace } item" ):
@@ -736,12 +719,7 @@ def add_base_classes(elist, nx_name=None, elem: ET.Element = None):
736719 if nxdl_file_path is None :
737720 nxdl_file_path = f"{ nx_name } .nxdl.xml"
738721
739- try :
740- elem = ET .parse (os .path .abspath (nxdl_file_path )).getroot ()
741- # elem = ET.parse(nxdl_file_path).getroot()
742- except OSError :
743- with open (nxdl_file_path , "r" ) as f :
744- elem = ET .parse (f ).getroot ()
722+ elem = xml_utils .read_xml_file (nxdl_file_path )
745723
746724 if not isinstance (nxdl_file_path , str ):
747725 nxdl_file_path = str (nxdl_file_path )
@@ -781,7 +759,7 @@ def get_direct_child(nxdl_elem, html_name):
781759 for child in nxdl_elem :
782760 if not isinstance (child .tag , str ):
783761 continue
784- if get_local_name_from_xml (child ) in (
762+ if get_nxdl_element_type (child ) in (
785763 "group" ,
786764 "field" ,
787765 "attribute" ,
@@ -798,7 +776,7 @@ def get_field_child(nxdl_elem, html_name):
798776 for child in nxdl_elem :
799777 if not isinstance (child .tag , str ):
800778 continue
801- if get_local_name_from_xml (child ) != "field" :
779+ if get_nxdl_element_type (child ) != "field" :
802780 continue
803781 if get_node_name (child ) == html_name :
804782 data_child = set_nxdlpath (child , nxdl_elem )
@@ -853,7 +831,7 @@ def get_best_child(nxdl_elem, hdf_node, hdf_name, hdf_class_name, nexus_type):
853831 if not isinstance (child .tag , str ):
854832 continue
855833 fit = - 2
856- if get_local_name_from_xml (child ) == nexus_type and (
834+ if get_nxdl_element_type (child ) == nexus_type and (
857835 nexus_type != "group" or get_nx_class (child ) == hdf_class_name
858836 ):
859837 name_any = is_name_type (child , "any" )
@@ -885,7 +863,7 @@ def walk_elist(elist, html_name):
885863 None ,
886864 html_name ,
887865 get_nx_class (main_child ),
888- get_local_name_from_xml (main_child ),
866+ get_nxdl_element_type (main_child ),
889867 )
890868 if fitting_child is not None :
891869 child = fitting_child
@@ -973,7 +951,7 @@ def get_rst_formatted_name(node):
973951 name = node .get ("name" , "" )
974952 nameType = node .get ("nameType" , "" )
975953
976- node_type = get_local_name_from_xml (node )
954+ node_type = get_nxdl_element_type (node )
977955
978956 if not name and node_type == "group" :
979957 # Derive the name from the type without the NX prefix
0 commit comments