|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the PEP 8 checker. |
|
8 """ |
|
9 |
|
10 import optparse |
|
11 |
|
12 from . import pep8 |
|
13 |
|
14 |
|
15 class Pep8Checker(pep8.Checker): |
|
16 """ |
|
17 Class implementing the PEP 8 checker. |
|
18 """ |
|
19 def __init__(self, filename, lines, repeat=False, |
|
20 select="", ignore=""): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param filename name of the file to check (string) |
|
25 @param lines source of the file (list of strings) |
|
26 @keyparam repeat flag indicating to repeat message categories (boolean) |
|
27 @keyparam select list of message IDs to check for |
|
28 (comma separated string) |
|
29 @keyparam ignore list of message IDs to ignore |
|
30 (comma separated string) |
|
31 """ |
|
32 pep8.options = optparse.Values() |
|
33 |
|
34 pep8.options.verbose = 0 |
|
35 |
|
36 pep8.options.repeat = repeat |
|
37 if select: |
|
38 pep8.options.select = [s.strip() for s in select.split(',') |
|
39 if s.strip()] |
|
40 else: |
|
41 pep8.options.select = [] |
|
42 if ignore: |
|
43 pep8.options.ignore = [i.strip() for i in ignore.split(',') |
|
44 if i.strip()] |
|
45 else: |
|
46 pep8.options.ignore = [] |
|
47 pep8.options.physical_checks = pep8.find_checks('physical_line') |
|
48 pep8.options.logical_checks = pep8.find_checks('logical_line') |
|
49 pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0) |
|
50 pep8.options.messages = {} |
|
51 |
|
52 pep8.Checker.__init__(self, filename, lines) |
|
53 |
|
54 self.messages = [] |
|
55 |
|
56 def __ignore_code(self, code): |
|
57 """ |
|
58 Private method to check, if the message for the given code should |
|
59 be ignored. |
|
60 |
|
61 If codes are selected and the code has a selected prefix and does not |
|
62 have an ignored prefix, it is not ignored. If codes are selected and |
|
63 the code does not have a selected prefix, it is ignored. If no codes |
|
64 are selected, the code is ignored, if it has a prefix, that is |
|
65 contained in the ignored codes. |
|
66 |
|
67 @param code code to be checked (string) |
|
68 @return flag indicating, that the code should be ignored (boolean) |
|
69 """ |
|
70 if pep8.options.select: |
|
71 if code.startswith(tuple(pep8.options.select)): |
|
72 if code.startswith(tuple(pep8.options.ignore)): |
|
73 return True |
|
74 else: |
|
75 return False |
|
76 else: |
|
77 return True |
|
78 else: |
|
79 if code.startswith(tuple(pep8.options.ignore)): |
|
80 return True |
|
81 else: |
|
82 return False |
|
83 |
|
84 def report_error_args(self, line_number, offset, code, check, *args): |
|
85 """ |
|
86 Public method to collect the error messages. |
|
87 |
|
88 @param line_number line number of the issue (integer) |
|
89 @param offset position within line of the issue (integer) |
|
90 @param code message code (string) |
|
91 @param check reference to the checker function (function) |
|
92 @param args arguments for the message (list) |
|
93 """ |
|
94 if self.__ignore_code(code): |
|
95 return |
|
96 |
|
97 text = pep8.getMessage(code, *args) |
|
98 if code in pep8.options.counters: |
|
99 pep8.options.counters[code] += 1 |
|
100 else: |
|
101 pep8.options.counters[code] = 1 |
|
102 pep8.options.messages[code] = text[5:] |
|
103 self.file_errors += 1 |
|
104 if pep8.options.counters[code] == 1 or pep8.options.repeat: |
|
105 self.messages.append( |
|
106 (self.filename, self.line_offset + line_number, |
|
107 offset + 1, text) |
|
108 ) |