Skip to content

Commit

Permalink
Merge pull request #19 from benzwick/import-problem-module
Browse files Browse the repository at this point in the history
Ensure that a unique problem module exists before importing
  • Loading branch information
mikaem authored Jan 7, 2019
2 parents 2449b2a + 4c0360e commit 3de552c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
16 changes: 10 additions & 6 deletions oasis/NSCoupled.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@

commandline_kwargs = parse_command_line()

# Find the problem module
default_problem = 'DrivenCavity'
#exec('from problems.NSCoupled.{} import *'.format(commandline_kwargs.get('problem', default_problem)))
problemname = commandline_kwargs.get('problem', default_problem)
try:
problemmod = importlib.import_module('.'.join(('oasis.problems.NSCoupled', problemname)))
except ImportError:
problemmod = importlib.import_module(problemname)
except:
problemspec = importlib.util.find_spec('.'.join(('oasis.problems.NSCoupled', problemname)))
if problemspec is None:
problemspec = importlib.util.find_spec(problemname)
if problemspec is None:
raise RuntimeError(problemname+' not found')

# Import the problem module
print('Importing problem module '+problemname+':\n'+problemspec.origin)
problemmod = importlib.util.module_from_spec(problemspec)
problemspec.loader.exec_module(problemmod)

vars().update(**vars(problemmod))

# Update problem spesific parameters
Expand Down
15 changes: 10 additions & 5 deletions oasis/NSfracStep.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,20 @@

commandline_kwargs = parse_command_line()

# Find the problem module
default_problem = 'DrivenCavity'
problemname = commandline_kwargs.get('problem', default_problem)
try:
problemmod = importlib.import_module('.'.join(('oasis.problems.NSfracStep', problemname)))
except ImportError:
problemmod = importlib.import_module(problemname)
except:
problemspec = importlib.util.find_spec('.'.join(('oasis.problems.NSfracStep', problemname)))
if problemspec is None:
problemspec = importlib.util.find_spec(problemname)
if problemspec is None:
raise RuntimeError(problemname+' not found')

# Import the problem module
print('Importing problem module '+problemname+':\n'+problemspec.origin)
problemmod = importlib.util.module_from_spec(problemspec)
problemspec.loader.exec_module(problemmod)

vars().update(**vars(problemmod))

# Update problem spesific parameters
Expand Down

0 comments on commit 3de552c

Please sign in to comment.