Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2c46c3

Browse files
committed
New example from Skip Montanaro <[email protected]>.
1 parent 037649e commit c2c46c3

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

Doc/lib/libmultifile.tex

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,35 @@ \subsection{MultiFile Objects \label{MultiFile-objects}}
132132

133133

134134
\subsection{\class{MultiFile} Example \label{multifile-example}}
135-
136-
% This is almost unreadable; should be re-written when someone gets time.
135+
\sectionauthor{Skip Montanaro}{[email protected]}
137136

138137
\begin{verbatim}
139-
fp = MultiFile(sys.stdin, 0)
140-
fp.push(outer_boundary)
141-
message1 = fp.readlines()
142-
# We should now be either at real EOF or stopped on a message
143-
# boundary. Re-enable the outer boundary.
144-
fp.next()
145-
# Read another message with the same delimiter
146-
message2 = fp.readlines()
147-
# Re-enable that delimiter again
148-
fp.next()
149-
# Now look for a message subpart with a different boundary
150-
fp.push(inner_boundary)
151-
sub_header = fp.readlines()
152-
# If no exception has been thrown, we're looking at the start of
153-
# the message subpart. Reset and grab the subpart
154-
fp.next()
155-
sub_body = fp.readlines()
156-
# Got it. Now pop the inner boundary to re-enable the outer one.
157-
fp.pop()
158-
# Read to next outer boundary
159-
message3 = fp.readlines()
138+
import mimetools
139+
import MultiFile
140+
import StringIO
141+
142+
def extract_mime_part_matching(stream, mimetype):
143+
"""Return the first element in a multipart MIME message on stream
144+
matching mimetype."""
145+
146+
msg = mimetools.Message(stream)
147+
msgtype = msg.gettype()
148+
params = msg.getplist()
149+
150+
data = StringIO.StringIO()
151+
if msgtype[:10] == "multipart/":
152+
153+
file = multifile.MultiFile(stream)
154+
file.push(msg.getparam("boundary"))
155+
while file.next():
156+
submsg = mimetools.Message(file)
157+
try:
158+
data = StringIO.StringIO()
159+
mimetools.decode(file, data, submsg.getencoding())
160+
except ValueError:
161+
continue
162+
if submsg.gettype() == mimetype:
163+
break
164+
file.pop()
165+
return data.getvalue()
160166
\end{verbatim}

0 commit comments

Comments
 (0)