okay fine

This commit is contained in:
pacnpal
2024-11-03 17:47:26 +00:00
parent 01c6004a79
commit 27eb239e97
10020 changed files with 1935769 additions and 2364 deletions

View File

@@ -0,0 +1,2 @@
"""Module containing the logic for the Flake8 entry-points."""
from __future__ import annotations

View File

@@ -0,0 +1,215 @@
"""Module containing the application logic for Flake8."""
from __future__ import annotations
import argparse
import json
import logging
import time
from typing import Sequence
import flake8
from flake8 import checker
from flake8 import defaults
from flake8 import exceptions
from flake8 import style_guide
from flake8.formatting.base import BaseFormatter
from flake8.main import debug
from flake8.options.parse_args import parse_args
from flake8.plugins import finder
from flake8.plugins import reporter
LOG = logging.getLogger(__name__)
class Application:
"""Abstract our application into a class."""
def __init__(self) -> None:
"""Initialize our application."""
#: The timestamp when the Application instance was instantiated.
self.start_time = time.time()
#: The timestamp when the Application finished reported errors.
self.end_time: float | None = None
self.plugins: finder.Plugins | None = None
#: The user-selected formatter from :attr:`formatting_plugins`
self.formatter: BaseFormatter | None = None
#: The :class:`flake8.style_guide.StyleGuideManager` built from the
#: user's options
self.guide: style_guide.StyleGuideManager | None = None
#: The :class:`flake8.checker.Manager` that will handle running all of
#: the checks selected by the user.
self.file_checker_manager: checker.Manager | None = None
#: The user-supplied options parsed into an instance of
#: :class:`argparse.Namespace`
self.options: argparse.Namespace | None = None
#: The number of errors, warnings, and other messages after running
#: flake8 and taking into account ignored errors and lines.
self.result_count = 0
#: The total number of errors before accounting for ignored errors and
#: lines.
self.total_result_count = 0
#: Whether or not something catastrophic happened and we should exit
#: with a non-zero status code
self.catastrophic_failure = False
def exit_code(self) -> int:
"""Return the program exit code."""
if self.catastrophic_failure:
return 1
assert self.options is not None
if self.options.exit_zero:
return 0
else:
return int(self.result_count > 0)
def make_formatter(self) -> None:
"""Initialize a formatter based on the parsed options."""
assert self.plugins is not None
assert self.options is not None
self.formatter = reporter.make(self.plugins.reporters, self.options)
def make_guide(self) -> None:
"""Initialize our StyleGuide."""
assert self.formatter is not None
assert self.options is not None
self.guide = style_guide.StyleGuideManager(
self.options, self.formatter
)
def make_file_checker_manager(self, argv: Sequence[str]) -> None:
"""Initialize our FileChecker Manager."""
assert self.guide is not None
assert self.plugins is not None
self.file_checker_manager = checker.Manager(
style_guide=self.guide,
plugins=self.plugins.checkers,
argv=argv,
)
def run_checks(self) -> None:
"""Run the actual checks with the FileChecker Manager.
This method encapsulates the logic to make a
:class:`~flake8.checker.Manger` instance run the checks it is
managing.
"""
assert self.file_checker_manager is not None
self.file_checker_manager.start()
try:
self.file_checker_manager.run()
except exceptions.PluginExecutionFailed as plugin_failed:
print(str(plugin_failed))
print("Run flake8 with greater verbosity to see more details")
self.catastrophic_failure = True
LOG.info("Finished running")
self.file_checker_manager.stop()
self.end_time = time.time()
def report_benchmarks(self) -> None:
"""Aggregate, calculate, and report benchmarks for this run."""
assert self.options is not None
if not self.options.benchmark:
return
assert self.file_checker_manager is not None
assert self.end_time is not None
time_elapsed = self.end_time - self.start_time
statistics = [("seconds elapsed", time_elapsed)]
add_statistic = statistics.append
for statistic in defaults.STATISTIC_NAMES + ("files",):
value = self.file_checker_manager.statistics[statistic]
total_description = f"total {statistic} processed"
add_statistic((total_description, value))
per_second_description = f"{statistic} processed per second"
add_statistic((per_second_description, int(value / time_elapsed)))
assert self.formatter is not None
self.formatter.show_benchmarks(statistics)
def report_errors(self) -> None:
"""Report all the errors found by flake8 3.0.
This also updates the :attr:`result_count` attribute with the total
number of errors, warnings, and other messages found.
"""
LOG.info("Reporting errors")
assert self.file_checker_manager is not None
results = self.file_checker_manager.report()
self.total_result_count, self.result_count = results
LOG.info(
"Found a total of %d violations and reported %d",
self.total_result_count,
self.result_count,
)
def report_statistics(self) -> None:
"""Aggregate and report statistics from this run."""
assert self.options is not None
if not self.options.statistics:
return
assert self.formatter is not None
assert self.guide is not None
self.formatter.show_statistics(self.guide.stats)
def initialize(self, argv: Sequence[str]) -> None:
"""Initialize the application to be run.
This finds the plugins, registers their options, and parses the
command-line arguments.
"""
self.plugins, self.options = parse_args(argv)
if self.options.bug_report:
info = debug.information(flake8.__version__, self.plugins)
print(json.dumps(info, indent=2, sort_keys=True))
raise SystemExit(0)
self.make_formatter()
self.make_guide()
self.make_file_checker_manager(argv)
def report(self) -> None:
"""Report errors, statistics, and benchmarks."""
assert self.formatter is not None
self.formatter.start()
self.report_errors()
self.report_statistics()
self.report_benchmarks()
self.formatter.stop()
def _run(self, argv: Sequence[str]) -> None:
self.initialize(argv)
self.run_checks()
self.report()
def run(self, argv: Sequence[str]) -> None:
"""Run our application.
This method will also handle KeyboardInterrupt exceptions for the
entirety of the flake8 application. If it sees a KeyboardInterrupt it
will forcibly clean up the :class:`~flake8.checker.Manager`.
"""
try:
self._run(argv)
except KeyboardInterrupt as exc:
print("... stopped")
LOG.critical("Caught keyboard interrupt from user")
LOG.exception(exc)
self.catastrophic_failure = True
except exceptions.ExecutionError as exc:
print("There was a critical error during execution of Flake8:")
print(exc)
LOG.exception(exc)
self.catastrophic_failure = True
except exceptions.EarlyQuit:
self.catastrophic_failure = True
print("... stopped while processing files")
else:
assert self.options is not None
if self.options.count:
print(self.result_count)

