diff options
author | Walter Dörwald <walter@livinglogic.de> | 2005-11-22 14:12:21 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2005-11-22 14:12:21 (GMT) |
commit | d331b433c31a20201ceafa6f0f10f560abad663d (patch) | |
tree | 5d6883a030320d1ba5cc177b90cf009c5d103669 | |
parent | 91043f32864b3d3cf5896811accb0dc70b44e531 (diff) | |
download | cpython-d331b433c31a20201ceafa6f0f10f560abad663d.zip cpython-d331b433c31a20201ceafa6f0f10f560abad663d.tar.gz cpython-d331b433c31a20201ceafa6f0f10f560abad663d.tar.bz2 |
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()).
-rwxr-xr-x | Lib/uu.py | 48 |
1 files changed, 20 insertions, 28 deletions
@@ -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' - sys.exit(1) + 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') - for o, a in optlist: - if o == '-d': dopt = 1 - if o == '-t': topt = 1 + (options, args) = parser.parse_args() + if len(args) > 2: + p.error('incorrect number of arguments') + sys.exit(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: |