diff --git a/ansiblelater/__main__.py b/ansiblelater/__main__.py index f9c1b28..464f5e8 100755 --- a/ansiblelater/__main__.py +++ b/ansiblelater/__main__.py @@ -29,7 +29,7 @@ def main(): settings = base.get_settings(args) config = settings.config - print(json.dumps(settings.config["logging"], indent=4, sort_keys=True)) + # print(json.dumps(settings.config["logging"], indent=4, sort_keys=True)) logger.update_logger(LOG, config["logging"]["level"], config["logging"]["json"]) @@ -54,7 +54,7 @@ def main(): errors = errors + candidate.review(settings, lines) else: LOG.info("Couldn't classify file %s" % filename) - # return errors + return errors if __name__ == "__main__": diff --git a/ansiblelater/command/candidates.py b/ansiblelater/command/candidates.py index c457b09..60d6d54 100644 --- a/ansiblelater/command/candidates.py +++ b/ansiblelater/command/candidates.py @@ -1,5 +1,4 @@ -"""Candidate module.""" - +"""Review candidates.""" import codecs import copy @@ -7,8 +6,9 @@ import os import re from distutils.version import LooseVersion +from six import iteritems + from ansiblelater import LOG, utils -from ansiblelater.command.review import Error from ansiblelater.exceptions import LaterAnsibleError, LaterError # noqa from ansiblelater.logger import flag_extra @@ -251,6 +251,46 @@ class Rolesfile(Unversioned): pass +class Error(object): + """Default error object created if a rule failed.""" + + def __init__(self, lineno, message, error_type=None, **kwargs): + """ + Initialize a new error object and returns None. + + :param lineno: Line number where the error from de rule occures + :param message: Detailed error description provided by the rule + + """ + self.lineno = lineno + self.message = message + self.kwargs = kwargs + for (key, value) in iteritems(kwargs): + setattr(self, key, value) + + def __repr__(self): # noqa + if self.lineno: + return "%s: %s" % (self.lineno, self.message) + else: + return " %s" % (self.message) + + def to_dict(self): + result = dict(lineno=self.lineno, message=self.message) + for (key, value) in iteritems(self.kwargs): + result[key] = value + return result + + +class Result(object): + def __init__(self, candidate, errors=None): + self.candidate = candidate + self.errors = errors or [] + + def message(self): + return "\n".join(["{0}:{1}".format(self.candidate, error) + for error in self.errors]) + + def classify(filename, settings={}, standards=[]): parentdir = os.path.basename(os.path.dirname(filename)) basename = os.path.basename(filename) diff --git a/ansiblelater/command/review.py b/ansiblelater/command/review.py deleted file mode 100644 index 30e373b..0000000 --- a/ansiblelater/command/review.py +++ /dev/null @@ -1,43 +0,0 @@ -"""Review candidates.""" - -from six import iteritems - - -class Error(object): - """Default error object created if a rule failed.""" - - def __init__(self, lineno, message, error_type=None, **kwargs): - """ - Initialize a new error object and returns None. - - :param lineno: Line number where the error from de rule occures - :param message: Detailed error description provided by the rule - - """ - self.lineno = lineno - self.message = message - self.kwargs = kwargs - for (key, value) in iteritems(kwargs): - setattr(self, key, value) - - def __repr__(self): # noqa - if self.lineno: - return "%s: %s" % (self.lineno, self.message) - else: - return " %s" % (self.message) - - def to_dict(self): - result = dict(lineno=self.lineno, message=self.message) - for (key, value) in iteritems(self.kwargs): - result[key] = value - return result - - -class Result(object): - def __init__(self, candidate, errors=None): - self.candidate = candidate - self.errors = errors or [] - - def message(self): - return "\n".join(["{0}:{1}".format(self.candidate, error) - for error in self.errors]) diff --git a/ansiblelater/rules/ansiblefiles.py b/ansiblelater/rules/ansiblefiles.py index 80b8ead..c3cdb30 100644 --- a/ansiblelater/rules/ansiblefiles.py +++ b/ansiblelater/rules/ansiblefiles.py @@ -2,7 +2,7 @@ import os import re from collections import defaultdict -from ansiblelater.command.review import Error, Result +from ansiblelater.command.candidates import Error, Result from ansiblelater.utils import count_spaces from ansiblelater.utils.rulehelper import (get_normalized_tasks, get_normalized_yaml) diff --git a/ansiblelater/rules/rolefiles.py b/ansiblelater/rules/rolefiles.py index 3e95a74..350ee34 100644 --- a/ansiblelater/rules/rolefiles.py +++ b/ansiblelater/rules/rolefiles.py @@ -1,6 +1,6 @@ from nested_lookup import nested_lookup -from ansiblelater.command.review import Error, Result +from ansiblelater.command.candidates import Error, Result from ansiblelater.utils.rulehelper import get_raw_yaml, get_tasks diff --git a/ansiblelater/rules/taskfiles.py b/ansiblelater/rules/taskfiles.py index 930f3f6..3355484 100644 --- a/ansiblelater/rules/taskfiles.py +++ b/ansiblelater/rules/taskfiles.py @@ -3,7 +3,7 @@ import re from collections import defaultdict -from ansiblelater.command.review import Error, Result +from ansiblelater.command.candidates import Error, Result from ansiblelater.utils.rulehelper import get_normalized_yaml diff --git a/ansiblelater/rules/yamlfiles.py b/ansiblelater/rules/yamlfiles.py index 4f926ad..e9b71fb 100644 --- a/ansiblelater/rules/yamlfiles.py +++ b/ansiblelater/rules/yamlfiles.py @@ -3,7 +3,7 @@ import os import yaml -from ansiblelater.command.review import Error, Result +from ansiblelater.command.candidates import Error, Result from ansiblelater.utils.rulehelper import (get_action_tasks, get_normalized_task, get_normalized_yaml, run_yamllint) diff --git a/ansiblelater/utils/rulehelper.py b/ansiblelater/utils/rulehelper.py index 1ae8274..bebd666 100644 --- a/ansiblelater/utils/rulehelper.py +++ b/ansiblelater/utils/rulehelper.py @@ -5,7 +5,7 @@ import yaml from yamllint import linter from yamllint.config import YamlLintConfig -from ansiblelater.command.review import Error +from ansiblelater.command.candidates import Error from ansiblelater.exceptions import LaterAnsibleError, LaterError from .yamlhelper import (action_tasks, normalize_task, normalized_yaml, diff --git a/ansiblelater/utils/yamlhelper.py b/ansiblelater/utils/yamlhelper.py index 32b5aff..a4aa42e 100644 --- a/ansiblelater/utils/yamlhelper.py +++ b/ansiblelater/utils/yamlhelper.py @@ -439,7 +439,7 @@ def extract_from_list(blocks, candidates): if isinstance(block[candidate], list): meta_data = dict(block) for key in delete_meta_keys: - del meta_data[key] + meta_data.pop(key, None) results.extend(add_action_type(block[candidate], candidate, meta_data)) elif block[candidate] is not None: raise RuntimeError( @@ -491,6 +491,7 @@ def parse_yaml_linenumbers(data, filename): except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e: raise LaterError("syntax error", e) except (yaml.composer.ComposerError) as e: + e.problem = "{} {}".format(e.context, e.problem) raise LaterError("syntax error", e) return data