1998-08-10 16:42:37 -03:00
|
|
|
\section{\module{multifile} ---
|
1999-04-23 11:46:18 -03:00
|
|
|
Support for files containing distinct parts}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
1999-04-23 11:46:18 -03:00
|
|
|
\declaremodule{standard}{multifile}
|
1998-08-07 12:55:14 -03:00
|
|
|
\modulesynopsis{Support for reading files which contain distinct
|
1999-04-23 11:46:18 -03:00
|
|
|
parts, such as some MIME data.}
|
|
|
|
\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
2006-02-21 15:23:49 -04:00
|
|
|
\deprecated{2.5}{The \refmodule{email} package should be used in
|
|
|
|
preference to the \module{multifile} module.
|
|
|
|
This module is present only to maintain backward
|
|
|
|
compatibility.}
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-07-02 16:36:50 -03:00
|
|
|
The \class{MultiFile} object enables you to treat sections of a text
|
|
|
|
file as file-like input objects, with \code{''} being returned by
|
|
|
|
\method{readline()} when a given delimiter pattern is encountered. The
|
1998-06-28 14:55:53 -03:00
|
|
|
defaults of this class are designed to make it useful for parsing
|
|
|
|
MIME multipart messages, but by subclassing it and overriding methods
|
|
|
|
it can be easily adapted for more general use.
|
|
|
|
|
1998-07-02 16:36:50 -03:00
|
|
|
\begin{classdesc}{MultiFile}{fp\optional{, seekable}}
|
1998-06-28 14:55:53 -03:00
|
|
|
Create a multi-file. You must instantiate this class with an input
|
1998-07-02 16:36:50 -03:00
|
|
|
object argument for the \class{MultiFile} instance to get lines from,
|
2003-08-25 01:28:05 -03:00
|
|
|
such as a file object returned by \function{open()}.
|
1998-07-02 16:36:50 -03:00
|
|
|
|
|
|
|
\class{MultiFile} only ever looks at the input object's
|
|
|
|
\method{readline()}, \method{seek()} and \method{tell()} methods, and
|
|
|
|
the latter two are only needed if you want random access to the
|
|
|
|
individual MIME parts. To use \class{MultiFile} on a non-seekable
|
|
|
|
stream object, set the optional \var{seekable} argument to false; this
|
|
|
|
will prevent using the input object's \method{seek()} and
|
|
|
|
\method{tell()} methods.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{classdesc}
|
|
|
|
|
1998-07-02 16:36:50 -03:00
|
|
|
It will be useful to know that in \class{MultiFile}'s view of the world, text
|
1998-06-28 14:55:53 -03:00
|
|
|
is composed of three kinds of lines: data, section-dividers, and
|
|
|
|
end-markers. MultiFile is designed to support parsing of
|
|
|
|
messages that may have multiple nested message parts, each with its
|
|
|
|
own pattern for section-divider and end-marker lines.
|
|
|
|
|
2002-08-06 18:26:01 -03:00
|
|
|
\begin{seealso}
|
2004-12-31 20:28:46 -04:00
|
|
|
\seemodule{email}{Comprehensive email handling package; supersedes
|
2002-08-06 18:26:01 -03:00
|
|
|
the \module{multifile} module.}
|
|
|
|
\end{seealso}
|
|
|
|
|
1998-08-07 12:55:14 -03:00
|
|
|
|
|
|
|
\subsection{MultiFile Objects \label{MultiFile-objects}}
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
A \class{MultiFile} instance has the following methods:
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{readline}{str}
|
1998-06-28 14:55:53 -03:00
|
|
|
Read a line. If the line is data (not a section-divider or end-marker
|
|
|
|
or real EOF) return it. If the line matches the most-recently-stacked
|
1998-06-30 13:35:25 -03:00
|
|
|
boundary, return \code{''} and set \code{self.last} to 1 or 0 according as
|
1998-06-28 14:55:53 -03:00
|
|
|
the match is or is not an end-marker. If the line matches any other
|
1998-07-02 16:36:50 -03:00
|
|
|
stacked boundary, raise an error. On encountering end-of-file on the
|
|
|
|
underlying stream object, the method raises \exception{Error} unless
|
|
|
|
all boundaries have been popped.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{readlines}{str}
|
1998-07-02 16:36:50 -03:00
|
|
|
Return all lines remaining in this part as a list of strings.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{read}{}
|
1998-06-28 14:55:53 -03:00
|
|
|
Read all lines, up to the next section. Return them as a single
|
|
|
|
(multiline) string. Note that this doesn't take a size argument!
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{seek}{pos\optional{, whence}}
|
1998-06-28 14:55:53 -03:00
|
|
|
Seek. Seek indices are relative to the start of the current section.
|
1998-07-02 16:36:50 -03:00
|
|
|
The \var{pos} and \var{whence} arguments are interpreted as for a file
|
|
|
|
seek.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{tell}{}
|
1998-07-02 16:36:50 -03:00
|
|
|
Return the file position relative to the start of the current section.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{next}{}
|
2001-03-08 18:46:41 -04:00
|
|
|
Skip lines to the next section (that is, read lines until a
|
|
|
|
section-divider or end-marker has been consumed). Return true if
|
|
|
|
there is such a section, false if an end-marker is seen. Re-enable
|
|
|
|
the most-recently-pushed boundary.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{is_data}{str}
|
1998-07-02 16:36:50 -03:00
|
|
|
Return true if \var{str} is data and false if it might be a section
|
1999-04-23 11:46:18 -03:00
|
|
|
boundary. As written, it tests for a prefix other than \code{'-}\code{-'} at
|
1998-07-02 16:36:50 -03:00
|
|
|
start of line (which all MIME boundaries have) but it is declared so
|
|
|
|
it can be overridden in derived classes.
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
Note that this test is used intended as a fast guard for the real
|
1998-07-02 16:36:50 -03:00
|
|
|
boundary tests; if it always returns false it will merely slow
|
|
|
|
processing, not cause it to fail.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{push}{str}
|
2005-11-22 15:42:45 -04:00
|
|
|
Push a boundary string. When a decorated version of this boundary
|
|
|
|
is found as an input line, it will be interpreted as a section-divider
|
|
|
|
or end-marker (depending on the decoration, see \rfc{2045}). All subsequent
|
2001-03-08 18:46:41 -04:00
|
|
|
reads will return the empty string to indicate end-of-file, until a
|
|
|
|
call to \method{pop()} removes the boundary a or \method{next()} call
|
|
|
|
reenables it.
|
|
|
|
|
|
|
|
It is possible to push more than one boundary. Encountering the
|
|
|
|
most-recently-pushed boundary will return EOF; encountering any other
|
|
|
|
boundary will raise an error.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{pop}{}
|
2001-03-08 18:46:41 -04:00
|
|
|
Pop a section boundary. This boundary will no longer be interpreted
|
|
|
|
as EOF.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{section_divider}{str}
|
1998-06-28 14:55:53 -03:00
|
|
|
Turn a boundary into a section-divider line. By default, this
|
1999-04-23 11:46:18 -03:00
|
|
|
method prepends \code{'-}\code{-'} (which MIME section boundaries have) but
|
1998-07-02 16:36:50 -03:00
|
|
|
it is declared so it can be overridden in derived classes. This
|
|
|
|
method need not append LF or CR-LF, as comparison with the result
|
|
|
|
ignores trailing whitespace.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{methoddesc}[MultiFile]{end_marker}{str}
|
1998-06-28 14:55:53 -03:00
|
|
|
Turn a boundary string into an end-marker line. By default, this
|
1999-04-23 11:46:18 -03:00
|
|
|
method prepends \code{'-}\code{-'} and appends \code{'-}\code{-'} (like a
|
1998-07-02 16:36:50 -03:00
|
|
|
MIME-multipart end-of-message marker) but it is declared so it can be
|
2003-09-22 12:00:55 -03:00
|
|
|
overridden in derived classes. This method need not append LF or
|
1998-07-02 16:36:50 -03:00
|
|
|
CR-LF, as comparison with the result ignores trailing whitespace.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
Finally, \class{MultiFile} instances have two public instance variables:
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{memberdesc}[MultiFile]{level}
|
1998-07-02 16:36:50 -03:00
|
|
|
Nesting depth of the current part.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{memberdesc}
|
|
|
|
|
2007-04-01 19:39:10 -03:00
|
|
|
\begin{memberdesc}[MultiFile]{last}
|
1998-07-02 16:36:50 -03:00
|
|
|
True if the last end-of-file was for an end-of-message marker.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{memberdesc}
|
|
|
|
|
1998-07-02 16:36:50 -03:00
|
|
|
|
1998-08-07 12:55:14 -03:00
|
|
|
\subsection{\class{MultiFile} Example \label{multifile-example}}
|
2000-04-08 01:53:29 -03:00
|
|
|
\sectionauthor{Skip Montanaro}{skip@mojam.com}
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
\begin{verbatim}
|
2000-04-07 13:09:59 -03:00
|
|
|
import mimetools
|
2000-09-30 14:04:40 -03:00
|
|
|
import multifile
|
2000-04-07 13:09:59 -03:00
|
|
|
import StringIO
|
|
|
|
|
|
|
|
def extract_mime_part_matching(stream, mimetype):
|
|
|
|
"""Return the first element in a multipart MIME message on stream
|
|
|
|
matching mimetype."""
|
|
|
|
|
|
|
|
msg = mimetools.Message(stream)
|
|
|
|
msgtype = msg.gettype()
|
|
|
|
params = msg.getplist()
|
|
|
|
|
|
|
|
data = StringIO.StringIO()
|
|
|
|
if msgtype[:10] == "multipart/":
|
|
|
|
|
|
|
|
file = multifile.MultiFile(stream)
|
|
|
|
file.push(msg.getparam("boundary"))
|
|
|
|
while file.next():
|
|
|
|
submsg = mimetools.Message(file)
|
|
|
|
try:
|
|
|
|
data = StringIO.StringIO()
|
|
|
|
mimetools.decode(file, data, submsg.getencoding())
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
if submsg.gettype() == mimetype:
|
|
|
|
break
|
|
|
|
file.pop()
|
|
|
|
return data.getvalue()
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{verbatim}
|