mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-12 07:44:16 +00:00
Propagate the stdin_display_name to checker and processor
This way plugins like flake8-putty can have access to the correct filename.
This commit is contained in:
parent
b2b4cae8e3
commit
7934f8dce2
5 changed files with 21 additions and 15 deletions
|
|
@ -398,20 +398,20 @@ class FileChecker(object):
|
||||||
:type style_guide:
|
:type style_guide:
|
||||||
flake8.style_guide.StyleGuide
|
flake8.style_guide.StyleGuide
|
||||||
"""
|
"""
|
||||||
self.filename = filename
|
|
||||||
self.checks = checks
|
self.checks = checks
|
||||||
self.style_guide = style_guide
|
self.style_guide = style_guide
|
||||||
self.results = []
|
self.results = []
|
||||||
self.processor = self._make_processor()
|
self.processor = self._make_processor(filename)
|
||||||
|
self.filename = self.processor.filename
|
||||||
self.statistics = {
|
self.statistics = {
|
||||||
'tokens': 0,
|
'tokens': 0,
|
||||||
'logical lines': 0,
|
'logical lines': 0,
|
||||||
'physical lines': len(self.processor.lines),
|
'physical lines': len(self.processor.lines),
|
||||||
}
|
}
|
||||||
|
|
||||||
def _make_processor(self):
|
def _make_processor(self, filename):
|
||||||
try:
|
try:
|
||||||
return processor.FileProcessor(self.filename,
|
return processor.FileProcessor(filename,
|
||||||
self.style_guide.options)
|
self.style_guide.options)
|
||||||
except IOError:
|
except IOError:
|
||||||
# If we can not read the file due to an IOError (e.g., the file
|
# If we can not read the file due to an IOError (e.g., the file
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ def register_default_options(option_manager):
|
||||||
)
|
)
|
||||||
|
|
||||||
add_option(
|
add_option(
|
||||||
'--stdin-display-name', default='stdin',
|
'--stdin-display-name',
|
||||||
help='The name used when reporting errors from code passed via stdin.'
|
help='The name used when reporting errors from code passed via stdin.'
|
||||||
' This is useful for editors piping the file contents to flake8.'
|
' This is useful for editors piping the file contents to flake8.'
|
||||||
' (Default: %default)',
|
' (Default: %default)',
|
||||||
|
|
|
||||||
|
|
@ -55,12 +55,13 @@ class FileProcessor(object):
|
||||||
:param str filename:
|
:param str filename:
|
||||||
Name of the file to process
|
Name of the file to process
|
||||||
"""
|
"""
|
||||||
|
self.options = options
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.lines = lines
|
self.lines = lines
|
||||||
if lines is None:
|
if lines is None:
|
||||||
self.lines = self.read_lines()
|
# allow for stdin filename substitution
|
||||||
|
self.filename, self.lines = self.read_lines(filename)
|
||||||
self.strip_utf_bom()
|
self.strip_utf_bom()
|
||||||
self.options = options
|
|
||||||
|
|
||||||
# Defaults for public attributes
|
# Defaults for public attributes
|
||||||
#: Number of preceding blank lines
|
#: Number of preceding blank lines
|
||||||
|
|
@ -268,13 +269,15 @@ class FileProcessor(object):
|
||||||
self.indent_char = line[0]
|
self.indent_char = line[0]
|
||||||
return line
|
return line
|
||||||
|
|
||||||
def read_lines(self):
|
def read_lines(self, filename):
|
||||||
# type: () -> List[str]
|
# type: () -> List[str]
|
||||||
"""Read the lines for this file checker."""
|
"""Read the lines for this file checker."""
|
||||||
if self.filename is None or self.filename == '-':
|
if filename is None or filename == '-':
|
||||||
self.filename = 'stdin'
|
filename = self.options.stdin_display_name or 'stdin'
|
||||||
return self.read_lines_from_stdin()
|
lines = self.read_lines_from_stdin()
|
||||||
return self.read_lines_from_filename()
|
else:
|
||||||
|
lines = self.read_lines_from_filename()
|
||||||
|
return (filename, lines)
|
||||||
|
|
||||||
def _readlines_py2(self):
|
def _readlines_py2(self):
|
||||||
# type: () -> List[str]
|
# type: () -> List[str]
|
||||||
|
|
|
||||||
|
|
@ -260,8 +260,6 @@ class StyleGuide(object):
|
||||||
"""
|
"""
|
||||||
error = Error(code, filename, line_number, column_number, text,
|
error = Error(code, filename, line_number, column_number, text,
|
||||||
physical_line)
|
physical_line)
|
||||||
if error.filename is None or error.filename == '-':
|
|
||||||
error = error._replace(filename=self.options.stdin_display_name)
|
|
||||||
error_is_selected = (self.should_report_error(error.code) is
|
error_is_selected = (self.should_report_error(error.code) is
|
||||||
Decision.Selected)
|
Decision.Selected)
|
||||||
is_not_inline_ignored = self.is_inline_ignored(error) is False
|
is_not_inline_ignored = self.is_inline_ignored(error) is False
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ def options_from(**kwargs):
|
||||||
kwargs.setdefault('hang_closing', True)
|
kwargs.setdefault('hang_closing', True)
|
||||||
kwargs.setdefault('max_line_length', 79)
|
kwargs.setdefault('max_line_length', 79)
|
||||||
kwargs.setdefault('verbose', False)
|
kwargs.setdefault('verbose', False)
|
||||||
|
kwargs.setdefault('stdin_display_name', None)
|
||||||
return optparse.Values(kwargs)
|
return optparse.Values(kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -63,13 +64,17 @@ def test_read_lines_from_stdin(stdin_get_value):
|
||||||
|
|
||||||
|
|
||||||
@mock.patch('flake8.utils.stdin_get_value')
|
@mock.patch('flake8.utils.stdin_get_value')
|
||||||
def test_read_lines_sets_filename_attribute(stdin_get_value):
|
def test_stdin_filename_attribute(stdin_get_value):
|
||||||
"""Verify that we update the filename attribute."""
|
"""Verify that we update the filename attribute."""
|
||||||
stdin_value = mock.Mock()
|
stdin_value = mock.Mock()
|
||||||
stdin_value.splitlines.return_value = []
|
stdin_value.splitlines.return_value = []
|
||||||
stdin_get_value.return_value = stdin_value
|
stdin_get_value.return_value = stdin_value
|
||||||
file_processor = processor.FileProcessor('-', options_from())
|
file_processor = processor.FileProcessor('-', options_from())
|
||||||
assert file_processor.filename == 'stdin'
|
assert file_processor.filename == 'stdin'
|
||||||
|
file_processor = processor.FileProcessor('-', options_from(
|
||||||
|
stdin_display_name="foo.py"
|
||||||
|
))
|
||||||
|
assert file_processor.filename == 'foo.py'
|
||||||
|
|
||||||
|
|
||||||
def test_line_for():
|
def test_line_for():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue