source: vendor/python/2.5/Doc/lib/libmultifile.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 6.6 KB
Line 
1\section{\module{multifile} ---
2 Support for files containing distinct parts}
3
4\declaremodule{standard}{multifile}
5\modulesynopsis{Support for reading files which contain distinct
6 parts, such as some MIME data.}
7\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
8
9\deprecated{2.5}{The \refmodule{email} package should be used in
10 preference to the \module{multifile} module.
11 This module is present only to maintain backward
12 compatibility.}
13
14The \class{MultiFile} object enables you to treat sections of a text
15file as file-like input objects, with \code{''} being returned by
16\method{readline()} when a given delimiter pattern is encountered. The
17defaults of this class are designed to make it useful for parsing
18MIME multipart messages, but by subclassing it and overriding methods
19it can be easily adapted for more general use.
20
21\begin{classdesc}{MultiFile}{fp\optional{, seekable}}
22Create a multi-file. You must instantiate this class with an input
23object argument for the \class{MultiFile} instance to get lines from,
24such as a file object returned by \function{open()}.
25
26\class{MultiFile} only ever looks at the input object's
27\method{readline()}, \method{seek()} and \method{tell()} methods, and
28the latter two are only needed if you want random access to the
29individual MIME parts. To use \class{MultiFile} on a non-seekable
30stream object, set the optional \var{seekable} argument to false; this
31will prevent using the input object's \method{seek()} and
32\method{tell()} methods.
33\end{classdesc}
34
35It will be useful to know that in \class{MultiFile}'s view of the world, text
36is composed of three kinds of lines: data, section-dividers, and
37end-markers. MultiFile is designed to support parsing of
38messages that may have multiple nested message parts, each with its
39own pattern for section-divider and end-marker lines.
40
41\begin{seealso}
42 \seemodule{email}{Comprehensive email handling package; supersedes
43 the \module{multifile} module.}
44\end{seealso}
45
46
47\subsection{MultiFile Objects \label{MultiFile-objects}}
48
49A \class{MultiFile} instance has the following methods:
50
51\begin{methoddesc}{readline}{str}
52Read a line. If the line is data (not a section-divider or end-marker
53or real EOF) return it. If the line matches the most-recently-stacked
54boundary, return \code{''} and set \code{self.last} to 1 or 0 according as
55the match is or is not an end-marker. If the line matches any other
56stacked boundary, raise an error. On encountering end-of-file on the
57underlying stream object, the method raises \exception{Error} unless
58all boundaries have been popped.
59\end{methoddesc}
60
61\begin{methoddesc}{readlines}{str}
62Return all lines remaining in this part as a list of strings.
63\end{methoddesc}
64
65\begin{methoddesc}{read}{}
66Read all lines, up to the next section. Return them as a single
67(multiline) string. Note that this doesn't take a size argument!
68\end{methoddesc}
69
70\begin{methoddesc}{seek}{pos\optional{, whence}}
71Seek. Seek indices are relative to the start of the current section.
72The \var{pos} and \var{whence} arguments are interpreted as for a file
73seek.
74\end{methoddesc}
75
76\begin{methoddesc}{tell}{}
77Return the file position relative to the start of the current section.
78\end{methoddesc}
79
80\begin{methoddesc}{next}{}
81Skip lines to the next section (that is, read lines until a
82section-divider or end-marker has been consumed). Return true if
83there is such a section, false if an end-marker is seen. Re-enable
84the most-recently-pushed boundary.
85\end{methoddesc}
86
87\begin{methoddesc}{is_data}{str}
88Return true if \var{str} is data and false if it might be a section
89boundary. As written, it tests for a prefix other than \code{'-}\code{-'} at
90start of line (which all MIME boundaries have) but it is declared so
91it can be overridden in derived classes.
92
93Note that this test is used intended as a fast guard for the real
94boundary tests; if it always returns false it will merely slow
95processing, not cause it to fail.
96\end{methoddesc}
97
98\begin{methoddesc}{push}{str}
99Push a boundary string. When a decorated version of this boundary
100is found as an input line, it will be interpreted as a section-divider
101or end-marker (depending on the decoration, see \rfc{2045}). All subsequent
102reads will return the empty string to indicate end-of-file, until a
103call to \method{pop()} removes the boundary a or \method{next()} call
104reenables it.
105
106It is possible to push more than one boundary. Encountering the
107most-recently-pushed boundary will return EOF; encountering any other
108boundary will raise an error.
109\end{methoddesc}
110
111\begin{methoddesc}{pop}{}
112Pop a section boundary. This boundary will no longer be interpreted
113as EOF.
114\end{methoddesc}
115
116\begin{methoddesc}{section_divider}{str}
117Turn a boundary into a section-divider line. By default, this
118method prepends \code{'-}\code{-'} (which MIME section boundaries have) but
119it is declared so it can be overridden in derived classes. This
120method need not append LF or CR-LF, as comparison with the result
121ignores trailing whitespace.
122\end{methoddesc}
123
124\begin{methoddesc}{end_marker}{str}
125Turn a boundary string into an end-marker line. By default, this
126method prepends \code{'-}\code{-'} and appends \code{'-}\code{-'} (like a
127MIME-multipart end-of-message marker) but it is declared so it can be
128overridden in derived classes. This method need not append LF or
129CR-LF, as comparison with the result ignores trailing whitespace.
130\end{methoddesc}
131
132Finally, \class{MultiFile} instances have two public instance variables:
133
134\begin{memberdesc}{level}
135Nesting depth of the current part.
136\end{memberdesc}
137
138\begin{memberdesc}{last}
139True if the last end-of-file was for an end-of-message marker.
140\end{memberdesc}
141
142
143\subsection{\class{MultiFile} Example \label{multifile-example}}
144\sectionauthor{Skip Montanaro}{skip@mojam.com}
145
146\begin{verbatim}
147import mimetools
148import multifile
149import StringIO
150
151def extract_mime_part_matching(stream, mimetype):
152 """Return the first element in a multipart MIME message on stream
153 matching mimetype."""
154
155 msg = mimetools.Message(stream)
156 msgtype = msg.gettype()
157 params = msg.getplist()
158
159 data = StringIO.StringIO()
160 if msgtype[:10] == "multipart/":
161
162 file = multifile.MultiFile(stream)
163 file.push(msg.getparam("boundary"))
164 while file.next():
165 submsg = mimetools.Message(file)
166 try:
167 data = StringIO.StringIO()
168 mimetools.decode(file, data, submsg.getencoding())
169 except ValueError:
170 continue
171 if submsg.gettype() == mimetype:
172 break
173 file.pop()
174 return data.getvalue()
175\end{verbatim}
Note: See TracBrowser for help on using the repository browser.