+++ /dev/null
-#! /usr/bin/env python\r
-\r
-"""The Tab Nanny despises ambiguous indentation. She knows no mercy.\r
-\r
-tabnanny -- Detection of ambiguous indentation\r
-\r
-For the time being this module is intended to be called as a script.\r
-However it is possible to import it into an IDE and use the function\r
-check() described below.\r
-\r
-Warning: The API provided by this module is likely to change in future\r
-releases; such changes may not be backward compatible.\r
-"""\r
-\r
-# Released to the public domain, by Tim Peters, 15 April 1998.\r
-\r
-# XXX Note: this is now a standard library module.\r
-# XXX The API needs to undergo changes however; the current code is too\r
-# XXX script-like. This will be addressed later.\r
-\r
-__version__ = "6"\r
-\r
-import os\r
-import sys\r
-import getopt\r
-import tokenize\r
-if not hasattr(tokenize, 'NL'):\r
- raise ValueError("tokenize.NL doesn't exist -- tokenize module too old")\r
-\r
-__all__ = ["check", "NannyNag", "process_tokens"]\r
-\r
-verbose = 0\r
-filename_only = 0\r
-\r
-def errprint(*args):\r
- sep = ""\r
- for arg in args:\r
- sys.stderr.write(sep + str(arg))\r
- sep = " "\r
- sys.stderr.write("\n")\r
-\r
-def main():\r
- global verbose, filename_only\r
- try:\r
- opts, args = getopt.getopt(sys.argv[1:], "qv")\r
- except getopt.error, msg:\r
- errprint(msg)\r
- return\r
- for o, a in opts:\r
- if o == '-q':\r
- filename_only = filename_only + 1\r
- if o == '-v':\r
- verbose = verbose + 1\r
- if not args:\r
- errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...")\r
- return\r
- for arg in args:\r
- check(arg)\r
-\r
-class NannyNag(Exception):\r
- """\r
- Raised by tokeneater() if detecting an ambiguous indent.\r
- Captured and handled in check().\r
- """\r
- def __init__(self, lineno, msg, line):\r
- self.lineno, self.msg, self.line = lineno, msg, line\r
- def get_lineno(self):\r
- return self.lineno\r
- def get_msg(self):\r
- return self.msg\r
- def get_line(self):\r
- return self.line\r
-\r
-def check(file):\r
- """check(file_or_dir)\r
-\r
- If file_or_dir is a directory and not a symbolic link, then recursively\r
- descend the directory tree named by file_or_dir, checking all .py files\r
- along the way. If file_or_dir is an ordinary Python source file, it is\r
- checked for whitespace related problems. The diagnostic messages are\r
- written to standard output using the print statement.\r
- """\r
-\r
- if os.path.isdir(file) and not os.path.islink(file):\r
- if verbose:\r
- print "%r: listing directory" % (file,)\r
- names = os.listdir(file)\r
- for name in names:\r
- fullname = os.path.join(file, name)\r
- if (os.path.isdir(fullname) and\r
- not os.path.islink(fullname) or\r
- os.path.normcase(name[-3:]) == ".py"):\r
- check(fullname)\r
- return\r
-\r
- try:\r
- f = open(file)\r
- except IOError, msg:\r
- errprint("%r: I/O Error: %s" % (file, msg))\r
- return\r
-\r
- if verbose > 1:\r
- print "checking %r ..." % file\r
-\r
- try:\r
- process_tokens(tokenize.generate_tokens(f.readline))\r
-\r
- except tokenize.TokenError, msg:\r
- errprint("%r: Token Error: %s" % (file, msg))\r
- return\r
-\r
- except IndentationError, msg:\r
- errprint("%r: Indentation Error: %s" % (file, msg))\r
- return\r
-\r
- except NannyNag, nag:\r
- badline = nag.get_lineno()\r
- line = nag.get_line()\r
- if verbose:\r
- print "%r: *** Line %d: trouble in tab city! ***" % (file, badline)\r
- print "offending line: %r" % (line,)\r
- print nag.get_msg()\r
- else:\r
- if ' ' in file: file = '"' + file + '"'\r
- if filename_only: print file\r
- else: print file, badline, repr(line)\r
- return\r
-\r
- if verbose:\r
- print "%r: Clean bill of health." % (file,)\r
-\r
-class Whitespace:\r
- # the characters used for space and tab\r
- S, T = ' \t'\r
-\r
- # members:\r
- # raw\r
- # the original string\r
- # n\r
- # the number of leading whitespace characters in raw\r
- # nt\r
- # the number of tabs in raw[:n]\r
- # norm\r
- # the normal form as a pair (count, trailing), where:\r
- # count\r
- # a tuple such that raw[:n] contains count[i]\r
- # instances of S * i + T\r
- # trailing\r
- # the number of trailing spaces in raw[:n]\r
- # It's A Theorem that m.indent_level(t) ==\r
- # n.indent_level(t) for all t >= 1 iff m.norm == n.norm.\r
- # is_simple\r
- # true iff raw[:n] is of the form (T*)(S*)\r
-\r
- def __init__(self, ws):\r
- self.raw = ws\r
- S, T = Whitespace.S, Whitespace.T\r
- count = []\r
- b = n = nt = 0\r
- for ch in self.raw:\r
- if ch == S:\r
- n = n + 1\r
- b = b + 1\r
- elif ch == T:\r
- n = n + 1\r
- nt = nt + 1\r
- if b >= len(count):\r
- count = count + [0] * (b - len(count) + 1)\r
- count[b] = count[b] + 1\r
- b = 0\r
- else:\r
- break\r
- self.n = n\r
- self.nt = nt\r
- self.norm = tuple(count), b\r
- self.is_simple = len(count) <= 1\r
-\r
- # return length of longest contiguous run of spaces (whether or not\r
- # preceding a tab)\r
- def longest_run_of_spaces(self):\r
- count, trailing = self.norm\r
- return max(len(count)-1, trailing)\r
-\r
- def indent_level(self, tabsize):\r
- # count, il = self.norm\r
- # for i in range(len(count)):\r
- # if count[i]:\r
- # il = il + (i/tabsize + 1)*tabsize * count[i]\r
- # return il\r
-\r
- # quicker:\r
- # il = trailing + sum (i/ts + 1)*ts*count[i] =\r
- # trailing + ts * sum (i/ts + 1)*count[i] =\r
- # trailing + ts * sum i/ts*count[i] + count[i] =\r
- # trailing + ts * [(sum i/ts*count[i]) + (sum count[i])] =\r
- # trailing + ts * [(sum i/ts*count[i]) + num_tabs]\r
- # and note that i/ts*count[i] is 0 when i < ts\r
-\r
- count, trailing = self.norm\r
- il = 0\r
- for i in range(tabsize, len(count)):\r
- il = il + i/tabsize * count[i]\r
- return trailing + tabsize * (il + self.nt)\r
-\r
- # return true iff self.indent_level(t) == other.indent_level(t)\r
- # for all t >= 1\r
- def equal(self, other):\r
- return self.norm == other.norm\r
-\r
- # return a list of tuples (ts, i1, i2) such that\r
- # i1 == self.indent_level(ts) != other.indent_level(ts) == i2.\r
- # Intended to be used after not self.equal(other) is known, in which\r
- # case it will return at least one witnessing tab size.\r
- def not_equal_witness(self, other):\r
- n = max(self.longest_run_of_spaces(),\r
- other.longest_run_of_spaces()) + 1\r
- a = []\r
- for ts in range(1, n+1):\r
- if self.indent_level(ts) != other.indent_level(ts):\r
- a.append( (ts,\r
- self.indent_level(ts),\r
- other.indent_level(ts)) )\r
- return a\r
-\r
- # Return True iff self.indent_level(t) < other.indent_level(t)\r
- # for all t >= 1.\r
- # The algorithm is due to Vincent Broman.\r
- # Easy to prove it's correct.\r
- # XXXpost that.\r
- # Trivial to prove n is sharp (consider T vs ST).\r
- # Unknown whether there's a faster general way. I suspected so at\r
- # first, but no longer.\r
- # For the special (but common!) case where M and N are both of the\r
- # form (T*)(S*), M.less(N) iff M.len() < N.len() and\r
- # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded.\r
- # XXXwrite that up.\r
- # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1.\r
- def less(self, other):\r
- if self.n >= other.n:\r
- return False\r
- if self.is_simple and other.is_simple:\r
- return self.nt <= other.nt\r
- n = max(self.longest_run_of_spaces(),\r
- other.longest_run_of_spaces()) + 1\r
- # the self.n >= other.n test already did it for ts=1\r
- for ts in range(2, n+1):\r
- if self.indent_level(ts) >= other.indent_level(ts):\r
- return False\r
- return True\r
-\r
- # return a list of tuples (ts, i1, i2) such that\r
- # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2.\r
- # Intended to be used after not self.less(other) is known, in which\r
- # case it will return at least one witnessing tab size.\r
- def not_less_witness(self, other):\r
- n = max(self.longest_run_of_spaces(),\r
- other.longest_run_of_spaces()) + 1\r
- a = []\r
- for ts in range(1, n+1):\r
- if self.indent_level(ts) >= other.indent_level(ts):\r
- a.append( (ts,\r
- self.indent_level(ts),\r
- other.indent_level(ts)) )\r
- return a\r
-\r
-def format_witnesses(w):\r
- firsts = map(lambda tup: str(tup[0]), w)\r
- prefix = "at tab size"\r
- if len(w) > 1:\r
- prefix = prefix + "s"\r
- return prefix + " " + ', '.join(firsts)\r
-\r
-def process_tokens(tokens):\r
- INDENT = tokenize.INDENT\r
- DEDENT = tokenize.DEDENT\r
- NEWLINE = tokenize.NEWLINE\r
- JUNK = tokenize.COMMENT, tokenize.NL\r
- indents = [Whitespace("")]\r
- check_equal = 0\r
-\r
- for (type, token, start, end, line) in tokens:\r
- if type == NEWLINE:\r
- # a program statement, or ENDMARKER, will eventually follow,\r
- # after some (possibly empty) run of tokens of the form\r
- # (NL | COMMENT)* (INDENT | DEDENT+)?\r
- # If an INDENT appears, setting check_equal is wrong, and will\r
- # be undone when we see the INDENT.\r
- check_equal = 1\r
-\r
- elif type == INDENT:\r
- check_equal = 0\r
- thisguy = Whitespace(token)\r
- if not indents[-1].less(thisguy):\r
- witness = indents[-1].not_less_witness(thisguy)\r
- msg = "indent not greater e.g. " + format_witnesses(witness)\r
- raise NannyNag(start[0], msg, line)\r
- indents.append(thisguy)\r
-\r
- elif type == DEDENT:\r
- # there's nothing we need to check here! what's important is\r
- # that when the run of DEDENTs ends, the indentation of the\r
- # program statement (or ENDMARKER) that triggered the run is\r
- # equal to what's left at the top of the indents stack\r
-\r
- # Ouch! This assert triggers if the last line of the source\r
- # is indented *and* lacks a newline -- then DEDENTs pop out\r
- # of thin air.\r
- # assert check_equal # else no earlier NEWLINE, or an earlier INDENT\r
- check_equal = 1\r
-\r
- del indents[-1]\r
-\r
- elif check_equal and type not in JUNK:\r
- # this is the first "real token" following a NEWLINE, so it\r
- # must be the first token of the next program statement, or an\r
- # ENDMARKER; the "line" argument exposes the leading whitespace\r
- # for this statement; in the case of ENDMARKER, line is an empty\r
- # string, so will properly match the empty string with which the\r
- # "indents" stack was seeded\r
- check_equal = 0\r
- thisguy = Whitespace(line)\r
- if not indents[-1].equal(thisguy):\r
- witness = indents[-1].not_equal_witness(thisguy)\r
- msg = "indent not equal e.g. " + format_witnesses(witness)\r
- raise NannyNag(start[0], msg, line)\r
-\r
-\r
-if __name__ == '__main__':\r
- main()\r