UtilitiesPython2/Py2SyntaxChecker.py

changeset 3203
61f05d1bf877
parent 3160
209a07d7e401
child 3205
157dcfafc5d2
equal deleted inserted replaced
3202:d21df6f384fc 3203:61f05d1bf877
10 10
11 import sys 11 import sys
12 import re 12 import re
13 import traceback 13 import traceback
14 import warnings 14 import warnings
15 import json
15 16
16 from Tools import readEncodedFile, normalizeCode, extractLineFlags 17 from Tools import readEncodedFile, normalizeCode, extractLineFlags
17 18
18 19
19 def compile(file, codestring): 20 def compile(file, codestring):
98 99
99 @param fileName name of the file (string) 100 @param fileName name of the file (string)
100 @param codestring source code to be checked (string) 101 @param codestring source code to be checked (string)
101 @param ignoreStarImportWarnings flag indicating to 102 @param ignoreStarImportWarnings flag indicating to
102 ignore 'star import' warnings (boolean) 103 ignore 'star import' warnings (boolean)
103 @return list of strings containing the warnings 104 @return list of lists containing the warnings
104 (marker, file name, line number, message) 105 (marker, file name, line number, message)
105 """ 106 """
106 from py2flakes.checker import Checker 107 from py2flakes.checker import Checker
107 from py2flakes.messages import ImportStarUsed 108 from py2flakes.messages import ImportStarUsed
108 109
109 strings = [] 110 flakesWarnings = []
110 lines = codestring.splitlines() 111 lines = codestring.splitlines()
111 try: 112 try:
112 warnings_ = Checker(codestring, fileName) 113 warnings_ = Checker(codestring, fileName)
113 warnings_.messages.sort(key=lambda a: a.lineno) 114 warnings_.messages.sort(key=lambda a: a.lineno)
114 for warning in warnings_.messages: 115 for warning in warnings_.messages:
117 continue 118 continue
118 119
119 _fn, lineno, message = warning.getMessageData() 120 _fn, lineno, message = warning.getMessageData()
120 if "__IGNORE_WARNING__" not in \ 121 if "__IGNORE_WARNING__" not in \
121 extractLineFlags(lines[lineno - 1].strip()): 122 extractLineFlags(lines[lineno - 1].strip()):
122 strings.extend(["FLAKES_WARNING", _fn, lineno, message]) 123 flakesWarnings.append(["FLAKES_WARNING", _fn, lineno, message])
123 except SyntaxError as err: 124 except SyntaxError as err:
124 if err.text.strip(): 125 if err.text.strip():
125 msg = err.text.strip() 126 msg = err.text.strip()
126 else: 127 else:
127 msg = err.msg 128 msg = err.msg
128 strings.extend(["FLAKES_ERROR", fileName, err.lineno, msg]) 129 flakesWarnings.append(["FLAKES_ERROR", fileName, err.lineno, msg])
129 130
130 return strings 131 return flakesWarnings
131 132
132 if __name__ == "__main__": 133 if __name__ == "__main__":
134 info = []
133 if len(sys.argv) < 2 or \ 135 if len(sys.argv) < 2 or \
134 len(sys.argv) > 3 or \ 136 len(sys.argv) > 3 or \
135 (len(sys.argv) == 3 and sys.argv[1] not in ["-fi", "-fs"]): 137 (len(sys.argv) == 3 and sys.argv[1] not in ["-fi", "-fs"]):
136 print "ERROR" 138 info.append(["ERROR", "", "", "", "", "No file name given."])
137 print ""
138 print ""
139 print ""
140 print ""
141 print "No file name given."
142 else: 139 else:
143 warnings.simplefilter("error") 140 warnings.simplefilter("error")
144 filename = sys.argv[-1] 141 filename = sys.argv[-1]
145 try: 142 try:
146 codestring = readEncodedFile(filename)[0] 143 codestring = readEncodedFile(filename)[0]
152 # fake a syntax error 149 # fake a syntax error
153 syntaxerror, fname, line, index, code, error = \ 150 syntaxerror, fname, line, index, code, error = \
154 1, filename, "1", "0", "", "I/O Error: %s" % unicode(msg) 151 1, filename, "1", "0", "", "I/O Error: %s" % unicode(msg)
155 152
156 if syntaxerror: 153 if syntaxerror:
157 print "ERROR" 154 info.append(["ERROR", fname, line, index, code, error])
158 else: 155 else:
159 print "NO_ERROR" 156 info.append(["NO_ERROR"])
160 print fname
161 print line
162 print index
163 print code
164 print error
165 157
166 if not syntaxerror and sys.argv[1] in ["-fi", "-fs"]: 158 if not syntaxerror and sys.argv[1] in ["-fi", "-fs"]:
167 # do pyflakes check 159 # do pyflakes check
168 warningLines = flakesCheck( 160 flakesWarnings = flakesCheck(
169 filename, codestring, sys.argv[1] == "-fi") 161 filename, codestring, sys.argv[1] == "-fi")
170 for warningLine in warningLines: 162 info.extend(flakesWarnings)
171 print warningLine 163
164 print json.dumps(info)
172 165
173 sys.exit(0) 166 sys.exit(0)
174 167
175 # 168 #
176 # eflag: FileType = Python2 169 # eflag: FileType = Python2

eric ide

mercurial