Skip to content

Commit

Permalink
Merge #422 (olaurino) - Improve error message with wrong xspec version
Browse files Browse the repository at this point in the history
  • Loading branch information
olaurino committed Sep 21, 2018
2 parents 7266f20 + 77db1c5 commit 54f019d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions sherpa/astro/xspec/utils.py
Expand Up @@ -22,6 +22,8 @@

__all__ = ['ModelMeta', 'include_if', 'version_at_least']

XSPEC_VERSION = LooseVersion(_xspec.get_xsversion())


class ModelMeta(type):
"""
Expand Down Expand Up @@ -73,8 +75,7 @@ def equal_or_greater_than(version_string):
:param version_string: the version against which to compare the current xspec version
:return: `True` if the version of xspec is equal or greater than the argument, `False` otherwise
"""
xspec_version = LooseVersion(_xspec.get_xsversion())
return xspec_version >= LooseVersion(version_string)
return XSPEC_VERSION >= LooseVersion(version_string)


class include_if(object):
Expand All @@ -93,14 +94,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 @@ -110,5 +114,11 @@ 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):
include_if.__init__(self, equal_or_greater_than(version_string))
self.version_string = version_string

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

0 comments on commit 54f019d

Please sign in to comment.