configuration options for single rules can be set in config file

This commit is contained in:
Robert Kaussow 2019-04-10 14:44:08 +02:00
parent 392aa9308d
commit 46d1e35ece
4 changed files with 52 additions and 16 deletions

View File

@ -10,7 +10,9 @@ from ansiblelater.utils.rulehelper import (get_normalized_tasks,
def check_braces_spaces(candidate, settings):
yamllines, errors = get_normalized_yaml(candidate, settings)
description = "no suitable numbers of spaces (required: 1)"
conf = settings["ansible"]["double-braces"]
description = "no suitable numbers of spaces (min: {min} max: {max})".format(
min=conf["min-spaces-inside"], max=conf["max-spaces-inside"])
matches = []
braces = re.compile("{{(.*?)}}")
@ -23,9 +25,13 @@ def check_braces_spaces(candidate, settings):
matches.append((i, item))
for i, line in matches:
leading, trailing = count_spaces(line)
[leading, trailing] = count_spaces(line)
sum_spaces = leading + trailing
if not leading == 1 or not trailing == 1:
if (
(sum_spaces < conf["min-spaces-inside"] * 2)
or (sum_spaces > conf["min-spaces-inside"] * 2)
):
errors.append(Error(i, description))
return Result(candidate.path, errors)

View File

@ -44,32 +44,36 @@ def check_native_yaml(candidate, settings):
def check_yaml_empty_lines(candidate, settings):
options = "rules: {empty-lines: {max: 1, max-start: 0, max-end: 1}}"
errors = run_yamllint(candidate, settings, options)
options = "rules: {{empty-lines: {conf}}}".format(
conf=settings["yamllint"]["empty-lines"])
errors = run_yamllint(candidate.path, options)
return Result(candidate.path, errors)
def check_yaml_indent(candidate, settings):
options = "rules: {indentation: {spaces: 2, check-multi-line-strings: false, indent-sequences: true}}"
errors = run_yamllint(candidate, settings, options)
options = "rules: {{indentation: {conf}}}".format(
conf=settings["yamllint"]["indentation"])
errors = run_yamllint(candidate.path, options)
return Result(candidate.path, errors)
def check_yaml_hyphens(candidate, settings):
options = "rules: {hyphens: {max-spaces-after: 1}}"
errors = run_yamllint(candidate, settings, options)
options = "rules: {{hyphens: {conf}}}".format(
conf=settings["yamllint"]["hyphens"])
errors = run_yamllint(candidate.path, options)
return Result(candidate.path, errors)
def check_yaml_document_start(candidate, settings):
options = "rules: {document-start: {present: true}}"
errors = run_yamllint(candidate, settings, options)
options = "rules: {{document-start: {conf}}}".format(
conf=settings["yamllint"]["document-start"])
errors = run_yamllint(candidate.path, options)
return Result(candidate.path, errors)
def check_yaml_colons(candidate, settings):
options = "rules: {colons: {max-spaces-before: 0, max-spaces-after: 1}}"
errors = run_yamllint(candidate, settings, options)
options = "rules: {{colons: {conf}}}"
errors = run_yamllint(candidate.path, options)
return Result(candidate.path, errors)

View File

@ -103,7 +103,33 @@ class Settings(object):
},
"ansible": {
"custom_modules": [],
}
"double-braces": {
"min-spaces-inside": 1,
"max-spaces-inside": 1,
},
},
"yamllint": {
"empty-lines": {
"max": 1,
"max-start": 0,
"max-end": 1,
},
"indentation": {
"spaces": 2,
"check-multi-line-strings": False,
"indent-sequences": True,
},
"hyphens": {
"max-spaces-after": 1
},
"document-start": {
"present": True
},
"colons": {
"max-spaces-before": 0,
"max-spaces-after": 1
},
},
}
self.schema = anyconfig.gen_schema(defaults)

View File

@ -121,10 +121,10 @@ def get_raw_yaml(candidate, settings):
return content, errors
def run_yamllint(candidate, settings, options="extends: default"):
def run_yamllint(path, options="extends: default"):
errors = []
try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f:
with codecs.open(path, mode="rb", encoding="utf-8") as f:
for problem in linter.run(f, YamlLintConfig(options)):
errors.append(Error(problem.line, problem.desc))
except LaterError as ex: