Skip to content

Commit 4f9df96

Browse files
csshermanjoshua-white
authored andcommitted
Improving the virtual python environment script, updating the pygeos package (#396)
1 parent 62280a4 commit 4f9df96

File tree

4 files changed

+77
-27
lines changed

4 files changed

+77
-27
lines changed

pygeos_package/pygeos/__main__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import argparse
3+
from pygeos import preprocessGEOSXML
4+
5+
6+
def main():
7+
# Parse the user arguments
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('input', type=str, help='Input file name')
10+
parser.add_argument('-o', '--output', type=str, help='Output file name (otherwise, it is randomly genrated)', default='')
11+
parser.add_argument('-s', '--schema', type=str, help='GEOSX schema to use for validation', default='')
12+
parser.add_argument('-v', '--verbose', type=int, help='Verbosity of outputs', default=0)
13+
args = parser.parse_args()
14+
15+
# Process the xml file
16+
output_name = preprocessGEOSXML(args.input,
17+
outputFile=args.output,
18+
schema=args.schema,
19+
verbose=args.verbose)
20+
21+
print(output_name)
22+
23+
24+
if __name__ == "__main__":
25+
main()
26+
27+

pygeos_package/pygeos/example/compile_xml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
#!/usr/gapps/GEOS/python/anaconda2/bin/python
2-
import os
3-
import sys
4-
import pygeos
51

6-
# Note: This script will compile an input xml file into one that can be run by GEOS.
7-
# Usage: compile_xml input.xml output.xml
2+
import argparse
3+
from pygeos import preprocessGEOSXML
4+
5+
6+
def main():
7+
# Parse the user arguments
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('input', type=str, help='Input file name')
10+
parser.add_argument('-o', '--output', type=str, help='Output file name (otherwise, it is randomly genrated)', default='')
11+
parser.add_argument('-s', '--schema', type=str, help='GEOSX schema to use for validation', default='')
12+
parser.add_argument('-v', '--verbose', type=int, help='Verbosity of outputs', default=0)
13+
args = parser.parse_args()
14+
15+
# Process the xml file
16+
output_name = preprocessGEOSXML(args.input,
17+
outputFile=args.output,
18+
schema=args.schema,
19+
verbose=args.verbose)
20+
21+
print(output_name)
22+
23+
24+
if __name__ == "__main__":
25+
main()
826

9-
if (len(sys.argv) == 3):
10-
newFile = pygeos.preprocessGEOSXML(sys.argv[1], schema='/usr/gapps/GEOS/schema/gpac_new.xsd')
11-
pygeos.format_xml_file(newFile, sys.argv[2])
12-
os.system('rm %s' % (newFile))
1327

14-
else:
15-
print('Usage: preprocess_xml inputFile outputFile')

pygeos_package/pygeos/xml_processor.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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('\nReading 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('\nWarning: 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

pygeos_package/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
description='GEOS front-end preprocessing package',
66
author='Chris Sherman',
77
author_email='[email protected]',
8-
packages=['pygeos', 'pygeos.tests'])
8+
packages=['pygeos', 'pygeos.tests'],
9+
entry_points={'console_scripts': ['pygeos = pygeos.__main__:main']})
910

0 commit comments

Comments
 (0)