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

Guido van Rossum python-dev@python.org
Wed, 28 Jun 2000 15:47:25 -0700


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

Modified Files:
	pindent.py 
Log Message:
Peter Schneider-Kamp:

Problem: 
A Python program can be completed and reformatted using
Tools/scripts/pindent.py. Unfortunately there is no option for removal
of the generated "# end"-tags.  Although a few Python commands or a
"grep -v '# end '" can do wonders here, there are two drawbacks:
- not everyone has grep/time to write a Python script
- it is not checked whether the "# end"-tags were used validly

Solution: 
add extra option "-e" (eliminate) to pindent.py


Index: pindent.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/scripts/pindent.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** pindent.py	1998/06/19 21:39:27	1.6
--- pindent.py	2000/06/28 22:47:22	1.7
***************
*** 1,9 ****
  #! /usr/bin/env python
  
! # This file contains a class and a main program that perform two
  # related (though complimentary) formatting operations on Python
! # programs.  When called as "pindend -c", it takes a valid Python
  # program as input and outputs a version augmented with block-closing
! # comments.  When called as "pindent -r" it assumes its input is a
  # Python program with block-closing comments but with its indentation
  # messed up, and outputs a properly indented version.
--- 1,11 ----
  #! /usr/bin/env python
  
! # This file contains a class and a main program that perform three
  # 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
! # 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
  # messed up, and outputs a properly indented version.
***************
*** 35,43 ****
  # comments).
  
! # Both operations are idempotent (i.e. applied to their own output
  # they yield an identical result).  Running first "pindent -c" and
  # then "pindent -r" on a valid Python program produces a program that
  # is semantically identical to the input (though its indentation may
! # be different).
  
  # Other options:
--- 37,46 ----
  # comments).
  
! # The operations are idempotent (i.e. applied to their own output
  # they yield an identical result).  Running first "pindent -c" and
  # then "pindent -r" on a valid Python program produces a program that
  # is semantically identical to the input (though its indentation may
! # be different). Running "pindent -e" on that output produces a
! # program that only differs from the original in indentation.
  
  # Other options:
***************
*** 194,197 ****
--- 197,228 ----
  	# end def reformat
  
+ 	def eliminate(self):
+ 		begin_counter = 0
+ 		end_counter = 0
+ 		while 1:
+ 			line = self.getline()
+ 			if not line: break	# EOF
+ 			# end if
+ 			m = self.endprog.match(line)
+ 			if m:
+ 				end_counter = end_counter + 1
+ 				continue
+ 			# end if
+ 			m = self.kwprog.match(line)
+ 			if m:
+ 				kw = m.group('kw')
+ 				if kw in start:
+ 					begin_counter = begin_counter + 1
+ 				# end if
+ 			# end if
+ 			self.putline(line)
+ 		# end while
+ 		if begin_counter - end_counter < 0:
+ 			sys.stderr.write('Warning: input contained more end tags than expected\n')
+ 		elif begin_counter - end_counter > 0:
+ 			sys.stderr.write('Warning: input contained less end tags than expected\n')
+ 		# end if
+ 	# end def eliminate
+ 	
  	def complete(self):
  		self.indentsize = 1
***************
*** 294,298 ****
  # - 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)
--- 325,329 ----
  # - 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)
***************
*** 300,303 ****
--- 331,340 ----
  # 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):
***************
*** 358,361 ****
--- 395,406 ----
  # 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)
***************
*** 381,384 ****
--- 426,444 ----
  # 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
+ 	import os
+ 	try: os.rename(filename, filename + '~')
+ 	except os.error: pass
+ 	# end try
+ 	f = open(filename, 'w')
+ 	f.write(result)
+ 	f.close()
+ 	return 1
+ # end def eliminate_file
+ 
  def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
  	source = open(filename, 'r').read()
***************
*** 387,391 ****
  	# end if
  	import os
! 	os.rename(filename, filename + '~')
  	f = open(filename, 'w')
  	f.write(result)
--- 447,453 ----
  	# end if
  	import os
! 	try: os.rename(filename, filename + '~')
! 	except os.error: pass
! 	# end try
  	f = open(filename, 'w')
  	f.write(result)
***************
*** 397,402 ****
  
  usage = """
! usage: pindent (-c|-r) [-s stepsize] [-t tabsize] [file] ...
  -c         : complete a correctly indented program (add #end directives)
  -r         : reformat a completed program (use #end directives)
  -s stepsize: indentation step (default %(STEPSIZE)d)
--- 459,465 ----
  
  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)
***************
*** 410,414 ****
  	import getopt
  	try:
! 		opts, args = getopt.getopt(sys.argv[1:], 'crs:t:')
  	except getopt.error, msg:
  		sys.stderr.write('Error: %s\n' % msg)
--- 473,477 ----
  	import getopt
  	try:
! 		opts, args = getopt.getopt(sys.argv[1:], 'cers:t:')
  	except getopt.error, msg:
  		sys.stderr.write('Error: %s\n' % msg)
***************
*** 422,425 ****
--- 485,490 ----
  		if o == '-c':
  			action = 'complete'
+ 		elif o == '-e':
+ 			action = 'eliminate'
  		elif o == '-r':
  			action = 'reformat'
***************
*** 432,436 ****
  	if not action:
  		sys.stderr.write(
! 			'You must specify -c(omplete) or -r(eformat)\n')
  		sys.stderr.write(usage)
  		sys.exit(2)
--- 497,501 ----
  	if not action:
  		sys.stderr.write(
! 			'You must specify -c(omplete), -e(eliminate) or -r(eformat)\n')
  		sys.stderr.write(usage)
  		sys.exit(2)