Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 1 | % Documentation by ESR |
| 2 | \section{Standard Module \module{multifile}} |
| 3 | \stmodindex{multiFile} |
| 4 | \label{module-multifile} |
| 5 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 6 | The \class{MultiFile} object enables you to treat sections of a text |
| 7 | file as file-like input objects, with \code{''} being returned by |
| 8 | \method{readline()} when a given delimiter pattern is encountered. The |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 9 | defaults of this class are designed to make it useful for parsing |
| 10 | MIME multipart messages, but by subclassing it and overriding methods |
| 11 | it can be easily adapted for more general use. |
| 12 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 13 | \begin{classdesc}{MultiFile}{fp\optional{, seekable}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 14 | Create a multi-file. You must instantiate this class with an input |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 15 | object argument for the \class{MultiFile} instance to get lines from, |
| 16 | such as as a file object returned by \function{open()}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 17 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 18 | \class{MultiFile} only ever looks at the input object's |
| 19 | \method{readline()}, \method{seek()} and \method{tell()} methods, and |
| 20 | the latter two are only needed if you want random access to the |
| 21 | individual MIME parts. To use \class{MultiFile} on a non-seekable |
| 22 | stream object, set the optional \var{seekable} argument to false; this |
| 23 | will prevent using the input object's \method{seek()} and |
| 24 | \method{tell()} methods. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 25 | \end{classdesc} |
| 26 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 27 | 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] | 28 | is composed of three kinds of lines: data, section-dividers, and |
| 29 | end-markers. MultiFile is designed to support parsing of |
| 30 | messages that may have multiple nested message parts, each with its |
| 31 | own pattern for section-divider and end-marker lines. |
| 32 | |
| 33 | \subsection{MultiFile Objects} |
| 34 | \label{MultiFile-objects} |
| 35 | |
| 36 | A \class{MultiFile} instance has the following methods: |
| 37 | |
| 38 | \begin{methoddesc}{push}{str} |
| 39 | Push a boundary string. When an appropriately decorated version of |
| 40 | this boundary is found as an input line, it will be interpreted as a |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 41 | section-divider or end-marker. All subsequent |
| 42 | reads will return the empty string to indicate end-of-file, until a |
| 43 | call to \method{pop()} removes the boundary a or \method{next()} call |
| 44 | reenables it. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 45 | |
| 46 | It is possible to push more than one boundary. Encountering the |
| 47 | most-recently-pushed boundary will return EOF; encountering any other |
| 48 | boundary will raise an error. |
| 49 | \end{methoddesc} |
| 50 | |
| 51 | \begin{methoddesc}{readline}{str} |
| 52 | Read a line. If the line is data (not a section-divider or end-marker |
| 53 | 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] | 54 | 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] | 55 | 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] | 56 | stacked boundary, raise an error. On encountering end-of-file on the |
| 57 | underlying stream object, the method raises \exception{Error} unless |
| 58 | all boundaries have been popped. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 59 | \end{methoddesc} |
| 60 | |
| 61 | \begin{methoddesc}{readlines}{str} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 62 | 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] | 63 | \end{methoddesc} |
| 64 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 65 | \begin{methoddesc}{read}{} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 66 | Read 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 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 70 | \begin{methoddesc}{next}{} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 71 | Skip lines to the next section (that is, read lines until a |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 72 | section-divider or end-marker has been consumed). Return true if |
| 73 | there is such a section, false if an end-marker is seen. Re-enable |
| 74 | the most-recently-pushed boundary. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 75 | \end{methoddesc} |
| 76 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 77 | \begin{methoddesc}{pop}{} |
| 78 | Pop a section boundary. This boundary will no longer be interpreted |
| 79 | as EOF. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 80 | \end{methoddesc} |
| 81 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 82 | \begin{methoddesc}{seek}{pos\optional{, whence}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 83 | Seek. Seek indices are relative to the start of the current section. |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 84 | The \var{pos} and \var{whence} arguments are interpreted as for a file |
| 85 | seek. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 86 | \end{methoddesc} |
| 87 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 88 | \begin{methoddesc}{tell}{} |
| 89 | 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] | 90 | \end{methoddesc} |
| 91 | |
| 92 | \begin{methoddesc}{is_data}{str} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 93 | Return true if \var{str} is data and false if it might be a section |
| 94 | boundary. As written, it tests for a prefix other than \code{'--'} at |
| 95 | start of line (which all MIME boundaries have) but it is declared so |
| 96 | it can be overridden in derived classes. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 97 | |
| 98 | 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] | 99 | boundary tests; if it always returns false it will merely slow |
| 100 | processing, not cause it to fail. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 101 | \end{methoddesc} |
| 102 | |
| 103 | \begin{methoddesc}{section_divider}{str} |
| 104 | Turn a boundary into a section-divider line. By default, this |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 105 | method prepends \code{'--'} (which MIME section boundaries have) but |
| 106 | it is declared so it can be overridden in derived classes. This |
| 107 | method need not append LF or CR-LF, as comparison with the result |
| 108 | ignores trailing whitespace. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 109 | \end{methoddesc} |
| 110 | |
| 111 | \begin{methoddesc}{end_marker}{str} |
| 112 | Turn a boundary string into an end-marker line. By default, this |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 113 | method prepends \code{'--'} and appends \code{'--'} (like a |
| 114 | MIME-multipart end-of-message marker) but it is declared so it can be |
| 115 | be overridden in derived classes. This method need not append LF or |
| 116 | CR-LF, as comparison with the result ignores trailing whitespace. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 117 | \end{methoddesc} |
| 118 | |
| 119 | Finally, \class{MultiFile} instances have two public instance variables: |
| 120 | |
| 121 | \begin{memberdesc}{level} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 122 | Nesting depth of the current part. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 123 | \end{memberdesc} |
| 124 | |
| 125 | \begin{memberdesc}{last} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 126 | 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] | 127 | \end{memberdesc} |
| 128 | |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 129 | |
| 130 | \subsection{\class{Multifile} Example} |
| 131 | \label{multifile-example} |
| 132 | |
| 133 | % This is almost unreadable; should be re-written when someone gets time. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 134 | |
| 135 | \begin{verbatim} |
Fred Drake | 1717ba4 | 1998-07-02 19:36:50 +0000 | [diff] [blame] | 136 | fp = MultiFile(sys.stdin, 0) |
| 137 | fp.push(outer_boundary) |
| 138 | message1 = fp.readlines() |
| 139 | # We should now be either at real EOF or stopped on a message |
| 140 | # boundary. Re-enable the outer boundary. |
| 141 | fp.next() |
| 142 | # Read another message with the same delimiter |
| 143 | message2 = fp.readlines() |
| 144 | # Re-enable that delimiter again |
| 145 | fp.next() |
| 146 | # Now look for a message subpart with a different boundary |
| 147 | fp.push(inner_boundary) |
| 148 | sub_header = fp.readlines() |
| 149 | # If no exception has been thrown, we're looking at the start of |
| 150 | # the message subpart. Reset and grab the subpart |
| 151 | fp.next() |
| 152 | sub_body = fp.readlines() |
| 153 | # Got it. Now pop the inner boundary to re-enable the outer one. |
| 154 | fp.pop() |
| 155 | # Read to next outer boundary |
| 156 | message3 = fp.readlines() |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 157 | \end{verbatim} |