[Python-checkins] commit of r41499 - python/trunk/Lib

walter.doerwald@python.org walter.doerwald at python.org
Tue Nov 22 15:12:26 CET 2005


Author: walter.doerwald
Date: Tue Nov 22 15:12:21 2005
New Revision: 41499
Modified:
 python/trunk/Lib/uu.py
Log:
Use optparse instead of getopt for command line options.
Use "raise instance" instead of "raise class, args".
Modernize the code in other spots (bools, startswith()).
Modified: python/trunk/Lib/uu.py
==============================================================================
--- python/trunk/Lib/uu.py	(original)
+++ python/trunk/Lib/uu.py	Tue Nov 22 15:12:21 2005
@@ -92,13 +92,13 @@
 #
 # Read until a begin is encountered or we've exhausted the file
 #
- while 1:
+ while True:
 hdr = in_file.readline()
 if not hdr:
- raise Error, 'No valid begin line found in input file'
- if hdr[:5] != 'begin':
+ raise Error('No valid begin line found in input file')
+ if not hdr.startswith('begin'):
 continue
- hdrfields = hdr.split(" ", 2)
+ hdrfields = hdr.split(' ', 2)
 if len(hdrfields) == 3 and hdrfields[0] == 'begin':
 try:
 int(hdrfields[1], 8)
@@ -108,7 +108,7 @@
 if out_file is None:
 out_file = hdrfields[2].rstrip()
 if os.path.exists(out_file):
- raise Error, 'Cannot overwrite existing file: %s' % out_file
+ raise Error('Cannot overwrite existing file: %s' % out_file)
 if mode is None:
 mode = int(hdrfields[1], 8)
 #
@@ -135,42 +135,34 @@
 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3
 data = binascii.a2b_uu(s[:nbytes])
 if not quiet:
- sys.stderr.write("Warning: %s\n" % str(v))
+ sys.stderr.write("Warning: %s\n" % v)
 out_file.write(data)
 s = in_file.readline()
 if not s:
- raise Error, 'Truncated input file'
+ raise Error('Truncated input file')
 
 def test():
 """uuencode/uudecode main program"""
- import getopt
 
- dopt = 0
- topt = 0
- input = sys.stdin
- output = sys.stdout
- ok = 1
- try:
- optlist, args = getopt.getopt(sys.argv[1:], 'dt')
- except getopt.error:
- ok = 0
- if not ok or len(args) > 2:
- print 'Usage:', sys.argv[0], '[-d] [-t] [input [output]]'
- print ' -d: Decode (in stead of encode)'
- print ' -t: data is text, encoded format unix-compatible text'
+ import optparse
+ parser = optparse.OptionParser(usage='usage: %prog [-d] [-t] [input [output]]')
+ parser.add_option('-d', '--decode', dest='decode', help='Decode (instead of encode)?', default=False, action='store_true')
+ parser.add_option('-t', '--text', dest='text', help='data is text, encoded format unix-compatible text?', default=False, action='store_true')
+
+ (options, args) = parser.parse_args()
+ if len(args) > 2:
+ p.error('incorrect number of arguments')
 sys.exit(1)
 
- for o, a in optlist:
- if o == '-d': dopt = 1
- if o == '-t': topt = 1
-
+ input = sys.stdin
+ output = sys.stdout
 if len(args) > 0:
 input = args[0]
 if len(args) > 1:
 output = args[1]
 
- if dopt:
- if topt:
+ if options.decode:
+ if options.text:
 if isinstance(output, basestring):
 output = open(output, 'w')
 else:
@@ -178,7 +170,7 @@
 sys.exit(1)
 decode(input, output)
 else:
- if topt:
+ if options.text:
 if isinstance(input, basestring):
 input = open(input, 'r')
 else:


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /