2010-03-11 18:53:45 -04:00
|
|
|
#! /usr/bin/env python3
|
2000-10-05 00:48:38 -03:00
|
|
|
|
|
|
|
# Released to the public domain, by Tim Peters, 03 October 2000.
|
|
|
|
|
2002-03-26 07:39:26 -04:00
|
|
|
"""reindent [-d][-r][-v] [ path ... ]
|
2000-10-05 00:48:38 -03:00
|
|
|
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
|
|
|
|
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
|
|
|
|
-n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
|
|
|
|
-v (--verbose) Verbose. Print informative msgs; else no output.
|
2011-07-26 12:38:04 -03:00
|
|
|
(--newline) Newline. Specify the newline character to use (CRLF, LF).
|
|
|
|
Default is the same as the original file.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
-h (--help) Help. Print this usage information and exit.
|
2000-10-05 00:48:38 -03:00
|
|
|
|
|
|
|
Change Python (.py) files to use 4-space indents and no hard tab characters.
|
2001-10-04 16:44:10 -03:00
|
|
|
Also trim excess spaces and tabs from ends of lines, and remove empty lines
|
|
|
|
at the end of files. Also ensure the last line ends with a newline.
|
2000-10-05 00:48:38 -03:00
|
|
|
|
2002-03-26 07:39:26 -04:00
|
|
|
If no paths are given on the command line, reindent operates as a filter,
|
|
|
|
reading a single source file from standard input and writing the transformed
|
|
|
|
source to standard output. In this case, the -d, -r and -v flags are
|
|
|
|
ignored.
|
2000-10-05 00:48:38 -03:00
|
|
|
|
2002-03-26 07:39:26 -04:00
|
|
|
You can pass one or more file and/or directory paths. When a directory
|
|
|
|
path, all .py files within the directory will be examined, and, if the -r
|
|
|
|
option is given, likewise recursively for subdirectories.
|
|
|
|
|
|
|
|
If output is not to standard output, reindent overwrites files in place,
|
|
|
|
renaming the originals with a .bak extension. If it finds nothing to
|
|
|
|
change, the file is left alone. If reindent does change a file, the changed
|
|
|
|
file is a fixed-point for future runs (i.e., running reindent on the
|
|
|
|
resulting .py file won't change it again).
|
2000-10-05 00:48:38 -03:00
|
|
|
|
|
|
|
The hard part of reindenting is figuring out what to do with comment
|
|
|
|
lines. So long as the input files get a clean bill of health from
|
|
|
|
tabnanny.py, reindent should do a good job.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
|
|
|
|
The backup file is a copy of the one that is being reindented. The ".bak"
|
|
|
|
file is generated with shutil.copy(), but some corner cases regarding
|
2011-07-26 12:18:40 -03:00
|
|
|
user/group and permissions could leave the backup file more readable than
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
you'd prefer. You can always use the --nobackup option to prevent this.
|
2000-10-05 00:48:38 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
__version__ = "1"
|
|
|
|
|
|
|
|
import tokenize
|
2010-08-09 09:24:20 -03:00
|
|
|
import os
|
|
|
|
import shutil
|
2000-10-05 00:48:38 -03:00
|
|
|
import sys
|
|
|
|
|
2010-08-09 09:24:20 -03:00
|
|
|
verbose = False
|
|
|
|
recurse = False
|
|
|
|
dryrun = False
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
makebackup = True
|
2013-03-25 21:11:54 -03:00
|
|
|
# A specified newline to be used in the output (set by --newline option)
|
2011-07-29 10:31:56 -03:00
|
|
|
spec_newline = None
|
2000-10-05 00:48:38 -03:00
|
|
|
|
2010-08-09 09:24:20 -03:00
|
|
|
|
2004-03-27 14:43:56 -04:00
|
|
|
def usage(msg=None):
|
2010-08-09 09:24:20 -03:00
|
|
|
if msg is None:
|
|
|
|
msg = __doc__
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
2004-03-27 14:43:56 -04:00
|
|
|
|
2000-10-05 00:48:38 -03:00
|
|
|
def errprint(*args):
|
2010-08-09 09:24:20 -03:00
|
|
|
sys.stderr.write(" ".join(str(arg) for arg in args))
|
2000-10-05 00:48:38 -03:00
|
|
|
sys.stderr.write("\n")
|
|
|
|
|
|
|
|
def main():
|
|
|
|
import getopt
|
2011-07-26 12:38:04 -03:00
|
|
|
global verbose, recurse, dryrun, makebackup, spec_newline
|
2000-10-05 00:48:38 -03:00
|
|
|
try:
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "drnvh",
|
2011-07-26 12:38:04 -03:00
|
|
|
["dryrun", "recurse", "nobackup", "verbose", "newline=", "help"])
|
2007-01-10 12:19:56 -04:00
|
|
|
except getopt.error as msg:
|
2004-03-27 14:43:56 -04:00
|
|
|
usage(msg)
|
2000-10-05 00:48:38 -03:00
|
|
|
return
|
|
|
|
for o, a in opts:
|
2004-03-27 14:43:56 -04:00
|
|
|
if o in ('-d', '--dryrun'):
|
2010-08-09 09:24:20 -03:00
|
|
|
dryrun = True
|
2004-03-27 14:43:56 -04:00
|
|
|
elif o in ('-r', '--recurse'):
|
2010-08-09 09:24:20 -03:00
|
|
|
recurse = True
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
elif o in ('-n', '--nobackup'):
|
|
|
|
makebackup = False
|
2004-03-27 14:43:56 -04:00
|
|
|
elif o in ('-v', '--verbose'):
|
2010-08-09 09:24:20 -03:00
|
|
|
verbose = True
|
2011-07-26 12:38:04 -03:00
|
|
|
elif o in ('--newline',):
|
|
|
|
if not a.upper() in ('CRLF', 'LF'):
|
|
|
|
usage()
|
|
|
|
return
|
|
|
|
spec_newline = dict(CRLF='\r\n', LF='\n')[a.upper()]
|
2004-03-27 14:43:56 -04:00
|
|
|
elif o in ('-h', '--help'):
|
|
|
|
usage()
|
|
|
|
return
|
2000-10-05 00:48:38 -03:00
|
|
|
if not args:
|
2002-03-26 07:39:26 -04:00
|
|
|
r = Reindenter(sys.stdin)
|
|
|
|
r.run()
|
|
|
|
r.write(sys.stdout)
|
2000-10-05 00:48:38 -03:00
|
|
|
return
|
|
|
|
for arg in args:
|
|
|
|
check(arg)
|
|
|
|
|
2010-08-09 09:24:20 -03:00
|
|
|
|
2000-10-05 00:48:38 -03:00
|
|
|
def check(file):
|
|
|
|
if os.path.isdir(file) and not os.path.islink(file):
|
|
|
|
if verbose:
|
2007-04-27 16:48:23 -03:00
|
|
|
print("listing directory", file)
|
2000-10-05 00:48:38 -03:00
|
|
|
names = os.listdir(file)
|
|
|
|
for name in names:
|
|
|
|
fullname = os.path.join(file, name)
|
|
|
|
if ((recurse and os.path.isdir(fullname) and
|
Merged revisions 66887,66891,66902-66903,66905-66906,66911-66913,66922,66927-66928,66936,66939-66940,66962,66964,66973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r66887 | benjamin.peterson | 2008-10-13 16:51:40 -0500 (Mon, 13 Oct 2008) | 1 line
document how to disable fixers
................
r66891 | amaury.forgeotdarc | 2008-10-14 16:47:22 -0500 (Tue, 14 Oct 2008) | 5 lines
#4122: On Windows, Py_UNICODE_ISSPACE cannot be used in an extension module:
compilation fails with "undefined reference to _Py_ascii_whitespace"
Will backport to 2.6.
................
r66902 | skip.montanaro | 2008-10-15 06:49:10 -0500 (Wed, 15 Oct 2008) | 1 line
easter egg
................
r66903 | benjamin.peterson | 2008-10-15 15:34:09 -0500 (Wed, 15 Oct 2008) | 1 line
don't recurse into directories that start with '.'
................
r66905 | benjamin.peterson | 2008-10-15 16:05:55 -0500 (Wed, 15 Oct 2008) | 1 line
support the optional line argument for idle
................
r66906 | benjamin.peterson | 2008-10-15 16:58:46 -0500 (Wed, 15 Oct 2008) | 1 line
add a much requested newline
................
r66911 | benjamin.peterson | 2008-10-15 18:10:28 -0500 (Wed, 15 Oct 2008) | 41 lines
Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
mention what the fixes directory is for
........
r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
use assertFalse and assertTrue
........
r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
instead of abusing the pattern matcher, use start_tree to find a next binding
........
r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
don't print tokens to stdout when -v is given
........
r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
add the -x option to disable fixers
........
r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
cut down on some crud
........
r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
add an optional set literal fixer
........
r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
don't write backup files by default
........
r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
add the -n option; it stops backupfiles from being written
........
................
r66912 | hirokazu.yamamoto | 2008-10-16 01:25:25 -0500 (Thu, 16 Oct 2008) | 2 lines
removed unused _PyUnicode_FromFileSystemEncodedObject.
made win32_chdir, win32_wchdir static.
................
r66913 | benjamin.peterson | 2008-10-16 13:52:14 -0500 (Thu, 16 Oct 2008) | 1 line
document that deque indexing is O(n) #4123
................
r66922 | benjamin.peterson | 2008-10-16 14:40:14 -0500 (Thu, 16 Oct 2008) | 1 line
use new showwarnings signature for idle #3391
................
r66927 | andrew.kuchling | 2008-10-16 15:15:47 -0500 (Thu, 16 Oct 2008) | 1 line
Fix wording (2.6.1 backport candidate)
................
r66928 | georg.brandl | 2008-10-16 15:20:56 -0500 (Thu, 16 Oct 2008) | 2 lines
Add more TOC to the whatsnew index page.
................
r66936 | georg.brandl | 2008-10-16 16:20:15 -0500 (Thu, 16 Oct 2008) | 2 lines
#4131: FF3 doesn't write cookies.txt files.
................
r66939 | georg.brandl | 2008-10-16 16:36:39 -0500 (Thu, 16 Oct 2008) | 2 lines
part of #4012: kill off old name "processing".
................
r66940 | georg.brandl | 2008-10-16 16:38:48 -0500 (Thu, 16 Oct 2008) | 2 lines
#4083: add "as" to except handler grammar as per PEP 3110.
................
r66962 | benjamin.peterson | 2008-10-17 15:01:01 -0500 (Fri, 17 Oct 2008) | 1 line
clarify CALL_FUNCTION #4141
................
r66964 | georg.brandl | 2008-10-17 16:41:49 -0500 (Fri, 17 Oct 2008) | 2 lines
Fix duplicate word.
................
r66973 | armin.ronacher | 2008-10-19 03:27:43 -0500 (Sun, 19 Oct 2008) | 3 lines
Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
2008-10-19 11:07:49 -03:00
|
|
|
not os.path.islink(fullname) and
|
|
|
|
not os.path.split(fullname)[1].startswith("."))
|
2000-10-05 00:48:38 -03:00
|
|
|
or name.lower().endswith(".py")):
|
|
|
|
check(fullname)
|
|
|
|
return
|
|
|
|
|
|
|
|
if verbose:
|
2007-04-27 16:48:23 -03:00
|
|
|
print("checking", file, "...", end=' ')
|
2011-07-26 12:18:40 -03:00
|
|
|
with open(file, 'rb') as f:
|
2017-04-20 11:12:37 -03:00
|
|
|
try:
|
|
|
|
encoding, _ = tokenize.detect_encoding(f.readline)
|
|
|
|
except SyntaxError as se:
|
|
|
|
errprint("%s: SyntaxError: %s" % (file, str(se)))
|
|
|
|
return
|
2000-10-05 00:48:38 -03:00
|
|
|
try:
|
2010-10-18 11:43:38 -03:00
|
|
|
with open(file, encoding=encoding) as f:
|
2010-08-09 09:24:20 -03:00
|
|
|
r = Reindenter(f)
|
2007-01-10 12:19:56 -04:00
|
|
|
except IOError as msg:
|
2000-10-05 00:48:38 -03:00
|
|
|
errprint("%s: I/O Error: %s" % (file, str(msg)))
|
|
|
|
return
|
|
|
|
|
2011-07-26 12:38:04 -03:00
|
|
|
newline = spec_newline if spec_newline else r.newlines
|
2011-07-26 12:18:40 -03:00
|
|
|
if isinstance(newline, tuple):
|
2011-07-26 12:38:04 -03:00
|
|
|
errprint("%s: mixed newlines detected; cannot continue without --newline" % file)
|
2011-07-26 12:18:40 -03:00
|
|
|
return
|
|
|
|
|
2000-10-05 00:48:38 -03:00
|
|
|
if r.run():
|
|
|
|
if verbose:
|
2007-04-27 16:48:23 -03:00
|
|
|
print("changed.")
|
2000-10-05 00:48:38 -03:00
|
|
|
if dryrun:
|
2007-04-27 16:48:23 -03:00
|
|
|
print("But this is a dry run, so leaving it alone.")
|
2000-10-05 00:48:38 -03:00
|
|
|
if not dryrun:
|
|
|
|
bak = file + ".bak"
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60876 | georg.brandl | 2008-02-17 16:14:10 +0100 (Sun, 17 Feb 2008) | 2 lines
Fix function name.
........
r60877 | facundo.batista | 2008-02-17 17:21:13 +0100 (Sun, 17 Feb 2008) | 4 lines
Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
........
r60878 | facundo.batista | 2008-02-17 19:59:29 +0100 (Sun, 17 Feb 2008) | 4 lines
Issue 2112. mmap does not raises EnvironmentError no more, but
a subclass of it. Thanks John Lenton.
........
r60882 | amaury.forgeotdarc | 2008-02-17 21:56:31 +0100 (Sun, 17 Feb 2008) | 5 lines
Compilation was broken on Windows since the introduction of Advanced String Formatting.
Only PCBuild (vs9) was really tested.
Changes for older compilers were done manually.
........
r60883 | georg.brandl | 2008-02-17 22:18:55 +0100 (Sun, 17 Feb 2008) | 2 lines
#2133: fix HTML color spec.
........
r60884 | facundo.batista | 2008-02-18 04:43:43 +0100 (Mon, 18 Feb 2008) | 5 lines
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and
corrections.
........
r60885 | facundo.batista | 2008-02-18 13:48:43 +0100 (Mon, 18 Feb 2008) | 4 lines
Issue 1224. Now we support again the double slash in the URL.
Thanks Anthony Lenton.
........
r60887 | eric.smith | 2008-02-18 15:25:02 +0100 (Mon, 18 Feb 2008) | 1 line
Temporarily removed float tests. See issue 1600.
........
r60891 | kristjan.jonsson | 2008-02-18 18:40:47 +0100 (Mon, 18 Feb 2008) | 1 line
Perform correct handling of stack overflow for windows: Catch the correct exception code and reset the overflow condition when handled.
........
2008-02-19 10:21:46 -04:00
|
|
|
if makebackup:
|
|
|
|
shutil.copyfile(file, bak)
|
|
|
|
if verbose:
|
|
|
|
print("backed up", file, "to", bak)
|
2011-07-26 12:18:40 -03:00
|
|
|
with open(file, "w", encoding=encoding, newline=newline) as f:
|
2010-08-09 09:24:20 -03:00
|
|
|
r.write(f)
|
2000-10-05 00:48:38 -03:00
|
|
|
if verbose:
|
2007-04-27 16:48:23 -03:00
|
|
|
print("wrote new", file)
|
Merged revisions 61520,61523-61528,61532 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61520 | thomas.heller | 2008-03-18 16:03:17 +0100 (Di, 18 Mär 2008) | 5 lines
Include <alloca.h> on Solaris, see issue #1506.
It would probably be better to have a configure test for that, but
this is outside of my configure expertise.
........
r61523 | brett.cannon | 2008-03-18 16:35:58 +0100 (Di, 18 Mär 2008) | 5 lines
Remove all traces of HAVE_STRERROR.
The removal of strerror.c led to the function check being removed from
configure.in.
........
r61524 | brett.cannon | 2008-03-18 16:52:00 +0100 (Di, 18 Mär 2008) | 2 lines
Fix test_errno to only check for error numbers that are defined by Standard C.
........
r61525 | steven.bethard | 2008-03-18 17:00:19 +0100 (Di, 18 Mär 2008) | 1 line
Use test_support.unlink instead of os.unlink in tearDown(). (Seems to fix an occasional failure in Windows Vista.)
........
r61526 | brett.cannon | 2008-03-18 17:47:51 +0100 (Di, 18 Mär 2008) | 3 lines
Cast the arguments to PyString_AsStringAndSize() to silence compiler warnings
on OS X.
........
r61527 | sean.reifschneider | 2008-03-18 18:24:12 +0100 (Di, 18 Mär 2008) | 3 lines
Issue 1577: shutil.move() where destination is a directory was doing a
copy, now it is doing a os.rename() if it's on the same file-system.
........
r61528 | brett.cannon | 2008-03-18 18:25:13 +0100 (Di, 18 Mär 2008) | 12 lines
Add Tools/scripts/patchcheck.py. Invoked from ``make check``, the script does
some verification:
- Runs reindent.py on all .py files.
- Checks if any changes in Doc exist.
- Whether Misc/ACKS was changed.
- Whether Misc/NEWS was changed.
The hope is that ``make check`` can become a command anybody can run to get
reminders about what all the requisite steps needed to create a proper
patch/checkin.
........
r61532 | neal.norwitz | 2008-03-18 18:58:02 +0100 (Di, 18 Mär 2008) | 1 line
Get regrtest working when re-running tests
........
2008-03-18 15:26:33 -03:00
|
|
|
return True
|
2000-10-05 00:48:38 -03:00
|
|
|
else:
|
|
|
|
if verbose:
|
2007-04-27 16:48:23 -03:00
|
|
|
print("unchanged.")
|
Merged revisions 61520,61523-61528,61532 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61520 | thomas.heller | 2008-03-18 16:03:17 +0100 (Di, 18 Mär 2008) | 5 lines
Include <alloca.h> on Solaris, see issue #1506.
It would probably be better to have a configure test for that, but
this is outside of my configure expertise.
........
r61523 | brett.cannon | 2008-03-18 16:35:58 +0100 (Di, 18 Mär 2008) | 5 lines
Remove all traces of HAVE_STRERROR.
The removal of strerror.c led to the function check being removed from
configure.in.
........
r61524 | brett.cannon | 2008-03-18 16:52:00 +0100 (Di, 18 Mär 2008) | 2 lines
Fix test_errno to only check for error numbers that are defined by Standard C.
........
r61525 | steven.bethard | 2008-03-18 17:00:19 +0100 (Di, 18 Mär 2008) | 1 line
Use test_support.unlink instead of os.unlink in tearDown(). (Seems to fix an occasional failure in Windows Vista.)
........
r61526 | brett.cannon | 2008-03-18 17:47:51 +0100 (Di, 18 Mär 2008) | 3 lines
Cast the arguments to PyString_AsStringAndSize() to silence compiler warnings
on OS X.
........
r61527 | sean.reifschneider | 2008-03-18 18:24:12 +0100 (Di, 18 Mär 2008) | 3 lines
Issue 1577: shutil.move() where destination is a directory was doing a
copy, now it is doing a os.rename() if it's on the same file-system.
........
r61528 | brett.cannon | 2008-03-18 18:25:13 +0100 (Di, 18 Mär 2008) | 12 lines
Add Tools/scripts/patchcheck.py. Invoked from ``make check``, the script does
some verification:
- Runs reindent.py on all .py files.
- Checks if any changes in Doc exist.
- Whether Misc/ACKS was changed.
- Whether Misc/NEWS was changed.
The hope is that ``make check`` can become a command anybody can run to get
reminders about what all the requisite steps needed to create a proper
patch/checkin.
........
r61532 | neal.norwitz | 2008-03-18 18:58:02 +0100 (Di, 18 Mär 2008) | 1 line
Get regrtest working when re-running tests
........
2008-03-18 15:26:33 -03:00
|
|
|
return False
|
2000-10-05 00:48:38 -03:00
|
|
|
|
2010-08-09 09:24:20 -03:00
|
|
|
|
2001-10-04 16:44:10 -03:00
|
|
|
def _rstrip(line, JUNK='\n \t'):
|
|
|
|
"""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>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
i = len(line)
|
2010-08-09 09:24:20 -03:00
|
|
|
while i > 0 and line[i - 1] in JUNK:
|
2001-10-04 16:44:10 -03:00
|
|
|
i -= 1
|
|
|
|
return line[:i]
|
|
|
|
|
2010-08-09 09:24:20 -03:00
|
|
|
|
2000-10-05 00:48:38 -03:00
|
|
|
class Reindenter:
|
|
|
|
|
|
|
|
def __init__(self, f):
|
|
|
|
self.find_stmt = 1 # next token begins a fresh stmt?
|
|
|
|
self.level = 0 # current indent level
|
|
|
|
|
|
|
|
# Raw file lines.
|
|
|
|
self.raw = f.readlines()
|
|
|
|
|
|
|
|
# File lines, rstripped & tab-expanded. Dummy at start is so
|
|
|
|
# that we can use tokenize's 1-based line numbering easily.
|
|
|
|
# Note that a line is all-blank iff it's "\n".
|
2001-10-04 16:44:10 -03:00
|
|
|
self.lines = [_rstrip(line).expandtabs() + "\n"
|
2000-10-05 00:48:38 -03:00
|
|
|
for line in self.raw]
|
|
|
|
self.lines.insert(0, None)
|
|
|
|
self.index = 1 # index into self.lines of next line
|
|
|
|
|
|
|
|
# List of (lineno, indentlevel) pairs, one for each stmt and
|
|
|
|
# comment line. indentlevel is -1 for comment lines, as a
|
|
|
|
# signal that tokenize doesn't know what to do about them;
|
|
|
|
# indeed, they're our headache!
|
|
|
|
self.stats = []
|
|
|
|
|
2011-07-26 12:18:40 -03:00
|
|
|
# Save the newlines found in the file so they can be used to
|
|
|
|
# create output without mutating the newlines.
|
|
|
|
self.newlines = f.newlines
|
|
|
|
|
2000-10-05 00:48:38 -03:00
|
|
|
def run(self):
|
2008-03-18 19:41:35 -03:00
|
|
|
tokens = tokenize.generate_tokens(self.getline)
|
|
|
|
for _token in tokens:
|
|
|
|
self.tokeneater(*_token)
|
2000-10-05 00:48:38 -03:00
|
|
|
# Remove trailing empty lines.
|
|
|
|
lines = self.lines
|
|
|
|
while lines and lines[-1] == "\n":
|
|
|
|
lines.pop()
|
|
|
|
# Sentinel.
|
|
|
|
stats = self.stats
|
|
|
|
stats.append((len(lines), 0))
|
|
|
|
# Map count of leading spaces to # we want.
|
|
|
|
have2want = {}
|
|
|
|
# Program after transformation.
|
|
|
|
after = self.after = []
|
2002-02-17 03:03:05 -04:00
|
|
|
# Copy over initial empty lines -- there's nothing to do until
|
|
|
|
# we see a line with *something* on it.
|
|
|
|
i = stats[0][0]
|
|
|
|
after.extend(lines[1:i])
|
2010-08-09 09:24:20 -03:00
|
|
|
for i in range(len(stats) - 1):
|
2000-10-05 00:48:38 -03:00
|
|
|
thisstmt, thislevel = stats[i]
|
2010-08-09 09:24:20 -03:00
|
|
|
nextstmt = stats[i + 1][0]
|
2000-10-05 00:48:38 -03:00
|
|
|
have = getlspace(lines[thisstmt])
|
|
|
|
want = thislevel * 4
|
|
|
|
if want < 0:
|
|
|
|
# A comment line.
|
|
|
|
if have:
|
|
|
|
# An indented comment line. If we saw the same
|
|
|
|
# indentation before, reuse what it most recently
|
|
|
|
# mapped to.
|
|
|
|
want = have2want.get(have, -1)
|
|
|
|
if want < 0:
|
|
|
|
# Then it probably belongs to the next real stmt.
|
2010-08-09 09:24:20 -03:00
|
|
|
for j in range(i + 1, len(stats) - 1):
|
2000-10-05 00:48:38 -03:00
|
|
|
jline, jlevel = stats[j]
|
|
|
|
if jlevel >= 0:
|
|
|
|
if have == getlspace(lines[jline]):
|
|
|
|
want = jlevel * 4
|
|
|
|
break
|
|
|
|
if want < 0: # Maybe it's a hanging
|
|
|
|
# comment like this one,
|
|
|
|
# in which case we should shift it like its base
|
|
|
|
# line got shifted.
|
2010-08-09 09:24:20 -03:00
|
|
|
for j in range(i - 1, -1, -1):
|
2000-10-05 00:48:38 -03:00
|
|
|
jline, jlevel = stats[j]
|
|
|
|
if jlevel >= 0:
|
2010-08-09 09:24:20 -03:00
|
|
|
want = have + (getlspace(after[jline - 1]) -
|
|
|
|
getlspace(lines[jline]))
|
2000-10-05 00:48:38 -03:00
|
|
|
break
|
|
|
|
if want < 0:
|
|
|
|
# Still no luck -- leave it alone.
|
|
|
|
want = have
|
|
|
|
else:
|
|
|
|
want = 0
|
|
|
|
assert want >= 0
|
|
|
|
have2want[have] = want
|
|
|
|
diff = want - have
|
|
|
|
if diff == 0 or have == 0:
|
|
|
|
after.extend(lines[thisstmt:nextstmt])
|
|
|
|
else:
|
|
|
|
for line in lines[thisstmt:nextstmt]:
|
|
|
|
if diff > 0:
|
|
|
|
if line == "\n":
|
|
|
|
after.append(line)
|
|
|
|
else:
|
|
|
|
after.append(" " * diff + line)
|
|
|
|
else:
|
|
|
|
remove = min(getlspace(line), -diff)
|
|
|
|
after.append(line[remove:])
|
|
|
|
return self.raw != self.after
|
|
|
|
|
|
|
|
def write(self, f):
|
|
|
|
f.writelines(self.after)
|
|
|
|
|
|
|
|
# Line-getter for tokenize.
|
|
|
|
def getline(self):
|
|
|
|
if self.index >= len(self.lines):
|
|
|
|
line = ""
|
|
|
|
else:
|
|
|
|
line = self.lines[self.index]
|
|
|
|
self.index += 1
|
|
|
|
return line
|
|
|
|
|
|
|
|
# Line-eater for tokenize.
|
2007-05-15 15:46:22 -03:00
|
|
|
def tokeneater(self, type, token, slinecol, end, line,
|
2000-10-05 00:48:38 -03:00
|
|
|
INDENT=tokenize.INDENT,
|
|
|
|
DEDENT=tokenize.DEDENT,
|
|
|
|
NEWLINE=tokenize.NEWLINE,
|
|
|
|
COMMENT=tokenize.COMMENT,
|
|
|
|
NL=tokenize.NL):
|
|
|
|
|
|
|
|
if type == NEWLINE:
|
|
|
|
# A program statement, or ENDMARKER, will eventually follow,
|
|
|
|
# after some (possibly empty) run of tokens of the form
|
|
|
|
# (NL | COMMENT)* (INDENT | DEDENT+)?
|
|
|
|
self.find_stmt = 1
|
|
|
|
|
|
|
|
elif type == INDENT:
|
|
|
|
self.find_stmt = 1
|
|
|
|
self.level += 1
|
|
|
|
|
|
|
|
elif type == DEDENT:
|
|
|
|
self.find_stmt = 1
|
|
|
|
self.level -= 1
|
|
|
|
|
|
|
|
elif type == COMMENT:
|
|
|
|
if self.find_stmt:
|
2007-05-15 15:46:22 -03:00
|
|
|
self.stats.append((slinecol[0], -1))
|
2000-10-05 00:48:38 -03:00
|
|
|
# but we're still looking for a new stmt, so leave
|
|
|
|
# find_stmt alone
|
|
|
|
|
|
|
|
elif type == NL:
|
|
|
|
pass
|
|
|
|
|
|
|
|
elif self.find_stmt:
|
|
|
|
# This is the first "real token" following a NEWLINE, so it
|
|
|
|
# must be the first token of the next program statement, or an
|
|
|
|
# ENDMARKER.
|
|
|
|
self.find_stmt = 0
|
|
|
|
if line: # not endmarker
|
2007-05-15 15:46:22 -03:00
|
|
|
self.stats.append((slinecol[0], self.level))
|
2000-10-05 00:48:38 -03:00
|
|
|
|
2010-08-09 09:24:20 -03:00
|
|
|
|
2000-10-05 00:48:38 -03:00
|
|
|
# Count number of leading blanks.
|
|
|
|
def getlspace(line):
|
|
|
|
i, n = 0, len(line)
|
|
|
|
while i < n and line[i] == " ":
|
|
|
|
i += 1
|
|
|
|
return i
|
|
|
|
|
2010-08-09 09:24:20 -03:00
|
|
|
|
2000-10-05 00:48:38 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|