annotate Lib/uu.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents cf0e68562604
children d8d33648d1b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5772
5f7ddb7ca347 Added /usr/bin/env python and made executable.
Guido van Rossum <guido@python.org>
parents: 5274
diff changeset
1 #! /usr/bin/env python
5f7ddb7ca347 Added /usr/bin/env python and made executable.
Guido van Rossum <guido@python.org>
parents: 5274
diff changeset
2
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Copyright 1994 by Lance Ellinghouse
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # Cathedral City, California Republic, United States of America.
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # All Rights Reserved
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15799
diff changeset
6 # Permission to use, copy, modify, and distribute this software and its
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15799
diff changeset
7 # documentation for any purpose and without fee is hereby granted,
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # provided that the above copyright notice appear in all copies and that
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15799
diff changeset
9 # both that copyright notice and this permission notice appear in
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # supporting documentation, and that the name of Lance Ellinghouse
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15799
diff changeset
11 # not be used in advertising or publicity pertaining to distribution
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # of the software without specific, written prior permission.
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2915
4509dfe19b68 Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents: 1872
diff changeset
20 #
4509dfe19b68 Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents: 1872
diff changeset
21 # Modified by Jack Jansen, CWI, July 1995:
4509dfe19b68 Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents: 1872
diff changeset
22 # - Use binascii module to do the actual line-by-line conversion
4509dfe19b68 Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents: 1872
diff changeset
23 # between ascii and binary. This results in a 1000-fold speedup. The C
4509dfe19b68 Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents: 1872
diff changeset
24 # version is still 5 times faster, though.
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
25 # - Arguments more compliant with python standard
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 9772
diff changeset
26
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 9772
diff changeset
27 """Implementation of the UUencode and UUdecode functions.
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 9772
diff changeset
29 encode(in_file, out_file [,name, mode])
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 9772
diff changeset
30 decode(in_file [, out_file, mode])
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 9772
diff changeset
31 """
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
2915
4509dfe19b68 Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents: 1872
diff changeset
33 import binascii
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
34 import os
9432
edf7553fb20e Move 'import sys' to top of module, as noted by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
35 import sys
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
37 __all__ = ["Error", "encode", "decode"]
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
38
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 11707
diff changeset
39 class Error(Exception):
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 11707
diff changeset
40 pass
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
41
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
42 def encode(in_file, out_file, name=None, mode=None):
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
43 """Uuencode file"""
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
44 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
45 # If in_file is a pathname open it and change defaults
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
46 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
47 if in_file == '-':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
48 in_file = sys.stdin
35902
9fd0cd362aff Use basestring instead of type.StringType for checking whether a input
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
49 elif isinstance(in_file, basestring):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
50 if name is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
51 name = os.path.basename(in_file)
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
52 if mode is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
53 try:
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 19346
diff changeset
54 mode = os.stat(in_file).st_mode
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
55 except AttributeError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
56 pass
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
57 in_file = open(in_file, 'rb')
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
58 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
59 # Open out_file if it is a pathname
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
60 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
61 if out_file == '-':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
62 out_file = sys.stdout
35902
9fd0cd362aff Use basestring instead of type.StringType for checking whether a input
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
63 elif isinstance(out_file, basestring):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
64 out_file = open(out_file, 'w')
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
65 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
66 # Set defaults for name and mode
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
67 #
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
68 if name is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
69 name = '-'
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
70 if mode is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
71 mode = 0666
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
72 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
73 # Write the data
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
74 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
75 out_file.write('begin %o %s\n' % ((mode&0777),name))
35907
4d1d277178a5 Avoid using str as a variable name.
Walter Dörwald <walter@livinglogic.de>
parents: 35902
diff changeset
76 data = in_file.read(45)
4d1d277178a5 Avoid using str as a variable name.
Walter Dörwald <walter@livinglogic.de>
parents: 35902
diff changeset
77 while len(data) > 0:
4d1d277178a5 Avoid using str as a variable name.
Walter Dörwald <walter@livinglogic.de>
parents: 35902
diff changeset
78 out_file.write(binascii.b2a_uu(data))
4d1d277178a5 Avoid using str as a variable name.
Walter Dörwald <walter@livinglogic.de>
parents: 35902
diff changeset
79 data = in_file.read(45)
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 out_file.write(' \nend\n')
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
19346
a48a3d75475e decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents: 18469
diff changeset
83 def decode(in_file, out_file=None, mode=None, quiet=0):
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
84 """Decode uuencoded file"""
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
85 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
86 # Open the input file, if needed.
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
87 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
88 if in_file == '-':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
89 in_file = sys.stdin
35902
9fd0cd362aff Use basestring instead of type.StringType for checking whether a input
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
90 elif isinstance(in_file, basestring):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
91 in_file = open(in_file)
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
92 #
5273
1148e7ebca35 Search for a valid begin line instead of requiring the first line.
Guido van Rossum <guido@python.org>
parents: 3143
diff changeset
93 # Read until a begin is encountered or we've exhausted the file
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
94 #
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
95 while True:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
96 hdr = in_file.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
97 if not hdr:
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
98 raise Error('No valid begin line found in input file')
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
99 if not hdr.startswith('begin'):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
100 continue
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
101 hdrfields = hdr.split(' ', 2)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
102 if len(hdrfields) == 3 and hdrfields[0] == 'begin':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
103 try:
15799
5a308a9f99a5 Adapted version of SF Patch #103173 by pyretic: make uu.decode work
Guido van Rossum <guido@python.org>
parents: 15543
diff changeset
104 int(hdrfields[1], 8)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
105 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
106 except ValueError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
107 pass
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
108 if out_file is None:
15799
5a308a9f99a5 Adapted version of SF Patch #103173 by pyretic: make uu.decode work
Guido van Rossum <guido@python.org>
parents: 15543
diff changeset
109 out_file = hdrfields[2].rstrip()
19346
a48a3d75475e decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents: 18469
diff changeset
110 if os.path.exists(out_file):
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
111 raise Error('Cannot overwrite existing file: %s' % out_file)
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14021
diff changeset
112 if mode is None:
15799
5a308a9f99a5 Adapted version of SF Patch #103173 by pyretic: make uu.decode work
Guido van Rossum <guido@python.org>
parents: 15543
diff changeset
113 mode = int(hdrfields[1], 8)
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
114 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
115 # Open the output file
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
116 #
40369
cf0e68562604 Jython compatibility fix: if uu.decode() opened its output file, be sure to
Andrew M. Kuchling <amk@amk.ca>
parents: 37531
diff changeset
117 opened = False
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
118 if out_file == '-':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
119 out_file = sys.stdout
35902
9fd0cd362aff Use basestring instead of type.StringType for checking whether a input
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
120 elif isinstance(out_file, basestring):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
121 fp = open(out_file, 'wb')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
122 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
123 os.path.chmod(out_file, mode)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
124 except AttributeError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
125 pass
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
126 out_file = fp
40369
cf0e68562604 Jython compatibility fix: if uu.decode() opened its output file, be sure to
Andrew M. Kuchling <amk@amk.ca>
parents: 37531
diff changeset
127 opened = True
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
128 #
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
129 # Main decoding loop
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
130 #
9772
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
131 s = in_file.readline()
19346
a48a3d75475e decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents: 18469
diff changeset
132 while s and s.strip() != 'end':
9772
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
133 try:
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
134 data = binascii.a2b_uu(s)
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
135 except binascii.Error, v:
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
136 # Workaround for broken uuencoders by /Fredrik Lundh
37280
ccbdcfc40c81 Make uu use floor division instead of classic division.
Georg Brandl <georg@python.org>
parents: 35908
diff changeset
137 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3
9772
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
138 data = binascii.a2b_uu(s[:nbytes])
19346
a48a3d75475e decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents: 18469
diff changeset
139 if not quiet:
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
140 sys.stderr.write("Warning: %s\n" % v)
9772
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
141 out_file.write(data)
8813627aac46 Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents: 9432
diff changeset
142 s = in_file.readline()
18469
6c501d892003 Backport Tim's checkin 1.17:
Thomas Wouters <thomas@python.org>
parents: 16889
diff changeset
143 if not s:
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
144 raise Error('Truncated input file')
40369
cf0e68562604 Jython compatibility fix: if uu.decode() opened its output file, be sure to
Andrew M. Kuchling <amk@amk.ca>
parents: 37531
diff changeset
145 if opened:
cf0e68562604 Jython compatibility fix: if uu.decode() opened its output file, be sure to
Andrew M. Kuchling <amk@amk.ca>
parents: 37531
diff changeset
146 out_file.close()
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 def test():
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
149 """uuencode/uudecode main program"""
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
150
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
151 import optparse
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
152 parser = optparse.OptionParser(usage='usage: %prog [-d] [-t] [input [output]]')
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
153 parser.add_option('-d', '--decode', dest='decode', help='Decode (instead of encode)?', default=False, action='store_true')
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
154 parser.add_option('-t', '--text', dest='text', help='data is text, encoded format unix-compatible text?', default=False, action='store_true')
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
155
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
156 (options, args) = parser.parse_args()
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
157 if len(args) > 2:
37531
d22cb14da671 minor error in uudecode main error handling
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37280
diff changeset
158 parser.error('incorrect number of arguments')
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
159 sys.exit(1)
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
160
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
161 input = sys.stdin
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
162 output = sys.stdout
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
163 if len(args) > 0:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
164 input = args[0]
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
165 if len(args) > 1:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
166 output = args[1]
3032
3b3e68075833 Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents: 2915
diff changeset
167
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
168 if options.decode:
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
169 if options.text:
35902
9fd0cd362aff Use basestring instead of type.StringType for checking whether a input
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
170 if isinstance(output, basestring):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
171 output = open(output, 'w')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
172 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
173 print sys.argv[0], ': cannot do -t to stdout'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
174 sys.exit(1)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
175 decode(input, output)
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 else:
35908
4e5e16a3be0d Use optparse instead of getopt for command line options.
Walter Dörwald <walter@livinglogic.de>
parents: 35907
diff changeset
177 if options.text:
35902
9fd0cd362aff Use basestring instead of type.StringType for checking whether a input
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
178 if isinstance(input, basestring):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
179 input = open(input, 'r')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
180 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
181 print sys.argv[0], ': cannot do -t from stdin'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
182 sys.exit(1)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5772
diff changeset
183 encode(input, output)
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 if __name__ == '__main__':
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 test()