summaryrefslogtreecommitdiff
path: root/src/flake8/api
diff options
context:
space:
mode:
authorIan Cordasco <graffatcolmingov@gmail.com>2016-07-07 13:29:53 -0500
committerIan Cordasco <graffatcolmingov@gmail.com>2016-07-07 13:29:53 -0500
commita4ce229fb6493c11fd8ed850d5c07a5964fade7d (patch)
treefb536e5f0dd4e9f7368291ef813a4cc043541a4a /src/flake8/api
parent41cd67f747bac544b023e56134a43f5ea9c0c896 (diff)
downloadflake8-a4ce229fb6493c11fd8ed850d5c07a5964fade7d.tar.gz
Fill in most of the legacy API
This does not handle setting custom options via the parameters to get_style_guide.
Diffstat (limited to 'src/flake8/api')
-rw-r--r--src/flake8/api/__init__.py5
-rw-r--r--src/flake8/api/legacy.py142
-rw-r--r--src/flake8/api/style_guide.py47
3 files changed, 142 insertions, 52 deletions
diff --git a/src/flake8/api/__init__.py b/src/flake8/api/__init__.py
index 9f95557..c2eefbe 100644
--- a/src/flake8/api/__init__.py
+++ b/src/flake8/api/__init__.py
@@ -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
diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py
new file mode 100644
index 0000000..42e83c3
--- /dev/null
+++ b/src/flake8/api/legacy.py
@@ -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')
diff --git a/src/flake8/api/style_guide.py b/src/flake8/api/style_guide.py
deleted file mode 100644
index 4e8595c..0000000
--- a/src/flake8/api/style_guide.py
+++ /dev/null
@@ -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')