View File

@@ -0,0 +1,24 @@
"""Command-line implementation of flake8."""
from __future__ import annotations
import sys
from typing import Sequence
from flake8.main import application
def main(argv: Sequence[str] | None = None) -> int:
"""Execute the main bit of the application.
This handles the creation of an instance of :class:`Application`, runs it,
and then exits the application.
:param argv:
The arguments to be passed to the application for parsing.
"""
if argv is None:
argv = sys.argv[1:]
app = application.Application()
app.run(argv)
return app.exit_code()

View File

@@ -0,0 +1,30 @@
"""Module containing the logic for our debugging logic."""
from __future__ import annotations
import platform
from typing import Any
from flake8.plugins.finder import Plugins
def information(version: str, plugins: Plugins) -> dict[str, Any]:
"""Generate the information to be printed for the bug report."""
versions = sorted(
{
(loaded.plugin.package, loaded.plugin.version)
for loaded in plugins.all_plugins()
if loaded.plugin.package not in {"flake8", "local"}
}
)
return {
"version": version,
"plugins": [
{"plugin": plugin, "version": version}
for plugin, version in versions
],
"platform": {
"python_implementation": platform.python_implementation(),
"python_version": platform.python_version(),
"system": platform.system(),
},
}

View File

@@ -0,0 +1,396 @@
"""Contains the logic for all of the default options for Flake8."""
from __future__ import annotations
import argparse
from flake8 import defaults
from flake8.options.manager import OptionManager
def stage1_arg_parser() -> argparse.ArgumentParser:
"""Register the preliminary options on our OptionManager.
The preliminary options include:
- ``-v``/``--verbose``
- ``--output-file``
- ``--append-config``
- ``--config``
- ``--isolated``
- ``--enable-extensions``
"""
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(
"-v",
"--verbose",
default=0,
action="count",
help="Print more information about what is happening in flake8. "
"This option is repeatable and will increase verbosity each "
"time it is repeated.",
)
parser.add_argument(
"--output-file", default=None, help="Redirect report to a file."
)
# Config file options
parser.add_argument(
"--append-config",
action="append",
default=[],
help="Provide extra config files to parse in addition to the files "
"found by Flake8 by default. These files are the last ones read "
"and so they take the highest precedence when multiple files "
"provide the same option.",
)
parser.add_argument(
"--config",
default=None,
help="Path to the config file that will be the authoritative config "
"source. This will cause Flake8 to ignore all other "
"configuration files.",
)
parser.add_argument(
"--isolated",
default=False,
action="store_true",
help="Ignore all configuration files.",
)
# Plugin enablement options
parser.add_argument(
"--enable-extensions",
help="Enable plugins and extensions that are otherwise disabled "
"by default",
)
parser.add_argument(
"--require-plugins",
help="Require specific plugins to be installed before running",
)
return parser
class JobsArgument:
"""Type callback for the --jobs argument."""
def __init__(self, arg: str) -> None:
"""Parse and validate the --jobs argument.
:param arg: The argument passed by argparse for validation
"""
self.is_auto = False
self.n_jobs = -1
if arg == "auto":
self.is_auto = True
elif arg.isdigit():
self.n_jobs = int(arg)
else:
raise argparse.ArgumentTypeError(
f"{arg!r} must be 'auto' or an integer.",
)
def __repr__(self) -> str:
"""Representation for debugging."""
return f"{type(self).__name__}({str(self)!r})"
def __str__(self) -> str:
"""Format our JobsArgument class."""
return "auto" if self.is_auto else str(self.n_jobs)
def register_default_options(option_manager: OptionManager) -> None:
"""Register the default options on our OptionManager.
The default options include:
- ``-q``/``--quiet``
- ``--color``
- ``--count``
- ``--exclude``
- ``--extend-exclude``
- ``--filename``
- ``--format``
- ``--hang-closing``
- ``--ignore``
- ``--extend-ignore``
- ``--per-file-ignores``
- ``--max-line-length``
- ``--max-doc-length``
- ``--indent-size``
- ``--select``
- ``--extend-select``
- ``--disable-noqa``
- ``--show-source``
- ``--statistics``
- ``--exit-zero``
- ``-j``/``--jobs``
- ``--tee``
- ``--benchmark``
- ``--bug-report``
"""
add_option = option_manager.add_option
add_option(
"-q",
"--quiet",
default=0,
action="count",
parse_from_config=True,
help="Report only file names, or nothing. This option is repeatable.",
)
add_option(
"--color",
choices=("auto", "always", "never"),
default="auto",
help="Whether to use color in output. Defaults to `%(default)s`.",
)
add_option(
"--count",
action="store_true",
parse_from_config=True,
help="Print total number of errors to standard output after "
"all other output.",
)
add_option(
"--exclude",
metavar="patterns",
default=",".join(defaults.EXCLUDE),
comma_separated_list=True,
parse_from_config=True,
normalize_paths=True,
help="Comma-separated list of files or directories to exclude. "
"(Default: %(default)s)",
)
add_option(
"--extend-exclude",
metavar="patterns",
default="",
parse_from_config=True,
comma_separated_list=True,
normalize_paths=True,
help="Comma-separated list of files or directories to add to the list "
"of excluded ones.",
)
add_option(
"--filename",
metavar="patterns",
default="*.py",
parse_from_config=True,
comma_separated_list=True,
help="Only check for filenames matching the patterns in this comma-"
"separated list. (Default: %(default)s)",
)
add_option(
"--stdin-display-name",
default="stdin",
help="The name used when reporting errors from code passed via stdin. "
"This is useful for editors piping the file contents to flake8. "
"(Default: %(default)s)",
)
# TODO(sigmavirus24): Figure out --first/--repeat
# NOTE(sigmavirus24): We can't use choices for this option since users can
# freely provide a format string and that will break if we restrict their
# choices.
add_option(
"--format",
metavar="format",
default="default",
parse_from_config=True,
help=(
f"Format errors according to the chosen formatter "
f"({', '.join(sorted(option_manager.formatter_names))}) "
f"or a format string containing %%-style "
f"mapping keys (code, col, path, row, text). "
f"For example, "
f"``--format=pylint`` or ``--format='%%(path)s %%(code)s'``. "
f"(Default: %(default)s)"
),
)
add_option(
"--hang-closing",
action="store_true",
parse_from_config=True,
help="Hang closing bracket instead of matching indentation of opening "
"bracket's line.",
)
add_option(
"--ignore",
metavar="errors",
parse_from_config=True,
comma_separated_list=True,
help=(
f"Comma-separated list of error codes to ignore (or skip). "
f"For example, ``--ignore=E4,E51,W234``. "
f"(Default: {','.join(defaults.IGNORE)})"
),
)
add_option(
"--extend-ignore",
metavar="errors",
parse_from_config=True,
comma_separated_list=True,
help="Comma-separated list of error codes to add to the list of "
"ignored ones. For example, ``--extend-ignore=E4,E51,W234``.",
)
add_option(
"--per-file-ignores",
default="",
parse_from_config=True,
help="A pairing of filenames and violation codes that defines which "
"violations to ignore in a particular file. The filenames can be "
"specified in a manner similar to the ``--exclude`` option and the "
"violations work similarly to the ``--ignore`` and ``--select`` "
"options.",
)
add_option(
"--max-line-length",
type=int,
metavar="n",
default=defaults.MAX_LINE_LENGTH,
parse_from_config=True,
help="Maximum allowed line length for the entirety of this run. "
"(Default: %(default)s)",
)
add_option(
"--max-doc-length",
type=int,
metavar="n",
default=None,
parse_from_config=True,
help="Maximum allowed doc line length for the entirety of this run. "
"(Default: %(default)s)",
)
add_option(
"--indent-size",
type=int,
metavar="n",
default=defaults.INDENT_SIZE,
parse_from_config=True,
help="Number of spaces used for indentation (Default: %(default)s)",
)
add_option(
"--select",
metavar="errors",
parse_from_config=True,
comma_separated_list=True,
help=(
"Limit the reported error codes to codes prefix-matched by this "
"list. "
"You usually do not need to specify this option as the default "
"includes all installed plugin codes. "
"For example, ``--select=E4,E51,W234``."
),
)
add_option(
"--extend-select",
metavar="errors",
parse_from_config=True,
comma_separated_list=True,
help=(
"Add additional error codes to the default ``--select``. "
"You usually do not need to specify this option as the default "
"includes all installed plugin codes. "
"For example, ``--extend-select=E4,E51,W234``."
),
)
add_option(
"--disable-noqa",
default=False,
parse_from_config=True,
action="store_true",
help='Disable the effect of "# noqa". This will report errors on '
'lines with "# noqa" at the end.',
)
# TODO(sigmavirus24): Decide what to do about --show-pep8
add_option(
"--show-source",
action="store_true",
parse_from_config=True,
help="Show the source generate each error or warning.",
)
add_option(
"--no-show-source",
action="store_false",
dest="show_source",
parse_from_config=False,
help="Negate --show-source",
)
add_option(
"--statistics",
action="store_true",
parse_from_config=True,
help="Count errors.",
)
# Flake8 options
add_option(
"--exit-zero",
action="store_true",
help='Exit with status code "0" even if there are errors.',
)
add_option(
"-j",
"--jobs",
default="auto",
parse_from_config=True,
type=JobsArgument,
help="Number of subprocesses to use to run checks in parallel. "
'This is ignored on Windows. The default, "auto", will '
"auto-detect the number of processors available to use. "
"(Default: %(default)s)",
)
add_option(
"--tee",
default=False,
parse_from_config=True,
action="store_true",
help="Write to stdout and output-file.",
)
# Benchmarking
add_option(
"--benchmark",
default=False,
action="store_true",
help="Print benchmark information about this run of Flake8",
)
# Debugging
add_option(
"--bug-report",
action="store_true",
help="Print information necessary when preparing a bug report",
)