summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/_internal/rules.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ansiblelint/_internal/rules.py')
-rw-r--r--src/ansiblelint/_internal/rules.py33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/ansiblelint/_internal/rules.py b/src/ansiblelint/_internal/rules.py
index acaf0f3..38cb835 100644
--- a/src/ansiblelint/_internal/rules.py
+++ b/src/ansiblelint/_internal/rules.py
@@ -1,4 +1,5 @@
"""Internally used rule classes."""
+
from __future__ import annotations
import inspect
@@ -9,6 +10,7 @@ from typing import TYPE_CHECKING, Any
from ansiblelint.constants import RULE_DOC_URL
if TYPE_CHECKING:
+ from ansiblelint.config import Options
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import RulesCollection
@@ -44,6 +46,8 @@ class BaseRule:
link: str = ""
has_dynamic_tags: bool = False
needs_raw_task: bool = False
+ # Used to mark rules that we will never unload (internal ones)
+ unloadable: bool = False
# We use _order to sort rules and to ensure that some run before others,
# _order 0 for internal rules
# _order 1 for rules that check that data can be loaded
@@ -54,7 +58,7 @@ class BaseRule:
_collection: RulesCollection | None = None
@property
- def help(self) -> str: # noqa: A003
+ def help(self) -> str:
"""Return a help markdown string for the rule."""
if self._help is None:
self._help = ""
@@ -92,10 +96,11 @@ class BaseRule:
_logger.warning(
"Ignored exception from %s.%s while processing %s: %s",
self.__class__.__name__,
- method,
+ method.__name__,
str(file),
exc,
)
+ _logger.debug("Ignored exception details", exc_info=True)
else:
matches.extend(self.matchdir(file))
return matches
@@ -157,6 +162,26 @@ class BaseRule:
"""
return getattr(cls, "_ids", {cls.id: cls.shortdesc})
+ @property
+ def rule_config(self) -> dict[str, Any]:
+ """Retrieve rule specific configuration."""
+ rule_config = {}
+ if self.options:
+ rule_config = self.options.rules.get(self.id, {})
+ if not isinstance(rule_config, dict): # pragma: no branch
+ msg = f"Invalid rule config for {self.id}: {rule_config}"
+ raise RuntimeError(msg) # noqa: TRY004
+ return rule_config
+
+ @property
+ def options(self) -> Options | None:
+ """Used to access linter configuration."""
+ if self._collection is None:
+ msg = f"A rule ({self.id}) that is not part of a collection cannot access its configuration."
+ _logger.warning(msg)
+ return None
+ return self._collection.options
+
# pylint: enable=unused-argument
@@ -170,6 +195,7 @@ class RuntimeErrorRule(BaseRule):
tags = ["core"]
version_added = "v5.0.0"
_order = 0
+ unloadable = True
class AnsibleParserErrorRule(BaseRule):
@@ -181,6 +207,7 @@ class AnsibleParserErrorRule(BaseRule):
tags = ["core"]
version_added = "v5.0.0"
_order = 0
+ unloadable = True
class LoadingFailureRule(BaseRule):
@@ -196,6 +223,7 @@ class LoadingFailureRule(BaseRule):
_ids = {
"load-failure[not-found]": "File not found",
}
+ unloadable = True
class WarningRule(BaseRule):
@@ -207,3 +235,4 @@ class WarningRule(BaseRule):
tags = ["core", "experimental"]
version_added = "v6.8.0"
_order = 0
+ unloadable = True