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