This commit is contained in:
Robert Kaussow 2019-04-05 22:05:06 +02:00
parent 66d5ceec76
commit 703b0e4e58
9 changed files with 52 additions and 54 deletions

View File

@ -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__":

View File

@ -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)

View File

@ -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])

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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,

View File

@ -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