Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/reindent.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 070dc6e359fb |
| children | 4feb889d3bed |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
49553
diff
changeset
|
1 #! /usr/bin/env python3 |
|
15018
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 |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
7 -d (--dryrun) Dry run. Analyze, but don't make any changes to, files. |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
8 -r (--recurse) Recurse. Search for all .py files in subdirectories too. |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
9 -n (--nobackup) No backup. Does not make a ".bak" file before reindenting. |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
10 -v (--verbose) Verbose. Print informative msgs; else no output. |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
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. |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
35 |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
36 The backup file is a copy of the one that is being reindented. The ".bak" |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
37 file is generated with shutil.copy(), but some corner cases regarding |
|
71504
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
38 user/group and permissions could leave the backup file more readable than |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
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 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
45 import os |
|
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
46 import shutil |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
47 import sys |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
48 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
49 verbose = False |
|
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
50 recurse = False |
|
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
51 dryrun = False |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
52 makebackup = True |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
53 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
54 |
|
31832
64e2f7ec5576
add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents:
22573
diff
changeset
|
55 def usage(msg=None): |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
56 if msg is None: |
|
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
57 msg = __doc__ |
|
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
58 print(msg, file=sys.stderr) |
|
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
59 |
|
31832
64e2f7ec5576
add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents:
22573
diff
changeset
|
60 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
61 def errprint(*args): |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
62 sys.stderr.write(" ".join(str(arg) for arg in args)) |
|
15018
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 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
65 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
66 def main(): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
67 import getopt |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
68 global verbose, recurse, dryrun, makebackup |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
69 try: |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
70 opts, args = getopt.getopt(sys.argv[1:], "drnvh", |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
71 ["dryrun", "recurse", "nobackup", "verbose", "help"]) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
31832
diff
changeset
|
72 except getopt.error as msg: |
|
31832
64e2f7ec5576
add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents:
22573
diff
changeset
|
73 usage(msg) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
74 return |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
75 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
|
76 if o in ('-d', '--dryrun'): |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
77 dryrun = True |
|
31832
64e2f7ec5576
add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents:
22573
diff
changeset
|
78 elif o in ('-r', '--recurse'): |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
79 recurse = True |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
80 elif o in ('-n', '--nobackup'): |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
81 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
|
82 elif o in ('-v', '--verbose'): |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
83 verbose = True |
|
31832
64e2f7ec5576
add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents:
22573
diff
changeset
|
84 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
|
85 usage() |
|
64e2f7ec5576
add usage() function, -h(elp) flag and long versions of short flags
Skip Montanaro <skip@pobox.com>
parents:
22573
diff
changeset
|
86 return |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
87 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
|
88 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
|
89 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
|
90 r.write(sys.stdout) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
91 return |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
92 for arg in args: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
93 check(arg) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
94 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
95 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
96 def check(file): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
97 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
|
98 if verbose: |
|
41553
adfabc57361d
Fix print syntax so this runs using 3.0.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
99 print("listing directory", file) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
100 names = os.listdir(file) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
101 for name in names: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
102 fullname = os.path.join(file, name) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
103 if ((recurse and os.path.isdir(fullname) and |
|
49553
3ff7bb830519
Merged revisions 66887,66891,66902-66903,66905-66906,66911-66913,66922,66927-66928,66936,66939-66940,66962,66964,66973 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
45888
diff
changeset
|
104 not os.path.islink(fullname) and |
|
3ff7bb830519
Merged revisions 66887,66891,66902-66903,66905-66906,66911-66913,66922,66927-66928,66936,66939-66940,66962,66964,66973 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
45888
diff
changeset
|
105 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
|
106 or name.lower().endswith(".py")): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
107 check(fullname) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
108 return |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
109 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
110 if verbose: |
|
41553
adfabc57361d
Fix print syntax so this runs using 3.0.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
111 print("checking", file, "...", end=' ') |
|
71504
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
112 with open(file, 'rb') as f: |
|
65525
86f00612cbd6
Issue #10117: Tools/scripts/reindent.py now accepts source files that
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63872
diff
changeset
|
113 encoding, _ = tokenize.detect_encoding(f.readline) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
114 try: |
|
65525
86f00612cbd6
Issue #10117: Tools/scripts/reindent.py now accepts source files that
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63872
diff
changeset
|
115 with open(file, encoding=encoding) as f: |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
116 r = Reindenter(f) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
31832
diff
changeset
|
117 except IOError as msg: |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
118 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
|
119 return |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
120 |
|
71504
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
121 newline = r.newlines |
|
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
122 if isinstance(newline, tuple): |
|
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
123 errprint("%s: mixed newlines detected; cannot process file" % file) |
|
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
124 return |
|
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
125 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
126 if r.run(): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
127 if verbose: |
|
41553
adfabc57361d
Fix print syntax so this runs using 3.0.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
128 print("changed.") |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
129 if dryrun: |
|
41553
adfabc57361d
Fix print syntax so this runs using 3.0.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
130 print("But this is a dry run, so leaving it alone.") |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
131 if not dryrun: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
132 bak = file + ".bak" |
|
45399
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
133 if makebackup: |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
134 shutil.copyfile(file, bak) |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
135 if verbose: |
|
15d5776f2703
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
Christian Heimes <christian@cheimes.de>
parents:
41717
diff
changeset
|
136 print("backed up", file, "to", bak) |
|
71504
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
137 with open(file, "w", encoding=encoding, newline=newline) as f: |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
138 r.write(f) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
139 if verbose: |
|
41553
adfabc57361d
Fix print syntax so this runs using 3.0.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
140 print("wrote new", file) |
|
45865
e0e8bef0d5ec
Merged revisions 61520,61523-61528,61532 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
141 return True |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
142 else: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
143 if verbose: |
|
41553
adfabc57361d
Fix print syntax so this runs using 3.0.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
144 print("unchanged.") |
|
45865
e0e8bef0d5ec
Merged revisions 61520,61523-61528,61532 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
145 return False |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
146 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
147 |
|
20258
318589404b95
Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents:
15018
diff
changeset
|
148 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
|
149 """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
|
150 |
|
318589404b95
Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents:
15018
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 """ |
|
318589404b95
Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents:
15018
diff
changeset
|
155 |
|
318589404b95
Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents:
15018
diff
changeset
|
156 i = len(line) |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
157 while i > 0 and line[i - 1] in JUNK: |
|
20258
318589404b95
Changed the reindenter to strip only trailing spaces and tabs from lines,
Tim Peters <tim.peters@gmail.com>
parents:
15018
diff
changeset
|
158 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
|
159 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
|
160 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
161 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
162 class Reindenter: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
163 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
164 def __init__(self, f): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
165 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
|
166 self.level = 0 # current indent level |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
167 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
168 # Raw file lines. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
169 self.raw = f.readlines() |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
170 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
171 # 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
|
172 # 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
|
173 # 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
|
174 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
|
175 for line in self.raw] |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
176 self.lines.insert(0, None) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
177 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
|
178 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
179 # 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
|
180 # 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
|
181 # 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
|
182 # indeed, they're our headache! |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
183 self.stats = [] |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
184 |
|
71504
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
185 # Save the newlines found in the file so they can be used to |
|
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
186 # create output without mutating the newlines. |
|
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
187 self.newlines = f.newlines |
|
070dc6e359fb
Fixes #10639: reindent.py should not convert newlines
Jason R. Coombs <jaraco@jaraco.com>
parents:
65525
diff
changeset
|
188 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
189 def run(self): |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
45865
diff
changeset
|
190 tokens = tokenize.generate_tokens(self.getline) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
45865
diff
changeset
|
191 for _token in tokens: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
45865
diff
changeset
|
192 self.tokeneater(*_token) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
193 # Remove trailing empty lines. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
194 lines = self.lines |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
195 while lines and lines[-1] == "\n": |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
196 lines.pop() |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
197 # Sentinel. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
198 stats = self.stats |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
199 stats.append((len(lines), 0)) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
200 # 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
|
201 have2want = {} |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
202 # Program after transformation. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
203 after = self.after = [] |
|
21996
0b15cf11431e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20258
diff
changeset
|
204 # 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
|
205 # 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
|
206 i = stats[0][0] |
|
0b15cf11431e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20258
diff
changeset
|
207 after.extend(lines[1:i]) |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
208 for i in range(len(stats) - 1): |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
209 thisstmt, thislevel = stats[i] |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
210 nextstmt = stats[i + 1][0] |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
211 have = getlspace(lines[thisstmt]) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
212 want = thislevel * 4 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
213 if want < 0: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
214 # A comment line. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
215 if have: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
216 # 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
|
217 # 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
|
218 # mapped to. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
219 want = have2want.get(have, -1) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
220 if want < 0: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
221 # Then it probably belongs to the next real stmt. |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
222 for j in range(i + 1, len(stats) - 1): |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
223 jline, jlevel = stats[j] |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
224 if jlevel >= 0: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
225 if have == getlspace(lines[jline]): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
226 want = jlevel * 4 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
227 break |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
228 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
|
229 # comment like this one, |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
230 # 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
|
231 # line got shifted. |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
232 for j in range(i - 1, -1, -1): |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
233 jline, jlevel = stats[j] |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
234 if jlevel >= 0: |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
235 want = have + (getlspace(after[jline - 1]) - |
|
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
236 getlspace(lines[jline])) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
237 break |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
238 if want < 0: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
239 # Still no luck -- leave it alone. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
240 want = have |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
241 else: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
242 want = 0 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
243 assert want >= 0 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
244 have2want[have] = want |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
245 diff = want - have |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
246 if diff == 0 or have == 0: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
247 after.extend(lines[thisstmt:nextstmt]) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
248 else: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
249 for line in lines[thisstmt:nextstmt]: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
250 if diff > 0: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
251 if line == "\n": |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
252 after.append(line) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
253 else: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
254 after.append(" " * diff + line) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
255 else: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
256 remove = min(getlspace(line), -diff) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
257 after.append(line[remove:]) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
258 return self.raw != self.after |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
259 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
260 def write(self, f): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
261 f.writelines(self.after) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
262 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
263 # Line-getter for tokenize. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
264 def getline(self): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
265 if self.index >= len(self.lines): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
266 line = "" |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
267 else: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
268 line = self.lines[self.index] |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
269 self.index += 1 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
270 return line |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
271 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
272 # Line-eater for tokenize. |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
273 def tokeneater(self, type, token, slinecol, end, line, |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
274 INDENT=tokenize.INDENT, |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
275 DEDENT=tokenize.DEDENT, |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
276 NEWLINE=tokenize.NEWLINE, |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
277 COMMENT=tokenize.COMMENT, |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
278 NL=tokenize.NL): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
279 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
280 if type == NEWLINE: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
281 # 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
|
282 # 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
|
283 # (NL | COMMENT)* (INDENT | DEDENT+)? |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
284 self.find_stmt = 1 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
285 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
286 elif type == INDENT: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
287 self.find_stmt = 1 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
288 self.level += 1 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
289 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
290 elif type == DEDENT: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
291 self.find_stmt = 1 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
292 self.level -= 1 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
293 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
294 elif type == COMMENT: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
295 if self.find_stmt: |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
296 self.stats.append((slinecol[0], -1)) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
297 # 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
|
298 # find_stmt alone |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
299 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
300 elif type == NL: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
301 pass |
|
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 elif self.find_stmt: |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
304 # 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
|
305 # 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
|
306 # ENDMARKER. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
307 self.find_stmt = 0 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
308 if line: # not endmarker |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
309 self.stats.append((slinecol[0], self.level)) |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
310 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
311 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
312 # Count number of leading blanks. |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
313 def getlspace(line): |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
314 i, n = 0, len(line) |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
315 while i < n and line[i] == " ": |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
316 i += 1 |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
317 return i |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
318 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
319 |
|
15018
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
320 if __name__ == '__main__': |
|
98b1fa6c1600
New tool for normalizing indentation of .py files.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
321 main() |
