Add support for dynamic parser extensions

Sunbeam can now be used to parse eclipse files to deck representations.
Added support for dynamic parser extensions which can be used to handle
unsupported eclipse keywords when parsing to deck representations.

Fixed missing function declarations
This commit is contained in:
Jens Gåsemyr Magnus
2017-08-31 13:49:09 +02:00
parent 9acb937361
commit 11fe32df4f
22 changed files with 343 additions and 74 deletions

View File

@@ -2,6 +2,7 @@ set(PYTHON_SOURCES
__init__.py
sunbeam.py
config.py
parser.py
properties.py
schedule.py)

View File

@@ -1,7 +1,7 @@
from .schedule import Well
from .libsunbeam import action
from .properties import parse
from .config import EclipseConfig
from .parser import parse_deck, parse
__version__ = '0.0.1'
__license__ = 'GNU General Public License version 3'

58
python/sunbeam/parser.py Normal file
View File

@@ -0,0 +1,58 @@
from os.path import isfile
import json
import libsunbeam as lib
from .properties import EclipseState
def _parse_context(actions):
ctx = lib.ParseContext()
if actions is None:
return ctx
# this might be a single tuple, in which case we unpack it and repack it
# into a list. If it's not a tuple we assume it's an iterable and just
# carry on
try:
key, action = actions
except ValueError:
pass
else:
actions = [(key, action)]
for key, action in actions:
ctx.update(key, action)
return ctx
def parse(deck, actions = None):
"""deck may be a deck string, or a file path"""
if isfile(deck):
return EclipseState(lib.parse(deck, _parse_context(actions)))
return EclipseState(lib.parse_data(deck, _parse_context(actions)))
def parse_deck(deck, **kwargs):
args = [deck]
if 'keywords' in kwargs:
keywords = kwargs['keywords']
# this might be a single keyword dictionary, in which case we pack it
# into a list. If it's not a dict we assume it's an iterable and just
# carry on
if isinstance(keywords, dict):
keywords = [keywords]
json_keywords = map(json.dumps, keywords)
args.append(json_keywords);
else:
args.append([])
args.append(isfile(deck)) # If the deck is a file, the deck is read from
# that file. Otherwise it is assumed to be a
# string representation of the the deck.
if 'actions' in kwargs:
args.append(_parse_context(kwargs['actions']))
return lib.parse_deck(*args)

View File

@@ -101,30 +101,3 @@ class EclipseGrid(object):
if na != g:
cnt += ', active = %s' % na
return 'EclipseGrid(%s)' % cnt
def _parse_context(actions):
ctx = lib.ParseContext()
if actions is None:
return ctx
# this might be a single tuple, in which case we unpack it and repack it
# into a list. If it's not a tuple we assume it's an iterable and just
# carry on
try:
key, action = actions
except ValueError:
pass
else:
actions = [(key, action)]
for key, action in actions:
ctx.update(key, action)
return ctx
def parse(deck, actions = None):
"""deck may be a deck string, or a file path"""
if isfile(deck):
return EclipseState(lib.parse(deck, _parse_context(actions)))
return EclipseState(lib.parseData(deck, _parse_context(actions)))