2021-01-30 15:52:48 +00:00
|
|
|
import re
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
from ansiblelater.rule import RuleBase
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
class CheckFilterSeparation(RuleBase):
|
2024-01-27 18:56:35 +00:00
|
|
|
rid = "ANS116"
|
2021-01-30 15:52:48 +00:00
|
|
|
description = "Jinja2 filters should be separated with spaces"
|
|
|
|
helptext = "no suitable numbers of spaces (required: 1)"
|
|
|
|
types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"]
|
|
|
|
|
|
|
|
def check(self, candidate, settings):
|
|
|
|
yamllines, errors = self.get_normalized_yaml(candidate, settings)
|
|
|
|
|
|
|
|
matches = []
|
|
|
|
braces = re.compile("{{(.*?)}}")
|
2022-12-11 11:43:04 +00:00
|
|
|
filters = re.compile(r"(?<=\|)((\s{2,})*\S+)|(\S+(\s{2,})*)(?=\|)")
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
if not errors:
|
|
|
|
for i, line in yamllines:
|
|
|
|
match = braces.findall(line)
|
|
|
|
if match:
|
|
|
|
for item in match:
|
2022-08-01 06:28:10 +00:00
|
|
|
# replace potential regex in filters
|
2022-12-11 11:43:04 +00:00
|
|
|
item = re.sub(r"\(.+\)", "(dummy)", item)
|
2021-01-30 15:52:48 +00:00
|
|
|
matches.append((i, item))
|
|
|
|
|
2022-12-12 08:15:05 +00:00
|
|
|
for i, item in matches:
|
|
|
|
if filters.findall(item):
|
2021-01-30 15:52:48 +00:00
|
|
|
errors.append(self.Error(i, self.helptext))
|
|
|
|
return self.Result(candidate.path, errors)
|