Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: tabnanny.py
#! /usr/bin/python2.7
[0] Fix | Delete
[1] Fix | Delete
"""The Tab Nanny despises ambiguous indentation. She knows no mercy.
[2] Fix | Delete
[3] Fix | Delete
tabnanny -- Detection of ambiguous indentation
[4] Fix | Delete
[5] Fix | Delete
For the time being this module is intended to be called as a script.
[6] Fix | Delete
However it is possible to import it into an IDE and use the function
[7] Fix | Delete
check() described below.
[8] Fix | Delete
[9] Fix | Delete
Warning: The API provided by this module is likely to change in future
[10] Fix | Delete
releases; such changes may not be backward compatible.
[11] Fix | Delete
"""
[12] Fix | Delete
[13] Fix | Delete
# Released to the public domain, by Tim Peters, 15 April 1998.
[14] Fix | Delete
[15] Fix | Delete
# XXX Note: this is now a standard library module.
[16] Fix | Delete
# XXX The API needs to undergo changes however; the current code is too
[17] Fix | Delete
# XXX script-like. This will be addressed later.
[18] Fix | Delete
[19] Fix | Delete
__version__ = "6"
[20] Fix | Delete
[21] Fix | Delete
import os
[22] Fix | Delete
import sys
[23] Fix | Delete
import getopt
[24] Fix | Delete
import tokenize
[25] Fix | Delete
if not hasattr(tokenize, 'NL'):
[26] Fix | Delete
raise ValueError("tokenize.NL doesn't exist -- tokenize module too old")
[27] Fix | Delete
[28] Fix | Delete
__all__ = ["check", "NannyNag", "process_tokens"]
[29] Fix | Delete
[30] Fix | Delete
verbose = 0
[31] Fix | Delete
filename_only = 0
[32] Fix | Delete
[33] Fix | Delete
def errprint(*args):
[34] Fix | Delete
sep = ""
[35] Fix | Delete
for arg in args:
[36] Fix | Delete
sys.stderr.write(sep + str(arg))
[37] Fix | Delete
sep = " "
[38] Fix | Delete
sys.stderr.write("\n")
[39] Fix | Delete
[40] Fix | Delete
def main():
[41] Fix | Delete
global verbose, filename_only
[42] Fix | Delete
try:
[43] Fix | Delete
opts, args = getopt.getopt(sys.argv[1:], "qv")
[44] Fix | Delete
except getopt.error, msg:
[45] Fix | Delete
errprint(msg)
[46] Fix | Delete
return
[47] Fix | Delete
for o, a in opts:
[48] Fix | Delete
if o == '-q':
[49] Fix | Delete
filename_only = filename_only + 1
[50] Fix | Delete
if o == '-v':
[51] Fix | Delete
verbose = verbose + 1
[52] Fix | Delete
if not args:
[53] Fix | Delete
errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...")
[54] Fix | Delete
return
[55] Fix | Delete
for arg in args:
[56] Fix | Delete
check(arg)
[57] Fix | Delete
[58] Fix | Delete
class NannyNag(Exception):
[59] Fix | Delete
"""
[60] Fix | Delete
Raised by process_tokens() if detecting an ambiguous indent.
[61] Fix | Delete
Captured and handled in check().
[62] Fix | Delete
"""
[63] Fix | Delete
def __init__(self, lineno, msg, line):
[64] Fix | Delete
self.lineno, self.msg, self.line = lineno, msg, line
[65] Fix | Delete
def get_lineno(self):
[66] Fix | Delete
return self.lineno
[67] Fix | Delete
def get_msg(self):
[68] Fix | Delete
return self.msg
[69] Fix | Delete
def get_line(self):
[70] Fix | Delete
return self.line
[71] Fix | Delete
[72] Fix | Delete
def check(file):
[73] Fix | Delete
"""check(file_or_dir)
[74] Fix | Delete
[75] Fix | Delete
If file_or_dir is a directory and not a symbolic link, then recursively
[76] Fix | Delete
descend the directory tree named by file_or_dir, checking all .py files
[77] Fix | Delete
along the way. If file_or_dir is an ordinary Python source file, it is
[78] Fix | Delete
checked for whitespace related problems. The diagnostic messages are
[79] Fix | Delete
written to standard output using the print statement.
[80] Fix | Delete
"""
[81] Fix | Delete
[82] Fix | Delete
if os.path.isdir(file) and not os.path.islink(file):
[83] Fix | Delete
if verbose:
[84] Fix | Delete
print "%r: listing directory" % (file,)
[85] Fix | Delete
names = os.listdir(file)
[86] Fix | Delete
for name in names:
[87] Fix | Delete
fullname = os.path.join(file, name)
[88] Fix | Delete
if (os.path.isdir(fullname) and
[89] Fix | Delete
not os.path.islink(fullname) or
[90] Fix | Delete
os.path.normcase(name[-3:]) == ".py"):
[91] Fix | Delete
check(fullname)
[92] Fix | Delete
return
[93] Fix | Delete
[94] Fix | Delete
try:
[95] Fix | Delete
f = open(file)
[96] Fix | Delete
except IOError, msg:
[97] Fix | Delete
errprint("%r: I/O Error: %s" % (file, msg))
[98] Fix | Delete
return
[99] Fix | Delete
[100] Fix | Delete
if verbose > 1:
[101] Fix | Delete
print "checking %r ..." % file
[102] Fix | Delete
[103] Fix | Delete
try:
[104] Fix | Delete
process_tokens(tokenize.generate_tokens(f.readline))
[105] Fix | Delete
[106] Fix | Delete
except tokenize.TokenError, msg:
[107] Fix | Delete
errprint("%r: Token Error: %s" % (file, msg))
[108] Fix | Delete
return
[109] Fix | Delete
[110] Fix | Delete
except IndentationError, msg:
[111] Fix | Delete
errprint("%r: Indentation Error: %s" % (file, msg))
[112] Fix | Delete
return
[113] Fix | Delete
[114] Fix | Delete
except NannyNag, nag:
[115] Fix | Delete
badline = nag.get_lineno()
[116] Fix | Delete
line = nag.get_line()
[117] Fix | Delete
if verbose:
[118] Fix | Delete
print "%r: *** Line %d: trouble in tab city! ***" % (file, badline)
[119] Fix | Delete
print "offending line: %r" % (line,)
[120] Fix | Delete
print nag.get_msg()
[121] Fix | Delete
else:
[122] Fix | Delete
if ' ' in file: file = '"' + file + '"'
[123] Fix | Delete
if filename_only: print file
[124] Fix | Delete
else: print file, badline, repr(line)
[125] Fix | Delete
return
[126] Fix | Delete
[127] Fix | Delete
if verbose:
[128] Fix | Delete
print "%r: Clean bill of health." % (file,)
[129] Fix | Delete
[130] Fix | Delete
class Whitespace:
[131] Fix | Delete
# the characters used for space and tab
[132] Fix | Delete
S, T = ' \t'
[133] Fix | Delete
[134] Fix | Delete
# members:
[135] Fix | Delete
# raw
[136] Fix | Delete
# the original string
[137] Fix | Delete
# n
[138] Fix | Delete
# the number of leading whitespace characters in raw
[139] Fix | Delete
# nt
[140] Fix | Delete
# the number of tabs in raw[:n]
[141] Fix | Delete
# norm
[142] Fix | Delete
# the normal form as a pair (count, trailing), where:
[143] Fix | Delete
# count
[144] Fix | Delete
# a tuple such that raw[:n] contains count[i]
[145] Fix | Delete
# instances of S * i + T
[146] Fix | Delete
# trailing
[147] Fix | Delete
# the number of trailing spaces in raw[:n]
[148] Fix | Delete
# It's A Theorem that m.indent_level(t) ==
[149] Fix | Delete
# n.indent_level(t) for all t >= 1 iff m.norm == n.norm.
[150] Fix | Delete
# is_simple
[151] Fix | Delete
# true iff raw[:n] is of the form (T*)(S*)
[152] Fix | Delete
[153] Fix | Delete
def __init__(self, ws):
[154] Fix | Delete
self.raw = ws
[155] Fix | Delete
S, T = Whitespace.S, Whitespace.T
[156] Fix | Delete
count = []
[157] Fix | Delete
b = n = nt = 0
[158] Fix | Delete
for ch in self.raw:
[159] Fix | Delete
if ch == S:
[160] Fix | Delete
n = n + 1
[161] Fix | Delete
b = b + 1
[162] Fix | Delete
elif ch == T:
[163] Fix | Delete
n = n + 1
[164] Fix | Delete
nt = nt + 1
[165] Fix | Delete
if b >= len(count):
[166] Fix | Delete
count = count + [0] * (b - len(count) + 1)
[167] Fix | Delete
count[b] = count[b] + 1
[168] Fix | Delete
b = 0
[169] Fix | Delete
else:
[170] Fix | Delete
break
[171] Fix | Delete
self.n = n
[172] Fix | Delete
self.nt = nt
[173] Fix | Delete
self.norm = tuple(count), b
[174] Fix | Delete
self.is_simple = len(count) <= 1
[175] Fix | Delete
[176] Fix | Delete
# return length of longest contiguous run of spaces (whether or not
[177] Fix | Delete
# preceding a tab)
[178] Fix | Delete
def longest_run_of_spaces(self):
[179] Fix | Delete
count, trailing = self.norm
[180] Fix | Delete
return max(len(count)-1, trailing)
[181] Fix | Delete
[182] Fix | Delete
def indent_level(self, tabsize):
[183] Fix | Delete
# count, il = self.norm
[184] Fix | Delete
# for i in range(len(count)):
[185] Fix | Delete
# if count[i]:
[186] Fix | Delete
# il = il + (i/tabsize + 1)*tabsize * count[i]
[187] Fix | Delete
# return il
[188] Fix | Delete
[189] Fix | Delete
# quicker:
[190] Fix | Delete
# il = trailing + sum (i/ts + 1)*ts*count[i] =
[191] Fix | Delete
# trailing + ts * sum (i/ts + 1)*count[i] =
[192] Fix | Delete
# trailing + ts * sum i/ts*count[i] + count[i] =
[193] Fix | Delete
# trailing + ts * [(sum i/ts*count[i]) + (sum count[i])] =
[194] Fix | Delete
# trailing + ts * [(sum i/ts*count[i]) + num_tabs]
[195] Fix | Delete
# and note that i/ts*count[i] is 0 when i < ts
[196] Fix | Delete
[197] Fix | Delete
count, trailing = self.norm
[198] Fix | Delete
il = 0
[199] Fix | Delete
for i in range(tabsize, len(count)):
[200] Fix | Delete
il = il + i/tabsize * count[i]
[201] Fix | Delete
return trailing + tabsize * (il + self.nt)
[202] Fix | Delete
[203] Fix | Delete
# return true iff self.indent_level(t) == other.indent_level(t)
[204] Fix | Delete
# for all t >= 1
[205] Fix | Delete
def equal(self, other):
[206] Fix | Delete
return self.norm == other.norm
[207] Fix | Delete
[208] Fix | Delete
# return a list of tuples (ts, i1, i2) such that
[209] Fix | Delete
# i1 == self.indent_level(ts) != other.indent_level(ts) == i2.
[210] Fix | Delete
# Intended to be used after not self.equal(other) is known, in which
[211] Fix | Delete
# case it will return at least one witnessing tab size.
[212] Fix | Delete
def not_equal_witness(self, other):
[213] Fix | Delete
n = max(self.longest_run_of_spaces(),
[214] Fix | Delete
other.longest_run_of_spaces()) + 1
[215] Fix | Delete
a = []
[216] Fix | Delete
for ts in range(1, n+1):
[217] Fix | Delete
if self.indent_level(ts) != other.indent_level(ts):
[218] Fix | Delete
a.append( (ts,
[219] Fix | Delete
self.indent_level(ts),
[220] Fix | Delete
other.indent_level(ts)) )
[221] Fix | Delete
return a
[222] Fix | Delete
[223] Fix | Delete
# Return True iff self.indent_level(t) < other.indent_level(t)
[224] Fix | Delete
# for all t >= 1.
[225] Fix | Delete
# The algorithm is due to Vincent Broman.
[226] Fix | Delete
# Easy to prove it's correct.
[227] Fix | Delete
# XXXpost that.
[228] Fix | Delete
# Trivial to prove n is sharp (consider T vs ST).
[229] Fix | Delete
# Unknown whether there's a faster general way. I suspected so at
[230] Fix | Delete
# first, but no longer.
[231] Fix | Delete
# For the special (but common!) case where M and N are both of the
[232] Fix | Delete
# form (T*)(S*), M.less(N) iff M.len() < N.len() and
[233] Fix | Delete
# M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded.
[234] Fix | Delete
# XXXwrite that up.
[235] Fix | Delete
# Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1.
[236] Fix | Delete
def less(self, other):
[237] Fix | Delete
if self.n >= other.n:
[238] Fix | Delete
return False
[239] Fix | Delete
if self.is_simple and other.is_simple:
[240] Fix | Delete
return self.nt <= other.nt
[241] Fix | Delete
n = max(self.longest_run_of_spaces(),
[242] Fix | Delete
other.longest_run_of_spaces()) + 1
[243] Fix | Delete
# the self.n >= other.n test already did it for ts=1
[244] Fix | Delete
for ts in range(2, n+1):
[245] Fix | Delete
if self.indent_level(ts) >= other.indent_level(ts):
[246] Fix | Delete
return False
[247] Fix | Delete
return True
[248] Fix | Delete
[249] Fix | Delete
# return a list of tuples (ts, i1, i2) such that
[250] Fix | Delete
# i1 == self.indent_level(ts) >= other.indent_level(ts) == i2.
[251] Fix | Delete
# Intended to be used after not self.less(other) is known, in which
[252] Fix | Delete
# case it will return at least one witnessing tab size.
[253] Fix | Delete
def not_less_witness(self, other):
[254] Fix | Delete
n = max(self.longest_run_of_spaces(),
[255] Fix | Delete
other.longest_run_of_spaces()) + 1
[256] Fix | Delete
a = []
[257] Fix | Delete
for ts in range(1, n+1):
[258] Fix | Delete
if self.indent_level(ts) >= other.indent_level(ts):
[259] Fix | Delete
a.append( (ts,
[260] Fix | Delete
self.indent_level(ts),
[261] Fix | Delete
other.indent_level(ts)) )
[262] Fix | Delete
return a
[263] Fix | Delete
[264] Fix | Delete
def format_witnesses(w):
[265] Fix | Delete
firsts = map(lambda tup: str(tup[0]), w)
[266] Fix | Delete
prefix = "at tab size"
[267] Fix | Delete
if len(w) > 1:
[268] Fix | Delete
prefix = prefix + "s"
[269] Fix | Delete
return prefix + " " + ', '.join(firsts)
[270] Fix | Delete
[271] Fix | Delete
def process_tokens(tokens):
[272] Fix | Delete
INDENT = tokenize.INDENT
[273] Fix | Delete
DEDENT = tokenize.DEDENT
[274] Fix | Delete
NEWLINE = tokenize.NEWLINE
[275] Fix | Delete
JUNK = tokenize.COMMENT, tokenize.NL
[276] Fix | Delete
indents = [Whitespace("")]
[277] Fix | Delete
check_equal = 0
[278] Fix | Delete
[279] Fix | Delete
for (type, token, start, end, line) in tokens:
[280] Fix | Delete
if type == NEWLINE:
[281] Fix | Delete
# a program statement, or ENDMARKER, will eventually follow,
[282] Fix | Delete
# after some (possibly empty) run of tokens of the form
[283] Fix | Delete
# (NL | COMMENT)* (INDENT | DEDENT+)?
[284] Fix | Delete
# If an INDENT appears, setting check_equal is wrong, and will
[285] Fix | Delete
# be undone when we see the INDENT.
[286] Fix | Delete
check_equal = 1
[287] Fix | Delete
[288] Fix | Delete
elif type == INDENT:
[289] Fix | Delete
check_equal = 0
[290] Fix | Delete
thisguy = Whitespace(token)
[291] Fix | Delete
if not indents[-1].less(thisguy):
[292] Fix | Delete
witness = indents[-1].not_less_witness(thisguy)
[293] Fix | Delete
msg = "indent not greater e.g. " + format_witnesses(witness)
[294] Fix | Delete
raise NannyNag(start[0], msg, line)
[295] Fix | Delete
indents.append(thisguy)
[296] Fix | Delete
[297] Fix | Delete
elif type == DEDENT:
[298] Fix | Delete
# there's nothing we need to check here! what's important is
[299] Fix | Delete
# that when the run of DEDENTs ends, the indentation of the
[300] Fix | Delete
# program statement (or ENDMARKER) that triggered the run is
[301] Fix | Delete
# equal to what's left at the top of the indents stack
[302] Fix | Delete
[303] Fix | Delete
# Ouch! This assert triggers if the last line of the source
[304] Fix | Delete
# is indented *and* lacks a newline -- then DEDENTs pop out
[305] Fix | Delete
# of thin air.
[306] Fix | Delete
# assert check_equal # else no earlier NEWLINE, or an earlier INDENT
[307] Fix | Delete
check_equal = 1
[308] Fix | Delete
[309] Fix | Delete
del indents[-1]
[310] Fix | Delete
[311] Fix | Delete
elif check_equal and type not in JUNK:
[312] Fix | Delete
# this is the first "real token" following a NEWLINE, so it
[313] Fix | Delete
# must be the first token of the next program statement, or an
[314] Fix | Delete
# ENDMARKER; the "line" argument exposes the leading whitespace
[315] Fix | Delete
# for this statement; in the case of ENDMARKER, line is an empty
[316] Fix | Delete
# string, so will properly match the empty string with which the
[317] Fix | Delete
# "indents" stack was seeded
[318] Fix | Delete
check_equal = 0
[319] Fix | Delete
thisguy = Whitespace(line)
[320] Fix | Delete
if not indents[-1].equal(thisguy):
[321] Fix | Delete
witness = indents[-1].not_equal_witness(thisguy)
[322] Fix | Delete
msg = "indent not equal e.g. " + format_witnesses(witness)
[323] Fix | Delete
raise NannyNag(start[0], msg, line)
[324] Fix | Delete
[325] Fix | Delete
[326] Fix | Delete
if __name__ == '__main__':
[327] Fix | Delete
main()
[328] Fix | Delete
[329] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function