Skip to content

Commit abd2460

Browse files
authored
Merge pull request #14 from brbsix/full-module
Use pydocstyle as a module
2 parents d712d4f + d3042df commit abd2460

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

linter.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,75 @@
1111

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

14-
from SublimeLinter.lint import PythonLinter, highlight, util
14+
from contextlib import contextmanager
15+
from functools import partial
16+
from SublimeLinter.lint import PythonLinter, highlight, persist, util
1517

1618

1719
class Pydocstyle(PythonLinter):
1820
"""Provides an interface to the pydocstyle python module/script."""
1921

2022
syntax = 'python'
21-
if PythonLinter.which('pydocstyle'):
22-
cmd = 'pydocstyle@python'
23-
else:
24-
cmd = 'pep257@python'
23+
cmd = 'pydocstyle@python'
2524
version_args = '--version'
2625
version_re = r'(?P<version>\d+\.\d+\.\d+)'
2726
version_requirement = '>= 0.3.0'
2827
regex = r'^.+?:(?P<line>\d+).*:\r?\n\s*(?P<message>.+)$'
2928
multiline = True
3029
default_type = highlight.WARNING
3130
error_stream = util.STREAM_STDERR
32-
line_col_base = (0, 0) # pydocstyle uses one-based line and zero-based column numbers
31+
line_col_base = (1, 0) # uses one-based line and zero-based column numbers
3332
tempfile_suffix = 'py'
33+
module = 'pydocstyle'
3434
defaults = {
35-
'--add-ignore=': ''
35+
'--add-ignore=': '',
36+
'--add-select=': '',
37+
'--ignore=': '',
38+
'--select=': '',
39+
'--config=': '',
40+
'--convention=': '',
41+
'--ignore-decorators=': ''
3642
}
37-
inline_overrides = ('add-ignore')
43+
inline_overrides = [
44+
'add-ignore',
45+
'add-select',
46+
'ignore',
47+
'select',
48+
'config',
49+
'convention',
50+
'ignore-decorators'
51+
]
52+
53+
def check(self, code, filename):
54+
"""Run pydocstyle on code and return the output."""
55+
args = self.build_args(self.get_view_settings(inline=True))
56+
57+
if persist.settings.get('debug'):
58+
persist.printf('{} args: {}'.format(self.name, args))
59+
60+
conf = self.module.config.ConfigurationParser()
61+
with partialpatched(conf,
62+
'_parse_args',
63+
args=args + [filename],
64+
values=None):
65+
conf.parse()
66+
67+
errors = []
68+
for fname, checked_codes, ignore_decorators in \
69+
conf.get_files_to_check():
70+
errors.extend(
71+
self.module.check(
72+
[fname],
73+
select=checked_codes,
74+
ignore_decorators=ignore_decorators))
75+
76+
return errors
77+
78+
79+
@contextmanager
80+
def partialpatched(obj, name, **kwargs):
81+
"""Monkey patch instance method with partial application."""
82+
pre_patched_value = getattr(obj, name)
83+
setattr(obj, name, partial(pre_patched_value, **kwargs))
84+
yield
85+
setattr(obj, name, pre_patched_value)

0 commit comments

Comments
 (0)