1- from lxml import etree as ElementTree
1+ from lxml import etree as ElementTree # type: ignore[import]
22import os
33from pathlib import Path
4- import argparse
4+ from typing import Any , Iterable , Dict
5+ from geosx_xml_tools import command_line_parsers
56
7+ record_type = Dict [str , Dict [str , Any ]]
68
7- def parse_schema_element (root ,
8- node ,
9- xsd = '{http://www.w3.org/2001/XMLSchema}' ,
10- recursive_types = ['PeriodicEvent' , 'SoloEvent' , 'HaltEvent' ],
11- folders = ['src' , 'examples' ]):
9+
10+ def parse_schema_element (root : ElementTree .Element ,
11+ node : ElementTree .Element ,
12+ xsd : str = '{http://www.w3.org/2001/XMLSchema}' ,
13+ recursive_types : Iterable [str ] = ['PeriodicEvent' , 'SoloEvent' , 'HaltEvent' ],
14+ folders : Iterable [str ] = ['src' , 'examples' ]) -> record_type :
1215 """Parse the xml schema at the current level
1316
14- @arg root the root schema node
15- @arg node current schema node
16- @arg xsd the file namespace
17- @arg recursive_types node tags that allow recursive nesting
18- @arg folders folders to sort xml attribute usage into
17+ Args:
18+ root (lxml.etree.Element): the root schema node
19+ node (lxml.etree.Element): current schema node
20+ xsd (str): the file namespace
21+ recursive_types (list): node tags that allow recursive nesting
22+ folders (list): folders to sort xml attribute usage into
23+
24+ Returns:
25+ dict: Dictionary of attributes and children for the current node
1926 """
2027
2128 element_type = node .get ('type' )
2229 element_name = node .get ('name' )
2330 element_def = root .find ("%scomplexType[@name='%s']" % (xsd , element_type ))
24- local_types = {'attributes' : {}, 'children' : {}}
31+ local_types : record_type = {'attributes' : {}, 'children' : {}}
2532
2633 # Parse attributes
2734 for attribute in element_def .findall ('%sattribute' % (xsd )):
@@ -41,23 +48,28 @@ def parse_schema_element(root,
4148 return local_types
4249
4350
44- def parse_schema (fname ) :
51+ def parse_schema (fname : str ) -> record_type :
4552 """Parse the schema file into the xml attribute usage dict
4653
47- @arg fname schema name
54+ Args:
55+ fname (str): schema name
56+
57+ Returns:
58+ dict: Dictionary of attributes and children for the entire schema
4859 """
4960 xml_tree = ElementTree .parse (fname )
5061 xml_root = xml_tree .getroot ()
5162 problem_node = xml_root .find ("{http://www.w3.org/2001/XMLSchema}element" )
5263 return {'Problem' : parse_schema_element (xml_root , problem_node )}
5364
5465
55- def collect_xml_attributes_level (local_types , node , folder ) :
66+ def collect_xml_attributes_level (local_types : record_type , node : ElementTree . Element , folder : str ) -> None :
5667 """Collect xml attribute usage at the current level
5768
58- @arg local_types dict containing attribute usage
59- @arg node current xml node
60- @arg folder the source folder for the current file
69+ Args:
70+ local_types (dict): dictionary containing attribute usage
71+ node (lxml.etree.Element): current xml node
72+ folder (str): the source folder for the current file
6173 """
6274 for ka in node .attrib .keys ():
6375 local_types ['attributes' ][ka ][folder ].append (node .get (ka ))
@@ -67,12 +79,13 @@ def collect_xml_attributes_level(local_types, node, folder):
6779 collect_xml_attributes_level (local_types ['children' ][child .tag ], child , folder )
6880
6981
70- def collect_xml_attributes (xml_types , fname , folder ) :
82+ def collect_xml_attributes (xml_types : record_type , fname : str , folder : str ) -> None :
7183 """Collect xml attribute usage in a file
7284
73- @arg xml_types dict containing attribute usage
74- @arg fname name of the target file
75- @arg folder the source folder for the current file
85+ Args:
86+ xml_types (dict): dictionary containing attribute usage
87+ fname (str): name of the target file
88+ folder (str): the source folder for the current file
7689 """
7790 parser = ElementTree .XMLParser (remove_comments = True , remove_blank_text = True )
7891 xml_tree = ElementTree .parse (fname , parser = parser )
@@ -81,11 +94,14 @@ def collect_xml_attributes(xml_types, fname, folder):
8194 collect_xml_attributes_level (xml_types ['Problem' ], xml_root , folder )
8295
8396
84- def write_attribute_usage_xml_level (local_types , node , folders = ['src' , 'examples' ]):
97+ def write_attribute_usage_xml_level (local_types : record_type ,
98+ node : ElementTree .Element ,
99+ folders : Iterable [str ] = ['src' , 'examples' ]) -> None :
85100 """Write xml attribute usage file at a given level
86101
87- @arg local_types dict containing attribute usage at the current level
88- @arg node current xml node
102+ Args:
103+ local_types (dict): dict containing attribute usage at the current level
104+ node (lxml.etree.Element): current xml node
89105 """
90106
91107 # Write attributes
@@ -112,11 +128,12 @@ def write_attribute_usage_xml_level(local_types, node, folders=['src', 'examples
112128 write_attribute_usage_xml_level (local_types ['children' ][ka ], child )
113129
114130
115- def write_attribute_usage_xml (xml_types , fname ) :
131+ def write_attribute_usage_xml (xml_types : record_type , fname : str ) -> None :
116132 """Write xml attribute usage file
117133
118- @arg xml_types dict containing attribute usage by xml type
119- @arg fname output file name
134+ Args:
135+ xml_types (dict): dictionary containing attribute usage by xml type
136+ fname (str): output file name
120137 """
121138 xml_root = ElementTree .Element ('Problem' )
122139 xml_tree = ElementTree .ElementTree (xml_root )
@@ -125,11 +142,12 @@ def write_attribute_usage_xml(xml_types, fname):
125142 xml_tree .write (fname , pretty_print = True )
126143
127144
128- def process_xml_files (geosx_root , output_name ) :
145+ def process_xml_files (geosx_root : str , output_name : str ) -> None :
129146 """Test for xml attribute usage
130147
131- @arg geosx_root GEOSX root directory
132- @arg output_name output file name
148+ Args:
149+ geosx_root (str): GEOSX root directory
150+ output_name (str): output file name
133151 """
134152
135153 # Parse the schema
@@ -149,17 +167,16 @@ def process_xml_files(geosx_root, output_name):
149167 write_attribute_usage_xml (xml_types , output_name )
150168
151169
152- def main ():
170+ def main () -> None :
153171 """Entry point for the xml attribute usage test script
154172
155- @arg -r/--root GEOSX root directory
156- @arg -o/--output output file name
173+ Args:
174+ -r/--root (str): GEOSX root directory
175+ -o/--output (str): output file name
157176 """
158177
159178 # Parse the user arguments
160- parser = argparse .ArgumentParser ()
161- parser .add_argument ('-r' , '--root' , type = str , help = 'GEOSX root' , default = '' )
162- parser .add_argument ('-o' , '--output' , type = str , help = 'Output file name' , default = 'attribute_test.xml' )
179+ parser = command_line_parsers .build_attribute_coverage_input_parser ()
163180 args = parser .parse_args ()
164181
165182 # Parse the xml files
0 commit comments