1010
1111"""This module exports the PEP8 plugin linter class."""
1212
13+ import os
14+
15+ try :
16+ from pep8 import StandardReport
17+ except ImportError :
18+ StandardReport = None
19+
1320from SublimeLinter .lint import PythonLinter
1421
1522
23+ if StandardReport is not None :
24+
25+ class Report (StandardReport ):
26+
27+ """Provides a report in the form of a single multiline string, without printing."""
28+
29+ def get_file_results (self ):
30+ """Collect and return the results for this file."""
31+ self ._deferred_print .sort ()
32+ results = ''
33+
34+ for line_number , offset , code , text , doc in self ._deferred_print :
35+ results += '{path}:{row}:{col}: {code} {text}\n ' .format_map ({
36+ 'path' : self .filename ,
37+ 'row' : self .line_offset + line_number ,
38+ 'col' : offset + 1 ,
39+ 'code' : code ,
40+ 'text' : text
41+ })
42+
43+ return results
44+
45+
1646class PEP8 (PythonLinter ):
1747
18- """Provides an interface to the pep8 python script."""
48+ """Provides an interface to the pep8 python module/ script."""
1949
2050 language = 'python'
2151 cmd = ('pep8@python' , '*' , '-' )
@@ -28,3 +58,24 @@ class PEP8(PythonLinter):
2858 }
2959 inline_settings = 'max-line-length'
3060 inline_overrides = ('select' , 'ignore' )
61+ module = 'pep8'
62+
63+ def check (self , code , filename ):
64+ """Run pep8.Checker on code and return the output."""
65+
66+ view_settings = self .get_view_settings ()
67+
68+ kwargs = {
69+ 'select' : view_settings .get ('select' , '' ),
70+ 'ignore' : view_settings .get ('ignore' , '' ),
71+ 'max_line_length' : view_settings .get ('max-line-length' ),
72+ 'reporter' : Report
73+ }
74+
75+ checker = self .module .Checker (
76+ filename = os .path .basename (filename ),
77+ lines = code .splitlines (keepends = True ),
78+ ** kwargs
79+ )
80+
81+ return checker .check_all ()
0 commit comments