3 """The Tab Nanny despises ambiguous indentation. She knows no mercy."""
5 # Released to the public domain, by Tim Peters, 15 April 1998.
19 opts
, args
= getopt
.getopt(sys
.argv
[1:], "v")
20 except getopt
.error
, msg
:
27 print "Usage:", sys
.argv
[0], "[-v] file_or_directory ..."
33 def __init__(self
, lineno
, msg
, line
):
34 self
.lineno
, self
.msg
, self
.line
= lineno
, msg
, line
43 if os
.path
.isdir(file) and not os
.path
.islink(file):
45 print "%s: listing directory" % `
file`
46 names
= os
.listdir(file)
48 fullname
= os
.path
.join(file, name
)
49 if (os
.path
.isdir(fullname
) and
50 not os
.path
.islink(fullname
) or
51 os
.path
.normcase(name
[-3:]) == ".py"):
58 print "%s: I/O Error: %s" % (`
file`
, str(msg
))
62 print "checking", `
file`
, "..."
66 tokenize
.tokenize(f
.readline
, tokeneater
)
68 except tokenize
.TokenError
, msg
:
69 print "%s: Token Error: %s" % (`
file`
, str(msg
))
73 badline
= nag
.get_lineno()
76 print "%s: *** Line %d: trouble in tab city! ***" % (
78 print "offending line:", `line`
81 print file, badline
, `line`
85 print "%s: Clean bill of health." % `
file`
88 # the characters used for space and tab
95 # the number of leading whitespace characters in raw
97 # the number of tabs in raw[:n]
99 # the normal form as a pair (count, trailing), where:
101 # a tuple such that raw[:n] contains count[i]
102 # instances of S * i + T
104 # the number of trailing spaces in raw[:n]
105 # It's A Theorem that m.indent_level(t) ==
106 # n.indent_level(t) for all t >= 1 iff m.norm == n.norm.
108 # true iff raw[:n] is of the form (T*)(S*)
110 def __init__(self
, ws
):
112 S
, T
= Whitespace
.S
, Whitespace
.T
123 count
= count
+ [0] * (b
- len(count
) + 1)
124 count
[b
] = count
[b
] + 1
130 self
.norm
= tuple(count
), b
131 self
.is_simple
= len(count
) <= 1
133 # return length of longest contiguous run of spaces (whether or not
135 def longest_run_of_spaces(self
):
136 count
, trailing
= self
.norm
137 return max(len(count
)-1, trailing
)
139 def indent_level(self
, tabsize
):
140 # count, il = self.norm
141 # for i in range(len(count)):
143 # il = il + (i/tabsize + 1)*tabsize * count[i]
147 # il = trailing + sum (i/ts + 1)*ts*count[i] =
148 # trailing + ts * sum (i/ts + 1)*count[i] =
149 # trailing + ts * sum i/ts*count[i] + count[i] =
150 # trailing + ts * [(sum i/ts*count[i]) + (sum count[i])] =
151 # trailing + ts * [(sum i/ts*count[i]) + num_tabs]
152 # and note that i/ts*count[i] is 0 when i < ts
154 count
, trailing
= self
.norm
156 for i
in range(tabsize
, len(count
)):
157 il
= il
+ i
/tabsize
* count
[i
]
158 return trailing
+ tabsize
* (il
+ self
.nt
)
160 # return true iff self.indent_level(t) == other.indent_level(t)
162 def equal(self
, other
):
163 return self
.norm
== other
.norm
165 # return a list of tuples (ts, i1, i2) such that
166 # i1 == self.indent_level(ts) != other.indent_level(ts) == i2.
167 # Intended to be used after not self.equal(other) is known, in which
168 # case it will return at least one witnessing tab size.
169 def not_equal_witness(self
, other
):
170 n
= max(self
.longest_run_of_spaces(),
171 other
.longest_run_of_spaces()) + 1
173 for ts
in range(1, n
+1):
174 if self
.indent_level(ts
) != other
.indent_level(ts
):
176 self
.indent_level(ts
),
177 other
.indent_level(ts
)) )
180 # Return true iff self.indent_level(t) < other.indent_level(t)
182 # The algorithm is due to Vincent Broman.
183 # Easy to prove it's correct.
185 # Trivial to prove n is sharp (consider T vs ST).
186 # Unknown whether there's a faster general way. I suspected so at
187 # first, but no longer.
188 # For the special (but common!) case where M and N are both of the
189 # form (T*)(S*), M.less(N) iff M.len() < N.len() and
190 # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded.
192 # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1.
193 def less(self
, other
):
194 if self
.n
>= other
.n
:
196 if self
.is_simple
and other
.is_simple
:
197 return self
.nt
<= other
.nt
198 n
= max(self
.longest_run_of_spaces(),
199 other
.longest_run_of_spaces()) + 1
200 # the self.n >= other.n test already did it for ts=1
201 for ts
in range(2, n
+1):
202 if self
.indent_level(ts
) >= other
.indent_level(ts
):
206 # return a list of tuples (ts, i1, i2) such that
207 # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2.
208 # Intended to be used after not self.less(other) is known, in which
209 # case it will return at least one witnessing tab size.
210 def not_less_witness(self
, other
):
211 n
= max(self
.longest_run_of_spaces(),
212 other
.longest_run_of_spaces()) + 1
214 for ts
in range(1, n
+1):
215 if self
.indent_level(ts
) >= other
.indent_level(ts
):
217 self
.indent_level(ts
),
218 other
.indent_level(ts
)) )
221 def format_witnesses(w
):
223 firsts
= map(lambda tup
: str(tup
[0]), w
)
224 prefix
= "at tab size"
226 prefix
= prefix
+ "s"
227 return prefix
+ " " + string
.join(firsts
, ', ')
229 # The collection of globals, the reset_globals() function, and the
230 # tokeneater() function, depend on which version of tokenize is
233 if hasattr(tokenize
, 'NL'):
234 # take advantage of Guido's patch!
240 global indents
, check_equal
242 indents
= [Whitespace("")]
244 def tokeneater(type, token
, start
, end
, line
,
245 INDENT
=tokenize
.INDENT
,
246 DEDENT
=tokenize
.DEDENT
,
247 NEWLINE
=tokenize
.NEWLINE
,
248 JUNK
=(tokenize
.COMMENT
, tokenize
.NL
) ):
249 global indents
, check_equal
252 # a program statement, or ENDMARKER, will eventually follow,
253 # after some (possibly empty) run of tokens of the form
254 # (NL | COMMENT)* (INDENT | DEDENT+)?
255 # If an INDENT appears, setting check_equal is wrong, and will
256 # be undone when we see the INDENT.
261 thisguy
= Whitespace(token
)
262 if not indents
[-1].less(thisguy
):
263 witness
= indents
[-1].not_less_witness(thisguy
)
264 msg
= "indent not greater e.g. " + format_witnesses(witness
)
265 raise NannyNag(start
[0], msg
, line
)
266 indents
.append(thisguy
)
269 # there's nothing we need to check here! what's important is
270 # that when the run of DEDENTs ends, the indentation of the
271 # program statement (or ENDMARKER) that triggered the run is
272 # equal to what's left at the top of the indents stack
274 # Ouch! This assert triggers if the last line of the source
275 # is indented *and* lacks a newline -- then DEDENTs pop out
277 # assert check_equal # else no earlier NEWLINE, or an earlier INDENT
282 elif check_equal
and type not in JUNK
:
283 # this is the first "real token" following a NEWLINE, so it
284 # must be the first token of the next program statement, or an
285 # ENDMARKER; the "line" argument exposes the leading whitespace
286 # for this statement; in the case of ENDMARKER, line is an empty
287 # string, so will properly match the empty string with which the
288 # "indents" stack was seeded
290 thisguy
= Whitespace(line
)
291 if not indents
[-1].equal(thisguy
):
292 witness
= indents
[-1].not_equal_witness(thisguy
)
293 msg
= "indent not equal e.g. " + format_witnesses(witness
)
294 raise NannyNag(start
[0], msg
, line
)
297 # unpatched version of tokenize
304 global nesting_level
, indents
, check_equal
305 nesting_level
= check_equal
= 0
306 indents
= [Whitespace("")]
308 def tokeneater(type, token
, start
, end
, line
,
309 INDENT
=tokenize
.INDENT
,
310 DEDENT
=tokenize
.DEDENT
,
311 NEWLINE
=tokenize
.NEWLINE
,
312 COMMENT
=tokenize
.COMMENT
,
314 global nesting_level
, indents
, check_equal
318 thisguy
= Whitespace(token
)
319 if not indents
[-1].less(thisguy
):
320 witness
= indents
[-1].not_less_witness(thisguy
)
321 msg
= "indent not greater e.g. " + format_witnesses(witness
)
322 raise NannyNag(start
[0], msg
, line
)
323 indents
.append(thisguy
)
328 elif type == NEWLINE
:
329 if nesting_level
== 0:
332 elif type == COMMENT
:
337 thisguy
= Whitespace(line
)
338 if not indents
[-1].equal(thisguy
):
339 witness
= indents
[-1].not_equal_witness(thisguy
)
340 msg
= "indent not equal e.g. " + format_witnesses(witness
)
341 raise NannyNag(start
[0], msg
, line
)
343 if type == OP
and token
in ('{', '[', '('):
344 nesting_level
= nesting_level
+ 1
346 elif type == OP
and token
in ('}', ']', ')'):
347 if nesting_level
== 0:
348 raise NannyNag(start
[0],
349 "unbalanced bracket '" + token
+ "'",
351 nesting_level
= nesting_level
- 1
353 if __name__
== '__main__':