Fill in most of the legacy API

This does not handle setting custom options via the parameters to
get_style_guide.
This commit is contained in:
Ian Cordasco 2016-07-07 13:29:53 -05:00
parent 41cd67f747
commit a4ce229fb6
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A
4 changed files with 153 additions and 59 deletions

View file

@ -3,8 +3,3 @@
This is the only submodule in Flake8 with a guaranteed stable API. All other
submodules are considered internal only and are subject to change.
"""
def get_style_guide(**kwargs):
"""Stub out the only function I'm aware of people using."""
pass

142
src/flake8/api/legacy.py Normal file
View file

@ -0,0 +1,142 @@
"""Module containing shims around Flake8 2.0 behaviour."""
import os.path
from flake8.formatting import base as formatter
from flake8.main import application as app
def get_style_guide(**kwargs):
"""Stub out the only function I'm aware of people using."""
application = app.Application()
application.initialize([])
return StyleGuide(application)
class StyleGuide(object):
"""Public facing object that mimic's Flake8 2.0's StyleGuide.
.. note::
There are important changes in how this object behaves compared to
the StyleGuide object provided in Flake8 2.x.
.. warning::
This object should not be instantiated directly by users.
.. versionchanged:: 3.0.0
"""
def __init__(self, application):
"""Initialize our StyleGuide."""
self._application = application
self._file_checker_manager = application.file_checker_manager
@property
def options(self):
"""The parsed options.
An instance of :class:`optparse.Values` containing parsed options.
"""
return self._application.options
@property
def paths(self):
"""The extra arguments passed as paths."""
return self._application.paths
def check_files(self, paths=None):
"""Run collected checks on the files provided.
This will check the files passed in and return a :class:`Report`
instance.
:param list paths:
List of filenames (or paths) to check.
:returns:
Object that mimic's Flake8 2.0's Reporter class.
:rtype:
flake8.api.legacy.Report
"""
self._application.run_checks(paths)
self._application.report_errors()
return Report(self._application)
def excluded(self, filename, parent=None):
"""Determine if a file is excluded.
:param str filename:
Path to the file to check if it is excluded.
:param str parent:
Name of the parent directory containing the file.
:returns:
True if the filename is excluded, False otherwise.
:rtype:
bool
"""
return (self._file_checker_manager.is_path_excluded(filename) or
(parent and
self._file_checker_manager.is_path_excluded(
os.path.join(parent, filename))))
def init_report(self, reporter=None):
"""Set up a formatter for this run of Flake8."""
if (reporter is not None and
not issubclass(reporter, formatter.BaseFormatter)):
raise ValueError("Report should be subclass of "
"flake8.formatter.BaseFormatter.")
self._application.make_formatter(reporter)
self._application.guide = None
# NOTE(sigmavirus24): This isn't the intended use of
# Application#make_guide but it works pretty well.
# Stop cringing... I know it's gross.
self._application.make_guide()
def input_file(self, filename, lines=None, expected=None, line_offset=0):
"""Run collected checks on a single file.
This will check the file passed in and return a :class:`Report`
instance.
:param str filename:
The path to the file to check.
:param list lines:
Ignored since Flake8 3.0.
:param expected:
Ignored since Flake8 3.0.
:param int line_offset:
Ignored since Flake8 3.0.
:returns:
Object that mimic's Flake8 2.0's Reporter class.
:rtype:
flake8.api.legacy.Report
"""
return self.check_files([filename])
class Report(object):
"""Public facing object that mimic's Flake8 2.0's API.
.. note::
There are important changes in how this object behaves compared to
the object provided in Flake8 2.x.
.. versionchanged:: 3.0.0
"""
def __init__(self, application):
"""Initialize the Report for the user.
.. warning:: This should not be instantiated by users.
"""
self._application = application
@property
def total_errors(self):
"""The total number of errors found by Flake8."""
return self._application.result_count
def get_statistics(self, violation):
"""Get the number of occurences of a violation."""
raise NotImplementedError('Statistics capturing needs to happen first')

View file

@ -1,47 +0,0 @@
"""Module containing shims around Flake8 2.0 behaviour."""
import os.path
from flake8.formatting import base as formatter
class LegacyStyleGuide(object):
"""Public facing object that mimic's Flake8 2.0's StyleGuide."""
def __init__(self, application):
self._application = application
self._file_checker_manager = application.file_checker_manager
@property
def options(self):
"""The parsed options.
An instance of :class:`optparse.Values` containing parsed options.
"""
return self._application.options
@property
def paths(self):
"""The extra arguments passed as paths."""
return self._application.paths
def check_files(self, paths=None):
raise NotImplementedError('This should be easy')
def excluded(self, filename, parent=None):
return (self._file_checker_manager.is_path_excluded(filename) or
(parent and
self._file_checker_manager.is_path_excluded(
os.path.join(parent, filename))))
def init_report(self, reporter=None):
if (reporter is not None and
isinstance(reporter, formatter.BaseFormatter)):
self._application.formatter = reporter
self._application.guide = None
# NOTE(sigmavirus24): This isn't the intended use of
# Application#make_guide but it works pretty well.
# Stop cringing... I know it's gross.
self._application.make_guide()
def input_file(self, filename, lines=None, expected=None, line_offset=0):
raise NotImplementedError('This should be a pain')

View file

@ -177,13 +177,15 @@ class Application(object):
self.options,
self.args)
def make_formatter(self):
def make_formatter(self, formatter_class=None):
# type: () -> NoneType
"""Initialize a formatter based on the parsed options."""
if self.formatter is None:
self.formatter = self.formatting_plugins.get(
self.options.format, self.formatting_plugins['default']
).execute(self.options)
if formatter_class is None:
formatter_class = self.formatting_plugins.get(
self.options.format, self.formatting_plugins['default']
).execute
self.formatter = formatter_class(self.options)
def make_notifier(self):
# type: () -> NoneType
@ -212,15 +214,17 @@ class Application(object):
checker_plugins=self.check_plugins,
)
def run_checks(self):
# type: () -> NoneType
def run_checks(self, files=None):
# type: (Union[List[str], NoneType]) -> NoneType
"""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.
:param list files:
List of filenames to process
"""
files = None
if self.running_against_diff:
files = list(sorted(self.parsed_diff.keys()))
self.file_checker_manager.start(files)