Mercurial > cpython-lru_cache-weakref
annotate Lib/mimetools.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | ff96a313cd1c |
| children |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
8394
diff
changeset
|
1 """Various tools used by MIME-reading or MIME-writing programs.""" |
|
914
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 |
|
48948
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
48227
diff
changeset
|
5 import sys |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
6 import tempfile |
|
48948
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
48227
diff
changeset
|
7 from warnings import filterwarnings, catch_warnings |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
48948
diff
changeset
|
8 with catch_warnings(): |
|
48948
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
48227
diff
changeset
|
9 if sys.py3kwarning: |
|
d563621d76db
Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents:
48227
diff
changeset
|
10 filterwarnings("ignore", ".*rfc822 has been removed", DeprecationWarning) |
|
48227
50fffc800845
Silence the DeprecationWarning of rfc822 triggered by its importation in
Brett Cannon <bcannon@gmail.com>
parents:
47958
diff
changeset
|
11 import rfc822 |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
47640
44019e37ac92
deprecated mimetools
Benjamin Peterson <benjamin@python.org>
parents:
37338
diff
changeset
|
13 from warnings import warnpy3k |
|
47958
e7a6a4515ab0
fix test_py3kwarns
Benjamin Peterson <benjamin@python.org>
parents:
47640
diff
changeset
|
14 warnpy3k("in 3.x, mimetools has been removed in favor of the email package", |
|
e7a6a4515ab0
fix test_py3kwarns
Benjamin Peterson <benjamin@python.org>
parents:
47640
diff
changeset
|
15 stacklevel=2) |
|
47640
44019e37ac92
deprecated mimetools
Benjamin Peterson <benjamin@python.org>
parents:
37338
diff
changeset
|
16 |
|
16247
a68c7032cdf2
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
17 __all__ = ["Message","choose_boundary","encode","decode","copyliteral", |
|
a68c7032cdf2
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
18 "copybinary"] |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 class Message(rfc822.Message): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
21 """A derived class of rfc822.Message that knows about MIME headers and |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
22 contains some hooks for decoding encoded and multipart messages.""" |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
24 def __init__(self, fp, seekable = 1): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
25 rfc822.Message.__init__(self, fp, seekable) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
26 self.encodingheader = \ |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
27 self.getheader('content-transfer-encoding') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
28 self.typeheader = \ |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
29 self.getheader('content-type') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
30 self.parsetype() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
31 self.parseplist() |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
33 def parsetype(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
34 str = self.typeheader |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
35 if str is None: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
36 str = 'text/plain' |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
37 if ';' in str: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
38 i = str.index(';') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
39 self.plisttext = str[i:] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
40 str = str[:i] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
41 else: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
42 self.plisttext = '' |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
43 fields = str.split('/') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
44 for i in range(len(fields)): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
45 fields[i] = fields[i].strip().lower() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
46 self.type = '/'.join(fields) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
47 self.maintype = fields[0] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
48 self.subtype = '/'.join(fields[1:]) |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
50 def parseplist(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
51 str = self.plisttext |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
52 self.plist = [] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
53 while str[:1] == ';': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
54 str = str[1:] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
55 if ';' in str: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
56 # XXX Should parse quotes! |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
57 end = str.index(';') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
58 else: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
59 end = len(str) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
60 f = str[:end] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
61 if '=' in f: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
62 i = f.index('=') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
63 f = f[:i].strip().lower() + \ |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
64 '=' + f[i+1:].strip() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
65 self.plist.append(f.strip()) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
66 str = str[end:] |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
68 def getplist(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
69 return self.plist |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
71 def getparam(self, name): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
72 name = name.lower() + '=' |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
73 n = len(name) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
74 for p in self.plist: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
75 if p[:n] == name: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
76 return rfc822.unquote(p[n:]) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
77 return None |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
79 def getparamnames(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
80 result = [] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
81 for p in self.plist: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
82 i = p.find('=') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
83 if i >= 0: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
84 result.append(p[:i].lower()) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
85 return result |
|
4357
2913597443b9
Added getparamnames() to Message class -- return a list of all
Guido van Rossum <guido@python.org>
parents:
4151
diff
changeset
|
86 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
87 def getencoding(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
88 if self.encodingheader is None: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
89 return '7bit' |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
90 return self.encodingheader.lower() |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
92 def gettype(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
93 return self.type |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
95 def getmaintype(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
96 return self.maintype |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
98 def getsubtype(self): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
99 return self.subtype |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 # Utility functions |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 # ----------------- |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 |
|
29481
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
107 try: |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
108 import thread |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
109 except ImportError: |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
110 import dummy_thread as thread |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
111 _counter_lock = thread.allocate_lock() |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
112 del thread |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
113 |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
114 _counter = 0 |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
115 def _get_next_counter(): |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
116 global _counter |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
117 _counter_lock.acquire() |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
118 _counter += 1 |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
119 result = _counter |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
120 _counter_lock.release() |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
121 return result |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 _prefix = None |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 |
|
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 def choose_boundary(): |
|
29481
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
126 """Return a string usable as a multipart boundary. |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
127 |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
128 The string chosen is unique within a single program run, and |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
129 incorporates the user id (if available), process id (if available), |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
130 and current time. So it's very unlikely the returned string appears |
|
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
131 in message text, but there's no guarantee. |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
8394
diff
changeset
|
132 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
133 The boundary contains dots so you have to quote it in the header.""" |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
8394
diff
changeset
|
134 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
135 global _prefix |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
136 import time |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
137 if _prefix is None: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
138 import socket |
|
37338
da770659f77b
Bug #1250170, Patch #1462230: handle socket.gethostname()
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
139 try: |
|
da770659f77b
Bug #1250170, Patch #1462230: handle socket.gethostname()
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
140 hostid = socket.gethostbyname(socket.gethostname()) |
|
da770659f77b
Bug #1250170, Patch #1462230: handle socket.gethostname()
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
141 except socket.gaierror: |
|
da770659f77b
Bug #1250170, Patch #1462230: handle socket.gethostname()
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
142 hostid = '127.0.0.1' |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
143 try: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29482
diff
changeset
|
144 uid = repr(os.getuid()) |
|
22478
6408384c8137
Only AttributeError can be raised in this situation - on systems without
Skip Montanaro <skip@pobox.com>
parents:
16760
diff
changeset
|
145 except AttributeError: |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
146 uid = '1' |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
147 try: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29482
diff
changeset
|
148 pid = repr(os.getpid()) |
|
22478
6408384c8137
Only AttributeError can be raised in this situation - on systems without
Skip Montanaro <skip@pobox.com>
parents:
16760
diff
changeset
|
149 except AttributeError: |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
150 pid = '1' |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
151 _prefix = hostid + '.' + uid + '.' + pid |
|
29481
06f28adea6d2
choose_boundary(): Incorporated a threadsafe incrementing counter, so that
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
152 return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter()) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
153 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
154 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
155 # 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
|
156 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
157 def decode(input, output, encoding): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
158 """Decode common content-transfer-encodings (base64, quopri, uuencode).""" |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
159 if encoding == 'base64': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
160 import base64 |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
161 return base64.decode(input, output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
162 if encoding == 'quoted-printable': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
163 import quopri |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
164 return quopri.decode(input, output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
165 if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
166 import uu |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
167 return uu.decode(input, output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
168 if encoding in ('7bit', '8bit'): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
169 return output.write(input.read()) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22586
diff
changeset
|
170 if encoding in decodetab: |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
171 pipethrough(input, decodetab[encoding], output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
172 else: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
173 raise ValueError, \ |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
174 'unknown Content-Transfer-Encoding: %s' % encoding |
|
1746
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 encode(input, output, encoding): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
177 """Encode common content-transfer-encodings (base64, quopri, uuencode).""" |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
178 if encoding == 'base64': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
179 import base64 |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
180 return base64.encode(input, output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
181 if encoding == 'quoted-printable': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
182 import quopri |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
183 return quopri.encode(input, output, 0) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
184 if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
185 import uu |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
186 return uu.encode(input, output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
187 if encoding in ('7bit', '8bit'): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
188 return output.write(input.read()) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22586
diff
changeset
|
189 if encoding in encodetab: |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
190 pipethrough(input, encodetab[encoding], output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
191 else: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
192 raise ValueError, \ |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
193 'unknown Content-Transfer-Encoding: %s' % encoding |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
194 |
|
5770
38ef091964f0
Use the modules base64, quopri, and uu, instead of external programs
Guido van Rossum <guido@python.org>
parents:
4357
diff
changeset
|
195 # The following is no longer used for standard encodings |
|
38ef091964f0
Use the modules base64, quopri, and uu, instead of external programs
Guido van Rossum <guido@python.org>
parents:
4357
diff
changeset
|
196 |
|
38ef091964f0
Use the modules base64, quopri, and uu, instead of external programs
Guido van Rossum <guido@python.org>
parents:
4357
diff
changeset
|
197 # XXX This requires that uudecode and mmencode are in $PATH |
|
38ef091964f0
Use the modules base64, quopri, and uu, instead of external programs
Guido van Rossum <guido@python.org>
parents:
4357
diff
changeset
|
198 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
199 uudecode_pipe = '''( |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
200 TEMP=/tmp/@uu.$$ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
201 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
|
202 cat $TEMP |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
203 rm $TEMP |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
204 )''' |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
205 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
206 decodetab = { |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
207 'uuencode': uudecode_pipe, |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
208 'x-uuencode': uudecode_pipe, |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
209 'uue': uudecode_pipe, |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
210 'x-uue': uudecode_pipe, |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
211 'quoted-printable': 'mmencode -u -q', |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
212 'base64': 'mmencode -u -b', |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
213 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
214 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
215 encodetab = { |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
216 'x-uuencode': 'uuencode tempfile', |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
217 'uuencode': 'uuencode tempfile', |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
218 'x-uue': 'uuencode tempfile', |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
219 'uue': 'uuencode tempfile', |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
220 'quoted-printable': 'mmencode -q', |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
221 'base64': 'mmencode -b', |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
222 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
223 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
224 def pipeto(input, command): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
225 pipe = os.popen(command, 'w') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
226 copyliteral(input, pipe) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
227 pipe.close() |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
228 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
229 def pipethrough(input, command, output): |
|
24710
11c701038805
Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
230 (fd, tempname) = tempfile.mkstemp() |
|
11c701038805
Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
231 temp = os.fdopen(fd, 'w') |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
232 copyliteral(input, temp) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
233 temp.close() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
234 pipe = os.popen(command + ' <' + tempname, 'r') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
235 copybinary(pipe, output) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
236 pipe.close() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
237 os.unlink(tempname) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
238 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
239 def copyliteral(input, output): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
240 while 1: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
241 line = input.readline() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
242 if not line: break |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
243 output.write(line) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
244 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
245 def copybinary(input, output): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
246 BUFSIZE = 8192 |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
247 while 1: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
248 line = input.read(BUFSIZE) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
249 if not line: break |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15584
diff
changeset
|
250 output.write(line) |
