annotate Lib/mimetools.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 2913597443b9
children 38ef091964f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Various tools used by MIME-reading or MIME-writing programs.
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
4 import os
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
5 import rfc822
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import string
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
7 import tempfile
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # A derived class of rfc822.Message that knows about MIME headers and
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # contains some hooks for decoding encoded and multipart messages.
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 class Message(rfc822.Message):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
2920
177f575708bb add seekable option to __init__
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
15 def __init__(self, fp, seekable = 1):
177f575708bb add seekable option to __init__
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
16 rfc822.Message.__init__(self, fp, seekable)
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 self.encodingheader = \
3030
405b473aa4a1 forget previous change (content-encoding)
Guido van Rossum <guido@python.org>
parents: 3029
diff changeset
18 self.getheader('content-transfer-encoding')
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 self.typeheader = \
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 self.getheader('content-type')
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 self.parsetype()
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 self.parseplist()
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 def parsetype(self):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 str = self.typeheader
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 if str == None:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 str = 'text/plain'
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 if ';' in str:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 i = string.index(str, ';')
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 self.plisttext = str[i:]
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 str = str[:i]
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 else:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 self.plisttext = ''
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 fields = string.splitfields(str, '/')
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 for i in range(len(fields)):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 fields[i] = string.lower(string.strip(fields[i]))
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 self.type = string.joinfields(fields, '/')
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 self.maintype = fields[0]
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 self.subtype = string.joinfields(fields[1:], '/')
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 def parseplist(self):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 str = self.plisttext
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 self.plist = []
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 while str[:1] == ';':
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 str = str[1:]
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 if ';' in str:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # XXX Should parse quotes!
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 end = string.index(str, ';')
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 else:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 end = len(str)
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 f = str[:end]
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 if '=' in f:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 i = string.index(f, '=')
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 f = string.lower(string.strip(f[:i])) + \
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 '=' + string.strip(f[i+1:])
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 self.plist.append(string.strip(f))
3353
f6a840e83d2a add missing loop step to parseplist()
Guido van Rossum <guido@python.org>
parents: 3030
diff changeset
57 str = str[end:]
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def getplist(self):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 return self.plist
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 def getparam(self, name):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 name = string.lower(name) + '='
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 n = len(name)
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 for p in self.plist:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if p[:n] == name:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 return rfc822.unquote(p[n:])
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 return None
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
4357
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
70 def getparamnames(self):
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
71 result = []
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
72 for p in self.plist:
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
73 i = string.find(p, '=')
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
74 if i >= 0:
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
75 result.append(string.lower(p[:i]))
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
76 return result
2913597443b9 Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents: 4151
diff changeset
77
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 def getencoding(self):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if self.encodingheader == None:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 return '7bit'
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
81 return string.lower(self.encodingheader)
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 def gettype(self):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 return self.type
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def getmaintype(self):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 return self.maintype
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 def getsubtype(self):
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return self.subtype
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 # Utility functions
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 # -----------------
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 # Return a random string usable as a multipart boundary.
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 # The method used is so that it is *very* unlikely that the same
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 # string of characters will every occur again in the Universe,
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 # so the caller needn't check the data it is packing for the
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 # occurrence of the boundary.
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 #
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 # The boundary contains dots so you have to quote it in the header.
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 _prefix = None
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 def choose_boundary():
3633
a7e3bc2a7cac removed unused globals
Guido van Rossum <guido@python.org>
parents: 3353
diff changeset
110 global _prefix
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 import time
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 import rand
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 if _prefix == None:
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 import socket
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 import os
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 hostid = socket.gethostbyname(socket.gethostname())
4151
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
117 try:
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
118 uid = `os.getuid()`
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
119 except:
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
120 uid = '1'
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
121 try:
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
122 pid = `os.getpid()`
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
123 except:
69a33fa3dbb8 Don't die when getuid() or getpid() aren't defined.
Guido van Rossum <guido@python.org>
parents: 3633
diff changeset
124 pid = '1'
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 seed = `rand.rand()`
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 _prefix = hostid + '.' + uid + '.' + pid
1178
f9bbb01388d5 * More changes due to stricter argument passing rules
Guido van Rossum <guido@python.org>
parents: 914
diff changeset
127 timestamp = `int(time.time())`
914
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 seed = `rand.rand()`
74b716e9ac49 New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 return _prefix + '.' + timestamp + '.' + seed
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
130
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
131
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
132 # Subroutines for decoding some common content-transfer-types
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
133
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
134 # XXX This requires that uudecode and mmencode are in $PATH
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
135
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
136 def decode(input, output, encoding):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
137 if decodetab.has_key(encoding):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
138 pipethrough(input, decodetab[encoding], output)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
139 else:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
140 raise ValueError, \
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
141 'unknown Content-Transfer-Encoding: %s' % encoding
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
142
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
143 def encode(input, output, encoding):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
144 if encodetab.has_key(encoding):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
145 pipethrough(input, encodetab[encoding], output)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
146 else:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
147 raise ValueError, \
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
148 'unknown Content-Transfer-Encoding: %s' % encoding
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
149
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
150 uudecode_pipe = '''(
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
151 TEMP=/tmp/@uu.$$
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
152 sed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
153 cat $TEMP
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
154 rm $TEMP
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
155 )'''
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
156
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
157 decodetab = {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
158 'uuencode': uudecode_pipe,
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
159 'x-uuencode': uudecode_pipe,
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
160 'quoted-printable': 'mmencode -u -q',
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
161 'base64': 'mmencode -u -b',
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
162 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
163
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
164 encodetab = {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
165 'x-uuencode': 'uuencode tempfile',
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
166 'uuencode': 'uuencode tempfile',
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
167 'quoted-printable': 'mmencode -q',
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
168 'base64': 'mmencode -b',
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
169 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
170
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
171 def pipeto(input, command):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
172 pipe = os.popen(command, 'w')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
173 copyliteral(input, pipe)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
174 pipe.close()
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
175
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
176 def pipethrough(input, command, output):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
177 tempname = tempfile.mktemp()
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
178 try:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
179 temp = open(tempname, 'w')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
180 except IOError:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
181 print '*** Cannot create temp file', `tempname`
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
182 return
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
183 copyliteral(input, temp)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
184 temp.close()
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
185 pipe = os.popen(command + ' <' + tempname, 'r')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
186 copybinary(pipe, output)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
187 pipe.close()
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
188 os.unlink(tempname)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
189
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
190 def copyliteral(input, output):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
191 while 1:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
192 line = input.readline()
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
193 if not line: break
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
194 output.write(line)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
195
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
196 def copybinary(input, output):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
197 BUFSIZE = 8192
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
198 while 1:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
199 line = input.read(BUFSIZE)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
200 if not line: break
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
201 output.write(line)