|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the PEP 8 checker. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt4.QtCore import QProcess, QCoreApplication |
|
13 |
|
14 from . import pep8 |
|
15 from .Pep8NamingChecker import Pep8NamingChecker |
|
16 from .DocStyleChecker import DocStyleChecker |
|
17 |
|
18 import Preferences |
|
19 import Utilities |
|
20 |
|
21 from eric5config import getConfig |
|
22 |
|
23 |
|
24 class CodeStyleCheckerPy2(object): |
|
25 """ |
|
26 Class implementing the PEP 8 checker interface for Python 2. |
|
27 """ |
|
28 def __init__(self, filename, lines, repeat=False, |
|
29 select="", ignore="", max_line_length=79, |
|
30 hang_closing=False, docType="pep257"): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param filename name of the file to check (string) |
|
35 @param lines source of the file (list of strings) (ignored) |
|
36 @keyparam repeat flag indicating to repeat message categories (boolean) |
|
37 @keyparam select list of message IDs to check for |
|
38 (comma separated string) |
|
39 @keyparam ignore list of message IDs to ignore |
|
40 (comma separated string) |
|
41 @keyparam max_line_length maximum allowed line length (integer) |
|
42 @keyparam hang_closing flag indicating to allow hanging closing |
|
43 brackets (boolean) |
|
44 @keyparam docType type of the documentation strings |
|
45 (string, one of 'eric' or 'pep257') |
|
46 """ |
|
47 assert docType in ("eric", "pep257") |
|
48 |
|
49 self.errors = [] |
|
50 self.counters = {} |
|
51 |
|
52 interpreter = Preferences.getDebugger("PythonInterpreter") |
|
53 if interpreter == "" or not Utilities.isExecutable(interpreter): |
|
54 self.errors.append((filename, 1, 1, |
|
55 QCoreApplication.translate("CodeStyleCheckerPy2", |
|
56 "Python2 interpreter not configured."))) |
|
57 return |
|
58 |
|
59 checker = os.path.join(getConfig('ericDir'), |
|
60 "UtilitiesPython2", "Pep8Checker.py") |
|
61 |
|
62 args = [checker] |
|
63 if repeat: |
|
64 args.append("-r") |
|
65 if select: |
|
66 args.append("-s") |
|
67 args.append(select) |
|
68 if ignore: |
|
69 args.append("-i") |
|
70 args.append(ignore) |
|
71 args.append("-m") |
|
72 args.append(str(max_line_length)) |
|
73 if hang_closing: |
|
74 args.append("-h") |
|
75 args.append("-d") |
|
76 args.append(docType) |
|
77 args.append("-f") |
|
78 args.append(filename) |
|
79 |
|
80 proc = QProcess() |
|
81 proc.setProcessChannelMode(QProcess.MergedChannels) |
|
82 proc.start(interpreter, args) |
|
83 finished = proc.waitForFinished(15000) |
|
84 if finished: |
|
85 output = \ |
|
86 str(proc.readAllStandardOutput(), |
|
87 Preferences.getSystem("IOEncoding"), |
|
88 'replace').splitlines() |
|
89 if output[0] == "ERROR": |
|
90 self.errors.append((filename, 1, 1, output[2])) |
|
91 return |
|
92 |
|
93 if output[0] == "NO_PEP8": |
|
94 return |
|
95 |
|
96 index = 0 |
|
97 while index < len(output): |
|
98 if output[index] == "PEP8_STATISTICS": |
|
99 index += 1 |
|
100 break |
|
101 |
|
102 fname = output[index + 1] |
|
103 lineno = int(output[index + 2]) |
|
104 position = int(output[index + 3]) |
|
105 code = output[index + 4] |
|
106 arglen = int(output[index + 5]) |
|
107 args = [] |
|
108 argindex = 0 |
|
109 while argindex < arglen: |
|
110 args.append(output[index + 6 + argindex]) |
|
111 argindex += 1 |
|
112 index += 6 + arglen |
|
113 |
|
114 if code in Pep8NamingChecker.Codes: |
|
115 text = Pep8NamingChecker.getMessage(code, *args) |
|
116 elif code in DocStyleChecker.Codes: |
|
117 text = DocStyleChecker.getMessage(code, *args) |
|
118 else: |
|
119 text = pep8.getMessage(code, *args) |
|
120 self.errors.append((fname, lineno, position, text)) |
|
121 while index < len(output): |
|
122 code, countStr = output[index].split(None, 1) |
|
123 self.counters[code] = int(countStr) |
|
124 index += 1 |
|
125 else: |
|
126 self.errors.append((filename, 1, 1, |
|
127 QCoreApplication.translate("CodeStyleCheckerPy2", |
|
128 "Python2 interpreter did not finish within 15s."))) |