Skip to content

Commit

Permalink
Added import_() method (also) to Node class.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-hen committed Oct 11, 2021
1 parent 46ad6a5 commit e5ed645
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
13 changes: 7 additions & 6 deletions mph/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,13 @@ def import_(self, node, file):
so that the Python parser does not treat the name as an
`import` statement.
"""
file = Path(file)
log.info(f'Loading external data from file "{file.name}".')
node.java.discardData()
node.property('filename', f'{file}')
node.java.importData()
log.info('Finished loading external data.')
if isinstance(node, str):
node = self/node
if not node.exists():
error = f'Node "{node}" does not exist in model tree.'
log.error(error)
raise ValueError(error)
node.import_(file)

def export(self, node=None, file=None):
"""
Expand Down
19 changes: 19 additions & 0 deletions mph/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,25 @@ def run(self):
raise RuntimeError(error)
java.run()

def import_(self, file):
"""
Imports external data from the given `file`.
Note the trailing underscore in the method name. It is needed
so that the Python parser does not treat the name as an
`import` statement.
"""
file = Path(file)
if not file.exists():
error = f'File "{file}" does not exist.'
log.error(error)
raise IOError(error)
log.info(f'Loading external data from file "{file.name}".')
self.property('filename', f'{file}')
self.java.discardData()
self.java.importData()
log.info('Finished loading external data.')

def create(self, *arguments, name=None):
"""
Creates a new child node.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ def test_run():
Node(model, 'functions').run()


def test_import():
# Skip here as we will test this for the `Model` class anyway and
# would have to create nodes first, which is the test to follow.
pass


def test_create():
functions = Node(model, 'functions')
functions.create('Analytic')
Expand Down

0 comments on commit e5ed645

Please sign in to comment.