3 # This file contains a class and a main program that perform three
4 # related (though complimentary) formatting operations on Python
5 # programs. When called as "pindent -c", it takes a valid Python
6 # program as input and outputs a version augmented with block-closing
7 # comments. When called as "pindent -d", it assumes its input is a
8 # Python program with block-closing comments and outputs a commentless
9 # version. When called as "pindent -r" it assumes its input is a
10 # Python program with block-closing comments but with its indentation
11 # messed up, and outputs a properly indented version.
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:
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
39 # The operations are idempotent (i.e. applied to their own output
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
43 # be different). Running "pindent -e" on that output produces a
44 # program that only differs from the original in indentation.
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)
49 # -e : expand TABs into spaces
50 # file ... : input file(s) (default standard input)
51 # The results always go to standard output
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
59 # - continued lines inside triple-quoted strings are totally garbled
62 # - On input, a block may also be closed with an "end statement" --
63 # this is a block-closing comment without the '#' sign.
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
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
77 # - optionally do much more thorough reformatting, a la C indent
88 next
['if'] = next
['elif'] = 'elif', 'else', 'end'
89 next
['while'] = next
['for'] = 'else', 'end'
90 next
['try'] = 'except', 'finally'
91 next
['except'] = 'except', 'else', 'finally', 'end'
92 next
['else'] = next
['finally'] = next
['def'] = next
['class'] = 'end'
94 start
= 'if', 'while', 'for', 'try', 'with', 'def', 'class'
98 def __init__(self
, fpi
= sys
.stdin
, fpo
= sys
.stdout
,
99 indentsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
102 self
.indentsize
= indentsize
103 self
.tabsize
= tabsize
105 self
.expandtabs
= expandtabs
106 self
._write
= fpo
.write
107 self
.kwprog
= re
.compile(
108 r
'^\s*(?P<kw>[a-z]+)'
109 r
'(\s+(?P<id>[a-zA-Z_]\w*))?'
111 self
.endprog
= re
.compile(
112 r
'^\s*#?\s*end\s+(?P<kw>[a-z]+)'
113 r
'(\s+(?P<id>[a-zA-Z_]\w*))?'
115 self
.wsprog
= re
.compile(r
'^[ \t]*')
118 def write(self
, line
):
120 self
._write
(line
.expandtabs(self
.tabsize
))
127 line
= self
.fpi
.readline()
128 if line
: self
.lineno
= self
.lineno
+ 1
133 def error(self
, fmt
, *args
):
134 if args
: fmt
= fmt
% args
136 sys
.stderr
.write('Error at line %d: %s\n' % (self
.lineno
, fmt
))
137 self
.write('### %s ###\n' % fmt
)
141 line
= self
.readline()
142 while line
[-2:] == '\\\n':
143 line2
= self
.readline()
151 def putline(self
, line
, indent
= None):
156 tabs
, spaces
= divmod(indent
*self
.indentsize
, self
.tabsize
)
158 m
= self
.wsprog
.match(line
)
161 self
.write('\t'*tabs
+ ' '*spaces
+ line
[i
:])
167 line
= self
.getline()
168 if not line
: break # EOF
170 m
= self
.endprog
.match(line
)
175 self
.error('unexpected end')
176 elif stack
[-1][0] != kw2
:
177 self
.error('unmatched end')
180 self
.putline(line
, len(stack
))
183 m
= self
.kwprog
.match(line
)
187 self
.putline(line
, len(stack
))
188 stack
.append((kw
, kw
))
191 if next
.has_key(kw
) and stack
:
192 self
.putline(line
, len(stack
)-1)
198 self
.putline(line
, len(stack
))
201 self
.error('unterminated keywords')
202 for kwa
, kwb
in stack
:
203 self
.write('\t%s\n' % kwa
)
212 line
= self
.getline()
213 if not line
: break # EOF
215 m
= self
.endprog
.match(line
)
217 end_counter
= end_counter
+ 1
220 m
= self
.kwprog
.match(line
)
224 begin_counter
= begin_counter
+ 1
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')
241 current
, firstkw
, lastkw
, topid
= 0, '', '', ''
243 line
= self
.getline()
245 m
= self
.wsprog
.match(line
)
248 m
= self
.endprog
.match(line
)
251 endkw
= m
.group('kw')
252 thisid
= m
.group('id')
254 m
= self
.kwprog
.match(line
)
256 thiskw
= m
.group('kw')
257 if not next
.has_key(thiskw
):
260 if thiskw
in ('def', 'class'):
261 thisid
= m
.group('id')
265 elif line
[i
:i
+1] in ('\n', '#'):
272 indent
= len(line
[:i
].expandtabs(self
.tabsize
))
273 while indent
< current
:
276 s
= '# end %s %s\n' % (
279 s
= '# end %s\n' % firstkw
281 self
.putline(s
, current
)
282 firstkw
= lastkw
= ''
284 current
, firstkw
, lastkw
, topid
= stack
[-1]
287 if indent
== current
and firstkw
:
290 self
.error('mismatched end')
292 firstkw
= lastkw
= ''
293 elif not thiskw
or thiskw
in start
:
295 s
= '# end %s %s\n' % (
298 s
= '# end %s\n' % firstkw
300 self
.putline(s
, current
)
301 firstkw
= lastkw
= topid
= ''
305 stack
.append((current
, firstkw
, lastkw
, topid
))
306 if thiskw
and thiskw
not in start
:
310 current
, firstkw
, lastkw
, topid
= \
311 indent
, thiskw
, thiskw
, thisid
315 firstkw
= lastkw
= thiskw
321 for l
in todo
: self
.write(l
)
330 # end class PythonIndenter
332 # Simplified user interface
333 # - xxx_filter(input, output): read and write file objects
334 # - xxx_string(s): take and return string object
335 # - xxx_file(filename): process file in place, return true iff changed
337 def complete_filter(input = sys
.stdin
, output
= sys
.stdout
,
338 stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
339 pi
= PythonIndenter(input, output
, stepsize
, tabsize
, expandtabs
)
341 # end def complete_filter
343 def delete_filter(input= sys
.stdin
, output
= sys
.stdout
,
344 stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
345 pi
= PythonIndenter(input, output
, stepsize
, tabsize
, expandtabs
)
347 # end def delete_filter
349 def reformat_filter(input = sys
.stdin
, output
= sys
.stdout
,
350 stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
351 pi
= PythonIndenter(input, output
, stepsize
, tabsize
, expandtabs
)
353 # end def reformat_filter
356 def __init__(self
, buf
):
359 self
.len = len(self
.buf
)
361 def read(self
, n
= 0):
363 n
= self
.len - self
.pos
365 n
= min(n
, self
.len - self
.pos
)
367 r
= self
.buf
[self
.pos
: self
.pos
+ n
]
368 self
.pos
= self
.pos
+ n
372 i
= self
.buf
.find('\n', self
.pos
)
373 return self
.read(i
+ 1 - self
.pos
)
377 line
= self
.readline()
380 line
= self
.readline()
384 # seek/tell etc. are left as an exercise for the reader
385 # end class StringReader
392 self
.buf
= self
.buf
+ s
397 # end class StringWriter
399 def complete_string(source
, stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
400 input = StringReader(source
)
401 output
= StringWriter()
402 pi
= PythonIndenter(input, output
, stepsize
, tabsize
, expandtabs
)
404 return output
.getvalue()
405 # end def complete_string
407 def delete_string(source
, stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
408 input = StringReader(source
)
409 output
= StringWriter()
410 pi
= PythonIndenter(input, output
, stepsize
, tabsize
, expandtabs
)
412 return output
.getvalue()
413 # end def delete_string
415 def reformat_string(source
, stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
416 input = StringReader(source
)
417 output
= StringWriter()
418 pi
= PythonIndenter(input, output
, stepsize
, tabsize
, expandtabs
)
420 return output
.getvalue()
421 # end def reformat_string
423 def complete_file(filename
, stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
424 source
= open(filename
, 'r').read()
425 result
= complete_string(source
, stepsize
, tabsize
, expandtabs
)
426 if source
== result
: return 0
429 try: os
.rename(filename
, filename
+ '~')
430 except os
.error
: pass
432 f
= open(filename
, 'w')
436 # end def complete_file
438 def delete_file(filename
, stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
439 source
= open(filename
, 'r').read()
440 result
= delete_string(source
, stepsize
, tabsize
, expandtabs
)
441 if source
== result
: return 0
444 try: os
.rename(filename
, filename
+ '~')
445 except os
.error
: pass
447 f
= open(filename
, 'w')
451 # end def delete_file
453 def reformat_file(filename
, stepsize
= STEPSIZE
, tabsize
= TABSIZE
, expandtabs
= EXPANDTABS
):
454 source
= open(filename
, 'r').read()
455 result
= reformat_string(source
, stepsize
, tabsize
, expandtabs
)
456 if source
== result
: return 0
459 try: os
.rename(filename
, filename
+ '~')
460 except os
.error
: pass
462 f
= open(filename
, 'w')
466 # end def reformat_file
468 # Test program when called as a script
471 usage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ...
472 -c : complete a correctly indented program (add #end directives)
473 -d : delete #end directives
474 -r : reformat a completed program (use #end directives)
475 -s stepsize: indentation step (default %(STEPSIZE)d)
476 -t tabsize : the worth in spaces of a tab (default %(TABSIZE)d)
477 -e : expand TABs into spaces (defailt OFF)
478 [file] ... : files are changed in place, with backups in file~
479 If no files are specified or a single - is given,
480 the program acts as a filter (reads stdin, writes stdout).
483 def error_both(op1
, op2
):
484 sys
.stderr
.write('Error: You can not specify both '+op1
+' and -'+op2
[0]+' at the same time\n')
485 sys
.stderr
.write(usage
)
492 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'cdrs:t:e')
493 except getopt
.error
, msg
:
494 sys
.stderr
.write('Error: %s\n' % msg
)
495 sys
.stderr
.write(usage
)
501 expandtabs
= EXPANDTABS
504 if action
: error_both(o
, action
)
508 if action
: error_both(o
, action
)
512 if action
: error_both(o
, action
)
525 'You must specify -c(omplete), -d(elete) or -r(eformat)\n')
526 sys
.stderr
.write(usage
)
529 if not args
or args
== ['-']:
530 action
= eval(action
+ '_filter')
531 action(sys
.stdin
, sys
.stdout
, stepsize
, tabsize
, expandtabs
)
533 action
= eval(action
+ '_file')
534 for filename
in args
:
535 action(filename
, stepsize
, tabsize
, expandtabs
)
540 if __name__
== '__main__':