Back to index

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

Classes

class  Reindenter

Functions

def usage
def errprint
def main
def check
def _rstrip
def getlspace

Variables

string __version__ = "1"
 verbose = False
 recurse = False
 dryrun = False
 makebackup = True

Function Documentation

def reindent._rstrip (   line,
  JUNK = '\n \t' 
) [private]
Return line stripped of trailing spaces, tabs, newlines.

Note that line.rstrip() instead also strips sundry control characters,
but at least one known Emacs user expects to keep junk like that, not
mentioning Barry by name or anything <wink>.

Definition at line 148 of file reindent.py.

00148 
00149 def _rstrip(line, JUNK='\n \t'):
00150     """Return line stripped of trailing spaces, tabs, newlines.
00151 
00152     Note that line.rstrip() instead also strips sundry control characters,
00153     but at least one known Emacs user expects to keep junk like that, not
00154     mentioning Barry by name or anything <wink>.
00155     """
00156 
00157     i = len(line)
00158     while i > 0 and line[i - 1] in JUNK:
00159         i -= 1
00160     return line[:i]
00161 

def reindent.check (   file)

Definition at line 96 of file reindent.py.

00096 
00097 def check(file):
00098     if os.path.isdir(file) and not os.path.islink(file):
00099         if verbose:
00100             print("listing directory", file)
00101         names = os.listdir(file)
00102         for name in names:
00103             fullname = os.path.join(file, name)
00104             if ((recurse and os.path.isdir(fullname) and
00105                  not os.path.islink(fullname) and
00106                  not os.path.split(fullname)[1].startswith("."))
00107                 or name.lower().endswith(".py")):
00108                 check(fullname)
00109         return
00110 
00111     if verbose:
00112         print("checking", file, "...", end=' ')
00113     with open(file, 'rb') as f:
00114         encoding, _ = tokenize.detect_encoding(f.readline)
00115     try:
00116         with open(file, encoding=encoding) as f:
00117             r = Reindenter(f)
00118     except IOError as msg:
00119         errprint("%s: I/O Error: %s" % (file, str(msg)))
00120         return
00121 
00122     newline = r.newlines
00123     if isinstance(newline, tuple):
00124         errprint("%s: mixed newlines detected; cannot process file" % file)
00125         return
00126 
00127     if r.run():
00128         if verbose:
00129             print("changed.")
00130             if dryrun:
00131                 print("But this is a dry run, so leaving it alone.")
00132         if not dryrun:
00133             bak = file + ".bak"
00134             if makebackup:
00135                 shutil.copyfile(file, bak)
00136                 if verbose:
00137                     print("backed up", file, "to", bak)
00138             with open(file, "w", encoding=encoding, newline=newline) as f:
00139                 r.write(f)
00140             if verbose:
00141                 print("wrote new", file)
00142         return True
00143     else:
00144         if verbose:
00145             print("unchanged.")
00146         return False
00147 

Here is the call graph for this function:

Here is the caller graph for this function:

def reindent.errprint (   args)

Definition at line 61 of file reindent.py.

00061 
00062 def errprint(*args):
00063     sys.stderr.write(" ".join(str(arg) for arg in args))
00064     sys.stderr.write("\n")
00065 

Here is the caller graph for this function:

def reindent.getlspace (   line)

Definition at line 313 of file reindent.py.

00313 
00314 def getlspace(line):
00315     i, n = 0, len(line)
00316     while i < n and line[i] == " ":
00317         i += 1
00318     return i
00319 

Here is the call graph for this function:

def reindent.main ( void  )

Definition at line 66 of file reindent.py.

00066 
00067 def main():
00068     import getopt
00069     global verbose, recurse, dryrun, makebackup
00070     try:
00071         opts, args = getopt.getopt(sys.argv[1:], "drnvh",
00072                         ["dryrun", "recurse", "nobackup", "verbose", "help"])
00073     except getopt.error as msg:
00074         usage(msg)
00075         return
00076     for o, a in opts:
00077         if o in ('-d', '--dryrun'):
00078             dryrun = True
00079         elif o in ('-r', '--recurse'):
00080             recurse = True
00081         elif o in ('-n', '--nobackup'):
00082             makebackup = False
00083         elif o in ('-v', '--verbose'):
00084             verbose = True
00085         elif o in ('-h', '--help'):
00086             usage()
00087             return
00088     if not args:
00089         r = Reindenter(sys.stdin)
00090         r.run()
00091         r.write(sys.stdout)
00092         return
00093     for arg in args:
00094         check(arg)
00095 

Here is the call graph for this function:

Here is the caller graph for this function:

def reindent.usage (   msg = None)

Definition at line 55 of file reindent.py.

00055 
00056 def usage(msg=None):
00057     if msg is None:
00058         msg = __doc__
00059     print(msg, file=sys.stderr)
00060 

Here is the caller graph for this function:


Variable Documentation

Definition at line 42 of file reindent.py.

reindent.dryrun = False

Definition at line 51 of file reindent.py.

Definition at line 52 of file reindent.py.

Definition at line 50 of file reindent.py.

Definition at line 49 of file reindent.py.