add expandtabs command (-e)
change eliminate to delete (-d)
This commit is contained in:
parent
acab3d61e9
commit
7f589fddad
@ -4,7 +4,7 @@
|
|||||||
# related (though complimentary) formatting operations on Python
|
# related (though complimentary) formatting operations on Python
|
||||||
# programs. When called as "pindent -c", it takes a valid Python
|
# programs. When called as "pindent -c", it takes a valid Python
|
||||||
# program as input and outputs a version augmented with block-closing
|
# 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
|
# Python program with block-closing comments and outputs a commentless
|
||||||
# version. When called as "pindent -r" it assumes its input is a
|
# version. When called as "pindent -r" it assumes its input is a
|
||||||
# Python program with block-closing comments but with its indentation
|
# Python program with block-closing comments but with its indentation
|
||||||
@ -46,6 +46,7 @@
|
|||||||
# Other options:
|
# Other options:
|
||||||
# -s stepsize: set the indentation step size (default 8)
|
# -s stepsize: set the indentation step size (default 8)
|
||||||
# -t tabsize : set the number of spaces a tab character is worth (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)
|
# file ... : input file(s) (default standard input)
|
||||||
# The results always go to standard output
|
# The results always go to standard output
|
||||||
|
|
||||||
@ -78,6 +79,7 @@
|
|||||||
# Defaults
|
# Defaults
|
||||||
STEPSIZE = 8
|
STEPSIZE = 8
|
||||||
TABSIZE = 8
|
TABSIZE = 8
|
||||||
|
EXPANDTABS = 0
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@ -96,13 +98,14 @@ start = 'if', 'while', 'for', 'try', 'def', 'class'
|
|||||||
class PythonIndenter:
|
class PythonIndenter:
|
||||||
|
|
||||||
def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
|
def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
|
||||||
indentsize = STEPSIZE, tabsize = TABSIZE):
|
indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
|
||||||
self.fpi = fpi
|
self.fpi = fpi
|
||||||
self.fpo = fpo
|
self.fpo = fpo
|
||||||
self.indentsize = indentsize
|
self.indentsize = indentsize
|
||||||
self.tabsize = tabsize
|
self.tabsize = tabsize
|
||||||
self.lineno = 0
|
self.lineno = 0
|
||||||
self.write = fpo.write
|
self.expandtabs = expandtabs
|
||||||
|
self._write = fpo.write
|
||||||
self.kwprog = re.compile(
|
self.kwprog = re.compile(
|
||||||
r'^\s*(?P<kw>[a-z]+)'
|
r'^\s*(?P<kw>[a-z]+)'
|
||||||
r'(\s+(?P<id>[a-zA-Z_]\w*))?'
|
r'(\s+(?P<id>[a-zA-Z_]\w*))?'
|
||||||
@ -114,6 +117,14 @@ class PythonIndenter:
|
|||||||
self.wsprog = re.compile(r'^[ \t]*')
|
self.wsprog = re.compile(r'^[ \t]*')
|
||||||
# end def __init__
|
# 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):
|
def readline(self):
|
||||||
line = self.fpi.readline()
|
line = self.fpi.readline()
|
||||||
if line: self.lineno = self.lineno + 1
|
if line: self.lineno = self.lineno + 1
|
||||||
@ -196,7 +207,7 @@ class PythonIndenter:
|
|||||||
# end if
|
# end if
|
||||||
# end def reformat
|
# end def reformat
|
||||||
|
|
||||||
def eliminate(self):
|
def delete(self):
|
||||||
begin_counter = 0
|
begin_counter = 0
|
||||||
end_counter = 0
|
end_counter = 0
|
||||||
while 1:
|
while 1:
|
||||||
@ -222,7 +233,7 @@ class PythonIndenter:
|
|||||||
elif begin_counter - end_counter > 0:
|
elif begin_counter - end_counter > 0:
|
||||||
sys.stderr.write('Warning: input contained less end tags than expected\n')
|
sys.stderr.write('Warning: input contained less end tags than expected\n')
|
||||||
# end if
|
# end if
|
||||||
# end def eliminate
|
# end def delete
|
||||||
|
|
||||||
def complete(self):
|
def complete(self):
|
||||||
self.indentsize = 1
|
self.indentsize = 1
|
||||||
@ -325,20 +336,20 @@ class PythonIndenter:
|
|||||||
# - xxx_file(filename): process file in place, return true iff changed
|
# - xxx_file(filename): process file in place, return true iff changed
|
||||||
|
|
||||||
def complete_filter(input = sys.stdin, output = sys.stdout,
|
def complete_filter(input = sys.stdin, output = sys.stdout,
|
||||||
stepsize = STEPSIZE, tabsize = TABSIZE):
|
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
|
||||||
pi = PythonIndenter(input, output, stepsize, tabsize)
|
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
|
||||||
pi.complete()
|
pi.complete()
|
||||||
# end def complete_filter
|
# end def complete_filter
|
||||||
|
|
||||||
def eliminate_filter(input= sys.stdin, output = sys.stdout,
|
def delete_filter(input= sys.stdin, output = sys.stdout,
|
||||||
stepsize = STEPSIZE, tabsize = TABSIZE):
|
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
|
||||||
pi = PythonIndenter(input, output, stepsize, tabsize)
|
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
|
||||||
pi.eliminate()
|
pi.delete()
|
||||||
# end def eliminate_filter
|
# end def delete_filter
|
||||||
|
|
||||||
def reformat_filter(input = sys.stdin, output = sys.stdout,
|
def reformat_filter(input = sys.stdin, output = sys.stdout,
|
||||||
stepsize = STEPSIZE, tabsize = TABSIZE):
|
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
|
||||||
pi = PythonIndenter(input, output, stepsize, tabsize)
|
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
|
||||||
pi.reformat()
|
pi.reformat()
|
||||||
# end def reformat_filter
|
# end def reformat_filter
|
||||||
|
|
||||||
@ -386,33 +397,33 @@ class StringWriter:
|
|||||||
# end def getvalue
|
# end def getvalue
|
||||||
# end class StringWriter
|
# end class StringWriter
|
||||||
|
|
||||||
def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE):
|
def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
|
||||||
input = StringReader(source)
|
input = StringReader(source)
|
||||||
output = StringWriter()
|
output = StringWriter()
|
||||||
pi = PythonIndenter(input, output, stepsize, tabsize)
|
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
|
||||||
pi.complete()
|
pi.complete()
|
||||||
return output.getvalue()
|
return output.getvalue()
|
||||||
# end def complete_string
|
# 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)
|
input = StringReader(source)
|
||||||
output = StringWriter()
|
output = StringWriter()
|
||||||
pi = PythonIndenter(input, output, stepsize, tabsize)
|
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
|
||||||
pi.eliminate()
|
pi.delete()
|
||||||
return output.getvalue()
|
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)
|
input = StringReader(source)
|
||||||
output = StringWriter()
|
output = StringWriter()
|
||||||
pi = PythonIndenter(input, output, stepsize, tabsize)
|
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
|
||||||
pi.reformat()
|
pi.reformat()
|
||||||
return output.getvalue()
|
return output.getvalue()
|
||||||
# end def reformat_string
|
# 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()
|
source = open(filename, 'r').read()
|
||||||
result = complete_string(source, stepsize, tabsize)
|
result = complete_string(source, stepsize, tabsize, expandtabs)
|
||||||
if source == result: return 0
|
if source == result: return 0
|
||||||
# end if
|
# end if
|
||||||
import os
|
import os
|
||||||
@ -425,9 +436,9 @@ def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
|
|||||||
return 1
|
return 1
|
||||||
# end def complete_file
|
# 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()
|
source = open(filename, 'r').read()
|
||||||
result = eliminate_string(source, stepsize, tabsize)
|
result = delete_string(source, stepsize, tabsize, expandtabs)
|
||||||
if source == result: return 0
|
if source == result: return 0
|
||||||
# end if
|
# end if
|
||||||
import os
|
import os
|
||||||
@ -438,11 +449,11 @@ def eliminate_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
|
|||||||
f.write(result)
|
f.write(result)
|
||||||
f.close()
|
f.close()
|
||||||
return 1
|
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()
|
source = open(filename, 'r').read()
|
||||||
result = reformat_string(source, stepsize, tabsize)
|
result = reformat_string(source, stepsize, tabsize, expandtabs)
|
||||||
if source == result: return 0
|
if source == result: return 0
|
||||||
# end if
|
# end if
|
||||||
import os
|
import os
|
||||||
@ -458,21 +469,28 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
|
|||||||
# Test program when called as a script
|
# Test program when called as a script
|
||||||
|
|
||||||
usage = """
|
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)
|
-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)
|
-r : reformat a completed program (use #end directives)
|
||||||
-s stepsize: indentation step (default %(STEPSIZE)d)
|
-s stepsize: indentation step (default %(STEPSIZE)d)
|
||||||
-t tabsize : the worth in spaces of a tab (default %(TABSIZE)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~
|
[file] ... : files are changed in place, with backups in file~
|
||||||
If no files are specified or a single - is given,
|
If no files are specified or a single - is given,
|
||||||
the program acts as a filter (reads stdin, writes stdout).
|
the program acts as a filter (reads stdin, writes stdout).
|
||||||
""" % vars()
|
""" % 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():
|
def test():
|
||||||
import getopt
|
import getopt
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'cers:t:')
|
opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e')
|
||||||
except getopt.error, msg:
|
except getopt.error, msg:
|
||||||
sys.stderr.write('Error: %s\n' % msg)
|
sys.stderr.write('Error: %s\n' % msg)
|
||||||
sys.stderr.write(usage)
|
sys.stderr.write(usage)
|
||||||
@ -481,32 +499,41 @@ def test():
|
|||||||
action = None
|
action = None
|
||||||
stepsize = STEPSIZE
|
stepsize = STEPSIZE
|
||||||
tabsize = TABSIZE
|
tabsize = TABSIZE
|
||||||
|
expandtabs = EXPANDTABS
|
||||||
for o, a in opts:
|
for o, a in opts:
|
||||||
if o == '-c':
|
if o == '-c':
|
||||||
|
if action: error_both(o, action)
|
||||||
|
# end if
|
||||||
action = 'complete'
|
action = 'complete'
|
||||||
elif o == '-e':
|
elif o == '-d':
|
||||||
action = 'eliminate'
|
if action: error_both(o, action)
|
||||||
|
# end if
|
||||||
|
action = 'delete'
|
||||||
elif o == '-r':
|
elif o == '-r':
|
||||||
|
if action: error_both(o, action)
|
||||||
|
# end if
|
||||||
action = 'reformat'
|
action = 'reformat'
|
||||||
elif o == '-s':
|
elif o == '-s':
|
||||||
stepsize = string.atoi(a)
|
stepsize = string.atoi(a)
|
||||||
elif o == '-t':
|
elif o == '-t':
|
||||||
tabsize = string.atoi(a)
|
tabsize = string.atoi(a)
|
||||||
|
elif o == '-e':
|
||||||
|
expandtabs = 1
|
||||||
# end if
|
# end if
|
||||||
# end for
|
# end for
|
||||||
if not action:
|
if not action:
|
||||||
sys.stderr.write(
|
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.stderr.write(usage)
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
# end if
|
# end if
|
||||||
if not args or args == ['-']:
|
if not args or args == ['-']:
|
||||||
action = eval(action + '_filter')
|
action = eval(action + '_filter')
|
||||||
action(sys.stdin, sys.stdout, stepsize, tabsize)
|
action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs)
|
||||||
else:
|
else:
|
||||||
action = eval(action + '_file')
|
action = eval(action + '_file')
|
||||||
for file in args:
|
for file in args:
|
||||||
action(file, stepsize, tabsize)
|
action(file, stepsize, tabsize, expandtabs)
|
||||||
# end for
|
# end for
|
||||||
# end if
|
# end if
|
||||||
# end def test
|
# end def test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user