You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.5 KiB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. _logger = logging.getLogger(__name__)
  4. class CheckResult(object):
  5. SUCCESS = 'success'
  6. WARNING = 'warning'
  7. FAIL = 'fail'
  8. def __init__(self, result, message, details = None):
  9. super(CheckResult, self).__init__()
  10. self.result = result
  11. self.message = message
  12. self.details = details
  13. class CheckSuccess(CheckResult):
  14. def __init__(self, message, **kwargs):
  15. super(CheckSuccess, self).__init__(CheckResult.SUCCESS, message, **kwargs)
  16. class CheckIssue(CheckResult, Exception):
  17. def __init__(self, result, message, **kwargs):
  18. Exception.__init__(self, message)
  19. CheckResult.__init__(self, result, message, **kwargs)
  20. class CheckFail(CheckIssue):
  21. def __init__(self, message, **kwargs):
  22. super(CheckFail, self).__init__(CheckResult.FAIL, message, **kwargs)
  23. class CheckWarning(CheckIssue):
  24. def __init__(self, message, **kwargs):
  25. super(CheckWarning, self).__init__(CheckResult.WARNING, message, **kwargs)
  26. class Check(object):
  27. def __init__(self, module):
  28. self.module = module
  29. def run(self, env):
  30. try:
  31. return self._run(env)
  32. except CheckIssue as issue:
  33. return issue
  34. except Exception as ex:
  35. _logger.exception(ex)
  36. return CheckFail('Check failed when processing: {}'.format(ex))
  37. def _run(self, env):
  38. raise NotImplementedError('Should be overriden by the subclass')