annotate Tools/scripts/pindent.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents c46dec051819
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 56378
diff changeset
1 #! /usr/bin/env python3
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
3 # This file contains a class and a main program that perform three
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # related (though complimentary) formatting operations on Python
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
5 # programs. When called as "pindent -c", it takes a valid Python
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # program as input and outputs a version augmented with block-closing
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
7 # comments. When called as "pindent -d", it assumes its input is a
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
8 # Python program with block-closing comments and outputs a commentless
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
9 # version. When called as "pindent -r" it assumes its input is a
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # Python program with block-closing comments but with its indentation
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # messed up, and outputs a properly indented version.
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # A "block-closing comment" is a comment of the form '# end <keyword>'
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # where <keyword> is the keyword that opened the block. If the
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # opening keyword is 'def' or 'class', the function or class name may
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # be repeated in the block-closing comment as well. Here is an
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # example of a program fully augmented with block-closing comments:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # def foobar(a, b):
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # if a == b:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # a = a+1
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # elif a < b:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # b = b-1
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # if b > a: a = a-1
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # # end if
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # else:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # print 'oops!'
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # # end if
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # # end def foobar
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Note that only the last part of an if...elif...else... block needs a
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # block-closing comment; the same is true for other compound
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # statements (e.g. try...except). Also note that "short-form" blocks
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # like the second 'if' in the example must be closed as well;
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # otherwise the 'else' in the example would be ambiguous (remember
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # that indentation is not significant when interpreting block-closing
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # comments).
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
39 # The operations are idempotent (i.e. applied to their own output
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # they yield an identical result). Running first "pindent -c" and
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # then "pindent -r" on a valid Python program produces a program that
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # is semantically identical to the input (though its indentation may
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
43 # be different). Running "pindent -e" on that output produces a
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
44 # program that only differs from the original in indentation.
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # Other options:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # -s stepsize: set the indentation step size (default 8)
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # -t tabsize : set the number of spaces a tab character is worth (default 8)
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
49 # -e : expand TABs into spaces
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # file ... : input file(s) (default standard input)
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # The results always go to standard output
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # Caveats:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # - comments ending in a backslash will be mistaken for continued lines
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # - continuations using backslash are always left unchanged
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # - continuations inside parentheses are not extra indented by -r
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # but must be indented for -c to work correctly (this breaks
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # idempotency!)
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # - continued lines inside triple-quoted strings are totally garbled
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # Secret feature:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # - On input, a block may also be closed with an "end statement" --
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # this is a block-closing comment without the '#' sign.
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 # Possible improvements:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # - check syntax based on transitions in 'next' table
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 # - better error reporting
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # - better error recovery
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # - check identifier after class/def
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # The following wishes need a more complete tokenization of the source:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 # - Don't get fooled by comments ending in backslash
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # - reindent continuation lines indicated by backslash
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # - handle continuation lines inside parentheses/braces/brackets
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 # - handle triple quoted strings spanning lines
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 # - realign comments
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 # - optionally do much more thorough reformatting, a la C indent
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
79 # Defaults
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
80 STEPSIZE = 8
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
81 TABSIZE = 8
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
82 EXPANDTABS = False
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
83
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
84 import io
8531
18a9289df99a Translated to use re instead of regex. Also replaced one use of
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
85 import re
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 import sys
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 next = {}
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 next['if'] = next['elif'] = 'elif', 'else', 'end'
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 next['while'] = next['for'] = 'else', 'end'
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 next['try'] = 'except', 'finally'
56378
8dbb0fc3998a Merged revisions 74865,75175,75180 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47100
diff changeset
92 next['except'] = 'except', 'else', 'finally', 'end'
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
93 next['else'] = next['finally'] = next['with'] = \
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
94 next['def'] = next['class'] = 'end'
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 next['end'] = ()
56378
8dbb0fc3998a Merged revisions 74865,75175,75180 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47100
diff changeset
96 start = 'if', 'while', 'for', 'try', 'with', 'def', 'class'
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 class PythonIndenter:
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
100 def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
101 indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
102 self.fpi = fpi
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
103 self.fpo = fpo
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
104 self.indentsize = indentsize
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
105 self.tabsize = tabsize
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
106 self.lineno = 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
107 self.expandtabs = expandtabs
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
108 self._write = fpo.write
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
109 self.kwprog = re.compile(
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
110 r'^(?:\s|\\\n)*(?P<kw>[a-z]+)'
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
111 r'((?:\s|\\\n)+(?P<id>[a-zA-Z_]\w*))?'
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
112 r'[^\w]')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
113 self.endprog = re.compile(
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
114 r'^(?:\s|\\\n)*#?\s*end\s+(?P<kw>[a-z]+)'
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
115 r'(\s+(?P<id>[a-zA-Z_]\w*))?'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
116 r'[^\w]')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
117 self.wsprog = re.compile(r'^[ \t]*')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
118 # end def __init__
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
120 def write(self, line):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
121 if self.expandtabs:
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
122 self._write(line.expandtabs(self.tabsize))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
123 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
124 self._write(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
125 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
126 # end def write
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
127
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
128 def readline(self):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
129 line = self.fpi.readline()
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
130 if line: self.lineno += 1
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
131 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
132 return line
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
133 # end def readline
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
135 def error(self, fmt, *args):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
136 if args: fmt = fmt % args
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
137 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
138 sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt))
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
139 self.write('### %s ###\n' % fmt)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
140 # end def error
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
142 def getline(self):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
143 line = self.readline()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
144 while line[-2:] == '\\\n':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
145 line2 = self.readline()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
146 if not line2: break
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
147 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
148 line += line2
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
149 # end while
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
150 return line
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
151 # end def getline
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
153 def putline(self, line, indent):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
154 tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
155 i = self.wsprog.match(line).end()
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
156 line = line[i:]
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
157 if line[:1] not in ('\n', '\r', ''):
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
158 line = '\t'*tabs + ' '*spaces + line
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
159 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
160 self.write(line)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
161 # end def putline
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
163 def reformat(self):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
164 stack = []
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
165 while True:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
166 line = self.getline()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
167 if not line: break # EOF
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
168 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
169 m = self.endprog.match(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
170 if m:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
171 kw = 'end'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
172 kw2 = m.group('kw')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
173 if not stack:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
174 self.error('unexpected end')
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
175 elif stack.pop()[0] != kw2:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
176 self.error('unmatched end')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
177 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
178 self.putline(line, len(stack))
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
179 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
180 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
181 m = self.kwprog.match(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
182 if m:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
183 kw = m.group('kw')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
184 if kw in start:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
185 self.putline(line, len(stack))
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
186 stack.append((kw, kw))
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
187 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
188 # end if
47100
893a4dc17a91 Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents: 45471
diff changeset
189 if kw in next and stack:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
190 self.putline(line, len(stack)-1)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
191 kwa, kwb = stack[-1]
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
192 stack[-1] = kwa, kw
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
193 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
194 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
195 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
196 self.putline(line, len(stack))
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
197 # end while
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
198 if stack:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
199 self.error('unterminated keywords')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
200 for kwa, kwb in stack:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
201 self.write('\t%s\n' % kwa)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
202 # end for
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
203 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
204 # end def reformat
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
206 def delete(self):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
207 begin_counter = 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
208 end_counter = 0
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
209 while True:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
210 line = self.getline()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
211 if not line: break # EOF
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
212 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
213 m = self.endprog.match(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
214 if m:
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
215 end_counter += 1
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
216 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
217 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
218 m = self.kwprog.match(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
219 if m:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
220 kw = m.group('kw')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
221 if kw in start:
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
222 begin_counter += 1
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
223 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
224 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
225 self.write(line)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
226 # end while
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
227 if begin_counter - end_counter < 0:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
228 sys.stderr.write('Warning: input contained more end tags than expected\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
229 elif begin_counter - end_counter > 0:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
230 sys.stderr.write('Warning: input contained less end tags than expected\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
231 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
232 # end def delete
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
233
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
234 def complete(self):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
235 stack = []
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
236 todo = []
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
237 currentws = thisid = firstkw = lastkw = topid = ''
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
238 while True:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
239 line = self.getline()
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
240 i = self.wsprog.match(line).end()
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
241 m = self.endprog.match(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
242 if m:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
243 thiskw = 'end'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
244 endkw = m.group('kw')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
245 thisid = m.group('id')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
246 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
247 m = self.kwprog.match(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
248 if m:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
249 thiskw = m.group('kw')
47100
893a4dc17a91 Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents: 45471
diff changeset
250 if thiskw not in next:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
251 thiskw = ''
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
252 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
253 if thiskw in ('def', 'class'):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
254 thisid = m.group('id')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
255 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
256 thisid = ''
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
257 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
258 elif line[i:i+1] in ('\n', '#'):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
259 todo.append(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
260 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
261 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
262 thiskw = ''
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
263 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
264 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
265 indentws = line[:i]
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
266 indent = len(indentws.expandtabs(self.tabsize))
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
267 current = len(currentws.expandtabs(self.tabsize))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
268 while indent < current:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
269 if firstkw:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
270 if topid:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
271 s = '# end %s %s\n' % (
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
272 firstkw, topid)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
273 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
274 s = '# end %s\n' % firstkw
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
275 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
276 self.write(currentws + s)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
277 firstkw = lastkw = ''
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
278 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
279 currentws, firstkw, lastkw, topid = stack.pop()
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
280 current = len(currentws.expandtabs(self.tabsize))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
281 # end while
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
282 if indent == current and firstkw:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
283 if thiskw == 'end':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
284 if endkw != firstkw:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
285 self.error('mismatched end')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
286 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
287 firstkw = lastkw = ''
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
288 elif not thiskw or thiskw in start:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
289 if topid:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
290 s = '# end %s %s\n' % (
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
291 firstkw, topid)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
292 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
293 s = '# end %s\n' % firstkw
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
294 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
295 self.write(currentws + s)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
296 firstkw = lastkw = topid = ''
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
297 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
298 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
299 if indent > current:
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
300 stack.append((currentws, firstkw, lastkw, topid))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
301 if thiskw and thiskw not in start:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
302 # error
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
303 thiskw = ''
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
304 # end if
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
305 currentws, firstkw, lastkw, topid = \
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
306 indentws, thiskw, thiskw, thisid
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
307 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
308 if thiskw:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
309 if thiskw in start:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
310 firstkw = lastkw = thiskw
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
311 topid = thisid
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
312 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
313 lastkw = thiskw
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
314 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
315 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
316 for l in todo: self.write(l)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
317 # end for
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
318 todo = []
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
319 if not line: break
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
320 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
321 self.write(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
322 # end while
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
323 # end def complete
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 # end class PythonIndenter
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
326 # Simplified user interface
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
327 # - xxx_filter(input, output): read and write file objects
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
328 # - xxx_string(s): take and return string object
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
329 # - xxx_file(filename): process file in place, return true iff changed
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
330
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
331 def complete_filter(input = sys.stdin, output = sys.stdout,
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
332 stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
333 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
334 pi.complete()
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
335 # end def complete_filter
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
336
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
337 def delete_filter(input= sys.stdin, output = sys.stdout,
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
338 stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
339 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
340 pi.delete()
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
341 # end def delete_filter
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
342
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
343 def reformat_filter(input = sys.stdin, output = sys.stdout,
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
344 stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
345 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
346 pi.reformat()
13105
fd8467d1375d Running the program through itself reveals that one end tag was
Guido van Rossum <guido@python.org>
parents: 13104
diff changeset
347 # end def reformat_filter
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
348
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
349 def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
350 input = io.StringIO(source)
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
351 output = io.StringIO()
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
352 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
353 pi.complete()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
354 return output.getvalue()
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
355 # end def complete_string
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
356
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
357 def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
358 input = io.StringIO(source)
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
359 output = io.StringIO()
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
360 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
361 pi.delete()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
362 return output.getvalue()
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
363 # end def delete_string
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
364
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
365 def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
366 input = io.StringIO(source)
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
367 output = io.StringIO()
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
368 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
369 pi.reformat()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
370 return output.getvalue()
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
371 # end def reformat_string
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
372
81424
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
373 def make_backup(filename):
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
374 import os, os.path
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
375 backup = filename + '~'
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
376 if os.path.lexists(backup):
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
377 try:
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
378 os.remove(backup)
81426
c46dec051819 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81407 81424
diff changeset
379 except OSError:
81424
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
380 print("Can't remove backup %r" % (backup,), file=sys.stderr)
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
381 # end try
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
382 # end if
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
383 try:
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
384 os.rename(filename, backup)
81426
c46dec051819 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81407 81424
diff changeset
385 except OSError:
81424
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
386 print("Can't rename %r to %r" % (filename, backup), file=sys.stderr)
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
387 # end try
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
388 # end def make_backup
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
389
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
390 def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
391 with open(filename, 'r') as f:
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
392 source = f.read()
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
393 # end with
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
394 result = complete_string(source, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
395 if source == result: return 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
396 # end if
81424
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
397 make_backup(filename)
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
398 with open(filename, 'w') as f:
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
399 f.write(result)
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
400 # end with
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
401 return 1
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
402 # end def complete_file
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
403
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
404 def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
405 with open(filename, 'r') as f:
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
406 source = f.read()
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
407 # end with
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
408 result = delete_string(source, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
409 if source == result: return 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
410 # end if
81424
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
411 make_backup(filename)
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
412 with open(filename, 'w') as f:
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
413 f.write(result)
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
414 # end with
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
415 return 1
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
416 # end def delete_file
13104
797acf3ffd26 Peter Schneider-Kamp:
Guido van Rossum <guido@python.org>
parents: 8531
diff changeset
417
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
418 def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
419 with open(filename, 'r') as f:
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
420 source = f.read()
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
421 # end with
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
422 result = reformat_string(source, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
423 if source == result: return 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
424 # end if
81424
a9990104f728 Issue #15539: Fix a backup file creation in pindent.py on Windows.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81405
diff changeset
425 make_backup(filename)
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
426 with open(filename, 'w') as f:
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
427 f.write(result)
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
428 # end with
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
429 return 1
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
430 # end def reformat_file
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
431
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
432 # Test program when called as a script
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
433
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
434 usage = """
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
435 usage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ...
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
436 -c : complete a correctly indented program (add #end directives)
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
437 -d : delete #end directives
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
438 -r : reformat a completed program (use #end directives)
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
439 -s stepsize: indentation step (default %(STEPSIZE)d)
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
440 -t tabsize : the worth in spaces of a tab (default %(TABSIZE)d)
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
441 -e : expand TABs into spaces (default OFF)
1662
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
442 [file] ... : files are changed in place, with backups in file~
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
443 If no files are specified or a single - is given,
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
444 the program acts as a filter (reads stdin, writes stdout).
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
445 """ % vars()
14b03418c90f Added convenience functions {complete,reformat}_{filter,string,file}.
Guido van Rossum <guido@python.org>
parents: 1654
diff changeset
446
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
447 def error_both(op1, op2):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
448 sys.stderr.write('Error: You can not specify both '+op1+' and -'+op2[0]+' at the same time\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
449 sys.stderr.write(usage)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
450 sys.exit(2)
13537
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
451 # end def error_both
b9f9da1a3fc4 add expandtabs command (-e)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13105
diff changeset
452
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 def test():
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
454 import getopt
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
455 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
456 opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 35147
diff changeset
457 except getopt.error as msg:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
458 sys.stderr.write('Error: %s\n' % msg)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
459 sys.stderr.write(usage)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
460 sys.exit(2)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
461 # end try
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
462 action = None
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
463 stepsize = STEPSIZE
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
464 tabsize = TABSIZE
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
465 expandtabs = EXPANDTABS
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
466 for o, a in opts:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
467 if o == '-c':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
468 if action: error_both(o, action)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
469 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
470 action = 'complete'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
471 elif o == '-d':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
472 if action: error_both(o, action)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
473 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
474 action = 'delete'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
475 elif o == '-r':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
476 if action: error_both(o, action)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
477 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
478 action = 'reformat'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
479 elif o == '-s':
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
480 stepsize = int(a)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
481 elif o == '-t':
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
482 tabsize = int(a)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
483 elif o == '-e':
81405
9df6b707aef9 Issue #15539: Fix a number of bugs in Tools/scripts/pindent.py.
Serhiy Storchaka <storchaka@gmail.com>
parents: 59522
diff changeset
484 expandtabs = True
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
485 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
486 # end for
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
487 if not action:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
488 sys.stderr.write(
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
489 'You must specify -c(omplete), -d(elete) or -r(eformat)\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
490 sys.stderr.write(usage)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
491 sys.exit(2)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
492 # end if
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
493 if not args or args == ['-']:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
494 action = eval(action + '_filter')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
495 action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
496 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
497 action = eval(action + '_file')
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
498 for filename in args:
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
499 action(filename, stepsize, tabsize, expandtabs)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
500 # end for
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
501 # end if
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 # end def test
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
503
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 if __name__ == '__main__':
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13537
diff changeset
505 test()
1632
007adf01543c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 # end if