Skip to content

Commit

Permalink
improve error message for xspec version decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
olaurino committed Dec 18, 2017
1 parent 25190d1 commit 7c46945
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions sherpa/astro/xspec/utils.py
Expand Up @@ -77,14 +77,17 @@ def __init__(self, condition):
def __call__(self, model_class):
if not self.condition:
model_class.version_enabled = False
model_class._calc = self._disabled(model_class.__name__)
model_class._calc = self._disabled(self.get_message(model_class))

return model_class

def get_message(self, model_class):
return self.DISABLED_MODEL_MESSAGE.format(model_class.__name__)

@staticmethod
def _disabled(cls_name):
def _disabled(message):
def wrapped(*args, **kwargs):
raise AttributeError(include_if.DISABLED_MODEL_MESSAGE.format(cls_name))
raise AttributeError(message)

return wrapped

Expand All @@ -94,6 +97,12 @@ class version_at_least(include_if):
Decorator which takes a version string as an argument and enables a model only if
the xspec version detected at runtime is equal or greater than the one provided to the decorator.
"""
DISABLED_MODEL_MESSAGE = "Model {} is disabled because XSPEC version >= {} is required"

def __init__(self, version_string):
self.version_string = version_string
xspec_version = LooseVersion(_xspec.get_xsversion())
include_if.__init__(self, xspec_version >= LooseVersion(version_string))

def get_message(self, model_class):
return self.DISABLED_MODEL_MESSAGE.format(model_class.__name__, self.version_string)

0 comments on commit 7c46945

Please sign in to comment.