diff --git a/Lib/uu.py b/Lib/uu.py index b20aa9cd22e..40e8bf00b11 100755 --- a/Lib/uu.py +++ b/Lib/uu.py @@ -92,13 +92,13 @@ def decode(in_file, out_file=None, mode=None, quiet=0): # # 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 @@ def decode(in_file, out_file=None, mode=None, quiet=0): 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 @@ def decode(in_file, out_file=None, mode=None, quiet=0): 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 @@ def test(): sys.exit(1) decode(input, output) else: - if topt: + if options.text: if isinstance(input, basestring): input = open(input, 'r') else: