Introduce Black to automate code formatting

This commit is contained in:
Ivan Kravets
2019-09-23 23:13:48 +03:00
parent 5e144a2c98
commit 7c41c7c2f3
90 changed files with 4064 additions and 3367 deletions

View File

@@ -28,39 +28,50 @@ from platformio.commands.check.defect import DefectItem
from platformio.commands.check.tools import CheckToolFactory
from platformio.compat import dump_json_to_unicode
from platformio.project.config import ProjectConfig
from platformio.project.helpers import (find_project_dir_above,
get_project_dir,
get_project_include_dir,
get_project_src_dir)
from platformio.project.helpers import (
find_project_dir_above,
get_project_dir,
get_project_include_dir,
get_project_src_dir,
)
@click.command("check", short_help="Run a static analysis tool on code")
@click.option("-e", "--environment", multiple=True)
@click.option("-d",
"--project-dir",
default=os.getcwd,
type=click.Path(exists=True,
file_okay=True,
dir_okay=True,
writable=True,
resolve_path=True))
@click.option("-c",
"--project-conf",
type=click.Path(exists=True,
file_okay=True,
dir_okay=False,
readable=True,
resolve_path=True))
@click.option(
"-d",
"--project-dir",
default=os.getcwd,
type=click.Path(
exists=True, file_okay=True, dir_okay=True, writable=True, resolve_path=True
),
)
@click.option(
"-c",
"--project-conf",
type=click.Path(
exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True
),
)
@click.option("--filter", multiple=True, help="Pattern: +<include> -<exclude>")
@click.option("--flags", multiple=True)
@click.option("--severity",
multiple=True,
type=click.Choice(DefectItem.SEVERITY_LABELS.values()))
@click.option(
"--severity", multiple=True, type=click.Choice(DefectItem.SEVERITY_LABELS.values())
)
@click.option("-s", "--silent", is_flag=True)
@click.option("-v", "--verbose", is_flag=True)
@click.option("--json-output", is_flag=True)
def cli(environment, project_dir, project_conf, filter, flags, severity,
silent, verbose, json_output):
def cli(
environment,
project_dir,
project_conf,
filter,
flags,
severity,
silent,
verbose,
json_output,
):
# find project directory on upper level
if isfile(project_dir):
project_dir = find_project_dir_above(project_dir)
@@ -68,15 +79,18 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
results = []
with fs.cd(project_dir):
config = ProjectConfig.get_instance(
project_conf or join(project_dir, "platformio.ini"))
project_conf or join(project_dir, "platformio.ini")
)
config.validate(environment)
default_envs = config.default_envs()
for envname in config.envs():
skipenv = any([
environment and envname not in environment, not environment
and default_envs and envname not in default_envs
])
skipenv = any(
[
environment and envname not in environment,
not environment and default_envs and envname not in default_envs,
]
)
env_options = config.items(env=envname, as_dict=True)
env_dump = []
@@ -84,7 +98,8 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
if k not in ("platform", "framework", "board"):
continue
env_dump.append(
"%s: %s" % (k, ", ".join(v) if isinstance(v, list) else v))
"%s: %s" % (k, ", ".join(v) if isinstance(v, list) else v)
)
default_filter = [
"+<%s/>" % basename(d)
@@ -94,13 +109,12 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
tool_options = dict(
verbose=verbose,
silent=silent,
filter=filter
or env_options.get("check_filter", default_filter),
filter=filter or env_options.get("check_filter", default_filter),
flags=flags or env_options.get("check_flags"),
severity=[
DefectItem.SEVERITY_LABELS[DefectItem.SEVERITY_HIGH]
] if silent else
(severity or env_options.get("check_severity")))
severity=[DefectItem.SEVERITY_LABELS[DefectItem.SEVERITY_HIGH]]
if silent
else (severity or env_options.get("check_severity")),
)
for tool in env_options.get("check_tool", ["cppcheck"]):
if skipenv:
@@ -109,26 +123,29 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
if not silent and not json_output:
print_processing_header(tool, envname, env_dump)
ct = CheckToolFactory.new(tool, project_dir, config, envname,
tool_options)
ct = CheckToolFactory.new(
tool, project_dir, config, envname, tool_options
)
result = {"env": envname, "tool": tool, "duration": time()}
rc = ct.check(on_defect_callback=None if (
json_output or verbose
) else lambda defect: click.echo(repr(defect)))
rc = ct.check(
on_defect_callback=None
if (json_output or verbose)
else lambda defect: click.echo(repr(defect))
)
result['defects'] = ct.get_defects()
result['duration'] = time() - result['duration']
result['succeeded'] = (
rc == 0 and not any(d.severity == DefectItem.SEVERITY_HIGH
for d in result['defects']))
result["defects"] = ct.get_defects()
result["duration"] = time() - result["duration"]
result["succeeded"] = rc == 0 and not any(
d.severity == DefectItem.SEVERITY_HIGH for d in result["defects"]
)
results.append(result)
if verbose:
click.echo("\n".join(repr(d) for d in result['defects']))
click.echo("\n".join(repr(d) for d in result["defects"]))
if not json_output and not silent:
if not result['defects']:
if not result["defects"]:
click.echo("No defects found")
print_processing_footer(result)
@@ -145,11 +162,13 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
def results_to_json(raw):
results = []
for item in raw:
item.update({
"ignored": item.get("succeeded") is None,
"succeeded": bool(item.get("succeeded")),
"defects": [d.to_json() for d in item.get("defects", [])]
})
item.update(
{
"ignored": item.get("succeeded") is None,
"succeeded": bool(item.get("succeeded")),
"defects": [d.to_json() for d in item.get("defects", [])],
}
)
results.append(item)
return results
@@ -157,8 +176,9 @@ def results_to_json(raw):
def print_processing_header(tool, envname, envdump):
click.echo(
"Checking %s > %s (%s)" %
(click.style(envname, fg="cyan", bold=True), tool, "; ".join(envdump)))
"Checking %s > %s (%s)"
% (click.style(envname, fg="cyan", bold=True), tool, "; ".join(envdump))
)
terminal_width, _ = click.get_terminal_size()
click.secho("-" * terminal_width, bold=True)
@@ -166,10 +186,17 @@ def print_processing_header(tool, envname, envdump):
def print_processing_footer(result):
is_failed = not result.get("succeeded")
util.print_labeled_bar(
"[%s] Took %.2f seconds" %
((click.style("FAILED", fg="red", bold=True) if is_failed else
click.style("PASSED", fg="green", bold=True)), result['duration']),
is_error=is_failed)
"[%s] Took %.2f seconds"
% (
(
click.style("FAILED", fg="red", bold=True)
if is_failed
else click.style("PASSED", fg="green", bold=True)
),
result["duration"],
),
is_error=is_failed,
)
def print_defects_stats(results):
@@ -178,8 +205,7 @@ def print_defects_stats(results):
def _append_defect(component, defect):
if not components.get(component):
components[component] = Counter()
components[component].update(
{DefectItem.SEVERITY_LABELS[defect.severity]: 1})
components[component].update({DefectItem.SEVERITY_LABELS[defect.severity]: 1})
for result in results:
for defect in result.get("defects", []):
@@ -235,20 +261,32 @@ def print_check_summary(results):
status_str = click.style("PASSED", fg="green")
tabular_data.append(
(click.style(result['env'], fg="cyan"), result['tool'], status_str,
util.humanize_duration_time(result.get("duration"))))
(
click.style(result["env"], fg="cyan"),
result["tool"],
status_str,
util.humanize_duration_time(result.get("duration")),
)
)
click.echo(tabulate(tabular_data,
headers=[
click.style(s, bold=True)
for s in ("Environment", "Tool", "Status",
"Duration")
]),
err=failed_nums)
click.echo(
tabulate(
tabular_data,
headers=[
click.style(s, bold=True)
for s in ("Environment", "Tool", "Status", "Duration")
],
),
err=failed_nums,
)
util.print_labeled_bar(
"%s%d succeeded in %s" %
("%d failed, " % failed_nums if failed_nums else "", succeeded_nums,
util.humanize_duration_time(duration)),
"%s%d succeeded in %s"
% (
"%d failed, " % failed_nums if failed_nums else "",
succeeded_nums,
util.humanize_duration_time(duration),
),
is_error=failed_nums,
fg="red" if failed_nums else "green")
fg="red" if failed_nums else "green",
)