mirror of https://github.com/python/cpython.git
New example from Skip Montanaro <skip@mojam.com>.
This commit is contained in:
parent
037649eaa6
commit
c2c46c3751
|
@ -132,29 +132,35 @@ True if the last end-of-file was for an end-of-message marker.
|
||||||
|
|
||||||
|
|
||||||
\subsection{\class{MultiFile} Example \label{multifile-example}}
|
\subsection{\class{MultiFile} Example \label{multifile-example}}
|
||||||
|
\sectionauthor{Skip Montanaro}{skip@mojam.org}
|
||||||
% This is almost unreadable; should be re-written when someone gets time.
|
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
fp = MultiFile(sys.stdin, 0)
|
import mimetools
|
||||||
fp.push(outer_boundary)
|
import MultiFile
|
||||||
message1 = fp.readlines()
|
import StringIO
|
||||||
# We should now be either at real EOF or stopped on a message
|
|
||||||
# boundary. Re-enable the outer boundary.
|
def extract_mime_part_matching(stream, mimetype):
|
||||||
fp.next()
|
"""Return the first element in a multipart MIME message on stream
|
||||||
# Read another message with the same delimiter
|
matching mimetype."""
|
||||||
message2 = fp.readlines()
|
|
||||||
# Re-enable that delimiter again
|
msg = mimetools.Message(stream)
|
||||||
fp.next()
|
msgtype = msg.gettype()
|
||||||
# Now look for a message subpart with a different boundary
|
params = msg.getplist()
|
||||||
fp.push(inner_boundary)
|
|
||||||
sub_header = fp.readlines()
|
data = StringIO.StringIO()
|
||||||
# If no exception has been thrown, we're looking at the start of
|
if msgtype[:10] == "multipart/":
|
||||||
# the message subpart. Reset and grab the subpart
|
|
||||||
fp.next()
|
file = multifile.MultiFile(stream)
|
||||||
sub_body = fp.readlines()
|
file.push(msg.getparam("boundary"))
|
||||||
# Got it. Now pop the inner boundary to re-enable the outer one.
|
while file.next():
|
||||||
fp.pop()
|
submsg = mimetools.Message(file)
|
||||||
# Read to next outer boundary
|
try:
|
||||||
message3 = fp.readlines()
|
data = StringIO.StringIO()
|
||||||
|
mimetools.decode(file, data, submsg.getencoding())
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if submsg.gettype() == mimetype:
|
||||||
|
break
|
||||||
|
file.pop()
|
||||||
|
return data.getvalue()
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
Loading…
Reference in New Issue