Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f73c1a

Browse files
committed
The Tab Police watches for possibly inconsistent indentation.
1 parent 78fc363 commit 6f73c1a

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Tools/scripts/tabpolice.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#! /usr/bin/env python
2+
3+
"""The Tab Police watches for possibly inconsistent indentation."""
4+
5+
import os
6+
import sys
7+
import getopt
8+
import string
9+
import tokenize
10+
11+
verbose = 0
12+
13+
def main():
14+
global verbose
15+
try:
16+
opts, args = getopt.getopt(sys.argv[1:], "v")
17+
except getopt.error, msg:
18+
print msg
19+
for o, a in opts:
20+
if o == '-v':
21+
verbose = verbose + 1
22+
for arg in args:
23+
check(arg)
24+
25+
def check(file):
26+
if os.path.isdir(file) and not os.path.islink(file):
27+
if verbose:
28+
print "%s: listing directory" % `file`
29+
names = os.listdir(file)
30+
for name in names:
31+
fullname = os.path.join(file, name)
32+
if (os.path.isdir(fullname) and
33+
not os.path.islink(fullname) or
34+
os.path.normcase(name[-3:]) == ".py"):
35+
check(fullname)
36+
return
37+
38+
try:
39+
f = open(file)
40+
except IOError, msg:
41+
print "%s: I/O Error: %s" % (`file`, str(msg))
42+
return
43+
44+
if verbose > 1:
45+
print "checking", `file`, "with tabsize 8..."
46+
tokens = []
47+
tokenize.tabsize = 8
48+
try:
49+
tokenize.tokenize(f.readline, tokens.append)
50+
except tokenize.TokenError, msg:
51+
print "%s: Token Error: %s" % (`file`, str(msg))
52+
53+
if verbose > 1:
54+
print "checking", `file`, "with tabsize 4..."
55+
f.seek(0)
56+
alttokens = []
57+
tokenize.tabsize = 4
58+
try:
59+
tokenize.tokenize(f.readline, alttokens.append)
60+
except tokenize.TokenError, msg:
61+
print "%s: Token Error: %s" % (`file`, str(msg))
62+
f.close()
63+
64+
if tokens != alttokens:
65+
if verbose:
66+
print "%s: *** Trouble in tab city! ***" % `file`
67+
else:
68+
print file
69+
else:
70+
if verbose:
71+
print "%s: Clean bill of health." % `file`
72+
73+
if __name__ == '__main__':
74+
main()

0 commit comments

Comments
 (0)