| 1 |
|
| 2 |
|
| 3 |
"""The Tab Nanny despises ambiguous indentation. She knows no mercy. |
| 4 |
|
| 5 |
tabnanny -- Detection of ambiguous indentation |
| 6 |
|
| 7 |
For the time being this module is intended to be called as a script. |
| 8 |
However it is possible to import it into an IDE and use the function |
| 9 |
check() described below. |
| 10 |
|
| 11 |
Warning: The API provided by this module is likely to change in future |
| 12 |
releases; such changes may not be backward compatible. |
| 13 |
""" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
__version__ = "6" |
| 22 |
|
| 23 |
import os |
| 24 |
import sys |
| 25 |
import tokenize |
| 26 |
|
| 27 |
__all__ = ["check", "NannyNag", "process_tokens"] |
| 28 |
|
| 29 |
verbose = 0 |
| 30 |
filename_only = 0 |
| 31 |
|
| 32 |
def errprint(*args): |
| 33 |
sep = "" |
| 34 |
for arg in args: |
| 35 |
sys.stderr.write(sep + str(arg)) |
| 36 |
sep = " " |
| 37 |
sys.stderr.write("\n") |
| 38 |
|
| 39 |
def main(): |
| 40 |
import getopt |
| 41 |
|
| 42 |
global verbose, filename_only |
| 43 |
try: |
| 44 |
opts, args = getopt.getopt(sys.argv[1:], "qv") |
| 45 |
except getopt.error as msg: |
| 46 |
errprint(msg) |
| 47 |
return |
| 48 |
for o, a in opts: |
| 49 |
if o == '-q': |
| 50 |
filename_only = filename_only + 1 |
| 51 |
if o == '-v': |
| 52 |
verbose = verbose + 1 |
| 53 |
if not args: |
| 54 |
errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...") |
| 55 |
return |
| 56 |
for arg in args: |
| 57 |
check(arg) |
| 58 |
|
| 59 |
class NannyNag(Exception): |
| 60 |
""" |
| 61 |
Raised by process_tokens() if detecting an ambiguous indent. |
| 62 |
Captured and handled in check(). |
| 63 |
""" |
| 64 |
def __init__(self, lineno, msg, line): |
| 65 |
self.lineno, self.msg, self.line = lineno, msg, line |
| 66 |
def get_lineno(self): |
| 67 |
return self.lineno |
| 68 |
def get_msg(self): |
| 69 |
return self.msg |
| 70 |
def get_line(self): |
| 71 |
return self.line |
| 72 |
|
| 73 |
def check(file): |
| 74 |
"""check(file_or_dir) |
| 75 |
|
| 76 |
If file_or_dir is a directory and not a symbolic link, then recursively |
| 77 |
descend the directory tree named by file_or_dir, checking all .py files |
| 78 |
along the way. If file_or_dir is an ordinary Python source file, it is |
| 79 |
checked for whitespace related problems. The diagnostic messages are |
| 80 |
written to standard output using the print statement. |
| 81 |
""" |
| 82 |
|
| 83 |
if os.path.isdir(file) and not os.path.islink(file): |
| 84 |
if verbose: |
| 85 |
print("%r: listing directory" % (file,)) |
| 86 |
names = os.listdir(file) |
| 87 |
for name in names: |
| 88 |
fullname = os.path.join(file, name) |
| 89 |
if (os.path.isdir(fullname) and |
| 90 |
not os.path.islink(fullname) or |
| 91 |
os.path.normcase(name[-3:]) == ".py"): |
| 92 |
check(fullname) |
| 93 |
return |
| 94 |
|
| 95 |
try: |
| 96 |
f = tokenize.open(file) |
| 97 |
except OSError as msg: |
| 98 |
errprint("%r: I/O Error: %s" % (file, msg)) |
| 99 |
return |
| 100 |
|
| 101 |
if verbose > 1: |
| 102 |
print("checking %r ..." % file) |
| 103 |
|
| 104 |
try: |
| 105 |
process_tokens(tokenize.generate_tokens(f.readline)) |
| 106 |
|
| 107 |
except tokenize.TokenError as msg: |
| 108 |
errprint("%r: Token Error: %s" % (file, msg)) |
| 109 |
return |
| 110 |
|
| 111 |
except IndentationError as msg: |
| 112 |
errprint("%r: Indentation Error: %s" % (file, msg)) |
| 113 |
return |
| 114 |
|
| 115 |
except NannyNag as nag: |
| 116 |
badline = nag.get_lineno() |
| 117 |
line = nag.get_line() |
| 118 |
if verbose: |
| 119 |
print("%r: *** Line %d: trouble in tab city! ***" % (file, badline)) |
| 120 |
print("offending line: %r" % (line,)) |
| 121 |
print(nag.get_msg()) |
| 122 |
else: |
| 123 |
if ' ' in file: file = '"' + file + '"' |
| 124 |
if filename_only: print(file) |
| 125 |
else: print(file, badline, repr(line)) |
| 126 |
return |
| 127 |
|
| 128 |
finally: |
| 129 |
f.close() |
| 130 |
|
| 131 |
if verbose: |
| 132 |
print("%r: Clean bill of health." % (file,)) |
| 133 |
|
| 134 |
class Whitespace: |
| 135 |
|
| 136 |
S, T = ' \t' |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
def __init__(self, ws): |
| 158 |
self.raw = ws |
| 159 |
S, T = Whitespace.S, Whitespace.T |
| 160 |
count = [] |
| 161 |
b = n = nt = 0 |
| 162 |
for ch in self.raw: |
| 163 |
if ch == S: |
| 164 |
n = n + 1 |
| 165 |
b = b + 1 |
| 166 |
elif ch == T: |
| 167 |
n = n + 1 |
| 168 |
nt = nt + 1 |
| 169 |
if b >= len(count): |
| 170 |
count = count + [0] * (b - len(count) + 1) |
| 171 |
count[b] = count[b] + 1 |
| 172 |
b = 0 |
| 173 |
else: |
| 174 |
break |
| 175 |
self.n = n |
| 176 |
self.nt = nt |
| 177 |
self.norm = tuple(count), b |
| 178 |
self.is_simple = len(count) <= 1 |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
def longest_run_of_spaces(self): |
| 183 |
count, trailing = self.norm |
| 184 |
return max(len(count)-1, trailing) |
| 185 |
|
| 186 |
def indent_level(self, tabsize): |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
count, trailing = self.norm |
| 202 |
il = 0 |
| 203 |
for i in range(tabsize, len(count)): |
| 204 |
il = il + i//tabsize * count[i] |
| 205 |
return trailing + tabsize * (il + self.nt) |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
def equal(self, other): |
| 210 |
return self.norm == other.norm |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
def not_equal_witness(self, other): |
| 217 |
n = max(self.longest_run_of_spaces(), |
| 218 |
other.longest_run_of_spaces()) + 1 |
| 219 |
a = [] |
| 220 |
for ts in range(1, n+1): |
| 221 |
if self.indent_level(ts) != other.indent_level(ts): |
| 222 |
a.append( (ts, |
| 223 |
self.indent_level(ts), |
| 224 |
other.indent_level(ts)) ) |
| 225 |
return a |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
def less(self, other): |
| 241 |
if self.n >= other.n: |
| 242 |
return False |
| 243 |
if self.is_simple and other.is_simple: |
| 244 |
return self.nt <= other.nt |
| 245 |
n = max(self.longest_run_of_spaces(), |
| 246 |
other.longest_run_of_spaces()) + 1 |
| 247 |
|
| 248 |
for ts in range(2, n+1): |
| 249 |
if self.indent_level(ts) >= other.indent_level(ts): |
| 250 |
return False |
| 251 |
return True |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
def not_less_witness(self, other): |
| 258 |
n = max(self.longest_run_of_spaces(), |
| 259 |
other.longest_run_of_spaces()) + 1 |
| 260 |
a = [] |
| 261 |
for ts in range(1, n+1): |
| 262 |
if self.indent_level(ts) >= other.indent_level(ts): |
| 263 |
a.append( (ts, |
| 264 |
self.indent_level(ts), |
| 265 |
other.indent_level(ts)) ) |
| 266 |
return a |
| 267 |
|
| 268 |
def format_witnesses(w): |
| 269 |
firsts = (str(tup[0]) for tup in w) |
| 270 |
prefix = "at tab size" |
| 271 |
if len(w) > 1: |
| 272 |
prefix = prefix + "s" |
| 273 |
return prefix + " " + ', '.join(firsts) |
| 274 |
|
| 275 |
def process_tokens(tokens): |
| 276 |
INDENT = tokenize.INDENT |
| 277 |
DEDENT = tokenize.DEDENT |
| 278 |
NEWLINE = tokenize.NEWLINE |
| 279 |
JUNK = tokenize.COMMENT, tokenize.NL |
| 280 |
indents = [Whitespace("")] |
| 281 |
check_equal = 0 |
| 282 |
|
| 283 |
for (type, token, start, end, line) in tokens: |
| 284 |
if type == NEWLINE: |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
check_equal = 1 |
| 291 |
|
| 292 |
elif type == INDENT: |
| 293 |
check_equal = 0 |
| 294 |
thisguy = Whitespace(token) |
| 295 |
if not indents[-1].less(thisguy): |
| 296 |
witness = indents[-1].not_less_witness(thisguy) |
| 297 |
msg = "indent not greater e.g. " + format_witnesses(witness) |
| 298 |
raise NannyNag(start[0], msg, line) |
| 299 |
indents.append(thisguy) |
| 300 |
|
| 301 |
elif type == DEDENT: |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
check_equal = 1 |
| 312 |
|
| 313 |
del indents[-1] |
| 314 |
|
| 315 |
elif check_equal and type not in JUNK: |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
check_equal = 0 |
| 323 |
thisguy = Whitespace(line) |
| 324 |
if not indents[-1].equal(thisguy): |
| 325 |
witness = indents[-1].not_equal_witness(thisguy) |
| 326 |
msg = "indent not equal e.g. " + format_witnesses(witness) |
| 327 |
raise NannyNag(start[0], msg, line) |
| 328 |
|
| 329 |
|
| 330 |
if __name__ == '__main__': |
| 331 |
main() |