add flake8-pep3101

This commit is contained in:
Robert Kaussow 2020-04-11 16:20:41 +02:00
parent 305330d147
commit 6dbfd81152
11 changed files with 68 additions and 49 deletions

View File

@ -64,18 +64,18 @@ def main():
candidate = candidates.classify(filename, settings, standards)
if candidate:
if candidate.binary:
LOG.info("Not reviewing binary file %s" % filename)
LOG.info("Not reviewing binary file {name}".format(name=filename))
continue
if candidate.vault:
LOG.info("Not reviewing vault file %s" % filename)
LOG.info("Not reviewing vault file {name}".format(name=filename))
continue
if lines:
LOG.info("Reviewing %s lines %s" % (candidate, lines))
LOG.info("Reviewing {candidate} lines {no}".format(candidate=candidate, no=lines))
else:
LOG.info("Reviewing all of %s" % candidate)
LOG.info("Reviewing all of {candidate}".format(candidate=candidate))
tasks.append((candidate, settings, lines))
else:
LOG.info("Couldn't classify file %s" % filename)
LOG.info("Couldn't classify file {name}".format(name=filename))
errors = (sum(p.map(_review_wrapper, tasks)))
p.close()

View File

@ -31,23 +31,29 @@ def get_standards(filepath):
standards = importlib.import_module("standards")
except ImportError as e:
utils.sysexit_with_message(
"Could not import standards from directory %s: %s" % (filepath, str(e))
"Could not import standards from directory {path}: {msg}".format(
path=filepath, msg=str(e)
)
)
if getattr(standards, "ansible_min_version", None) and \
LooseVersion(standards.ansible_min_version) > LooseVersion(ansible.__version__):
utils.sysexit_with_message(
"Standards require ansible version %s (current version %s). "
"Please upgrade ansible." % (standards.ansible_min_version, ansible.__version__)
"Standards require ansible version {min_version} (current version {version}). "
"Please upgrade ansible.".format(
min_version=standards.ansible_min_version, version=ansible.__version__
)
)
if getattr(standards, "ansible_later_min_version", None) and \
LooseVersion(standards.ansible_later_min_version) > LooseVersion(
utils.get_property("__version__")):
utils.sysexit_with_message(
"Standards require ansible-later version %s (current version %s). "
"Please upgrade ansible-later." %
(standards.ansible_later_min_version, utils.get_property("__version__"))
"Standards require ansible-later version {min_version} (current version {version}). "
"Please upgrade ansible-later.".format(
min_version=standards.ansible_later_min_version,
version=utils.get_property("__version__")
)
)
normalized_std = (list(toolz.remove(lambda x: x.id == "", standards.standards)))

View File

@ -74,20 +74,24 @@ class Candidate(object):
if self.expected_version:
if isinstance(self, RoleFile):
LOG.warning(
"%s %s is in a role that contains a meta/main.yml without a declared "
"standards version. "
"Using latest standards version %s" %
(type(self).__name__, self.path, version)
"{name} {path} is in a role that contains a meta/main.yml without a "
"declared standards version. "
"Using latest standards version {version}".format(
name=type(self).__name__, path=self.path, version=version
)
)
else:
LOG.warning(
"%s %s does not present standards version. "
"Using latest standards version %s" %
(type(self).__name__, self.path, version)
"{name} {path} does not present standards version. "
"Using latest standards version {version}".format(
name=type(self).__name__, path=self.path, version=version
)
)
else:
LOG.info(
"%s %s declares standards version %s" % (type(self).__name__, self.path, version)
"{name} {path} declares standards version {version}".format(
name=type(self).__name__, path=self.path, version=version
)
)
return version
@ -182,7 +186,7 @@ class Candidate(object):
return standard_id
def __repr__(self): # noqa
return "%s (%s)" % (type(self).__name__, self.path)
return "{name} ({path})".format(name=type(self).__name__, path=self.path)
def __getitem__(self, item): # noqa
return self.__dict__.get(item)
@ -330,9 +334,9 @@ class Error(object):
def __repr__(self): # noqa
if self.lineno:
return "%s: %s" % (self.lineno, self.message)
return "{no}: {msg}".format(no=self.lineno, msg=self.message)
else:
return " %s" % (self.message)
return " {msg}".format(msg=self.message)
def to_dict(self):
result = dict(lineno=self.lineno, message=self.message)

View File

@ -8,7 +8,7 @@ class LaterError(Exception):
def __init__(self, msg, original):
"""Initialize new exception."""
super(LaterError, self).__init__(msg + (": %s" % original))
super(LaterError, self).__init__("{msg}: {org}".format(msg=msg, org=original))
self.original = original

View File

@ -47,20 +47,20 @@ def check_named_task(candidate, settings):
"meta", "debug", "include_role", "import_role", "include_tasks", "import_tasks",
"include_vars", "block"
]
description = "module '%s' used without or empty name attribute"
description = "module '{module}' used without or empty name attribute"
if not errors:
for task in tasks:
module = task["action"]["__ansible_module__"]
if ("name" not in task or not task["name"]) and module not in nameless_tasks:
errors.append(Error(task["__line__"], description % module))
errors.append(Error(task["__line__"], description.format(module=module)))
return Result(candidate.path, errors)
def check_name_format(candidate, settings):
tasks, errors = get_normalized_tasks(candidate, settings)
description = "name '%s' should start with uppercase"
description = "name '{name}' should start with uppercase"
namelines = defaultdict(list)
if not errors:
@ -69,14 +69,14 @@ def check_name_format(candidate, settings):
namelines[task["name"]].append(task["__line__"])
for (name, lines) in namelines.items():
if name and not name[0].isupper():
errors.append(Error(lines[-1], description % name))
errors.append(Error(lines[-1], description.format(name=name)))
return Result(candidate.path, errors)
def check_unique_named_task(candidate, settings):
tasks, errors = get_normalized_tasks(candidate, settings)
description = "name '%s' appears multiple times"
description = "name '{name}' appears multiple times"
namelines = defaultdict(list)
@ -86,7 +86,7 @@ def check_unique_named_task(candidate, settings):
namelines[task["name"]].append(task["__line__"])
for (name, lines) in namelines.items():
if name and len(lines) > 1:
errors.append(Error(lines[-1], description % name))
errors.append(Error(lines[-1], description.format(name=name)))
return Result(candidate.path, errors)
@ -113,7 +113,7 @@ def check_command_instead_of_module(candidate, settings):
"systemctl": "systemd",
"sed": "template or lineinfile"
}
description = "%s command used in place of %s module"
description = "{exec} command used in place of {module} module"
if not errors:
for task in tasks:
@ -129,7 +129,10 @@ def check_command_instead_of_module(candidate, settings):
and "register" not in task
):
errors.append(
Error(task["__line__"], description % (executable, modules[executable]))
Error(
task["__line__"],
description.format(exec=executable, module=modules[executable])
)
)
return Result(candidate.path, errors)

View File

@ -12,12 +12,12 @@ from ansiblelater.utils.rulehelper import get_tasks
def check_meta_main(candidate, settings):
content, errors = get_raw_yaml(candidate, settings)
keys = ["author", "description", "min_ansible_version", "platforms", "dependencies"]
description = "file should contain '%s' key"
description = "file should contain '{key}' key"
if not errors:
for key in keys:
if not nested_lookup(key, content):
errors.append(Error(None, description % (key)))
errors.append(Error(None, description.format(key=key)))
return Result(candidate.path, errors)

View File

@ -97,6 +97,8 @@ def check_yaml_file(candidate, settings):
try:
yaml.safe_load(f)
except Exception as e:
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(
Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem))
)
return Result(candidate.path, errors)

View File

@ -23,4 +23,6 @@ class Standard(object):
self.types = standard_dict.get("types")
def __repr__(self): # noqa
return "Standard: %s (version: %s, types: %s)" % (self.name, self.version, self.types)
return "Standard: {name} (version: {version}, types: {types})".format(
name=self.name, version=self.version, types=self.types
)

View File

@ -25,9 +25,9 @@ def get_tasks(candidate, settings):
except LaterError as ex:
e = ex.original
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
except LaterAnsibleError as e:
errors.append(Error(e.line, "syntax error: %s" % (e.message)))
errors.append(Error(e.line, "syntax error: {msg}".format(e.message)))
return yamllines, errors
@ -43,9 +43,9 @@ def get_action_tasks(candidate, settings):
tasks = action_tasks(yamllines, candidate)
except LaterError as ex:
e = ex.original
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
except LaterAnsibleError as e:
errors.append(Error(e.line, "syntax error: %s" % (e.message)))
errors.append(Error(e.line, "syntax error: {}".format(e.message)))
return tasks, errors
@ -57,9 +57,9 @@ def get_normalized_task(task, candidate, settings):
normalized = normalize_task(task, candidate.path, settings["ansible"]["custom_modules"])
except LaterError as ex:
e = ex.original
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
except LaterAnsibleError as e:
errors.append(Error(e.line, "syntax error: %s" % (e.message)))
errors.append(Error(e.line, "syntax error: {msg}".format(msg=e.message)))
return normalized, errors
@ -86,9 +86,9 @@ def get_normalized_tasks(candidate, settings):
except LaterError as ex:
e = ex.original
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
except LaterAnsibleError as e:
errors.append(Error(e.line, "syntax error: %s" % (e.message)))
errors.append(Error(e.line, "syntax error: {msg}".format(msg=e.message)))
return normalized, errors
@ -105,9 +105,9 @@ def get_normalized_yaml(candidate, settings, options=None):
yamllines = normalized_yaml(candidate.path, options)
except LaterError as ex:
e = ex.original
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
except LaterAnsibleError as e:
errors.append(Error(e.line, "syntax error: %s" % (e.message)))
errors.append(Error(e.line, "syntax error: {msg}".format(msg=e.message)))
return yamllines, errors
@ -122,7 +122,7 @@ def get_raw_yaml(candidate, settings):
except LaterError as ex:
e = ex.original
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
return content, errors
@ -135,6 +135,6 @@ def run_yamllint(path, options="extends: default"):
errors.append(Error(problem.line, problem.desc))
except LaterError as ex:
e = ex.original
errors.append(Error(e.problem_mark.line + 1, "syntax error: %s" % (e.problem)))
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
return errors

View File

@ -483,8 +483,9 @@ def extract_from_list(blocks, candidates):
results.extend(add_action_type(block[candidate], candidate, meta_data))
elif block[candidate] is not None:
raise RuntimeError(
"Key '%s' defined, but bad value: '%s'" %
(candidate, str(block[candidate]))
"Key '{candidate}' defined, but bad value: '{block}'".format(
candidate=candidate, block=str(block[candidate])
)
)
return results

View File

@ -8,6 +8,7 @@ flake8-isort
flake8-logging-format
flake8-polyfill
flake8-quotes
flake8-pep3101
flake8-eradicate; python_version >= "3.6"
pep8-naming
wheel