Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{multifile} --- |
Fred Drake | 812860e | 1999-04-23 14:46:18 +0000 | [diff] [blame] | 2 | Support for files containing distinct parts} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 812860e | 1999-04-23 14:46:18 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{multifile} |
Fred Drake | d795c5c | 1998-08-07 15:55:14 +0000 | [diff] [blame] | 5 | \modulesynopsis{Support for reading files which contain distinct |
Fred Drake | 812860e | 1999-04-23 14:46:18 +0000 | [diff] [blame] | 6 | parts, such as some MIME data.} |
| 7 | \sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 9 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 10 | The \class{MultiFile} object enables you to treat sections of a text |
| 11 | file as file-like input objects, with \code{''} being returned by |
| 12 | \method{readline()} when a given delimiter pattern is encountered. The |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 13 | defaults of this class are designed to make it useful for parsing |
| 14 | MIME multipart messages, but by subclassing it and overriding methods |
| 15 | it can be easily adapted for more general use. |
| 16 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 17 | \begin{classdesc}{MultiFile}{fp\optional{, seekable}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 18 | Create a multi-file. You must instantiate this class with an input |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 19 | object argument for the \class{MultiFile} instance to get lines from, |
| 20 | such as as a file object returned by \function{open()}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 21 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 22 | \class{MultiFile} only ever looks at the input object's |
| 23 | \method{readline()}, \method{seek()} and \method{tell()} methods, and |
| 24 | the latter two are only needed if you want random access to the |
| 25 | individual MIME parts. To use \class{MultiFile} on a non-seekable |
| 26 | stream object, set the optional \var{seekable} argument to false; this |
| 27 | will prevent using the input object's \method{seek()} and |
| 28 | \method{tell()} methods. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 29 | \end{classdesc} |
| 30 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 31 | It will be useful to know that in \class{MultiFile}'s view of the world, text |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 32 | is composed of three kinds of lines: data, section-dividers, and |
| 33 | end-markers. MultiFile is designed to support parsing of |
| 34 | messages that may have multiple nested message parts, each with its |
| 35 | own pattern for section-divider and end-marker lines. |
| 36 | |
Fred Drake | d795c5c | 1998-08-07 15:55:14 +0000 | [diff] [blame] | 37 | |
| 38 | \subsection{MultiFile Objects \label{MultiFile-objects}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 39 | |
| 40 | A \class{MultiFile} instance has the following methods: |
| 41 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 42 | \begin{methoddesc}{readline}{str} |
| 43 | Read a line. If the line is data (not a section-divider or end-marker |
| 44 | or real EOF) return it. If the line matches the most-recently-stacked |
Guido van Rossum | 8ec619f | 1998-06-30 16:35:25 +0000 | [diff] [blame] | 45 | boundary, return \code{''} and set \code{self.last} to 1 or 0 according as |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 46 | the match is or is not an end-marker. If the line matches any other |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 47 | stacked boundary, raise an error. On encountering end-of-file on the |
| 48 | underlying stream object, the method raises \exception{Error} unless |
| 49 | all boundaries have been popped. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 50 | \end{methoddesc} |
| 51 | |
| 52 | \begin{methoddesc}{readlines}{str} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 53 | Return all lines remaining in this part as a list of strings. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 54 | \end{methoddesc} |
| 55 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 56 | \begin{methoddesc}{read}{} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 57 | Read all lines, up to the next section. Return them as a single |
| 58 | (multiline) string. Note that this doesn't take a size argument! |
| 59 | \end{methoddesc} |
| 60 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 61 | \begin{methoddesc}{seek}{pos\optional{, whence}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 62 | Seek. Seek indices are relative to the start of the current section. |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 63 | The \var{pos} and \var{whence} arguments are interpreted as for a file |
| 64 | seek. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 65 | \end{methoddesc} |
| 66 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 67 | \begin{methoddesc}{tell}{} |
| 68 | Return the file position relative to the start of the current section. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 69 | \end{methoddesc} |
| 70 | |
Fred Drake | f0ebbe0 | 2001-03-08 22:46:41 +0000 | [diff] [blame] | 71 | \begin{methoddesc}{next}{} |
| 72 | Skip lines to the next section (that is, read lines until a |
| 73 | section-divider or end-marker has been consumed). Return true if |
| 74 | there is such a section, false if an end-marker is seen. Re-enable |
| 75 | the most-recently-pushed boundary. |
| 76 | \end{methoddesc} |
| 77 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 78 | \begin{methoddesc}{is_data}{str} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 79 | Return true if \var{str} is data and false if it might be a section |
Fred Drake | 812860e | 1999-04-23 14:46:18 +0000 | [diff] [blame] | 80 | boundary. As written, it tests for a prefix other than \code{'-}\code{-'} at |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 81 | start of line (which all MIME boundaries have) but it is declared so |
| 82 | it can be overridden in derived classes. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 83 | |
| 84 | Note that this test is used intended as a fast guard for the real |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 85 | boundary tests; if it always returns false it will merely slow |
| 86 | processing, not cause it to fail. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 87 | \end{methoddesc} |
| 88 | |
Fred Drake | f0ebbe0 | 2001-03-08 22:46:41 +0000 | [diff] [blame] | 89 | \begin{methoddesc}{push}{str} |
| 90 | Push a boundary string. When an appropriately decorated version of |
| 91 | this boundary is found as an input line, it will be interpreted as a |
| 92 | section-divider or end-marker. All subsequent |
| 93 | reads will return the empty string to indicate end-of-file, until a |
| 94 | call to \method{pop()} removes the boundary a or \method{next()} call |
| 95 | reenables it. |
| 96 | |
| 97 | It is possible to push more than one boundary. Encountering the |
| 98 | most-recently-pushed boundary will return EOF; encountering any other |
| 99 | boundary will raise an error. |
| 100 | \end{methoddesc} |
| 101 | |
| 102 | \begin{methoddesc}{pop}{} |
| 103 | Pop a section boundary. This boundary will no longer be interpreted |
| 104 | as EOF. |
| 105 | \end{methoddesc} |
| 106 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 107 | \begin{methoddesc}{section_divider}{str} |
| 108 | Turn a boundary into a section-divider line. By default, this |
Fred Drake | 812860e | 1999-04-23 14:46:18 +0000 | [diff] [blame] | 109 | method prepends \code{'-}\code{-'} (which MIME section boundaries have) but |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 110 | it is declared so it can be overridden in derived classes. This |
| 111 | method need not append LF or CR-LF, as comparison with the result |
| 112 | ignores trailing whitespace. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 113 | \end{methoddesc} |
| 114 | |
| 115 | \begin{methoddesc}{end_marker}{str} |
| 116 | Turn a boundary string into an end-marker line. By default, this |
Fred Drake | 812860e | 1999-04-23 14:46:18 +0000 | [diff] [blame] | 117 | method prepends \code{'-}\code{-'} and appends \code{'-}\code{-'} (like a |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 118 | MIME-multipart end-of-message marker) but it is declared so it can be |
| 119 | be overridden in derived classes. This method need not append LF or |
| 120 | CR-LF, as comparison with the result ignores trailing whitespace. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 121 | \end{methoddesc} |
| 122 | |
| 123 | Finally, \class{MultiFile} instances have two public instance variables: |
| 124 | |
| 125 | \begin{memberdesc}{level} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 126 | Nesting depth of the current part. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 127 | \end{memberdesc} |
| 128 | |
| 129 | \begin{memberdesc}{last} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 130 | True if the last end-of-file was for an end-of-message marker. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 131 | \end{memberdesc} |
| 132 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 133 | |
Fred Drake | d795c5c | 1998-08-07 15:55:14 +0000 | [diff] [blame] | 134 | \subsection{\class{MultiFile} Example \label{multifile-example}} |
Fred Drake | 9164f88 | 2000-04-08 04:53:29 +0000 | [diff] [blame] | 135 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 136 | |
| 137 | \begin{verbatim} |
Fred Drake | c2c46c3 | 2000-04-07 16:09:59 +0000 | [diff] [blame] | 138 | import mimetools |
Martin v. Löwis | d15a942 | 2000-09-30 17:04:40 +0000 | [diff] [blame] | 139 | import multifile |
Fred Drake | c2c46c3 | 2000-04-07 16:09:59 +0000 | [diff] [blame] | 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() |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 166 | \end{verbatim} |