From 46d1e35eced34307d20e5c29ce7048a965e1a949 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 10 Apr 2019 14:44:08 +0200 Subject: [PATCH] configuration options for single rules can be set in config file --- ansiblelater/rules/ansiblefiles.py | 12 +++++++++--- ansiblelater/rules/yamlfiles.py | 24 ++++++++++++++---------- ansiblelater/settings.py | 28 +++++++++++++++++++++++++++- ansiblelater/utils/rulehelper.py | 4 ++-- 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/ansiblelater/rules/ansiblefiles.py b/ansiblelater/rules/ansiblefiles.py index c3cdb30..53f420a 100644 --- a/ansiblelater/rules/ansiblefiles.py +++ b/ansiblelater/rules/ansiblefiles.py @@ -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) diff --git a/ansiblelater/rules/yamlfiles.py b/ansiblelater/rules/yamlfiles.py index e9b71fb..6b0c4e4 100644 --- a/ansiblelater/rules/yamlfiles.py +++ b/ansiblelater/rules/yamlfiles.py @@ -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) diff --git a/ansiblelater/settings.py b/ansiblelater/settings.py index 6b349e1..bf12f29 100644 --- a/ansiblelater/settings.py +++ b/ansiblelater/settings.py @@ -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) diff --git a/ansiblelater/utils/rulehelper.py b/ansiblelater/utils/rulehelper.py index bebd666..d1c0da8 100644 --- a/ansiblelater/utils/rulehelper.py +++ b/ansiblelater/utils/rulehelper.py @@ -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: