From 7f589fddad94f0f2da38a8d0ef6eba2597c4876d Mon Sep 17 00:00:00 2001 From: Peter Schneider-Kamp Date: Tue, 11 Jul 2000 16:43:16 +0000 Subject: add expandtabs command (-e) change eliminate to delete (-d) --- Tools/scripts/pindent.py | 101 ++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/Tools/scripts/pindent.py b/Tools/scripts/pindent.py index 1c27b13..6eba021 100755 --- a/Tools/scripts/pindent.py +++ b/Tools/scripts/pindent.py @@ -4,7 +4,7 @@ # related (though complimentary) formatting operations on Python # programs. When called as "pindent -c", it takes a valid Python # program as input and outputs a version augmented with block-closing -# comments. When called as "pindent -e", it assumes its input is a +# comments. When called as "pindent -d", it assumes its input is a # Python program with block-closing comments and outputs a commentless # version. When called as "pindent -r" it assumes its input is a # Python program with block-closing comments but with its indentation @@ -46,6 +46,7 @@ # Other options: # -s stepsize: set the indentation step size (default 8) # -t tabsize : set the number of spaces a tab character is worth (default 8) +# -e : expand TABs into spaces # file ... : input file(s) (default standard input) # The results always go to standard output @@ -78,6 +79,7 @@ # Defaults STEPSIZE = 8 TABSIZE = 8 +EXPANDTABS = 0 import os import re @@ -96,13 +98,14 @@ start = 'if', 'while', 'for', 'try', 'def', 'class' class PythonIndenter: def __init__(self, fpi = sys.stdin, fpo = sys.stdout, - indentsize = STEPSIZE, tabsize = TABSIZE): + indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): self.fpi = fpi self.fpo = fpo self.indentsize = indentsize self.tabsize = tabsize self.lineno = 0 - self.write = fpo.write + self.expandtabs = expandtabs + self._write = fpo.write self.kwprog = re.compile( r'^\s*(?P[a-z]+)' r'(\s+(?P[a-zA-Z_]\w*))?' @@ -114,6 +117,14 @@ class PythonIndenter: self.wsprog = re.compile(r'^[ \t]*') # end def __init__ + def write(self, line): + if self.expandtabs: + self._write(string.expandtabs(line, self.tabsize)) + else: + self._write(line) + # end if + # end def write + def readline(self): line = self.fpi.readline() if line: self.lineno = self.lineno + 1 @@ -196,7 +207,7 @@ class PythonIndenter: # end if # end def reformat - def eliminate(self): + def delete(self): begin_counter = 0 end_counter = 0 while 1: @@ -222,7 +233,7 @@ class PythonIndenter: elif begin_counter - end_counter > 0: sys.stderr.write('Warning: input contained less end tags than expected\n') # end if - # end def eliminate + # end def delete def complete(self): self.indentsize = 1 @@ -325,20 +336,20 @@ class PythonIndenter: # - xxx_file(filename): process file in place, return true iff changed def complete_filter(input = sys.stdin, output = sys.stdout, - stepsize = STEPSIZE, tabsize = TABSIZE): - pi = PythonIndenter(input, output, stepsize, tabsize) + stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): + pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) pi.complete() # end def complete_filter -def eliminate_filter(input= sys.stdin, output = sys.stdout, - stepsize = STEPSIZE, tabsize = TABSIZE): - pi = PythonIndenter(input, output, stepsize, tabsize) - pi.eliminate() -# end def eliminate_filter +def delete_filter(input= sys.stdin, output = sys.stdout, + stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): + pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) + pi.delete() +# end def delete_filter def reformat_filter(input = sys.stdin, output = sys.stdout, - stepsize = STEPSIZE, tabsize = TABSIZE): - pi = PythonIndenter(input, output, stepsize, tabsize) + stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): + pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) pi.reformat() # end def reformat_filter @@ -386,33 +397,33 @@ class StringWriter: # end def getvalue # end class StringWriter -def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE): +def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): input = StringReader(source) output = StringWriter() - pi = PythonIndenter(input, output, stepsize, tabsize) + pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) pi.complete() return output.getvalue() # end def complete_string -def eliminate_string(source, stepsize = STEPSIZE, tabsize = TABSIZE): +def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): input = StringReader(source) output = StringWriter() - pi = PythonIndenter(input, output, stepsize, tabsize) - pi.eliminate() + pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) + pi.delete() return output.getvalue() -# end def eliminate_string +# end def delete_string -def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE): +def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): input = StringReader(source) output = StringWriter() - pi = PythonIndenter(input, output, stepsize, tabsize) + pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) pi.reformat() return output.getvalue() # end def reformat_string -def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE): +def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): source = open(filename, 'r').read() - result = complete_string(source, stepsize, tabsize) + result = complete_string(source, stepsize, tabsize, expandtabs) if source == result: return 0 # end if import os @@ -425,9 +436,9 @@ def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE): return 1 # end def complete_file -def eliminate_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE): +def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): source = open(filename, 'r').read() - result = eliminate_string(source, stepsize, tabsize) + result = delete_string(source, stepsize, tabsize, expandtabs) if source == result: return 0 # end if import os @@ -438,11 +449,11 @@ def eliminate_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE): f.write(result) f.close() return 1 -# end def eliminate_file +# end def delete_file -def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE): +def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): source = open(filename, 'r').read() - result = reformat_string(source, stepsize, tabsize) + result = reformat_string(source, stepsize, tabsize, expandtabs) if source == result: return 0 # end if import os @@ -458,21 +469,28 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE): # Test program when called as a script usage = """ -usage: pindent (-c|-e|-r) [-s stepsize] [-t tabsize] [file] ... +usage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ... -c : complete a correctly indented program (add #end directives) --e : eliminate #end directives +-d : delete #end directives -r : reformat a completed program (use #end directives) -s stepsize: indentation step (default %(STEPSIZE)d) -t tabsize : the worth in spaces of a tab (default %(TABSIZE)d) +-e : expand TABs into spaces (defailt OFF) [file] ... : files are changed in place, with backups in file~ If no files are specified or a single - is given, the program acts as a filter (reads stdin, writes stdout). """ % vars() +def error_both(op1, op2): + sys.stderr.write('Error: You can not specify both '+op1+' and -'+op2[0]+' at the same time\n') + sys.stderr.write(usage) + sys.exit(2) +# end def error_both + def test(): import getopt try: - opts, args = getopt.getopt(sys.argv[1:], 'cers:t:') + opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e') except getopt.error, msg: sys.stderr.write('Error: %s\n' % msg) sys.stderr.write(usage) @@ -481,32 +499,41 @@ def test(): action = None stepsize = STEPSIZE tabsize = TABSIZE + expandtabs = EXPANDTABS for o, a in opts: if o == '-c': + if action: error_both(o, action) + # end if action = 'complete' - elif o == '-e': - action = 'eliminate' + elif o == '-d': + if action: error_both(o, action) + # end if + action = 'delete' elif o == '-r': + if action: error_both(o, action) + # end if action = 'reformat' elif o == '-s': stepsize = string.atoi(a) elif o == '-t': tabsize = string.atoi(a) + elif o == '-e': + expandtabs = 1 # end if # end for if not action: sys.stderr.write( - 'You must specify -c(omplete), -e(eliminate) or -r(eformat)\n') + 'You must specify -c(omplete), -d(elete) or -r(eformat)\n') sys.stderr.write(usage) sys.exit(2) # end if if not args or args == ['-']: action = eval(action + '_filter') - action(sys.stdin, sys.stdout, stepsize, tabsize) + action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs) else: action = eval(action + '_file') for file in args: - action(file, stepsize, tabsize) + action(file, stepsize, tabsize, expandtabs) # end for # end if # end def test -- cgit v0.12