|
11 | 11 |
|
12 | 12 | """This module exports the Pydocstyle plugin linter class.""" |
13 | 13 |
|
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 |
15 | 17 |
|
16 | 18 |
|
17 | 19 | class Pydocstyle(PythonLinter): |
18 | 20 | """Provides an interface to the pydocstyle python module/script.""" |
19 | 21 |
|
20 | 22 | syntax = 'python' |
21 | | - if PythonLinter.which('pydocstyle'): |
22 | | - cmd = 'pydocstyle@python' |
23 | | - else: |
24 | | - cmd = 'pep257@python' |
| 23 | + cmd = 'pydocstyle@python' |
25 | 24 | version_args = '--version' |
26 | 25 | version_re = r'(?P<version>\d+\.\d+\.\d+)' |
27 | 26 | version_requirement = '>= 0.3.0' |
28 | 27 | regex = r'^.+?:(?P<line>\d+).*:\r?\n\s*(?P<message>.+)$' |
29 | 28 | multiline = True |
30 | 29 | default_type = highlight.WARNING |
31 | 30 | 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 |
33 | 32 | tempfile_suffix = 'py' |
| 33 | + module = 'pydocstyle' |
34 | 34 | defaults = { |
35 | | - '--add-ignore=': '' |
| 35 | + '--add-ignore=': '', |
| 36 | + '--add-select=': '', |
| 37 | + '--ignore=': '', |
| 38 | + '--select=': '', |
| 39 | + '--config=': '', |
| 40 | + '--convention=': '', |
| 41 | + '--ignore-decorators=': '' |
36 | 42 | } |
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