Utilities/SyntaxCheck.py

branch
Py2 comp.
changeset 3065
070b35dde35e
parent 2571
e6bb19eb87ea
child 3161
06f57a834adf
equal deleted inserted replaced
3061:1c0ea3a87390 3065:070b35dde35e
91 if Preferences.getEditor("AdvancedEncodingDetection"): 91 if Preferences.getEditor("AdvancedEncodingDetection"):
92 # Try the universal character encoding detector 92 # Try the universal character encoding detector
93 try: 93 try:
94 import ThirdParty.CharDet.chardet 94 import ThirdParty.CharDet.chardet
95 guess = ThirdParty.CharDet.chardet.detect(text) 95 guess = ThirdParty.CharDet.chardet.detect(text)
96 if guess and guess['confidence'] > 0.95 and guess['encoding'] is not None: 96 if guess and guess['confidence'] > 0.95 \
97 and guess['encoding'] is not None:
97 codec = guess['encoding'].lower() 98 codec = guess['encoding'].lower()
98 return str(text, codec), '{0}-guessed'.format(codec) 99 return str(text, codec), '{0}-guessed'.format(codec)
99 except (UnicodeError, LookupError, ImportError): 100 except (UnicodeError, LookupError, ImportError):
100 pass 101 pass
101 except (NameError): 102 except (NameError):
163 return codestring 164 return codestring
164 165
165 166
166 def extractLineFlags(line, startComment="#", endComment=""): 167 def extractLineFlags(line, startComment="#", endComment=""):
167 """ 168 """
168 Function to extract flags starting and ending with '__' from a line comment. 169 Function to extract flags starting and ending with '__' from a line
170 comment.
169 171
170 @param line line to extract flags from (string) 172 @param line line to extract flags from (string)
171 @keyparam startComment string identifying the start of the comment (string) 173 @keyparam startComment string identifying the start of the comment (string)
172 @keyparam endComment string identifying the end of a comment (string) 174 @keyparam endComment string identifying the end of a comment (string)
173 @return list containing the extracted flags (list of strings) 175 @return list containing the extracted flags (list of strings)
182 flags = [f.strip() for f in comment.split() 184 flags = [f.strip() for f in comment.split()
183 if (f.startswith("__") and f.endswith("__"))] 185 if (f.startswith("__") and f.endswith("__"))]
184 return flags 186 return flags
185 187
186 188
187 def compile_and_check(file_, codestring="", checkFlakes=True, ignoreStarImportWarnings=False): 189 def compile_and_check(file_, codestring="", checkFlakes=True,
190 ignoreStarImportWarnings=False):
188 """ 191 """
189 Function to compile one Python source file to Python bytecode 192 Function to compile one Python source file to Python bytecode
190 and to perform a pyflakes check. 193 and to perform a pyflakes check.
191 194
192 @param file_ source filename (string) 195 @param file_ source filename (string)
199 and the error message (boolean, string, string, string, string, 202 and the error message (boolean, string, string, string, string,
200 string). If checkFlakes is True, a list of strings containing the 203 string). If checkFlakes is True, a list of strings containing the
201 warnings (marker, file name, line number, message) 204 warnings (marker, file name, line number, message)
202 The values are only valid, if the status is True. 205 The values are only valid, if the status is True.
203 """ 206 """
204
205 try: 207 try:
206 import builtins 208 import builtins
207 except ImportError: 209 except ImportError:
208 import __builtin__ as builtins #__IGNORE_WARNING__ 210 import __builtin__ as builtins #__IGNORE_WARNING__
209 211
235 index = 0 237 index = 0
236 code = "" 238 code = ""
237 error = "" 239 error = ""
238 lines = traceback.format_exception_only(SyntaxError, detail) 240 lines = traceback.format_exception_only(SyntaxError, detail)
239 match = re.match('\s*File "(.+)", line (\d+)', 241 match = re.match('\s*File "(.+)", line (\d+)',
240 lines[0].replace('<string>', '{0}'.format(file_))) 242 lines[0].replace('<string>', '{0}'.format(file_)))
241 if match is not None: 243 if match is not None:
242 fn, line = match.group(1, 2) 244 fn, line = match.group(1, 2)
243 if lines[1].startswith('SyntaxError:'): 245 if lines[1].startswith('SyntaxError:'):
244 error = re.match('SyntaxError: (.+)', lines[1]).group(1) 246 error = re.match('SyntaxError: (.+)', lines[1]).group(1)
245 else: 247 else:
286 try: 288 try:
287 warnings = Checker(module, file_) 289 warnings = Checker(module, file_)
288 warnings.messages.sort(key=lambda a: a.lineno) 290 warnings.messages.sort(key=lambda a: a.lineno)
289 for warning in warnings.messages: 291 for warning in warnings.messages:
290 if ignoreStarImportWarnings and \ 292 if ignoreStarImportWarnings and \
291 isinstance(warning, ImportStarUsed): 293 isinstance(warning, ImportStarUsed):
292 continue 294 continue
293 295
294 _fn, lineno, message, msg_args = warning.getMessageData() 296 _fn, lineno, message, msg_args = warning.getMessageData()
295 if "__IGNORE_WARNING__" not in extractLineFlags(lines[lineno - 1].strip()): 297 if "__IGNORE_WARNING__" not in extractLineFlags(
296 strings.append(["FLAKES_WARNING", _fn, lineno, message, msg_args]) 298 lines[lineno - 1].strip()):
299 strings.append([
300 "FLAKES_WARNING", _fn, lineno, message, msg_args])
297 except SyntaxError as err: 301 except SyntaxError as err:
298 if err.text.strip(): 302 if err.text.strip():
299 msg = err.text.strip() 303 msg = err.text.strip()
300 else: 304 else:
301 msg = err.msg 305 msg = err.msg
315 print("") 319 print("")
316 print("No file name given.") 320 print("No file name given.")
317 else: 321 else:
318 filename = sys.argv[-1] 322 filename = sys.argv[-1]
319 checkFlakes = len(sys.argv) == 3 323 checkFlakes = len(sys.argv) == 3
320 ignoreStarImportWarnings = sys.argv[1] == "-fi" # Setting is ignored if checkFlakes is False 324 # Setting is ignored if checkFlakes is False
325 ignoreStarImportWarnings = sys.argv[1] == "-fi"
321 326
322 try: 327 try:
323 codestring = readEncodedFile(filename)[0] 328 codestring = readEncodedFile(filename)[0]
324 329
325 syntaxerror, fname, line, index, code, error, warnings = \ 330 syntaxerror, fname, line, index, code, error, warnings = \
326 compile_and_check(filename, codestring, checkFlakes, ignoreStarImportWarnings) 331 compile_and_check(filename, codestring, checkFlakes,
332 ignoreStarImportWarnings)
327 except IOError as msg: 333 except IOError as msg:
328 # fake a syntax error 334 # fake a syntax error
329 syntaxerror, fname, line, index, code, error, warnings = \ 335 syntaxerror, fname, line, index, code, error, warnings = \
330 True, filename, 1, 0, "", "I/O Error: %s" % str(msg), [] 336 True, filename, 1, 0, "", "I/O Error: %s" % str(msg), []
331 337

eric ide

mercurial