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 | |
Serhiy Storchaka | dfae912 | 2013-01-11 22:16:15 +0200 | [diff] [blame] | 79 | from __future__ import print_function |
| 80 | |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 81 | # Defaults |
| 82 | STEPSIZE = 8 |
| 83 | TABSIZE = 8 |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 84 | EXPANDTABS = False |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 85 | |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 86 | import io |
Guido van Rossum | f57736e | 1998-06-19 21:39:27 +0000 | [diff] [blame] | 87 | import re |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 88 | import sys |
| 89 | |
| 90 | next = {} |
| 91 | next['if'] = next['elif'] = 'elif', 'else', 'end' |
| 92 | next['while'] = next['for'] = 'else', 'end' |
| 93 | next['try'] = 'except', 'finally' |
Georg Brandl | af09b60 | 2009-09-17 07:49:37 +0000 | [diff] [blame] | 94 | next['except'] = 'except', 'else', 'finally', 'end' |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 95 | next['else'] = next['finally'] = next['with'] = \ |
| 96 | next['def'] = next['class'] = 'end' |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 97 | next['end'] = () |
Georg Brandl | af09b60 | 2009-09-17 07:49:37 +0000 | [diff] [blame] | 98 | start = 'if', 'while', 'for', 'try', 'with', 'def', 'class' |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 99 | |
| 100 | class PythonIndenter: |
| 101 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 102 | def __init__(self, fpi = sys.stdin, fpo = sys.stdout, |
| 103 | indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 104 | self.fpi = fpi |
| 105 | self.fpo = fpo |
| 106 | self.indentsize = indentsize |
| 107 | self.tabsize = tabsize |
| 108 | self.lineno = 0 |
| 109 | self.expandtabs = expandtabs |
| 110 | self._write = fpo.write |
| 111 | self.kwprog = re.compile( |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 112 | r'^(?:\s|\\\n)*(?P<kw>[a-z]+)' |
| 113 | r'((?:\s|\\\n)+(?P<id>[a-zA-Z_]\w*))?' |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 114 | r'[^\w]') |
| 115 | self.endprog = re.compile( |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 116 | r'^(?:\s|\\\n)*#?\s*end\s+(?P<kw>[a-z]+)' |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 117 | r'(\s+(?P<id>[a-zA-Z_]\w*))?' |
| 118 | r'[^\w]') |
| 119 | self.wsprog = re.compile(r'^[ \t]*') |
| 120 | # end def __init__ |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 121 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 122 | def write(self, line): |
| 123 | if self.expandtabs: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 124 | self._write(line.expandtabs(self.tabsize)) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 125 | else: |
| 126 | self._write(line) |
| 127 | # end if |
| 128 | # end def write |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 129 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 130 | def readline(self): |
| 131 | line = self.fpi.readline() |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 132 | if line: self.lineno += 1 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 133 | # end if |
| 134 | return line |
| 135 | # end def readline |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 136 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 137 | def error(self, fmt, *args): |
| 138 | if args: fmt = fmt % args |
| 139 | # end if |
| 140 | sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt)) |
| 141 | self.write('### %s ###\n' % fmt) |
| 142 | # end def error |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 143 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 144 | def getline(self): |
| 145 | line = self.readline() |
| 146 | while line[-2:] == '\\\n': |
| 147 | line2 = self.readline() |
| 148 | if not line2: break |
| 149 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 150 | line += line2 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 151 | # end while |
| 152 | return line |
| 153 | # end def getline |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 154 | |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 155 | def putline(self, line, indent): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 156 | tabs, spaces = divmod(indent*self.indentsize, self.tabsize) |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 157 | i = self.wsprog.match(line).end() |
| 158 | line = line[i:] |
| 159 | if line[:1] not in ('\n', '\r', ''): |
| 160 | line = '\t'*tabs + ' '*spaces + line |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 161 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 162 | self.write(line) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 163 | # end def putline |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 164 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 165 | def reformat(self): |
| 166 | stack = [] |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 167 | while True: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 168 | line = self.getline() |
| 169 | if not line: break # EOF |
| 170 | # end if |
| 171 | m = self.endprog.match(line) |
| 172 | if m: |
| 173 | kw = 'end' |
| 174 | kw2 = m.group('kw') |
| 175 | if not stack: |
| 176 | self.error('unexpected end') |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 177 | elif stack.pop()[0] != kw2: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 178 | self.error('unmatched end') |
| 179 | # end if |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 180 | self.putline(line, len(stack)) |
| 181 | continue |
| 182 | # end if |
| 183 | m = self.kwprog.match(line) |
| 184 | if m: |
| 185 | kw = m.group('kw') |
| 186 | if kw in start: |
| 187 | self.putline(line, len(stack)) |
| 188 | stack.append((kw, kw)) |
| 189 | continue |
| 190 | # end if |
| 191 | if next.has_key(kw) and stack: |
| 192 | self.putline(line, len(stack)-1) |
| 193 | kwa, kwb = stack[-1] |
| 194 | stack[-1] = kwa, kw |
| 195 | continue |
| 196 | # end if |
| 197 | # end if |
| 198 | self.putline(line, len(stack)) |
| 199 | # end while |
| 200 | if stack: |
| 201 | self.error('unterminated keywords') |
| 202 | for kwa, kwb in stack: |
| 203 | self.write('\t%s\n' % kwa) |
| 204 | # end for |
| 205 | # end if |
| 206 | # end def reformat |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 207 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 208 | def delete(self): |
| 209 | begin_counter = 0 |
| 210 | end_counter = 0 |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 211 | while True: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 212 | line = self.getline() |
| 213 | if not line: break # EOF |
| 214 | # end if |
| 215 | m = self.endprog.match(line) |
| 216 | if m: |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 217 | end_counter += 1 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 218 | continue |
| 219 | # end if |
| 220 | m = self.kwprog.match(line) |
| 221 | if m: |
| 222 | kw = m.group('kw') |
| 223 | if kw in start: |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 224 | begin_counter += 1 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 225 | # end if |
| 226 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 227 | self.write(line) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 228 | # end while |
| 229 | if begin_counter - end_counter < 0: |
| 230 | sys.stderr.write('Warning: input contained more end tags than expected\n') |
| 231 | elif begin_counter - end_counter > 0: |
| 232 | sys.stderr.write('Warning: input contained less end tags than expected\n') |
| 233 | # end if |
| 234 | # end def delete |
| 235 | |
| 236 | def complete(self): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 237 | stack = [] |
| 238 | todo = [] |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 239 | currentws = thisid = firstkw = lastkw = topid = '' |
| 240 | while True: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 241 | line = self.getline() |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 242 | i = self.wsprog.match(line).end() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 243 | m = self.endprog.match(line) |
| 244 | if m: |
| 245 | thiskw = 'end' |
| 246 | endkw = m.group('kw') |
| 247 | thisid = m.group('id') |
| 248 | else: |
| 249 | m = self.kwprog.match(line) |
| 250 | if m: |
| 251 | thiskw = m.group('kw') |
| 252 | if not next.has_key(thiskw): |
| 253 | thiskw = '' |
| 254 | # end if |
| 255 | if thiskw in ('def', 'class'): |
| 256 | thisid = m.group('id') |
| 257 | else: |
| 258 | thisid = '' |
| 259 | # end if |
| 260 | elif line[i:i+1] in ('\n', '#'): |
| 261 | todo.append(line) |
| 262 | continue |
| 263 | else: |
| 264 | thiskw = '' |
| 265 | # end if |
| 266 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 267 | indentws = line[:i] |
| 268 | indent = len(indentws.expandtabs(self.tabsize)) |
| 269 | current = len(currentws.expandtabs(self.tabsize)) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 270 | while indent < current: |
| 271 | if firstkw: |
| 272 | if topid: |
| 273 | s = '# end %s %s\n' % ( |
| 274 | firstkw, topid) |
| 275 | else: |
| 276 | s = '# end %s\n' % firstkw |
| 277 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 278 | self.write(currentws + s) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 279 | firstkw = lastkw = '' |
| 280 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 281 | currentws, firstkw, lastkw, topid = stack.pop() |
| 282 | current = len(currentws.expandtabs(self.tabsize)) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 283 | # end while |
| 284 | if indent == current and firstkw: |
| 285 | if thiskw == 'end': |
| 286 | if endkw != firstkw: |
| 287 | self.error('mismatched end') |
| 288 | # end if |
| 289 | firstkw = lastkw = '' |
| 290 | elif not thiskw or thiskw in start: |
| 291 | if topid: |
| 292 | s = '# end %s %s\n' % ( |
| 293 | firstkw, topid) |
| 294 | else: |
| 295 | s = '# end %s\n' % firstkw |
| 296 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 297 | self.write(currentws + s) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 298 | firstkw = lastkw = topid = '' |
| 299 | # end if |
| 300 | # end if |
| 301 | if indent > current: |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 302 | stack.append((currentws, firstkw, lastkw, topid)) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 303 | if thiskw and thiskw not in start: |
| 304 | # error |
| 305 | thiskw = '' |
| 306 | # end if |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 307 | currentws, firstkw, lastkw, topid = \ |
| 308 | indentws, thiskw, thiskw, thisid |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 309 | # end if |
| 310 | if thiskw: |
| 311 | if thiskw in start: |
| 312 | firstkw = lastkw = thiskw |
| 313 | topid = thisid |
| 314 | else: |
| 315 | lastkw = thiskw |
| 316 | # end if |
| 317 | # end if |
| 318 | for l in todo: self.write(l) |
| 319 | # end for |
| 320 | todo = [] |
| 321 | if not line: break |
| 322 | # end if |
| 323 | self.write(line) |
| 324 | # end while |
| 325 | # end def complete |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 326 | # end class PythonIndenter |
| 327 | |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 328 | # Simplified user interface |
| 329 | # - xxx_filter(input, output): read and write file objects |
| 330 | # - xxx_string(s): take and return string object |
| 331 | # - xxx_file(filename): process file in place, return true iff changed |
| 332 | |
Guido van Rossum | 59811b1 | 2000-06-28 22:47:22 +0000 | [diff] [blame] | 333 | def complete_filter(input = sys.stdin, output = sys.stdout, |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 334 | stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 335 | pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 336 | pi.complete() |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 337 | # end def complete_filter |
| 338 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 339 | def delete_filter(input= sys.stdin, output = sys.stdout, |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 340 | stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 341 | pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 342 | pi.delete() |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 343 | # end def delete_filter |
Guido van Rossum | 59811b1 | 2000-06-28 22:47:22 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 345 | def reformat_filter(input = sys.stdin, output = sys.stdout, |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 346 | stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 347 | pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 348 | pi.reformat() |
Guido van Rossum | a04ff0f | 2000-06-28 22:55:20 +0000 | [diff] [blame] | 349 | # end def reformat_filter |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 350 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 351 | def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 352 | input = io.BytesIO(source) |
| 353 | output = io.BytesIO() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 354 | pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 355 | pi.complete() |
| 356 | return output.getvalue() |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 357 | # end def complete_string |
| 358 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 359 | def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 360 | input = io.BytesIO(source) |
| 361 | output = io.BytesIO() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 362 | pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 363 | pi.delete() |
| 364 | return output.getvalue() |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 365 | # end def delete_string |
Guido van Rossum | 59811b1 | 2000-06-28 22:47:22 +0000 | [diff] [blame] | 366 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 367 | def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 368 | input = io.BytesIO(source) |
| 369 | output = io.BytesIO() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 370 | pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 371 | pi.reformat() |
| 372 | return output.getvalue() |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 373 | # end def reformat_string |
| 374 | |
Serhiy Storchaka | dfae912 | 2013-01-11 22:16:15 +0200 | [diff] [blame] | 375 | def make_backup(filename): |
| 376 | import os, os.path |
| 377 | backup = filename + '~' |
| 378 | if os.path.lexists(backup): |
| 379 | try: |
| 380 | os.remove(backup) |
| 381 | except os.error: |
| 382 | print("Can't remove backup %r" % (backup,), file=sys.stderr) |
| 383 | # end try |
| 384 | # end if |
| 385 | try: |
| 386 | os.rename(filename, backup) |
| 387 | except os.error: |
| 388 | print("Can't rename %r to %r" % (filename, backup), file=sys.stderr) |
| 389 | # end try |
| 390 | # end def make_backup |
| 391 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 392 | def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 393 | with open(filename, 'r') as f: |
| 394 | source = f.read() |
| 395 | # end with |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 396 | result = complete_string(source, stepsize, tabsize, expandtabs) |
| 397 | if source == result: return 0 |
| 398 | # end if |
Serhiy Storchaka | dfae912 | 2013-01-11 22:16:15 +0200 | [diff] [blame] | 399 | make_backup(filename) |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 400 | with open(filename, 'w') as f: |
| 401 | f.write(result) |
| 402 | # end with |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 403 | return 1 |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 404 | # end def complete_file |
| 405 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 406 | def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 407 | with open(filename, 'r') as f: |
| 408 | source = f.read() |
| 409 | # end with |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 410 | result = delete_string(source, stepsize, tabsize, expandtabs) |
| 411 | if source == result: return 0 |
| 412 | # end if |
Serhiy Storchaka | dfae912 | 2013-01-11 22:16:15 +0200 | [diff] [blame] | 413 | make_backup(filename) |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 414 | with open(filename, 'w') as f: |
| 415 | f.write(result) |
| 416 | # end with |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 417 | return 1 |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 418 | # end def delete_file |
Guido van Rossum | 59811b1 | 2000-06-28 22:47:22 +0000 | [diff] [blame] | 419 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 420 | def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 421 | with open(filename, 'r') as f: |
| 422 | source = f.read() |
| 423 | # end with |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 424 | result = reformat_string(source, stepsize, tabsize, expandtabs) |
| 425 | if source == result: return 0 |
| 426 | # end if |
Serhiy Storchaka | dfae912 | 2013-01-11 22:16:15 +0200 | [diff] [blame] | 427 | make_backup(filename) |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 428 | with open(filename, 'w') as f: |
| 429 | f.write(result) |
| 430 | # end with |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 431 | return 1 |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 432 | # end def reformat_file |
| 433 | |
| 434 | # Test program when called as a script |
| 435 | |
| 436 | usage = """ |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 437 | usage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ... |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 438 | -c : complete a correctly indented program (add #end directives) |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 439 | -d : delete #end directives |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 440 | -r : reformat a completed program (use #end directives) |
| 441 | -s stepsize: indentation step (default %(STEPSIZE)d) |
| 442 | -t tabsize : the worth in spaces of a tab (default %(TABSIZE)d) |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 443 | -e : expand TABs into spaces (default OFF) |
Guido van Rossum | 0038cd9 | 1994-06-07 22:19:41 +0000 | [diff] [blame] | 444 | [file] ... : files are changed in place, with backups in file~ |
| 445 | If no files are specified or a single - is given, |
| 446 | the program acts as a filter (reads stdin, writes stdout). |
| 447 | """ % vars() |
| 448 | |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 449 | def error_both(op1, op2): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 450 | sys.stderr.write('Error: You can not specify both '+op1+' and -'+op2[0]+' at the same time\n') |
| 451 | sys.stderr.write(usage) |
| 452 | sys.exit(2) |
Peter Schneider-Kamp | 7f589fd | 2000-07-11 16:43:16 +0000 | [diff] [blame] | 453 | # end def error_both |
| 454 | |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 455 | def test(): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 456 | import getopt |
| 457 | try: |
| 458 | opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e') |
| 459 | except getopt.error, msg: |
| 460 | sys.stderr.write('Error: %s\n' % msg) |
| 461 | sys.stderr.write(usage) |
| 462 | sys.exit(2) |
| 463 | # end try |
| 464 | action = None |
| 465 | stepsize = STEPSIZE |
| 466 | tabsize = TABSIZE |
| 467 | expandtabs = EXPANDTABS |
| 468 | for o, a in opts: |
| 469 | if o == '-c': |
| 470 | if action: error_both(o, action) |
| 471 | # end if |
| 472 | action = 'complete' |
| 473 | elif o == '-d': |
| 474 | if action: error_both(o, action) |
| 475 | # end if |
| 476 | action = 'delete' |
| 477 | elif o == '-r': |
| 478 | if action: error_both(o, action) |
| 479 | # end if |
| 480 | action = 'reformat' |
| 481 | elif o == '-s': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 482 | stepsize = int(a) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 483 | elif o == '-t': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 484 | tabsize = int(a) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 485 | elif o == '-e': |
Serhiy Storchaka | 8cd7f82 | 2013-01-11 11:59:59 +0200 | [diff] [blame] | 486 | expandtabs = True |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 487 | # end if |
| 488 | # end for |
| 489 | if not action: |
| 490 | sys.stderr.write( |
| 491 | 'You must specify -c(omplete), -d(elete) or -r(eformat)\n') |
| 492 | sys.stderr.write(usage) |
| 493 | sys.exit(2) |
| 494 | # end if |
| 495 | if not args or args == ['-']: |
| 496 | action = eval(action + '_filter') |
| 497 | action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs) |
| 498 | else: |
| 499 | action = eval(action + '_file') |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 500 | for filename in args: |
| 501 | action(filename, stepsize, tabsize, expandtabs) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 502 | # end for |
| 503 | # end if |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 504 | # end def test |
| 505 | |
| 506 | if __name__ == '__main__': |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 507 | test() |
Guido van Rossum | 1d28e17 | 1994-05-15 18:14:33 +0000 | [diff] [blame] | 508 | # end if |