Back to index

python3.2  3.2.2
Classes | Functions | Variables
tabnanny Namespace Reference

Classes

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)
check(file_or_dir)

If file_or_dir is a directory and not a symbolic link, then recursively
descend the directory tree named by file_or_dir, checking all .py files
along the way. If file_or_dir is an ordinary Python source file, it is
checked for whitespace related problems. The diagnostic messages are
written to standard output using the print statement.

Definition at line 74 of file tabnanny.py.

00074 
00075 def check(file):
00076     """check(file_or_dir)
00077 
00078     If file_or_dir is a directory and not a symbolic link, then recursively
00079     descend the directory tree named by file_or_dir, checking all .py files
00080     along the way. If file_or_dir is an ordinary Python source file, it is
00081     checked for whitespace related problems. The diagnostic messages are
00082     written to standard output using the print statement.
00083     """
00084 
00085     if os.path.isdir(file) and not os.path.islink(file):
00086         if verbose:
00087             print("%r: listing directory" % (file,))
00088         names = os.listdir(file)
00089         for name in names:
00090             fullname = os.path.join(file, name)
00091             if (os.path.isdir(fullname) and
00092                 not os.path.islink(fullname) or
00093                 os.path.normcase(name[-3:]) == ".py"):
00094                 check(fullname)
00095         return
00096 
00097     try:
00098         f = tokenize.open(file)
00099     except IOError as msg:
00100         errprint("%r: I/O Error: %s" % (file, msg))
00101         return
00102 
00103     if verbose > 1:
00104         print("checking %r ..." % file)
00105 
00106     try:
00107         process_tokens(tokenize.generate_tokens(f.readline))
00108 
00109     except tokenize.TokenError as msg:
00110         errprint("%r: Token Error: %s" % (file, msg))
00111         return
00112 
00113     except IndentationError as msg:
00114         errprint("%r: Indentation Error: %s" % (file, msg))
00115         return
00116 
00117     except NannyNag as nag:
00118         badline = nag.get_lineno()
00119         line = nag.get_line()
00120         if verbose:
00121             print("%r: *** Line %d: trouble in tab city! ***" % (file, badline))
00122             print("offending line: %r" % (line,))
00123             print(nag.get_msg())
00124         else:
00125             if ' ' in file: file = '"' + file + '"'
00126             if filename_only: print(file)
00127             else: print(file, badline, repr(line))
00128         return
00129 
00130     if verbose:
00131         print("%r: Clean bill of health." % (file,))

Here is the call graph for this function:

Here is the caller graph for this function:

def tabnanny.errprint (   args)

Definition at line 35 of file tabnanny.py.

00035 
00036 def errprint(*args):
00037     sep = ""
00038     for arg in args:
00039         sys.stderr.write(sep + str(arg))
00040         sep = " "
00041     sys.stderr.write("\n")

Here is the caller graph for this function:

Definition at line 266 of file tabnanny.py.

00266 
00267 def format_witnesses(w):
00268     firsts = (str(tup[0]) for tup in w)
00269     prefix = "at tab size"
00270     if len(w) > 1:
00271         prefix = prefix + "s"
00272     return prefix + " " + ', '.join(firsts)

Here is the caller graph for this function:

def tabnanny.main ( void  )

Definition at line 42 of file tabnanny.py.

00042 
00043 def main():
00044     global verbose, filename_only
00045     try:
00046         opts, args = getopt.getopt(sys.argv[1:], "qv")
00047     except getopt.error as msg:
00048         errprint(msg)
00049         return
00050     for o, a in opts:
00051         if o == '-q':
00052             filename_only = filename_only + 1
00053         if o == '-v':
00054             verbose = verbose + 1
00055     if not args:
00056         errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...")
00057         return
00058     for arg in args:
00059         check(arg)

Here is the call graph for this function:

Here is the caller graph for this function:

def tabnanny.process_tokens (   tokens)

Definition at line 273 of file tabnanny.py.

00273 
00274 def process_tokens(tokens):
00275     INDENT = tokenize.INDENT
00276     DEDENT = tokenize.DEDENT
00277     NEWLINE = tokenize.NEWLINE
00278     JUNK = tokenize.COMMENT, tokenize.NL
00279     indents = [Whitespace("")]
00280     check_equal = 0
00281 
00282     for (type, token, start, end, line) in tokens:
00283         if type == NEWLINE:
00284             # a program statement, or ENDMARKER, will eventually follow,
00285             # after some (possibly empty) run of tokens of the form
00286             #     (NL | COMMENT)* (INDENT | DEDENT+)?
00287             # If an INDENT appears, setting check_equal is wrong, and will
00288             # be undone when we see the INDENT.
00289             check_equal = 1
00290 
00291         elif type == INDENT:
00292             check_equal = 0
00293             thisguy = Whitespace(token)
00294             if not indents[-1].less(thisguy):
00295                 witness = indents[-1].not_less_witness(thisguy)
00296                 msg = "indent not greater e.g. " + format_witnesses(witness)
00297                 raise NannyNag(start[0], msg, line)
00298             indents.append(thisguy)
00299 
00300         elif type == DEDENT:
00301             # there's nothing we need to check here!  what's important is
00302             # that when the run of DEDENTs ends, the indentation of the
00303             # program statement (or ENDMARKER) that triggered the run is
00304             # equal to what's left at the top of the indents stack
00305 
00306             # Ouch!  This assert triggers if the last line of the source
00307             # is indented *and* lacks a newline -- then DEDENTs pop out
00308             # of thin air.
00309             # assert check_equal  # else no earlier NEWLINE, or an earlier INDENT
00310             check_equal = 1
00311 
00312             del indents[-1]
00313 
00314         elif check_equal and type not in JUNK:
00315             # this is the first "real token" following a NEWLINE, so it
00316             # must be the first token of the next program statement, or an
00317             # ENDMARKER; the "line" argument exposes the leading whitespace
00318             # for this statement; in the case of ENDMARKER, line is an empty
00319             # string, so will properly match the empty string with which the
00320             # "indents" stack was seeded
00321             check_equal = 0
00322             thisguy = Whitespace(line)
00323             if not indents[-1].equal(thisguy):
00324                 witness = indents[-1].not_equal_witness(thisguy)
00325                 msg = "indent not equal e.g. " + format_witnesses(witness)
00326                 raise NannyNag(start[0], msg, line)
00327 

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

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

Definition at line 30 of file tabnanny.py.

Definition at line 21 of file tabnanny.py.

Definition at line 33 of file tabnanny.py.

Definition at line 32 of file tabnanny.py.