@@ -109,12 +109,13 @@ def generateRandomName(prefix='', suffix='.xml'):
109109 from time import time
110110 from os import getpid
111111
112- return '%s%s%s' % (prefix , md5 (str (time ())+ str (getpid ())).hexdigest (), suffix )
112+ tmp = str (time ()) + str (getpid ())
113+ return '%s%s%s' % (prefix , md5 (tmp .encode ('utf-8' )).hexdigest (), suffix )
113114
114115
115- def preprocessGEOSXML (inputFile , schema = '/g/g17/sherman/GEOS/geosx/src/components/core/src/ schema/gpac_new.xsd' , verbose = 1 ):
116+ def preprocessGEOSXML (inputFile , outputFile = '' , schema = '' , verbose = 0 ):
116117
117- if ( verbose > 0 ) :
118+ if verbose :
118119 print ('\n Reading input xml parameters and parsing symbolic math...' )
119120
120121 # Expand the input path
@@ -146,31 +147,38 @@ def preprocessGEOSXML(inputFile, schema='/g/g17/sherman/GEOS/geosx/src/component
146147 Pmap [p .get ('name' )] = p .get ('value' )
147148 parameterHandler .target = Pmap
148149
149- # Apply regexes and write the new file
150+ # Process the xml
150151 applyRegexToNode (root )
151- recordName = generateRandomName (prefix = 'prep_' )
152- tree .write (recordName , pretty_print = True )
152+
153+ # Generate a random output name if not specified
154+ if not outputFile :
155+ outputFile = generateRandomName (prefix = 'prep_' )
156+
157+ # Write the output file
158+ tree .write (outputFile , pretty_print = True )
153159
154160 # Check for un-matched special characters
155- with open (recordName , 'r' ) as ofile :
161+ with open (outputFile , 'r' ) as ofile :
156162 for line in ofile :
157163 if any ([sc in line for sc in ['$' , '[' , ']' , '{' , '}' ]]):
158164 raise Exception ('Found un-matched special characters in the pre-processed input file on line:\n %s\n Check your input xml for errors!' % (line ))
159165
160- if (verbose > 0 ):
161- print ('Preprocessed xml file stored in %s' % (recordName ))
162- validateXML (recordName , schema )
166+ if verbose :
167+ print ('Preprocessed xml file stored in %s' % (outputFile ))
168+
169+ if schema :
170+ validateXML (outputFile , schema , verbose )
163171
164- return recordName
172+ return outputFile
165173
166174
167- def validateXML (fname , schema ):
168- print ('Validating the xml against the schema...' )
175+ def validateXML (fname , schema , verbose ):
176+ if verbose :
177+ print ('Validating the xml against the schema...' )
169178 try :
170179 ofile = ElementTree .parse (fname )
171180 sfile = ElementTree .XMLSchema (ElementTree .parse (os .path .expanduser (schema )))
172181 sfile .assertValid (ofile )
173- print ('Done!' )
174182 except ElementTree .DocumentInvalid as err :
175183 print (err )
176184 print ('\n Warning: input XML contains potentially invalid input parameters:' )
@@ -179,4 +187,6 @@ def validateXML(fname, schema):
179187 print ('\n ' + '-' * 20 )
180188 print ('(Total schema warnings: %i)\n ' % (len (sfile .error_log )))
181189
190+ if verbose :
191+ print ('Done!' )
182192
0 commit comments