annotate Tools/scripts/reindent.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 82ccc7e77ec5
children f9cf55bbe9b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1 #! /usr/bin/env python
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
3 # Released to the public domain, by Tim Peters, 03 October 2000.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4
22573
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
5 """reindent [-d][-r][-v] [ path ... ]
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
7 -d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
8 -r (--recurse) Recurse. Search for all .py files in subdirectories too.
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
9 -n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
10 -v (--verbose) Verbose. Print informative msgs; else no output.
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
11 -h (--help) Help. Print this usage information and exit.
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13 Change Python (.py) files to use 4-space indents and no hard tab characters.
20258
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
14 Also trim excess spaces and tabs from ends of lines, and remove empty lines
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
15 at the end of files. Also ensure the last line ends with a newline.
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16
22573
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
17 If no paths are given on the command line, reindent operates as a filter,
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
18 reading a single source file from standard input and writing the transformed
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
19 source to standard output. In this case, the -d, -r and -v flags are
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
20 ignored.
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
21
22573
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
22 You can pass one or more file and/or directory paths. When a directory
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
23 path, all .py files within the directory will be examined, and, if the -r
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
24 option is given, likewise recursively for subdirectories.
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
25
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
26 If output is not to standard output, reindent overwrites files in place,
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
27 renaming the originals with a .bak extension. If it finds nothing to
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
28 change, the file is left alone. If reindent does change a file, the changed
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
29 file is a fixed-point for future runs (i.e., running reindent on the
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
30 resulting .py file won't change it again).
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
31
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
32 The hard part of reindenting is figuring out what to do with comment
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
33 lines. So long as the input files get a clean bill of health from
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34 tabnanny.py, reindent should do a good job.
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
35
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
36 The backup file is a copy of the one that is being reindented. The ".bak"
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
37 file is generated with shutil.copy(), but some corner cases regarding
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
38 user/group and permissions could leave the backup file more readable that
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
39 you'd prefer. You can always use the --nobackup option to prevent this.
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
40 """
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
41
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
42 __version__ = "1"
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
43
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
44 import tokenize
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
45 import os, shutil
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
46 import sys
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
47
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
48 verbose = 0
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
49 recurse = 0
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
50 dryrun = 0
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
51 makebackup = True
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
52
31832
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
53 def usage(msg=None):
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
54 if msg is not None:
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
55 print >> sys.stderr, msg
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
56 print >> sys.stderr, __doc__
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
57
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
58 def errprint(*args):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
59 sep = ""
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
60 for arg in args:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
61 sys.stderr.write(sep + str(arg))
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
62 sep = " "
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
63 sys.stderr.write("\n")
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
64
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
65 def main():
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
66 import getopt
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
67 global verbose, recurse, dryrun, makebackup
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
68 try:
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
69 opts, args = getopt.getopt(sys.argv[1:], "drnvh",
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
70 ["dryrun", "recurse", "nobackup", "verbose", "help"])
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
71 except getopt.error, msg:
31832
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
72 usage(msg)
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
73 return
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
74 for o, a in opts:
31832
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
75 if o in ('-d', '--dryrun'):
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
76 dryrun += 1
31832
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
77 elif o in ('-r', '--recurse'):
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
78 recurse += 1
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
79 elif o in ('-n', '--nobackup'):
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
80 makebackup = False
31832
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
81 elif o in ('-v', '--verbose'):
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
82 verbose += 1
31832
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
83 elif o in ('-h', '--help'):
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
84 usage()
64e2f7ec5576 add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents: 22573
diff changeset
85 return
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
86 if not args:
22573
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
87 r = Reindenter(sys.stdin)
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
88 r.run()
298802abd38d operate as a filter when no files or directories appear on the command line
Skip Montanaro <skip@pobox.com>
parents: 21996
diff changeset
89 r.write(sys.stdout)
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
90 return
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
91 for arg in args:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
92 check(arg)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
93
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
94 def check(file):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
95 if os.path.isdir(file) and not os.path.islink(file):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
96 if verbose:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
97 print "listing directory", file
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
98 names = os.listdir(file)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
99 for name in names:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
100 fullname = os.path.join(file, name)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
101 if ((recurse and os.path.isdir(fullname) and
49677
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45859
diff changeset
102 not os.path.islink(fullname) and
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45859
diff changeset
103 not os.path.split(fullname)[1].startswith("."))
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
104 or name.lower().endswith(".py")):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
105 check(fullname)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
106 return
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
107
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
108 if verbose:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
109 print "checking", file, "...",
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
110 try:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
111 f = open(file)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
112 except IOError, msg:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
113 errprint("%s: I/O Error: %s" % (file, str(msg)))
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
114 return
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
115
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
116 r = Reindenter(f)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
117 f.close()
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
118 if r.run():
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
119 if verbose:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
120 print "changed."
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
121 if dryrun:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
122 print "But this is a dry run, so leaving it alone."
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
123 if not dryrun:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
124 bak = file + ".bak"
45379
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
125 if makebackup:
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
126 shutil.copyfile(file, bak)
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
127 if verbose:
08cb5abfb5b6 Now we handle different the backup copy, because of security
Facundo Batista <facundobatista@gmail.com>
parents: 31832
diff changeset
128 print "backed up", file, "to", bak
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
129 f = open(file, "w")
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
130 r.write(f)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
131 f.close()
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
132 if verbose:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
133 print "wrote new", file
45859
c424099b4ce4 Add Tools/scripts/patchcheck.py. Invoked from ``make check``, the script does
Brett Cannon <bcannon@gmail.com>
parents: 45379
diff changeset
134 return True
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
135 else:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
136 if verbose:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
137 print "unchanged."
45859
c424099b4ce4 Add Tools/scripts/patchcheck.py. Invoked from ``make check``, the script does
Brett Cannon <bcannon@gmail.com>
parents: 45379
diff changeset
138 return False
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
139
20258
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
140 def _rstrip(line, JUNK='\n \t'):
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
141 """Return line stripped of trailing spaces, tabs, newlines.
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
142
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
143 Note that line.rstrip() instead also strips sundry control characters,
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
144 but at least one known Emacs user expects to keep junk like that, not
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
145 mentioning Barry by name or anything <wink>.
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
146 """
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
147
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
148 i = len(line)
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
149 while i > 0 and line[i-1] in JUNK:
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
150 i -= 1
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
151 return line[:i]
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
152
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
153 class Reindenter:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
154
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
155 def __init__(self, f):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
156 self.find_stmt = 1 # next token begins a fresh stmt?
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
157 self.level = 0 # current indent level
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
158
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
159 # Raw file lines.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
160 self.raw = f.readlines()
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
161
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
162 # File lines, rstripped & tab-expanded. Dummy at start is so
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
163 # that we can use tokenize's 1-based line numbering easily.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
164 # Note that a line is all-blank iff it's "\n".
20258
318589404b95 Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents: 15018
diff changeset
165 self.lines = [_rstrip(line).expandtabs() + "\n"
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
166 for line in self.raw]
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
167 self.lines.insert(0, None)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
168 self.index = 1 # index into self.lines of next line
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
169
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
170 # List of (lineno, indentlevel) pairs, one for each stmt and
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
171 # comment line. indentlevel is -1 for comment lines, as a
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
172 # signal that tokenize doesn't know what to do about them;
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
173 # indeed, they're our headache!
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
174 self.stats = []
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
175
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
176 def run(self):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
177 tokenize.tokenize(self.getline, self.tokeneater)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
178 # Remove trailing empty lines.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
179 lines = self.lines
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
180 while lines and lines[-1] == "\n":
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
181 lines.pop()
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
182 # Sentinel.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
183 stats = self.stats
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
184 stats.append((len(lines), 0))
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
185 # Map count of leading spaces to # we want.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
186 have2want = {}
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
187 # Program after transformation.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
188 after = self.after = []
21996
0b15cf11431e backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20258
diff changeset
189 # Copy over initial empty lines -- there's nothing to do until
0b15cf11431e backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20258
diff changeset
190 # we see a line with *something* on it.
0b15cf11431e backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20258
diff changeset
191 i = stats[0][0]
0b15cf11431e backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20258
diff changeset
192 after.extend(lines[1:i])
15018
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
193 for i in range(len(stats)-1):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
194 thisstmt, thislevel = stats[i]
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
195 nextstmt = stats[i+1][0]
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
196 have = getlspace(lines[thisstmt])
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
197 want = thislevel * 4
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
198 if want < 0:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
199 # A comment line.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
200 if have:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
201 # An indented comment line. If we saw the same
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
202 # indentation before, reuse what it most recently
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
203 # mapped to.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
204 want = have2want.get(have, -1)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
205 if want < 0:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
206 # Then it probably belongs to the next real stmt.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
207 for j in xrange(i+1, len(stats)-1):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
208 jline, jlevel = stats[j]
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
209 if jlevel >= 0:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
210 if have == getlspace(lines[jline]):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
211 want = jlevel * 4
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
212 break
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
213 if want < 0: # Maybe it's a hanging
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
214 # comment like this one,
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
215 # in which case we should shift it like its base
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
216 # line got shifted.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
217 for j in xrange(i-1, -1, -1):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
218 jline, jlevel = stats[j]
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
219 if jlevel >= 0:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
220 want = have + getlspace(after[jline-1]) - \
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
221 getlspace(lines[jline])
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
222 break
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
223 if want < 0:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
224 # Still no luck -- leave it alone.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
225 want = have
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
226 else:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
227 want = 0
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
228 assert want >= 0
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
229 have2want[have] = want
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
230 diff = want - have
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
231 if diff == 0 or have == 0:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
232 after.extend(lines[thisstmt:nextstmt])
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
233 else:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
234 for line in lines[thisstmt:nextstmt]:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
235 if diff > 0:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
236 if line == "\n":
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
237 after.append(line)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
238 else:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
239 after.append(" " * diff + line)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
240 else:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
241 remove = min(getlspace(line), -diff)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
242 after.append(line[remove:])
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
243 return self.raw != self.after
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
244
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
245 def write(self, f):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
246 f.writelines(self.after)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
247
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
248 # Line-getter for tokenize.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
249 def getline(self):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
250 if self.index >= len(self.lines):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
251 line = ""
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
252 else:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
253 line = self.lines[self.index]
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
254 self.index += 1
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
255 return line
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
256
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
257 # Line-eater for tokenize.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
258 def tokeneater(self, type, token, (sline, scol), end, line,
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
259 INDENT=tokenize.INDENT,
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
260 DEDENT=tokenize.DEDENT,
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
261 NEWLINE=tokenize.NEWLINE,
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
262 COMMENT=tokenize.COMMENT,
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
263 NL=tokenize.NL):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
264
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
265 if type == NEWLINE:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
266 # A program statement, or ENDMARKER, will eventually follow,
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
267 # after some (possibly empty) run of tokens of the form
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
268 # (NL | COMMENT)* (INDENT | DEDENT+)?
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
269 self.find_stmt = 1
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
270
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
271 elif type == INDENT:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
272 self.find_stmt = 1
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
273 self.level += 1
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
274
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
275 elif type == DEDENT:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
276 self.find_stmt = 1
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
277 self.level -= 1
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
278
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
279 elif type == COMMENT:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
280 if self.find_stmt:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
281 self.stats.append((sline, -1))
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
282 # but we're still looking for a new stmt, so leave
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
283 # find_stmt alone
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
284
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
285 elif type == NL:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
286 pass
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
287
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
288 elif self.find_stmt:
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
289 # This is the first "real token" following a NEWLINE, so it
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
290 # must be the first token of the next program statement, or an
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
291 # ENDMARKER.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
292 self.find_stmt = 0
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
293 if line: # not endmarker
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
294 self.stats.append((sline, self.level))
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
295
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
296 # Count number of leading blanks.
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
297 def getlspace(line):
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
298 i, n = 0, len(line)
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
299 while i < n and line[i] == " ":
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
300 i += 1
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
301 return i
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
302
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
303 if __name__ == '__main__':
98b1fa6c1600 New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
304 main()