/
__init__.py
76 lines (64 loc) · 2.57 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from prospector.encoding import read_py_file, CouldNotHandleEncoding
from pydocstyle.checker import ConventionChecker as PEP257Checker, AllError
from prospector.message import Location, Message, make_tool_error_message
from prospector.tools.base import ToolBase
__all__ = (
'Pep257Tool',
)
class Pep257Tool(ToolBase):
def __init__(self, *args, **kwargs):
super(Pep257Tool, self).__init__(*args, **kwargs)
self._code_files = []
self.ignore_codes = ()
def configure(self, prospector_config, found_files):
self.ignore_codes = prospector_config.get_disabled_messages('pep257')
def run(self, found_files):
messages = []
checker = PEP257Checker()
for code_file in found_files.iter_module_paths():
try:
for error in checker.check_source(
read_py_file(code_file),
code_file,
None
):
location = Location(
path=code_file,
module=None,
function='',
line=error.line,
character=0,
absolute_path=True,
)
message = Message(
source='pep257',
code=error.code,
location=location,
message=error.message.partition(':')[2].strip(),
)
messages.append(message)
except CouldNotHandleEncoding as err:
messages.append(make_tool_error_message(
code_file, 'pep257', 'D000',
message='Could not handle the encoding of this file: %s' % err.encoding
))
continue
except AllError as exc:
# pep257's Parser.parse_all method raises AllError when an
# attempt to analyze the __all__ definition has failed. This
# occurs when __all__ is too complex to be parsed.
messages.append(make_tool_error_message(
code_file, 'pep257', 'D000',
line=1, character=0,
message=exc.args[0]
))
continue
return self.filter_messages(messages)
def filter_messages(self, messages):
return [
message
for message in messages
if message.code not in self.ignore_codes
]