Skip to content

Commit

Permalink
Remove the usage of is_transformed attribute and make the output look…
Browse files Browse the repository at this point in the history
… better on the terminal
  • Loading branch information
shatakshiiii committed Sep 15, 2023
1 parent c29fd15 commit b7c62df
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/ansiblelint/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ansiblelint.config import PROFILES
from ansiblelint.constants import RULE_DOC_URL
from ansiblelint.rules import RulesCollection
from ansiblelint.rules import RulesCollection, TransformMixin

DOC_HEADER = """
# Default Rules
Expand All @@ -27,6 +27,8 @@ def rules_as_str(rules: RulesCollection) -> RenderableType:
"""Return rules as string."""
table = Table(show_header=False, header_style="title", box=box.SIMPLE)
for rule in rules.alphabetical():
if issubclass(rule.__class__, TransformMixin):
rule.tags.insert(0, "transform")
tag = f"[dim] ({', '.join(rule.tags)})[/dim]" if rule.tags else ""
table.add_row(
f"[link={RULE_DOC_URL}{rule.id}/]{rule.id}[/link]",
Expand Down
17 changes: 14 additions & 3 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import re
import sys
from abc import ABC, abstractmethod
from collections import defaultdict
from collections.abc import Iterable, Iterator, MutableMapping, MutableSequence
from importlib import import_module
Expand Down Expand Up @@ -251,7 +252,7 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
return matches


class TransformMixin:
class TransformMixin(ABC):
"""A mixin for AnsibleLintRule to enable transforming files.
If ansible-lint is started with the ``--write`` option, then the ``Transformer``
Expand All @@ -260,6 +261,7 @@ class TransformMixin:
a MatchError can do transforms to fix that match.
"""

@abstractmethod
def transform(
self,
match: MatchError,
Expand Down Expand Up @@ -550,16 +552,25 @@ def list_write_rules(self) -> str:
transformed_rules = []
rule_description = []
for rule in self.rules:
if rule.is_transformed:
if issubclass(rule.__class__, TransformMixin):
transformed_rules.append(rule.id)
rule_description.append(rule.shortdesc)
result = "# List of rules covered under write mode\n\n"
rule_dict = dict(zip(transformed_rules, rule_description))
for key, value in rule_dict.items():
result += f"{key}: # {value}\n\n"
result += link(f"{RULE_DOC_URL}{key}/", key) + f": # {value}\n"
return result


def link(uri: str, label: str) -> str:
"""Generate the ANSI escape code for creating a hyperlinked label in a terminal."""
if label is None:
label = uri
parameters = ""
escape_mask = "\033[94m\033]8;{};{}\033\\{}\033]8;;\033\\\033[0m"
return escape_mask.format(parameters, uri, label)


def filter_rules_with_profile(rule_col: list[BaseRule], profile: str) -> None:
"""Unload rules that are not part of the specified profile."""
included = set()
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/command_instead_of_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class UseCommandInsteadOfShellRule(AnsibleLintRule, TransformMixin):
)
severity = "HIGH"
tags = ["command-shell", "idiom"]
is_transformed = True
version_added = "historic"

def matchtask(
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/deprecated_local_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class TaskNoLocalAction(AnsibleLintRule, TransformMixin):
needs_raw_task = True
severity = "MEDIUM"
tags = ["deprecations"]
is_transformed = True
version_added = "v4.0.0"

def matchtask(
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class JinjaRule(AnsibleLintRule, TransformMixin):
id = "jinja"
severity = "LOW"
tags = ["formatting"]
is_transformed = True
version_added = "v6.5.0"
_ansible_error_re = re.compile(
r"^(?P<error>.*): (?P<detail>.*)\. String: (?P<string>.*)$",
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/key_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class KeyOrderRule(AnsibleLintRule, TransformMixin):
shortdesc = __doc__
severity = "LOW"
tags = ["formatting"]
is_transformed = True
version_added = "v6.6.2"
needs_raw_task = True
_ids = {
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class NameRule(AnsibleLintRule, TransformMixin):
)
severity = "MEDIUM"
tags = ["idiom"]
is_transformed = True
version_added = "v6.9.1 (last update)"
_re_templated_inside = re.compile(r".*\{\{.*\}\}.*\w.*$")
_ids = {
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/no_free_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class NoFreeFormRule(AnsibleLintRule, TransformMixin):
description = "Avoid free-form inside files as it can produce subtle bugs."
severity = "MEDIUM"
tags = ["syntax", "risk"]
is_transformed = True
version_added = "v6.8.0"
needs_raw_task = True
cmd_shell_re = re.compile(
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/no_jinja_when.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class NoFormattingInWhenRule(AnsibleLintRule, TransformMixin):
)
severity = "HIGH"
tags = ["deprecations"]
is_transformed = True
version_added = "historic"

@staticmethod
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/no_log_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class NoLogPasswordsRule(AnsibleLintRule, TransformMixin):
)
severity = "LOW"
tags = ["opt-in", "security", "experimental"]
is_transformed = True
version_added = "v5.0.9"

def matchtask(
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/partial_become.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class BecomeUserWithoutBecomeRule(AnsibleLintRule, TransformMixin):
description = "``become_user`` should have a corresponding ``become`` at the play or task level."
severity = "VERY_HIGH"
tags = ["unpredictability"]
is_transformed = True
version_added = "historic"

def matchplay(
Expand Down

0 comments on commit b7c62df

Please sign in to comment.