Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _run_flake8(module_paths, config_file=None):
flake8_opts = ["--statistics"]
if config_file:
flake8_opts.append("--append-config={}".format(config_file))
flake8_opts.extend(module_paths)
app = application.Application()
app.run(flake8_opts)
try:
app.exit()
except SystemExit:
pass
if app.result_count > 0 or app.catastrophic_failure:
sys.exit(app.result_count or 1)
def test_enable_local_plugin_at_non_installed_path():
"""Can add a paths option in local-plugins config section for finding."""
app = application.Application()
app.initialize(['flake8', '--config', LOCAL_PLUGIN_PATH_CONFIG])
assert app.check_plugins['XE'].plugin.name == 'ExtensionTestPlugin2'
def get_style_guide(argv=None):
# this is a fork of flake8.api.legacy.get_style_guide
# to allow passing command line argument
application = Application()
if not hasattr(application, 'parse_preliminary_options_and_args'): # flake8 >= 3.8
prelim_opts, remaining_args = application.parse_preliminary_options(argv)
config_finder = config.ConfigFileFinder(
application.program,
prelim_opts.append_config,
config_file=prelim_opts.config,
ignore_config_files=prelim_opts.isolated,
)
application.find_plugins(config_finder)
application.register_plugin_options()
application.parse_configuration_and_cli(config_finder, remaining_args)
else:
application.parse_preliminary_options_and_args(argv)
configure_logging(
application.prelim_opts.verbose, application.prelim_opts.output_file)
application.make_config_finder()
def get_style_guide(argv=None):
# this is a fork of flake8.api.legacy.get_style_guide
# to allow passing command line argument
application = Application()
application.parse_preliminary_options_and_args([])
application.make_config_finder()
application.find_plugins()
application.register_plugin_options()
application.parse_configuration_and_cli(argv)
application.make_formatter()
application.make_guide()
application.make_file_checker_manager()
return StyleGuide(application)
def _run_flake8(module_paths, config_file=None):
flake8_opts = ["--statistics"]
if config_file:
flake8_opts.append("--append-config={}".format(config_file))
flake8_opts.extend(module_paths)
app = application.Application()
app.run(flake8_opts)
try:
app.exit()
except SystemExit:
pass
if app.result_count > 0 or app.catastrophic_failure:
sys.exit(app.result_count or 1)
def get_style_guide(**kwargs):
r"""Provision a StyleGuide for use.
:param \*\*kwargs:
Keyword arguments that provide some options for the StyleGuide.
:returns:
An initialized StyleGuide
:rtype:
:class:`StyleGuide`
"""
application = app.Application()
prelim_opts, remaining_args = application.parse_preliminary_options([])
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
config_finder = config.ConfigFileFinder(
application.program, prelim_opts.append_config
)
application.find_plugins(
config_finder, prelim_opts.config, prelim_opts.isolated
)
application.register_plugin_options()
application.parse_configuration_and_cli(config_finder, remaining_args)
# We basically want application.initialize to be called but with these
# options set instead before we make our formatter, notifier, internal
# style guide and file checker manager.
options = application.options
for key, value in kwargs.items():
:param bool lazy:
Find files not added to the index prior to committing. This is useful
if you frequently use ``git commit -a`` for example. This defaults to
False since it will otherwise include files not in the index.
:param bool strict:
If True, return the total number of errors/violations found by Flake8.
This will cause the hook to fail.
:returns:
Total number of errors found during the run.
:rtype:
int
"""
# NOTE(sigmavirus24): Delay import of application until we need it.
from flake8.main import application
app = application.Application()
with make_temporary_directory() as tempdir:
filepaths = list(copy_indexed_files_to(tempdir, lazy))
app.initialize(["."])
app.options.exclude = update_excludes(app.options.exclude, tempdir)
app.options._running_from_vcs = True
# Apparently there are times when there are no files to check (e.g.,
# when amending a commit). In those cases, let's not try to run checks
# against nothing.
if filepaths:
app.run_checks(filepaths)
# If there were files to check, update their paths and report the errors
if filepaths:
update_paths(app.file_checker_manager, tempdir)
app.report_errors()
from flake8.main import options
from flake8.main.application import Application as Flake8Application
from flake8.options import manager
from . import __version__
from . import checker
class Application(Flake8Application):
def __init__(self, program='flake8-rst', version=__version__):
super(Application, self).__init__(program, version)
self.option_manager = manager.OptionManager(
prog=program, version=version
)
self.option_manager.add_option(
'--bootstrap', default=None, parse_from_config=True,
help='Bootstrap code snippets. Useful for add imports.',
)
self.option_manager.add_option(
'--default-groupnames', default="*.rst->*: default", parse_from_config=True,
help='Set default group names.', type='string',
)
options.register_default_options(self.option_manager)