diff --git a/ansiblelater/rules/ansiblefiles.py b/ansiblelater/rules/ansiblefiles.py index f566b52..2593cdc 100644 --- a/ansiblelater/rules/ansiblefiles.py +++ b/ansiblelater/rules/ansiblefiles.py @@ -3,6 +3,7 @@ import os from collections import defaultdict from ansiblelater import Result, Error +from ansiblelater.utils import count_spaces from ansiblelater.utils.rulehelper import (get_normalized_tasks, get_normalized_yaml) @@ -11,7 +12,7 @@ def check_braces_spaces(candidate, settings): yamllines, errors = get_normalized_yaml(candidate, settings) description = "no suitable numbers of spaces (required: 1)" - lineno = 0 + lineno = 1 matches = [] braces = re.compile("{{(.*?)}}") @@ -24,29 +25,9 @@ def check_braces_spaces(candidate, settings): matches.append((item, lineno)) for item, lineno in matches: - error_count = 0 - string_length = len(item) - strip_length = item.rstrip() + leading, trailing = count_spaces(item) - if strip_length == 0 and not string_length == 1: - error_count += 1 - else: - x = 0 - leading_spaces = 0 - while (x < string_length - 1 and item[x].isspace()): - x += 1 - leading_spaces += 1 - - x = string_length - 1 - trailing_spaces = 0 - while (x > 0 and item[x].isspace()): - x -= 1 - trailing_spaces += 1 - - if not leading_spaces == 1 or not trailing_spaces == 1: - error_count += 1 - - if not error_count == 0: + if not leading == 1 or not trailing == 1: errors.append(Error(lineno, description)) return Result(candidate.path, errors) diff --git a/ansiblelater/utils/__init__.py b/ansiblelater/utils/__init__.py index 862a0d9..208b540 100644 --- a/ansiblelater/utils/__init__.py +++ b/ansiblelater/utils/__init__.py @@ -41,6 +41,23 @@ def info(message, settings, file=sys.stdout): print(stringc("INFO: %s" % message, 'green'), file=file) +def count_spaces(c_string): + leading_spaces = 0 + trailing_spaces = 0 + + for i, e in enumerate(c_string): + if not e.isspace(): + break + leading_spaces += 1 + + for i, e in reversed(list(enumerate(c_string))): + if not e.isspace(): + break + trailing_spaces += 1 + + return((leading_spaces, trailing_spaces)) + + def get_property(prop): currentdir = os.path.dirname(os.path.realpath(__file__)) parentdir = os.path.dirname(currentdir)