Plugins/CheckerPlugins/Tabnanny/Tabnanny.py

changeset 12
1d8dd9706f46
parent 0
de9c2efb9d02
child 15
f6ccc31d6e72
equal deleted inserted replaced
11:b0996e4a289e 12:1d8dd9706f46
41 __version__ = "6_eric" 41 __version__ = "6_eric"
42 42
43 import os 43 import os
44 import sys 44 import sys
45 import tokenize 45 import tokenize
46 import cStringIO 46 import io
47 47
48 import Utilities 48 import Utilities
49 49
50 if not hasattr(tokenize, 'NL'): 50 if not hasattr(tokenize, 'NL'):
51 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") 51 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old")
104 global indents, check_equal 104 global indents, check_equal
105 indents = [Whitespace("")] 105 indents = [Whitespace("")]
106 check_equal = 0 106 check_equal = 0
107 107
108 try: 108 try:
109 f = open(file) 109 f = open(file, "r")
110 except IOError, msg: 110 except IOError as msg:
111 return (True, file, "1", "I/O Error: %s" % unicode(msg)) 111 return (True, file, "1", "I/O Error: %s" % str(msg))
112 112
113 try: 113 try:
114 text = Utilities.decode(f.read())[0].encode('utf-8') 114 text = f.read()
115 finally: 115 finally:
116 f.close() 116 f.close()
117 117
118 # convert eols 118 # convert eols
119 text = Utilities.convertLineEnds(text, os.linesep) 119 text = Utilities.convertLineEnds(text, os.linesep)
120 120
121 source = cStringIO.StringIO(text) 121 source = io.StringIO(text)
122 try: 122 try:
123 process_tokens(tokenize.generate_tokens(source.readline)) 123 process_tokens(tokenize.generate_tokens(source.readline))
124 124
125 except tokenize.TokenError, msg: 125 except tokenize.TokenError as msg:
126 f.close() 126 f.close()
127 return (True, file, "1", "Token Error: %s" % unicode(msg)) 127 return (True, file, "1", "Token Error: %s" % str(msg))
128 128
129 except IndentationError, err: 129 except IndentationError as err:
130 f.close() 130 f.close()
131 return (True, file, err.lineno, "Indentation Error: %s" % unicode(err.msg)) 131 return (True, file, err.lineno, "Indentation Error: %s" % str(err.msg))
132 132
133 except NannyNag, nag: 133 except NannyNag as nag:
134 badline = nag.get_lineno() 134 badline = nag.get_lineno()
135 line = nag.get_line() 135 line = nag.get_line()
136 f.close() 136 f.close()
137 return (True, file, str(badline), line) 137 return (True, file, str(badline), line)
138 138
139 except Exception, err: 139 except Exception as err:
140 f.close() 140 f.close()
141 return (True, file, "1", "Unspecific Error: %s" % unicode(err)) 141 return (True, file, "1", "Unspecific Error: %s" % str(err))
142 142
143 f.close() 143 f.close()
144 return (False, None, None, None) 144 return (False, None, None, None)
145 145
146 class Whitespace(object): 146 class Whitespace(object):
335 Function to format the witnesses as a readable string. 335 Function to format the witnesses as a readable string.
336 336
337 @param w A list of witnesses 337 @param w A list of witnesses
338 @return A formated string of the witnesses. 338 @return A formated string of the witnesses.
339 """ 339 """
340 firsts = map(lambda tup: str(tup[0]), w) 340 firsts = [str(tup[0]) for tup in w]
341 prefix = "at tab size" 341 prefix = "at tab size"
342 if len(w) > 1: 342 if len(w) > 1:
343 prefix = prefix + "s" 343 prefix = prefix + "s"
344 return prefix + " " + ', '.join(firsts) 344 return prefix + " " + ', '.join(firsts)
345 345

eric ide

mercurial