mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-14 16:34:46 +00:00
Simplify StyleGuide and add handle_error method
This commit is contained in:
parent
1d241a22c9
commit
99b96e95ce
1 changed files with 14 additions and 5 deletions
|
|
@ -43,22 +43,21 @@ Error = collections.namedtuple('Error', ['code',
|
||||||
class StyleGuide(object):
|
class StyleGuide(object):
|
||||||
"""Manage a Flake8 user's style guide."""
|
"""Manage a Flake8 user's style guide."""
|
||||||
|
|
||||||
def __init__(self, options, arguments, checker_plugins, listening_plugins,
|
def __init__(self, options, arguments, listener_trie, formatter):
|
||||||
formatting_plugins):
|
|
||||||
"""Initialize our StyleGuide.
|
"""Initialize our StyleGuide.
|
||||||
|
|
||||||
.. todo:: Add parameter documentation.
|
.. todo:: Add parameter documentation.
|
||||||
"""
|
"""
|
||||||
self.options = options
|
self.options = options
|
||||||
self.arguments = arguments
|
self.arguments = arguments
|
||||||
self.checkers = checker_plugins
|
self.listener = listener_trie
|
||||||
self.listeners = listening_plugins
|
self.formatter = formatter
|
||||||
self.formatters = formatting_plugins
|
|
||||||
self._selected = tuple(options.select)
|
self._selected = tuple(options.select)
|
||||||
self._ignored = tuple(options.ignore)
|
self._ignored = tuple(options.ignore)
|
||||||
self._decision_cache = {}
|
self._decision_cache = {}
|
||||||
|
|
||||||
def is_user_selected(self, code):
|
def is_user_selected(self, code):
|
||||||
|
# type: (Error) -> Union[Selected, Ignored]
|
||||||
"""Determine if the code has been selected by the user.
|
"""Determine if the code has been selected by the user.
|
||||||
|
|
||||||
:param str code:
|
:param str code:
|
||||||
|
|
@ -79,6 +78,7 @@ class StyleGuide(object):
|
||||||
return Ignored.Implicitly
|
return Ignored.Implicitly
|
||||||
|
|
||||||
def is_user_ignored(self, code):
|
def is_user_ignored(self, code):
|
||||||
|
# type: (Error) -> Union[Selected, Ignored]
|
||||||
"""Determine if the code has been ignored by the user.
|
"""Determine if the code has been ignored by the user.
|
||||||
|
|
||||||
:param str code:
|
:param str code:
|
||||||
|
|
@ -96,6 +96,7 @@ class StyleGuide(object):
|
||||||
return Selected.Implicitly
|
return Selected.Implicitly
|
||||||
|
|
||||||
def _decision_for(self, code):
|
def _decision_for(self, code):
|
||||||
|
# type: (Error) -> Decision
|
||||||
startswith = code.startswith
|
startswith = code.startswith
|
||||||
selected = sorted([s for s in self._selected if startswith(s)])[0]
|
selected = sorted([s for s in self._selected if startswith(s)])[0]
|
||||||
ignored = sorted([i for i in self._ignored if startswith(i)])[0]
|
ignored = sorted([i for i in self._ignored if startswith(i)])[0]
|
||||||
|
|
@ -105,6 +106,7 @@ class StyleGuide(object):
|
||||||
return Decision.Ignored
|
return Decision.Ignored
|
||||||
|
|
||||||
def should_report_error(self, code):
|
def should_report_error(self, code):
|
||||||
|
# type: (Error) -> Decision
|
||||||
"""Determine if the error code should be reported or ignored.
|
"""Determine if the error code should be reported or ignored.
|
||||||
|
|
||||||
:param str code:
|
:param str code:
|
||||||
|
|
@ -133,6 +135,13 @@ class StyleGuide(object):
|
||||||
LOG.debug('"%s" will be "%s"', code, decision)
|
LOG.debug('"%s" will be "%s"', code, decision)
|
||||||
return decision
|
return decision
|
||||||
|
|
||||||
|
def handle_error(self, code, filename, line_number, column_number, text):
|
||||||
|
# type: (str, str, int, int, str) -> NoneType
|
||||||
|
"""Handle an error reported by a check."""
|
||||||
|
error = Error(code, filename, line_number, column_number, text)
|
||||||
|
if self.should_report_error(error):
|
||||||
|
self.formatter.handle(error)
|
||||||
|
self.notifier.notify(error.code, error)
|
||||||
|
|
||||||
# Should separate style guide logic from code that runs checks
|
# Should separate style guide logic from code that runs checks
|
||||||
# StyleGuide should manage select/ignore logic as well as include/exclude
|
# StyleGuide should manage select/ignore logic as well as include/exclude
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue