Skip to content

Commit 7d3d6b3

Browse files
committed
Remove cruft
* Never use builtin python 3.3 for linting Fixes SublimeLinter#33 * Make `show-code` actually work Fixes SublimeLinter#35 * Add option `executable` so you can point explicitly to a flake8 installation in case SL's which isn't smart enough (which it isn't on Windows)
1 parent 9d3426d commit 7d3d6b3

File tree

1 file changed

+14
-104
lines changed

1 file changed

+14
-104
lines changed

linter.py

Lines changed: 14 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
"""This module exports the Flake8 plugin linter class."""
1313

14-
import os
15-
from SublimeLinter.lint import persist, PythonLinter
14+
from SublimeLinter.lint import PythonLinter
1615

1716

1817
class Flake8(PythonLinter):
@@ -56,83 +55,11 @@ class Flake8(PythonLinter):
5655
'--max-line-length=': None,
5756
'--max-complexity=': -1,
5857
'--jobs=': '1',
59-
'--show-code=': False
58+
'show-code': False,
59+
'executable': ''
6060
}
61-
inline_settings = ('max-line-length', 'max-complexity', 'show_code')
61+
inline_settings = ('max-line-length', 'max-complexity')
6262
inline_overrides = ('select', 'ignore', 'builtins')
63-
module = 'flake8.engine'
64-
check_version = True
65-
66-
# Internal
67-
report = None
68-
show_code = False
69-
pyflakes_checker_module = None
70-
pyflakes_checker_class = None
71-
72-
@classmethod
73-
def initialize(cls):
74-
"""Initialize the class after plugin load."""
75-
76-
super().initialize()
77-
78-
if cls.module is None:
79-
return
80-
81-
# This is tricky. Unfortunately pyflakes chooses to store
82-
# builtins in a class variable and union that with the builtins option
83-
# on every execution. This results in the builtins never being removed.
84-
# To fix that, we get a reference to the pyflakes.checker module and
85-
# pyflakes.checker.Checker class used by flake8. We can then reset
86-
# the Checker.builtIns class variable on each execution.
87-
88-
try:
89-
from pkg_resources import iter_entry_points
90-
except ImportError:
91-
persist.printf('WARNING: {} could not import pkg_resources.iter_entry_points'.format(cls.name))
92-
else:
93-
for entry in iter_entry_points('flake8.extension'):
94-
check = entry.load()
95-
96-
if check.name == 'pyflakes':
97-
from pyflakes import checker
98-
cls.pyflakes_checker_module = checker
99-
cls.pyflakes_checker_class = check
100-
break
101-
102-
def check(self, code, filename):
103-
"""Run flake8 on code and return the output."""
104-
105-
options = {
106-
'reporter': self.get_report(),
107-
'jobs': '1' # No multiprocessing
108-
}
109-
110-
type_map = {
111-
'select': [],
112-
'ignore': [],
113-
'builtins': '',
114-
'max-line-length': 0,
115-
'max-complexity': 0,
116-
'show-code': False
117-
}
118-
119-
self.build_options(options, type_map, transform=lambda s: s.replace('-', '_'))
120-
self.show_code = options.pop('show_code', False)
121-
122-
if persist.debug_mode():
123-
persist.printf('{} options: {}'.format(self.name, options))
124-
125-
if self.pyflakes_checker_class is not None:
126-
# Reset the builtins to the initial value used by pyflakes.
127-
builtins = set(self.pyflakes_checker_module.builtin_vars).union(self.pyflakes_checker_module._MAGIC_GLOBALS)
128-
self.pyflakes_checker_class.builtIns = builtins
129-
130-
linter = self.module.get_style_guide(**options)
131-
132-
return linter.input_file(
133-
filename=os.path.basename(filename),
134-
lines=code.splitlines(keepends=True)
135-
)
13663

13764
def split_match(self, match):
13865
"""
@@ -148,34 +75,17 @@ def split_match(self, match):
14875
if near:
14976
col = None
15077

151-
if self.show_code:
78+
if self.get_view_settings().get('show-code'):
15279
message = ' '.join([error or warning or '', message])
15380
return match, line, col, error, warning, message, near
15481

155-
def get_report(self):
156-
"""Return the Report class for use by flake8."""
157-
if self.report is None:
158-
from pep8 import StandardReport
159-
160-
class Report(StandardReport):
161-
"""Provides a report in the form of a single multiline string, without printing."""
82+
def build_cmd(self, cmd=None):
83+
"""Return a tuple with the command line to execute."""
16284

163-
def get_file_results(self):
164-
"""Collect and return the results for this file."""
165-
self._deferred_print.sort()
166-
results = ''
167-
168-
for line_number, offset, code, text, doc in self._deferred_print:
169-
results += '{path}:{row}:{col}: {code} {text}\n'.format_map({
170-
'path': self.filename,
171-
'row': self.line_offset + line_number,
172-
'col': offset + 1,
173-
'code': code,
174-
'text': text
175-
})
176-
177-
return results
178-
179-
self.__class__.report = Report
180-
181-
return self.report
85+
executable = self.get_view_settings().get('executable', None)
86+
if executable:
87+
args = (cmd or self.cmd)[1:]
88+
cmd = (executable, ) + args
89+
return self.insert_args(cmd)
90+
else:
91+
return super().build_cmd(cmd)

0 commit comments

Comments
 (0)