Skip to content

Commit e3653ee

Browse files
author
Francois Hamon
committed
merged with develop
1 parent ca9b120 commit e3653ee

25 files changed

+795
-3027
lines changed

pygeos_package/pygeos/__init__.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
2-
__version__ = '0.3.1'
3-
4-
5-
from .regex_config import regexConfig, symbolicMathRegexHandler, parameterHandler, DictRegexHandler
6-
from .unit_manager import unitManager
7-
from .xml_processor import preprocessGEOSXML, validateXML
8-
from .table_generator import writeGEOSTable, readGEOSTable
9-
from .test_manager import runUnitTests
10-
from .format_xml import format_xml_file
11-
1+
"""@package pygeos
2+
A python module that enables advanced xml features for GEOSX.
3+
"""

pygeos_package/pygeos/__main__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
"""Command line tools for pygeos"""
12

23
import argparse
3-
from pygeos import preprocessGEOSXML
4+
from pygeos import xml_processor
45

56

67
def main():
8+
"""Entry point for the pygeos console script
9+
10+
@arg input Input file name
11+
@arg -o/--output Output filename (default = randomly generated string)
12+
@arg -s/--schema GEOSX schema to use for validating the generated xml
13+
@arg -v/--verbose Verbosity level
14+
"""
15+
716
# Parse the user arguments
817
parser = argparse.ArgumentParser()
918
parser.add_argument('input', type=str, help='Input file name')
@@ -13,10 +22,10 @@ def main():
1322
args = parser.parse_args()
1423

1524
# Process the xml file
16-
output_name = preprocessGEOSXML(args.input,
17-
outputFile=args.output,
18-
schema=args.schema,
19-
verbose=args.verbose)
25+
output_name = xml_processor.process(args.input,
26+
outputFile=args.output,
27+
schema=args.schema,
28+
verbose=args.verbose)
2029

2130
print(output_name)
2231

pygeos_package/pygeos/format_xml.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

pygeos_package/pygeos/pygeos_documentation.rst

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ It allows users to include child files, parameters, units, and symbolic math.
88

99
Setup
1010
=================================
11-
The pygeos module is located in the GEOSX repository: `src/coreComponents/python/modules/pygeos_package` .
11+
The pygeos module is located in the GEOSX repository: `src/coreComponents/python/modules/pygeos_package`.
1212
The module is compabitable with python 2/3, and depends on the `lxml`, `numpy`, and `re` packages.
1313
It can be installed into an existing python distribution via pip:
1414

@@ -19,18 +19,18 @@ Virtual Python Environment
1919
---------------------------------
2020

2121
On systems with a shared python installation, we recommend that users install the package within a virtual python environment.
22-
A script designed to automatically setup a virtual environment, install the dependencies, and install pygeos is included in `scripts/setupVirtualPythonEnvironment.bash` .
22+
A script designed to automatically setup a virtual environment, install the dependencies, and install pygeos is included in `scripts/setupVirtualPythonEnvironment.bash`.
2323
The options for the script include:
2424

25-
- -p/--python_root : This specifies the root path for the parent python environment
26-
- -o/--output_path : This specifies location where the virtual environment will be installed
25+
- ``-p/--python_root``: This specifies the root path for the parent python environment
26+
- ``-o/--output_path``: This specifies location where the virtual environment will be installed
2727

28-
The default settings for the script will build a virtual environment for the python-3.6.4 installation on LC systems, and will place the environment in `~/Python/virtual/geosx` . The script will also install pygeos and its pre-requisites.
28+
The default settings for the script will build a virtual environment for the python-3.6.4 installation on LC systems, and will place the environment in `~/Python/virtual/geosx`. The script will also install pygeos and its pre-requisites.
2929

3030
To use the virtual environment, do one of the following:
3131

32-
1) Load the startup script: `source ~/Python/virtual/geosx/bin/activate` . This will add the (geosx) decorator to your shell, and will set the proper aliases for python, pip, etc. To exit the environment, run `deactivate`.
33-
2) Access the bin directory of the virtual python environment directly (e.g. `~/Python/virtual/geosx/bin/python some_script.py` ).
32+
1) Load the startup script: `source ~/Python/virtual/geosx/bin/activate`. This will add the (geosx) decorator to your shell, and will set the proper aliases for python, pip, etc. To exit the environment, run `deactivate`.
33+
2) Access the bin directory of the virtual python environment directly (e.g. `~/Python/virtual/geosx/bin/python some_script.py`).
3434

3535

3636

@@ -51,9 +51,9 @@ The following will read a raw .xml file, generate a processed version, and retur
5151

5252
Optional arguments include:
5353

54-
- -o / --output = The desired name for the output file (otherwise, it is randomly generated)
55-
- -s / --schema = The location of a schema to validate the final .xml file
56-
- -v / --verbose = Increase module verbosity
54+
- ``-o/--output``: The desired name for the output file (otherwise, it is randomly generated)
55+
- ``-s/--schema``: The location of a schema to validate the final .xml file
56+
- ``-v/--verbose``: Increase module verbosity
5757

5858
For convenience, this script can be embedded within GEOSX arguments:
5959

@@ -78,11 +78,11 @@ The pygeos module can also be called from within a python script. For example:
7878
Advanced XML Features
7979
=================================
8080

81-
The xml preprocessor in pygeos is designed to take an raw input file, and generate an new file that can be directly read by GEOSX.
82-
The syntax for the advanced xml format is given below.
81+
The xml preprocessor in pygeos is designed to take a raw input file, and generate a new file that can be directly read by GEOSX.
82+
The syntax for the advanced XML format is given below.
8383
During the processing the order of operations are:
8484

85-
1) Merging any included xml files into the root structure
85+
1) Merging any included XML files into the root structure
8686
2) Substituting in any parameters
8787
3) Evaluating unit strings
8888
4) Evaluating symbolic math
@@ -95,10 +95,11 @@ XML inputs can point to included children (these children can then include grand
9595
During processing, these are recursively inserted into the root XML structure by the following scheme:
9696

9797
- Merge two objects if:
98-
- At the root level and an object with the matching tag exists.
99-
- If the “name” attribute is present and a object with the matching tag and name exist.
98+
99+
- At the root level an object with the matching tag exists.
100+
- If the "name" attribute is present and an object with the matching tag and name exists.
100101
- Any preexisting attributes are overwritten by the donor.
101-
- Otherwise append the xml structure with the target.
102+
- Otherwise append the XML structure with the target.
102103

103104

104105
.. code-block:: xml
@@ -112,8 +113,8 @@ During processing, these are recursively inserted into the root XML structure by
112113
113114
Parameters
114115
------------------------------
115-
Parameters are a convenient way to build a configurable and human-readable input xml.
116-
They are defined via a block in the xml structure.
116+
Parameters are a convenient way to build a configurable and human-readable input XML.
117+
They are defined via a block in the XML structure.
117118
Parameter names may only include upper/lower case letters and underscores (to avoid conflicts with symbolic math).
118119
Parameters may have any value:
119120

@@ -124,7 +125,7 @@ Parameters may have any value:
124125
- Etc.
125126

126127

127-
They can be used in any field within in the xml file (except in Includes) as follows:
128+
They can be used in any field within in the XML file (except in Includes) as follows:
128129

129130
- $x_par
130131
- $:x_par
@@ -150,8 +151,8 @@ Units
150151
By default, input values are specified using SI units.
151152
In some cases, it is useful to override this behavior by explicitly specifying the units of the input.
152153
These are specified by appending a valid number with a unit definition in square braces.
153-
The unit manager supports most common units and SI prefixes, using both long- and abbreviated names (e.g.: c, centi, k, kilo, etc.)
154-
Units may include predefined composite units (dyne, N, etc.) or may be built up from sub-units using a python syntax (e.g.: [N], [kg*m/s**2].
154+
The unit manager supports most common units and SI prefixes, using both long- and abbreviated names (e.g.: c, centi, k, kilo, etc.).
155+
Units may include predefined composite units (dyne, N, etc.) or may be built up from sub-units using a python syntax (e.g.: [N], [kg*m/s**2]).
155156
Any (or no) amount of whitespace is allowed between the number and the unit bracket.
156157

157158

@@ -170,7 +171,7 @@ Examples:
170171
171172
Symbolic Math
172173
------------------------------
173-
Input xml files can also include symbolic mathematical expressions.
174+
Input XML files can also include symbolic mathematical expressions.
174175
These are placed within pairs of backticks (\`), and use a python syntax.
175176
Parameters and units are evaluated before symbolic expressions.
176177
Note: symbolic expressions are sanitized by removing any residual alpha characters, but this can be relaxed if more complicated function are needed.
@@ -195,6 +196,6 @@ Validation
195196
------------------------------
196197
Unmatched special characters ($, [, \`, etc.) mean that parameters, units, or symbolic math were not specified correctly.
197198
If the code detects these, it will throw an error.
198-
The XML is validated against the input schema to check if all of the required field are present, and that input parameters match their expected types.
199+
The XML is validated against the input schema to check if all of the requireds field are present, and that input parameters match their expected types.
199200

200201

pygeos_package/pygeos/regex_config.py

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Tools for managing regular expressions in pygeos"""
2+
3+
import re
4+
5+
6+
# Define regex patterns used throughout the module:
7+
#
8+
# Pattern | Example targets | Notes
9+
# ------------------------------------------------------------------------------------
10+
# parameters | $Parameter, $Parameter$ | Matches entire parameter string
11+
# units | 9.81[m**2/s], 1.0 [bbl/day] | Matches entire unit string
12+
# units_b | m, bbl, day | Matches unit names
13+
# symbolic | `1 + 2.34e5*2` | Matches the entire symbolic string
14+
# sanitize | | Removes any residual characters before
15+
# | | evaluating symbolic expressions
16+
# strip_trailing | 3.0000, 5.150050 | Removes unnecessary float strings
17+
# strip_trailing_b| 3.0000e0, 1.23e0 | Removes unnecessary float strings
18+
#
19+
patterns = {'parameters': r"\$:?([a-zA-Z_]*)\$?",
20+
'units': r"([0-9]*?\.?[0-9]+(?:[eE][-+]?[0-9]*?)?)\ *?\[([-+.*/()a-zA-Z0-9]*)\]",
21+
'units_b': r"([a-zA-Z]*)",
22+
'symbolic': r"\`([-+.*/() 0-9eE]*)\`",
23+
'sanitize': r"[a-z-[e]A-Z-[E]]",
24+
'strip_trailing': r"\.?0+(?=e)",
25+
'strip_trailing_b': r"e\+00|\+0?|(?<=-)0"}
26+
27+
# String formatting for symbolic expressions
28+
symbolic_format = '%1.6e'
29+
30+
31+
def SymbolicMathRegexHandler(match):
32+
"""Evaluate symbolic expressions that are identified using the regex_tools.patterns['symbolic'].
33+
34+
@param match A matching string identified by the regex.
35+
"""
36+
k = match.group(1)
37+
if k:
38+
# Sanitize the input
39+
sanitized = re.sub(patterns['sanitize'], '', k).strip()
40+
value = eval(sanitized, {'__builtins__': None})
41+
42+
# Format the string, removing any trailing zeros, decimals, etc.
43+
str_value = re.sub(patterns['strip_trailing'], '', symbolic_format % (value))
44+
str_value = re.sub(patterns['strip_trailing_b'], '', str_value)
45+
return str_value
46+
else:
47+
return
48+
49+
50+
class DictRegexHandler():
51+
"""This class is used to substitute matched values with those stored in a dict."""
52+
53+
def __init__(self):
54+
"""Initialize the handler with an empty target list.
55+
The key/value pairs of self.target indicate which values
56+
to look for and the values they will replace with.
57+
"""
58+
self.target = {}
59+
60+
def __call__(self, match):
61+
"""Replace the matching strings with their target.
62+
63+
@param match A matching string identified by the regex.
64+
"""
65+
66+
k = match.group(1)
67+
if k:
68+
if (k not in self.target.keys()):
69+
raise Exception('Error: Target (%s) is not defined in the regex handler' % k)
70+
value = self.target[k]
71+
return str(value)
72+
else:
73+
return
74+
75+

0 commit comments

Comments
 (0)