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