Skip to content

Commit

Permalink
Make pylint disables more specific
Browse files Browse the repository at this point in the history
And beautify the code a bit.
  • Loading branch information
rhcarvalho committed Aug 8, 2017
1 parent 738ac65 commit 4824662
Showing 1 changed file with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
"""
Ansible action plugin to execute health checks in OpenShift clusters.
"""
# pylint: disable=wrong-import-position,missing-docstring,invalid-name
import sys
import os
from collections import defaultdict

from ansible.plugins.action import ActionBase
from ansible.module_utils.six import string_types

try:
from __main__ import display
except ImportError:
# pylint: disable=ungrouped-imports; this is the standard way how to import
# the default display object in Ansible action plugins.
from ansible.utils.display import Display
display = Display()

from ansible.plugins.action import ActionBase
from ansible.module_utils.six import string_types

# Augment sys.path so that we can import checks from a directory relative to
# this callback plugin.
sys.path.insert(1, os.path.dirname(os.path.dirname(__file__)))

# pylint: disable=wrong-import-position; the import statement must come after
# the manipulation of sys.path.
from openshift_checks import OpenShiftCheck, OpenShiftCheckException, load_checks # noqa: E402


class ActionModule(ActionBase):
"""Action plugin to execute health checks."""

def run(self, tmp=None, task_vars=None):
result = super(ActionModule, self).run(tmp, task_vars)
Expand All @@ -38,9 +42,9 @@ def run(self, tmp=None, task_vars=None):
return result

resolved_checks = resolve_checks(requested_checks, known_checks.values())
except OpenShiftCheckException as e:
except OpenShiftCheckException as exc:
result["failed"] = True
result["msg"] = str(e)
result["msg"] = str(exc)
return result

task_vars = task_vars or {}
Expand Down Expand Up @@ -70,19 +74,21 @@ def run(self, tmp=None, task_vars=None):
return result

def load_known_checks(self, tmp, task_vars):
"""Find all existing checks and return a mapping of names to instances."""
load_checks()

known_checks = {}
for cls in OpenShiftCheck.subclasses():
check_name = cls.name
if check_name in known_checks:
other_cls = known_checks[check_name].__class__
raise OpenShiftCheckException(
"non-unique check name '{}' in: '{}.{}' and '{}.{}'".format(
check_name,
cls.__module__, cls.__name__,
other_cls.__module__, other_cls.__name__))
known_checks[check_name] = cls(execute_module=self._execute_module, tmp=tmp, task_vars=task_vars)
name = cls.name
if name in known_checks:
other_cls = known_checks[name].__class__
msg = "non-unique check name '{}' in: '{}' and '{}'".format(
name,
full_class_name(cls),
full_class_name(other_cls),
)
raise OpenShiftCheckException(msg)
known_checks[name] = cls(execute_module=self._execute_module, tmp=tmp, task_vars=task_vars)
return known_checks


Expand Down Expand Up @@ -185,3 +191,8 @@ def run_check(name, check, user_disabled_checks):
return check.run()
except Exception as exc:
return dict(failed=True, msg=str(exc))


def full_class_name(cls):
"""Return the name of a class prefixed with its module name."""
return '{}.{}'.format(cls.__module__, cls.__name__)

0 comments on commit 4824662

Please sign in to comment.