blob: d3fb862779ff3e8508639925ffcf3755f0a93228 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{multifile} ---
2 Support for reading files which contain distinct parts.}
Fred Draked795c5c1998-08-07 15:55:14 +00003\declaremodule{standard}{multifile}
Fred Drake295da241998-08-10 19:42:37 +00004\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00005
Fred Draked795c5c1998-08-07 15:55:14 +00006\modulesynopsis{Support for reading files which contain distinct
7parts, such as some MIME data.}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Guido van Rossum8668e8e1998-06-28 17:55:53 +00009
Fred Drake1717ba41998-07-02 19:36:50 +000010The \class{MultiFile} object enables you to treat sections of a text
11file as file-like input objects, with \code{''} being returned by
12\method{readline()} when a given delimiter pattern is encountered. The
Guido van Rossum8668e8e1998-06-28 17:55:53 +000013defaults of this class are designed to make it useful for parsing
14MIME multipart messages, but by subclassing it and overriding methods
15it can be easily adapted for more general use.
16
Fred Drake1717ba41998-07-02 19:36:50 +000017\begin{classdesc}{MultiFile}{fp\optional{, seekable}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000018Create a multi-file. You must instantiate this class with an input
Fred Drake1717ba41998-07-02 19:36:50 +000019object argument for the \class{MultiFile} instance to get lines from,
20such as as a file object returned by \function{open()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000021
Fred Drake1717ba41998-07-02 19:36:50 +000022\class{MultiFile} only ever looks at the input object's
23\method{readline()}, \method{seek()} and \method{tell()} methods, and
24the latter two are only needed if you want random access to the
25individual MIME parts. To use \class{MultiFile} on a non-seekable
26stream object, set the optional \var{seekable} argument to false; this
27will prevent using the input object's \method{seek()} and
28\method{tell()} methods.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000029\end{classdesc}
30
Fred Drake1717ba41998-07-02 19:36:50 +000031It will be useful to know that in \class{MultiFile}'s view of the world, text
Guido van Rossum8668e8e1998-06-28 17:55:53 +000032is composed of three kinds of lines: data, section-dividers, and
33end-markers. MultiFile is designed to support parsing of
34messages that may have multiple nested message parts, each with its
35own pattern for section-divider and end-marker lines.
36
Fred Draked795c5c1998-08-07 15:55:14 +000037
38\subsection{MultiFile Objects \label{MultiFile-objects}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000039
40A \class{MultiFile} instance has the following methods:
41
42\begin{methoddesc}{push}{str}
43Push a boundary string. When an appropriately decorated version of
44this boundary is found as an input line, it will be interpreted as a
Fred Drake1717ba41998-07-02 19:36:50 +000045section-divider or end-marker. All subsequent
46reads will return the empty string to indicate end-of-file, until a
47call to \method{pop()} removes the boundary a or \method{next()} call
48reenables it.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000049
50It is possible to push more than one boundary. Encountering the
51most-recently-pushed boundary will return EOF; encountering any other
52boundary will raise an error.
53\end{methoddesc}
54
55\begin{methoddesc}{readline}{str}
56Read a line. If the line is data (not a section-divider or end-marker
57or real EOF) return it. If the line matches the most-recently-stacked
Guido van Rossum8ec619f1998-06-30 16:35:25 +000058boundary, return \code{''} and set \code{self.last} to 1 or 0 according as
Guido van Rossum8668e8e1998-06-28 17:55:53 +000059the match is or is not an end-marker. If the line matches any other
Fred Drake1717ba41998-07-02 19:36:50 +000060stacked boundary, raise an error. On encountering end-of-file on the
61underlying stream object, the method raises \exception{Error} unless
62all boundaries have been popped.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000063\end{methoddesc}
64
65\begin{methoddesc}{readlines}{str}
Fred Drake1717ba41998-07-02 19:36:50 +000066Return all lines remaining in this part as a list of strings.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000067\end{methoddesc}
68
Fred Drake1717ba41998-07-02 19:36:50 +000069\begin{methoddesc}{read}{}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000070Read all lines, up to the next section. Return them as a single
71(multiline) string. Note that this doesn't take a size argument!
72\end{methoddesc}
73
Fred Drake1717ba41998-07-02 19:36:50 +000074\begin{methoddesc}{next}{}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000075Skip lines to the next section (that is, read lines until a
Fred Drake1717ba41998-07-02 19:36:50 +000076section-divider or end-marker has been consumed). Return true if
77there is such a section, false if an end-marker is seen. Re-enable
78the most-recently-pushed boundary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000079\end{methoddesc}
80
Fred Drake1717ba41998-07-02 19:36:50 +000081\begin{methoddesc}{pop}{}
82Pop a section boundary. This boundary will no longer be interpreted
83as EOF.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000084\end{methoddesc}
85
Fred Drake1717ba41998-07-02 19:36:50 +000086\begin{methoddesc}{seek}{pos\optional{, whence}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000087Seek. Seek indices are relative to the start of the current section.
Fred Drake1717ba41998-07-02 19:36:50 +000088The \var{pos} and \var{whence} arguments are interpreted as for a file
89seek.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000090\end{methoddesc}
91
Fred Drake1717ba41998-07-02 19:36:50 +000092\begin{methoddesc}{tell}{}
93Return the file position relative to the start of the current section.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000094\end{methoddesc}
95
96\begin{methoddesc}{is_data}{str}
Fred Drake1717ba41998-07-02 19:36:50 +000097Return true if \var{str} is data and false if it might be a section
98boundary. As written, it tests for a prefix other than \code{'--'} at
99start of line (which all MIME boundaries have) but it is declared so
100it can be overridden in derived classes.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000101
102Note that this test is used intended as a fast guard for the real
Fred Drake1717ba41998-07-02 19:36:50 +0000103boundary tests; if it always returns false it will merely slow
104processing, not cause it to fail.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000105\end{methoddesc}
106
107\begin{methoddesc}{section_divider}{str}
108Turn a boundary into a section-divider line. By default, this
Fred Drake1717ba41998-07-02 19:36:50 +0000109method prepends \code{'--'} (which MIME section boundaries have) but
110it is declared so it can be overridden in derived classes. This
111method need not append LF or CR-LF, as comparison with the result
112ignores trailing whitespace.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000113\end{methoddesc}
114
115\begin{methoddesc}{end_marker}{str}
116Turn a boundary string into an end-marker line. By default, this
Fred Drake1717ba41998-07-02 19:36:50 +0000117method prepends \code{'--'} and appends \code{'--'} (like a
118MIME-multipart end-of-message marker) but it is declared so it can be
119be overridden in derived classes. This method need not append LF or
120CR-LF, as comparison with the result ignores trailing whitespace.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000121\end{methoddesc}
122
123Finally, \class{MultiFile} instances have two public instance variables:
124
125\begin{memberdesc}{level}
Fred Drake1717ba41998-07-02 19:36:50 +0000126Nesting depth of the current part.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000127\end{memberdesc}
128
129\begin{memberdesc}{last}
Fred Drake1717ba41998-07-02 19:36:50 +0000130True if the last end-of-file was for an end-of-message marker.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000131\end{memberdesc}
132
Fred Drake1717ba41998-07-02 19:36:50 +0000133
Fred Draked795c5c1998-08-07 15:55:14 +0000134\subsection{\class{MultiFile} Example \label{multifile-example}}
Fred Drake1717ba41998-07-02 19:36:50 +0000135
136% This is almost unreadable; should be re-written when someone gets time.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000137
138\begin{verbatim}
Fred Drake1717ba41998-07-02 19:36:50 +0000139fp = MultiFile(sys.stdin, 0)
140fp.push(outer_boundary)
141message1 = fp.readlines()
142# We should now be either at real EOF or stopped on a message
143# boundary. Re-enable the outer boundary.
144fp.next()
145# Read another message with the same delimiter
146message2 = fp.readlines()
147# Re-enable that delimiter again
148fp.next()
149# Now look for a message subpart with a different boundary
150fp.push(inner_boundary)
151sub_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
154fp.next()
155sub_body = fp.readlines()
156# Got it. Now pop the inner boundary to re-enable the outer one.
157fp.pop()
158# Read to next outer boundary
159message3 = fp.readlines()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000160\end{verbatim}