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()).
This commit is contained in:
parent
91043f3286
commit
d331b433c3
48
Lib/uu.py
48
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
|
# Read until a begin is encountered or we've exhausted the file
|
||||||
#
|
#
|
||||||
while 1:
|
while True:
|
||||||
hdr = in_file.readline()
|
hdr = in_file.readline()
|
||||||
if not hdr:
|
if not hdr:
|
||||||
raise Error, 'No valid begin line found in input file'
|
raise Error('No valid begin line found in input file')
|
||||||
if hdr[:5] != 'begin':
|
if not hdr.startswith('begin'):
|
||||||
continue
|
continue
|
||||||
hdrfields = hdr.split(" ", 2)
|
hdrfields = hdr.split(' ', 2)
|
||||||
if len(hdrfields) == 3 and hdrfields[0] == 'begin':
|
if len(hdrfields) == 3 and hdrfields[0] == 'begin':
|
||||||
try:
|
try:
|
||||||
int(hdrfields[1], 8)
|
int(hdrfields[1], 8)
|
||||||
|
@ -108,7 +108,7 @@ def decode(in_file, out_file=None, mode=None, quiet=0):
|
||||||
if out_file is None:
|
if out_file is None:
|
||||||
out_file = hdrfields[2].rstrip()
|
out_file = hdrfields[2].rstrip()
|
||||||
if os.path.exists(out_file):
|
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:
|
if mode is None:
|
||||||
mode = int(hdrfields[1], 8)
|
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
|
nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3
|
||||||
data = binascii.a2b_uu(s[:nbytes])
|
data = binascii.a2b_uu(s[:nbytes])
|
||||||
if not quiet:
|
if not quiet:
|
||||||
sys.stderr.write("Warning: %s\n" % str(v))
|
sys.stderr.write("Warning: %s\n" % v)
|
||||||
out_file.write(data)
|
out_file.write(data)
|
||||||
s = in_file.readline()
|
s = in_file.readline()
|
||||||
if not s:
|
if not s:
|
||||||
raise Error, 'Truncated input file'
|
raise Error('Truncated input file')
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
"""uuencode/uudecode main program"""
|
"""uuencode/uudecode main program"""
|
||||||
import getopt
|
|
||||||
|
|
||||||
dopt = 0
|
import optparse
|
||||||
topt = 0
|
parser = optparse.OptionParser(usage='usage: %prog [-d] [-t] [input [output]]')
|
||||||
input = sys.stdin
|
parser.add_option('-d', '--decode', dest='decode', help='Decode (instead of encode)?', default=False, action='store_true')
|
||||||
output = sys.stdout
|
parser.add_option('-t', '--text', dest='text', help='data is text, encoded format unix-compatible text?', default=False, action='store_true')
|
||||||
ok = 1
|
|
||||||
try:
|
(options, args) = parser.parse_args()
|
||||||
optlist, args = getopt.getopt(sys.argv[1:], 'dt')
|
if len(args) > 2:
|
||||||
except getopt.error:
|
p.error('incorrect number of arguments')
|
||||||
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)
|
sys.exit(1)
|
||||||
|
|
||||||
for o, a in optlist:
|
input = sys.stdin
|
||||||
if o == '-d': dopt = 1
|
output = sys.stdout
|
||||||
if o == '-t': topt = 1
|
|
||||||
|
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
input = args[0]
|
input = args[0]
|
||||||
if len(args) > 1:
|
if len(args) > 1:
|
||||||
output = args[1]
|
output = args[1]
|
||||||
|
|
||||||
if dopt:
|
if options.decode:
|
||||||
if topt:
|
if options.text:
|
||||||
if isinstance(output, basestring):
|
if isinstance(output, basestring):
|
||||||
output = open(output, 'w')
|
output = open(output, 'w')
|
||||||
else:
|
else:
|
||||||
|
@ -178,7 +170,7 @@ def test():
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
decode(input, output)
|
decode(input, output)
|
||||||
else:
|
else:
|
||||||
if topt:
|
if options.text:
|
||||||
if isinstance(input, basestring):
|
if isinstance(input, basestring):
|
||||||
input = open(input, 'r')
|
input = open(input, 'r')
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue