3 """The Tab Nanny despises ambiguous indentation. She knows no mercy."""
5 # Released to the public domain, by Tim Peters, 15 April 1998.
21 sys
.stderr
.write(sep
+ str(arg
))
23 sys
.stderr
.write("\n")
26 global verbose
, filename_only
28 opts
, args
= getopt
.getopt(sys
.argv
[1:], "qv")
29 except getopt
.error
, msg
:
34 filename_only
= filename_only
+ 1
38 errprint("Usage:", sys
.argv
[0], "[-v] file_or_directory ...")
44 def __init__(self
, lineno
, msg
, line
):
45 self
.lineno
, self
.msg
, self
.line
= lineno
, msg
, line
54 if os
.path
.isdir(file) and not os
.path
.islink(file):
56 print "%s: listing directory" % `
file`
57 names
= os
.listdir(file)
59 fullname
= os
.path
.join(file, name
)
60 if (os
.path
.isdir(fullname
) and
61 not os
.path
.islink(fullname
) or
62 os
.path
.normcase(name
[-3:]) == ".py"):
69 errprint("%s: I/O Error: %s" % (`
file`
, str(msg
)))
73 print "checking", `
file`
, "..."
77 tokenize
.tokenize(f
.readline
, tokeneater
)
79 except tokenize
.TokenError
, msg
:
80 errprint("%s: Token Error: %s" % (`
file`
, str(msg
)))
84 badline
= nag
.get_lineno()
87 print "%s: *** Line %d: trouble in tab city! ***" % (
89 print "offending line:", `line`
92 if ' ' in file: file = '"' + file + '"'
93 if filename_only
: print file
94 else: print file, badline
, `line`
98 print "%s: Clean bill of health." % `
file`
101 # the characters used for space and tab
106 # the original string
108 # the number of leading whitespace characters in raw
110 # the number of tabs in raw[:n]
112 # the normal form as a pair (count, trailing), where:
114 # a tuple such that raw[:n] contains count[i]
115 # instances of S * i + T
117 # the number of trailing spaces in raw[:n]
118 # It's A Theorem that m.indent_level(t) ==
119 # n.indent_level(t) for all t >= 1 iff m.norm == n.norm.
121 # true iff raw[:n] is of the form (T*)(S*)
123 def __init__(self
, ws
):
125 S
, T
= Whitespace
.S
, Whitespace
.T
136 count
= count
+ [0] * (b
- len(count
) + 1)
137 count
[b
] = count
[b
] + 1
143 self
.norm
= tuple(count
), b
144 self
.is_simple
= len(count
) <= 1
146 # return length of longest contiguous run of spaces (whether or not
148 def longest_run_of_spaces(self
):
149 count
, trailing
= self
.norm
150 return max(len(count
)-1, trailing
)
152 def indent_level(self
, tabsize
):
153 # count, il = self.norm
154 # for i in range(len(count)):
156 # il = il + (i/tabsize + 1)*tabsize * count[i]
160 # il = trailing + sum (i/ts + 1)*ts*count[i] =
161 # trailing + ts * sum (i/ts + 1)*count[i] =
162 # trailing + ts * sum i/ts*count[i] + count[i] =
163 # trailing + ts * [(sum i/ts*count[i]) + (sum count[i])] =
164 # trailing + ts * [(sum i/ts*count[i]) + num_tabs]
165 # and note that i/ts*count[i] is 0 when i < ts
167 count
, trailing
= self
.norm
169 for i
in range(tabsize
, len(count
)):
170 il
= il
+ i
/tabsize
* count
[i
]
171 return trailing
+ tabsize
* (il
+ self
.nt
)
173 # return true iff self.indent_level(t) == other.indent_level(t)
175 def equal(self
, other
):
176 return self
.norm
== other
.norm
178 # return a list of tuples (ts, i1, i2) such that
179 # i1 == self.indent_level(ts) != other.indent_level(ts) == i2.
180 # Intended to be used after not self.equal(other) is known, in which
181 # case it will return at least one witnessing tab size.
182 def not_equal_witness(self
, other
):
183 n
= max(self
.longest_run_of_spaces(),
184 other
.longest_run_of_spaces()) + 1
186 for ts
in range(1, n
+1):
187 if self
.indent_level(ts
) != other
.indent_level(ts
):
189 self
.indent_level(ts
),
190 other
.indent_level(ts
)) )
193 # Return true iff self.indent_level(t) < other.indent_level(t)
195 # The algorithm is due to Vincent Broman.
196 # Easy to prove it's correct.
198 # Trivial to prove n is sharp (consider T vs ST).
199 # Unknown whether there's a faster general way. I suspected so at
200 # first, but no longer.
201 # For the special (but common!) case where M and N are both of the
202 # form (T*)(S*), M.less(N) iff M.len() < N.len() and
203 # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded.
205 # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1.
206 def less(self
, other
):
207 if self
.n
>= other
.n
:
209 if self
.is_simple
and other
.is_simple
:
210 return self
.nt
<= other
.nt
211 n
= max(self
.longest_run_of_spaces(),
212 other
.longest_run_of_spaces()) + 1
213 # the self.n >= other.n test already did it for ts=1
214 for ts
in range(2, n
+1):
215 if self
.indent_level(ts
) >= other
.indent_level(ts
):
219 # return a list of tuples (ts, i1, i2) such that
220 # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2.
221 # Intended to be used after not self.less(other) is known, in which
222 # case it will return at least one witnessing tab size.
223 def not_less_witness(self
, other
):
224 n
= max(self
.longest_run_of_spaces(),
225 other
.longest_run_of_spaces()) + 1
227 for ts
in range(1, n
+1):
228 if self
.indent_level(ts
) >= other
.indent_level(ts
):
230 self
.indent_level(ts
),
231 other
.indent_level(ts
)) )
234 def format_witnesses(w
):
236 firsts
= map(lambda tup
: str(tup
[0]), w
)
237 prefix
= "at tab size"
239 prefix
= prefix
+ "s"
240 return prefix
+ " " + string
.join(firsts
, ', ')
242 # The collection of globals, the reset_globals() function, and the
243 # tokeneater() function, depend on which version of tokenize is
246 if hasattr(tokenize
, 'NL'):
247 # take advantage of Guido's patch!
253 global indents
, check_equal
255 indents
= [Whitespace("")]
257 def tokeneater(type, token
, start
, end
, line
,
258 INDENT
=tokenize
.INDENT
,
259 DEDENT
=tokenize
.DEDENT
,
260 NEWLINE
=tokenize
.NEWLINE
,
261 JUNK
=(tokenize
.COMMENT
, tokenize
.NL
) ):
262 global indents
, check_equal
265 # a program statement, or ENDMARKER, will eventually follow,
266 # after some (possibly empty) run of tokens of the form
267 # (NL | COMMENT)* (INDENT | DEDENT+)?
268 # If an INDENT appears, setting check_equal is wrong, and will
269 # be undone when we see the INDENT.
274 thisguy
= Whitespace(token
)
275 if not indents
[-1].less(thisguy
):
276 witness
= indents
[-1].not_less_witness(thisguy
)
277 msg
= "indent not greater e.g. " + format_witnesses(witness
)
278 raise NannyNag(start
[0], msg
, line
)
279 indents
.append(thisguy
)
282 # there's nothing we need to check here! what's important is
283 # that when the run of DEDENTs ends, the indentation of the
284 # program statement (or ENDMARKER) that triggered the run is
285 # equal to what's left at the top of the indents stack
287 # Ouch! This assert triggers if the last line of the source
288 # is indented *and* lacks a newline -- then DEDENTs pop out
290 # assert check_equal # else no earlier NEWLINE, or an earlier INDENT
295 elif check_equal
and type not in JUNK
:
296 # this is the first "real token" following a NEWLINE, so it
297 # must be the first token of the next program statement, or an
298 # ENDMARKER; the "line" argument exposes the leading whitespace
299 # for this statement; in the case of ENDMARKER, line is an empty
300 # string, so will properly match the empty string with which the
301 # "indents" stack was seeded
303 thisguy
= Whitespace(line
)
304 if not indents
[-1].equal(thisguy
):
305 witness
= indents
[-1].not_equal_witness(thisguy
)
306 msg
= "indent not equal e.g. " + format_witnesses(witness
)
307 raise NannyNag(start
[0], msg
, line
)
310 # unpatched version of tokenize
317 global nesting_level
, indents
, check_equal
318 nesting_level
= check_equal
= 0
319 indents
= [Whitespace("")]
321 def tokeneater(type, token
, start
, end
, line
,
322 INDENT
=tokenize
.INDENT
,
323 DEDENT
=tokenize
.DEDENT
,
324 NEWLINE
=tokenize
.NEWLINE
,
325 COMMENT
=tokenize
.COMMENT
,
327 global nesting_level
, indents
, check_equal
331 thisguy
= Whitespace(token
)
332 if not indents
[-1].less(thisguy
):
333 witness
= indents
[-1].not_less_witness(thisguy
)
334 msg
= "indent not greater e.g. " + format_witnesses(witness
)
335 raise NannyNag(start
[0], msg
, line
)
336 indents
.append(thisguy
)
341 elif type == NEWLINE
:
342 if nesting_level
== 0:
345 elif type == COMMENT
:
350 thisguy
= Whitespace(line
)
351 if not indents
[-1].equal(thisguy
):
352 witness
= indents
[-1].not_equal_witness(thisguy
)
353 msg
= "indent not equal e.g. " + format_witnesses(witness
)
354 raise NannyNag(start
[0], msg
, line
)
356 if type == OP
and token
in ('{', '[', '('):
357 nesting_level
= nesting_level
+ 1
359 elif type == OP
and token
in ('}', ']', ')'):
360 if nesting_level
== 0:
361 raise NannyNag(start
[0],
362 "unbalanced bracket '" + token
+ "'",
364 nesting_level
= nesting_level
- 1
366 if __name__
== '__main__':