1+ """Tools for processing xml files in GEOSX"""
2+
13from lxml import etree as ElementTree
24from lxml .etree import XMLSyntaxError
35import re
1012parameterHandler = regex_tools .DictRegexHandler ()
1113
1214
13- # Merge nodes in an included file into the current
14- # structure level by level
1515def merge_xml_nodes (existingNode , targetNode , level ):
16+ """Merge nodes in an included file into the current structure level by level.
17+
18+ @param existingNode The current node in the base xml structure.
19+ @param targetNode The node to insert.
20+ @param level The xml file depth.
21+ """
22+
1623 # Copy attributes on the current level
1724 for tk in targetNode .attrib .keys ():
1825 existingNode .set (tk , targetNode .get (tk ))
@@ -51,8 +58,15 @@ def merge_xml_nodes(existingNode, targetNode, level):
5158 existingNode .insert (- 1 , target )
5259
5360
54- # Recursively merge included files into the current structure
5561def merge_included_xml_files (root , fname , includeCount , maxInclude = 100 ):
62+ """Recursively merge included files into the current structure.
63+
64+ @param root The root node of the base xml structure.
65+ @param fname The name of the target xml file to merge.
66+ @param includeCount The current recursion depth.
67+ @param maxInclude The maximum number of xml files to include (default = 100)
68+ """
69+
5670 # Expand the input path
5771 pwd = os .getcwd ()
5872 includePath , fname = os .path .split (os .path .abspath (os .path .expanduser (fname )))
@@ -88,9 +102,13 @@ def merge_included_xml_files(root, fname, includeCount, maxInclude=100):
88102 os .chdir (pwd )
89103
90104
91- # Apply regexes that handle parameters, units, and symbolic math to
92- # each xml attribute in the structure
93105def apply_regex_to_node (node ):
106+ """Apply regexes that handle parameters, units, and symbolic math to each
107+ xml attribute in the structure.
108+
109+ @param node The target node in the xml structure.
110+ """
111+
94112 for k in node .attrib .keys ():
95113 value = node .get (k )
96114
@@ -126,8 +144,12 @@ def apply_regex_to_node(node):
126144 apply_regex_to_node (subNode )
127145
128146
129- # If the target name is not specified, generate a random name for the compiled xml
130147def generate_random_name (prefix = '' , suffix = '.xml' ):
148+ """If the target name is not specified, generate a random name for the compiled xml
149+
150+ @param prefix The file prefix (default = '').
151+ @param suffix The file suffix (default = '.xml')
152+ """
131153 from hashlib import md5
132154 from time import time
133155 from os import getpid
@@ -136,8 +158,16 @@ def generate_random_name(prefix='', suffix='.xml'):
136158 return '%s%s%s' % (prefix , md5 (tmp .encode ('utf-8' )).hexdigest (), suffix )
137159
138160
139- # Process an xml file
140161def process (inputFile , outputFile = '' , schema = '' , verbose = 0 , keep_parameters = True , keep_includes = True ):
162+ """Process an xml file
163+
164+ @param inputFile Input file name.
165+ @param outputFile Output file name (if not specified, then generate randomly).
166+ @param schema Schema file name to validate the final xml (if not specified, then do not validate).
167+ @param verbose Verbosity level.
168+ @param keep_parameters If True, then keep parameters in the compiled file (default = True)
169+ @param keep_includes If True, then keep includes in the compiled file (default = True)
170+ """
141171 if verbose :
142172 print ('\n Reading input xml parameters and parsing symbolic math...' )
143173
@@ -206,8 +236,13 @@ def process(inputFile, outputFile='', schema='', verbose=0, keep_parameters=True
206236 return outputFile
207237
208238
209- # Validate an xml file, and parse the warnings
210239def validate_xml (fname , schema , verbose ):
240+ """Validate an xml file, and parse the warnings.
241+
242+ @param fname Target xml file name.
243+ @param schema Schema file name.
244+ @param verbose Verbosity level.
245+ """
211246 if verbose :
212247 print ('Validating the xml against the schema...' )
213248 try :
0 commit comments