Skip to content

Commit

Permalink
Improve error handling when importing fuel handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
youngmit committed May 6, 2020
1 parent ca34beb commit 1bc49b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
11 changes: 11 additions & 0 deletions armi/physics/fuelCycle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ def defineSettingsValidators(inspector):
Implementation of settings inspections for fuel cycle settings.
"""
queries = []

queries.append(settingsValidation.Query(
lambda: bool(inspector.cs["shuffleLogic"]) ^
bool(inspector.cs["fuelHandlerName"]),
"A value was provided for `fuelHandlerName` or `shuffleLogic`, but not "
"the other. Either both `fuelHandlerName` and `shuffleLogic` should be "
"defined, or neither of them.",
"",
inspector.NO_ACTION,
))

# Check for code fixes for input code on the fuel shuffling outside the version control of ARMI
# These are basically auto-migrations for untracked code using
# the ARMI API. (This may make sense at a higher level)
Expand Down
2 changes: 1 addition & 1 deletion armi/physics/fuelCycle/fuelHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def fuelHandlerFactory(operator):

# User did request a custom fuel handler. We must go find and import it
# from the input directory.
with directoryChangers.DirectoryChanger(cs.inputDirectory):
with directoryChangers.DirectoryChanger(cs.inputDirectory, dumpOnException=False):
try:
module = pathTools.importCustomPyModule(fuelHandlerModulePath)

Expand Down
27 changes: 15 additions & 12 deletions armi/utils/pathTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,18 @@ def getModAndClassFromPath(path):
def separateModuleAndAttribute(pathAttr):
"""
Return True of the specified python module, and attribute of the module exist.
Parameters
----------
pathAttr : str
Path to a python module followed by the desired attribute.
e.g.: `/path/to/my/thing.py:MyClass`
Notes
-----
The attribute of the module could be a class, function, variable, etc.
Raises
------
ValueError:
Expand All @@ -190,17 +190,20 @@ def separateModuleAndAttribute(pathAttr):
def importCustomPyModule(modulePath):
"""
Dynamically import a custom module.
Parameters
----------
modulePath : str
Path to a python module.
Returns
-------
userSpecifiedModule : module
The imported python module.
"""
modulePath = pathlib.Path(modulePath)
if not modulePath.exists() or not modulePath.is_file():
raise IOError(r"Cannot import module from the given path: `{modulePath}`")
_dir, moduleName = os.path.split(modulePath)
moduleName = os.path.splitext(moduleName)[0] # take off the extension
spec = importlib.util.spec_from_file_location(moduleName, modulePath)
Expand All @@ -212,19 +215,19 @@ def importCustomPyModule(modulePath):
def moduleAndAttributeExist(pathAttr):
"""
Return True if the specified python module, and attribute of the module exist.
Parameters
----------
pathAttr : str
Path to a python module followed by the desired attribute.
e.g.: `/path/to/my/thing.py:MyClass`
Returns
Returns
-------
bool
bool
True if the specified python module, and attribute of the module exist.
Notes
-----
The attribute of the module could be a class, function, variable, etc.
Expand Down

0 comments on commit 1bc49b3

Please sign in to comment.