Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
tabnanny Namespace Reference

Data Structures

class  NannyNag
 
class  Whitespace
 

Functions

def errprint
 
def main
 
def check
 
def format_witnesses
 
def process_tokens
 

Variables

string __version__ = "6"
 
list __all__ = ["check", "NannyNag", "process_tokens"]
 
int verbose = 0
 
int filename_only = 0
 

Function Documentation

def tabnanny.check (   file)

Definition at line 60 of file tabnanny.py.

References errprint(), aifc.open(), process_tokens(), and locale.str().

60 
61 def check(file):
62  if os.path.isdir(file) and not os.path.islink(file):
63  if verbose:
64  print "%s: listing directory" % `file`
65  names = os.listdir(file)
66  for name in names:
67  fullname = os.path.join(file, name)
68  if (os.path.isdir(fullname) and
69  not os.path.islink(fullname) or
70  os.path.normcase(name[-3:]) == ".py"):
71  check(fullname)
72  return
73 
74  try:
75  f = open(file)
76  except IOError, msg:
77  errprint("%s: I/O Error: %s" % (`file`, str(msg)))
78  return
79 
80  if verbose > 1:
81  print "checking", `file`, "..."
82 
83  try:
84  process_tokens(tokenize.generate_tokens(f.readline))
85 
86  except tokenize.TokenError, msg:
87  errprint("%s: Token Error: %s" % (`file`, str(msg)))
88  return
89 
90  except NannyNag, nag:
91  badline = nag.get_lineno()
92  line = nag.get_line()
93  if verbose:
94  print "%s: *** Line %d: trouble in tab city! ***" % (
95  `file`, badline)
96  print "offending line:", `line`
97  print nag.get_msg()
98  else:
99  if ' ' in file: file = '"' + file + '"'
100  if filename_only: print file
101  else: print file, badline, `line`
102  return
103 
104  if verbose:
105  print "%s: Clean bill of health." % `file`
def tabnanny.errprint (   args)

Definition at line 25 of file tabnanny.py.

References locale.str().

25 
26 def errprint(*args):
27  sep = ""
28  for arg in args:
29  sys.stderr.write(sep + str(arg))
30  sep = " "
31  sys.stderr.write("\n")
def tabnanny.format_witnesses (   w)

Definition at line 240 of file tabnanny.py.

References string.join(), and locale.str().

241 def format_witnesses(w):
242  import string
243  firsts = map(lambda tup: str(tup[0]), w)
244  prefix = "at tab size"
245  if len(w) > 1:
246  prefix = prefix + "s"
247  return prefix + " " + string.join(firsts, ', ')
def tabnanny.main ( )

Definition at line 32 of file tabnanny.py.

References check(), errprint(), and getopt.getopt().

32 
33 def main():
34  global verbose, filename_only
35  try:
36  opts, args = getopt.getopt(sys.argv[1:], "qv")
37  except getopt.error, msg:
38  errprint(msg)
39  return
40  for o, a in opts:
41  if o == '-q':
42  filename_only = filename_only + 1
43  if o == '-v':
44  verbose = verbose + 1
45  if not args:
46  errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...")
47  return
48  for arg in args:
49  check(arg)
def tabnanny.process_tokens (   tokens)

Definition at line 248 of file tabnanny.py.

References format_witnesses(), and main().

249 def process_tokens(tokens):
250  INDENT = tokenize.INDENT
251  DEDENT = tokenize.DEDENT
252  NEWLINE = tokenize.NEWLINE
253  JUNK = tokenize.COMMENT, tokenize.NL
254  indents = [Whitespace("")]
255  check_equal = 0
256 
257  for (type, token, start, end, line) in tokens:
258  if type == NEWLINE:
259  # a program statement, or ENDMARKER, will eventually follow,
260  # after some (possibly empty) run of tokens of the form
261  # (NL | COMMENT)* (INDENT | DEDENT+)?
262  # If an INDENT appears, setting check_equal is wrong, and will
263  # be undone when we see the INDENT.
264  check_equal = 1
265 
266  elif type == INDENT:
267  check_equal = 0
268  thisguy = Whitespace(token)
269  if not indents[-1].less(thisguy):
270  witness = indents[-1].not_less_witness(thisguy)
271  msg = "indent not greater e.g. " + format_witnesses(witness)
272  raise NannyNag(start[0], msg, line)
273  indents.append(thisguy)
274 
275  elif type == DEDENT:
276  # there's nothing we need to check here! what's important is
277  # that when the run of DEDENTs ends, the indentation of the
278  # program statement (or ENDMARKER) that triggered the run is
279  # equal to what's left at the top of the indents stack
280 
281  # Ouch! This assert triggers if the last line of the source
282  # is indented *and* lacks a newline -- then DEDENTs pop out
283  # of thin air.
284  # assert check_equal # else no earlier NEWLINE, or an earlier INDENT
285  check_equal = 1
286 
287  del indents[-1]
288 
289  elif check_equal and type not in JUNK:
290  # this is the first "real token" following a NEWLINE, so it
291  # must be the first token of the next program statement, or an
292  # ENDMARKER; the "line" argument exposes the leading whitespace
293  # for this statement; in the case of ENDMARKER, line is an empty
294  # string, so will properly match the empty string with which the
295  # "indents" stack was seeded
296  check_equal = 0
297  thisguy = Whitespace(line)
298  if not indents[-1].equal(thisguy):
299  witness = indents[-1].not_equal_witness(thisguy)
300  msg = "indent not equal e.g. " + format_witnesses(witness)
301  raise NannyNag(start[0], msg, line)
302 

Variable Documentation

list __all__ = ["check", "NannyNag", "process_tokens"]

Definition at line 20 of file tabnanny.py.

string __version__ = "6"

Definition at line 11 of file tabnanny.py.

int filename_only = 0

Definition at line 23 of file tabnanny.py.

int verbose = 0

Definition at line 22 of file tabnanny.py.