[Python-checkins] CVS: python/dist/src/Tools/scripts pindent.py,1.8,1.9

Peter Schneider-Kamp python-dev@python.org
Tue, 11 Jul 2000 09:43:19 -0700


Update of /cvsroot/python/python/dist/src/Tools/scripts
In directory slayer.i.sourceforge.net:/tmp/cvs-serv22421

Modified Files:
	pindent.py 
Log Message:

add expandtabs command (-e)
change eliminate to delete (-d)



Index: pindent.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/scripts/pindent.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** pindent.py	2000/06/28 22:55:20	1.8
--- pindent.py	2000/07/11 16:43:16	1.9
***************
*** 5,9 ****
  # 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
  # Python program with block-closing comments and outputs a commentless
  # version.   When called as "pindent -r" it assumes its input is a
--- 5,9 ----
  # 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 -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
***************
*** 47,50 ****
--- 47,51 ----
  # -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
***************
*** 79,82 ****
--- 80,84 ----
  STEPSIZE = 8
  TABSIZE = 8
+ EXPANDTABS = 0
  
  import os
***************
*** 97,101 ****
  
  	def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
! 		     indentsize = STEPSIZE, tabsize = TABSIZE):
  		self.fpi = fpi
  		self.fpo = fpo
--- 99,103 ----
  
  	def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
! 		     indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
  		self.fpi = fpi
  		self.fpo = fpo
***************
*** 103,107 ****
  		self.tabsize = tabsize
  		self.lineno = 0
! 		self.write = fpo.write
  		self.kwprog = re.compile(
  			r'^\s*(?P<kw>[a-z]+)'
--- 105,110 ----
  		self.tabsize = tabsize
  		self.lineno = 0
! 		self.expandtabs = expandtabs
! 		self._write = fpo.write
  		self.kwprog = re.compile(
  			r'^\s*(?P<kw>[a-z]+)'
***************
*** 115,118 ****
--- 118,129 ----
  	# 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()
***************
*** 197,201 ****
  	# end def reformat
  
! 	def eliminate(self):
  		begin_counter = 0
  		end_counter = 0
--- 208,212 ----
  	# end def reformat
  
! 	def delete(self):
  		begin_counter = 0
  		end_counter = 0
***************
*** 223,227 ****
  			sys.stderr.write('Warning: input contained less end tags than expected\n')
  		# end if
! 	# end def eliminate
  	
  	def complete(self):
--- 234,238 ----
  			sys.stderr.write('Warning: input contained less end tags than expected\n')
  		# end if
! 	# end def delete
  	
  	def complete(self):
***************
*** 326,343 ****
  
  def complete_filter(input = sys.stdin, output = sys.stdout,
! 		    stepsize = STEPSIZE, tabsize = TABSIZE):
! 	pi = PythonIndenter(input, output, stepsize, tabsize)
  	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 reformat_filter(input = sys.stdin, output = sys.stdout,
! 		    stepsize = STEPSIZE, tabsize = TABSIZE):
! 	pi = PythonIndenter(input, output, stepsize, tabsize)
  	pi.reformat()
  # end def reformat_filter
--- 337,354 ----
  
  def complete_filter(input = sys.stdin, output = sys.stdout,
! 		    stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
! 	pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
  	pi.complete()
  # end def complete_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, expandtabs = EXPANDTABS):
! 	pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
  	pi.reformat()
  # end def reformat_filter
***************
*** 387,417 ****
  # end class StringWriter
  
! def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE):
  	input = StringReader(source)
  	output = StringWriter()
! 	pi = PythonIndenter(input, output, stepsize, tabsize)
  	pi.complete()
  	return output.getvalue()
  # end def complete_string
  
! def eliminate_string(source, stepsize = STEPSIZE, tabsize = TABSIZE):
  	input = StringReader(source)
  	output = StringWriter()
! 	pi = PythonIndenter(input, output, stepsize, tabsize)
! 	pi.eliminate()
  	return output.getvalue()
! # end def eliminate_string
  
! def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE):
  	input = StringReader(source)
  	output = StringWriter()
! 	pi = PythonIndenter(input, output, stepsize, tabsize)
  	pi.reformat()
  	return output.getvalue()
  # end def reformat_string
  
! def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
  	source = open(filename, 'r').read()
! 	result = complete_string(source, stepsize, tabsize)
  	if source == result: return 0
  	# end if
--- 398,428 ----
  # end class StringWriter
  
! def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
  	input = StringReader(source)
  	output = StringWriter()
! 	pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
  	pi.complete()
  	return output.getvalue()
  # end def complete_string
  
! def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
  	input = StringReader(source)
  	output = StringWriter()
! 	pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
! 	pi.delete()
  	return output.getvalue()
! # end def delete_string
  
! def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
  	input = StringReader(source)
  	output = StringWriter()
! 	pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
  	pi.reformat()
  	return output.getvalue()
  # end def reformat_string
  
! def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
  	source = open(filename, 'r').read()
! 	result = complete_string(source, stepsize, tabsize, expandtabs)
  	if source == result: return 0
  	# end if
***************
*** 426,432 ****
  # end def complete_file
  
! def eliminate_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
  	source = open(filename, 'r').read()
! 	result = eliminate_string(source, stepsize, tabsize)
  	if source == result: return 0
  	# end if
--- 437,443 ----
  # end def complete_file
  
! def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
  	source = open(filename, 'r').read()
! 	result = delete_string(source, stepsize, tabsize, expandtabs)
  	if source == result: return 0
  	# end if
***************
*** 439,447 ****
  	f.close()
  	return 1
! # end def eliminate_file
  
! def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
  	source = open(filename, 'r').read()
! 	result = reformat_string(source, stepsize, tabsize)
  	if source == result: return 0
  	# end if
--- 450,458 ----
  	f.close()
  	return 1
! # end def delete_file
  
! def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
  	source = open(filename, 'r').read()
! 	result = reformat_string(source, stepsize, tabsize, expandtabs)
  	if source == result: return 0
  	# end if
***************
*** 459,468 ****
  
  usage = """
! usage: pindent (-c|-e|-r) [-s stepsize] [-t tabsize] [file] ...
  -c         : complete a correctly indented program (add #end directives)
! -e         : eliminate #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)
  [file] ... : files are changed in place, with backups in file~
  If no files are specified or a single - is given,
--- 470,480 ----
  
  usage = """
! usage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ...
  -c         : complete a correctly indented program (add #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,
***************
*** 470,477 ****
  """ % vars()
  
  def test():
  	import getopt
  	try:
! 		opts, args = getopt.getopt(sys.argv[1:], 'cers:t:')
  	except getopt.error, msg:
  		sys.stderr.write('Error: %s\n' % msg)
--- 482,495 ----
  """ % 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:], 'cdrs:t:e')
  	except getopt.error, msg:
  		sys.stderr.write('Error: %s\n' % msg)
***************
*** 482,491 ****
  	stepsize = STEPSIZE
  	tabsize = TABSIZE
  	for o, a in opts:
  		if o == '-c':
  			action = 'complete'
! 		elif o == '-e':
! 			action = 'eliminate'
  		elif o == '-r':
  			action = 'reformat'
  		elif o == '-s':
--- 500,516 ----
  	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 == '-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':
***************
*** 493,501 ****
  		elif o == '-t':
  			tabsize = string.atoi(a)
  		# end if
  	# end for
  	if not action:
  		sys.stderr.write(
! 			'You must specify -c(omplete), -e(eliminate) or -r(eformat)\n')
  		sys.stderr.write(usage)
  		sys.exit(2)
--- 518,528 ----
  		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), -d(elete) or -r(eformat)\n')
  		sys.stderr.write(usage)
  		sys.exit(2)
***************
*** 503,511 ****
  	if not args or args == ['-']:
  		action = eval(action + '_filter')
! 		action(sys.stdin, sys.stdout, stepsize, tabsize)
  	else:
  		action = eval(action + '_file')
  		for file in args:
! 			action(file, stepsize, tabsize)
  		# end for
  	# end if
--- 530,538 ----
  	if not args or args == ['-']:
  		action = eval(action + '_filter')
! 		action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs)
  	else:
  		action = eval(action + '_file')
  		for file in args:
! 			action(file, stepsize, tabsize, expandtabs)
  		# end for
  	# end if