Merge pull request #6 from jokva/sunbeam.parse

Sunbeam.parse
This commit is contained in:
Pål Grønås Drange
2016-12-02 10:27:43 +01:00
committed by GitHub
9 changed files with 94 additions and 7 deletions

View File

@@ -19,6 +19,5 @@ find_package( Boost COMPONENTS python REQUIRED )
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/pycmake/cmake/Modules)
add_subdirectory( src )
add_subdirectory( python )
add_subdirectory( tests )

View File

@@ -1,11 +1,12 @@
# sunbeam ![build status](https://travis-ci.org/Statoil/sunbeam.svg?branch=master "TravisCI Build Status")
# Quick start (WIP)
Assumes you have built (and maybe installed) opm-parser
```
git clone --recursive git@github.com:statoil/sunbeam`
cd sunbeam
mkdir build
cd build
cmake ..
cmake .. -DCMAKE_MODULE_PATH=$OPM_COMMON_ROOT/cmake/Modules
make
```

View File

@@ -1,3 +1,9 @@
include_directories(SYSTEM ${PYTHON_INCLUDE_DIRS}
${Boost_INCLUDE_DIR}
${opm-parser_INCLUDE_DIRS})
add_library( sunbeam SHARED sunbeam.cpp )
target_link_libraries( sunbeam ${Boost_LIBRARIES} ${opm-parser_LIBRARIES} )
set(PYTHON_SOURCES
__init__.py
sunbeam.py)

View File

@@ -1,2 +1,5 @@
from .sunbeam import parse
from .libsunbeam import action
__version__ = '0.0.1'
__license__ = 'GNU General Public License version 3'

View File

@@ -0,0 +1,28 @@
#include <boost/python.hpp>
#include <opm/parser/eclipse/EclipseState/EclipseState.hpp>
#include <opm/parser/eclipse/Parser/Parser.hpp>
namespace py = boost::python;
using namespace Opm;
BOOST_PYTHON_MODULE(libsunbeam) {
EclipseState (*parse_file)( const std::string&, const ParseContext& ) = &Parser::parse;
py::def( "parse", parse_file );
py::class_< EclipseState >( "EclipseState", py::no_init )
.add_property( "title", &EclipseState::getTitle )
;
void (ParseContext::*ctx_update)(const std::string&, InputError::Action) = &ParseContext::update;
py::class_< ParseContext >( "ParseContext" )
.def( "update", ctx_update )
;
py::enum_< InputError::Action >( "action" )
.value( "throw", InputError::Action::THROW_EXCEPTION )
.value( "warn", InputError::Action::WARN )
.value( "ignore", InputError::Action::IGNORE )
;
}

View File

@@ -0,0 +1,29 @@
import libsunbeam as lib
class EclipseState(lib.EclipseState):
pass
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(path, actions = None):
return lib.parse(path, _parse_context(actions))

View File

@@ -1,5 +0,0 @@
include_directories(SYSTEM ${PYTHON_INCLUDE_DIRS}
${Boost_INCLUDE_DIR}
${opm-parser_INCLUDE_DIRS})
add_library( sunbeam SHARED sunbeam.cpp )
target_link_libraries( sunbeam ${Boost_LIBRARIES} ${opm-parser_LIBRARIES} )

View File

View File

@@ -8,3 +8,29 @@ class TestParse(unittest.TestCase):
def parse(self):
sunbeam.parse(self.spe3)
def testParse(self):
spe3 = sunbeam.parse(self.spe3)
self.assertEqual('SPE 3 - CASE 1', spe3.title)
def testParseWithAction(self):
action = ("PARSE_RANDOM_SLASH", sunbeam.action.ignore)
spe3 = sunbeam.parse(self.spe3, action)
self.assertEqual('SPE 3 - CASE 1', spe3.title)
def testParseWithMultipleActions(self):
actions = [ ("PARSE_RANDOM_SLASH", sunbeam.action.ignore),
("FOO", sunbeam.action.warn),
("PARSE_RANDOM_TEXT", sunbeam.action.throw) ]
spe3 = sunbeam.parse(self.spe3, actions)
self.assertEqual('SPE 3 - CASE 1', spe3.title)
def testThrowOnInvalidAction(self):
actions = [ ("PARSE_RANDOM_SLASH", 3.14 ) ]
with self.assertRaises(TypeError):
sunbeam.parse(self.spe3, actions)
with self.assertRaises(ValueError):
sunbeam.parse(self.spe3, "PARSE_RANDOM_SLASH")