annotate Doc/lib/emailparser.tex @ 22360:c8a6ea00a9e1 legacy-trunk

restructure a bit to not rely on test case execution ordering add test case for bug #482460
author Skip Montanaro <skip@pobox.com>
date Sun, 17 Mar 2002 23:03:42 +0000
parents 52aa3e3e577f
children 13fb4d16f604 d1b6b2ca9d9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 \declaremodule{standard}{email.Parser}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
2 \modulesynopsis{Parse flat text email messages to produce a message
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 object tree.}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
4
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
5 Message object trees can be created in one of two ways: they can be
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
6 created from whole cloth by instantiating \class{Message} objects and
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
7 stringing them together via \method{add_payload()} and
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
8 \method{set_payload()} calls, or they can be created by parsing a flat text
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
9 representation of the email message.
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
10
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
11 The \module{email} package provides a standard parser that understands
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
12 most email document structures, including MIME documents. You can
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
13 pass the parser a string or a file object, and the parser will return
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
14 to you the root \class{Message} instance of the object tree. For
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
15 simple, non-MIME messages the payload of this root object will likely
20459
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
16 be a string containing the text of the message. For MIME
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
17 messages, the root object will return true from its
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
18 \method{is_multipart()} method, and the subparts can be accessed via
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
19 the \method{get_payload()} and \method{walk()} methods.
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
20
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
21 Note that the parser can be extended in limited ways, and of course
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
22 you can implement your own parser completely from scratch. There is
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
23 no magical connection between the \module{email} package's bundled
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
24 parser and the \class{Message} class, so your custom parser can create
22005
52aa3e3e577f Merge trunk.
Greg Ward <gward@python.net>
parents: 20459
diff changeset
25 message object trees any way it finds necessary.
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
26
20374
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
27 The primary parser class is \class{Parser} which parses both the
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
28 headers and the payload of the message. In the case of
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
29 \mimetype{multipart} messages, it will recursively parse the body of
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
30 the container message. The \module{email.Parser} module also provides
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
31 a second class, called \class{HeaderParser} which can be used if
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
32 you're only interested in the headers of the message.
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
33 \class{HeaderParser} can be much faster in this situations, since it
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
34 does not attempt to parse the message body, instead setting the
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
35 payload to the raw body as a string. \class{HeaderParser} has the
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
36 same API as the \class{Parser} class.
94983942c94b Describe the HeaderParser class.
Barry Warsaw <barry@python.org>
parents: 20121
diff changeset
37
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
38 \subsubsection{Parser class API}
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
39
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
40 \begin{classdesc}{Parser}{\optional{_class}}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
41 The constructor for the \class{Parser} class takes a single optional
20459
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
42 argument \var{_class}. This must be a callable factory (such as a
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
43 function or a class), and it is used whenever a sub-message object
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
44 needs to be created. It defaults to \class{Message} (see
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
45 \refmodule{email.Message}). The factory will be called without
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
46 arguments.
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
47 \end{classdesc}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
48
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
49 The other public \class{Parser} methods are:
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
50
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
51 \begin{methoddesc}[Parser]{parse}{fp}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
52 Read all the data from the file-like object \var{fp}, parse the
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
53 resulting text, and return the root message object. \var{fp} must
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
54 support both the \method{readline()} and the \method{read()} methods
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
55 on file-like objects.
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
56
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
57 The text contained in \var{fp} must be formatted as a block of \rfc{2822}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
58 style headers and header continuation lines, optionally preceeded by a
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
59 \emph{Unix-From} header. The header block is terminated either by the
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
60 end of the data or by a blank line. Following the header block is the
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
61 body of the message (which may contain MIME-encoded subparts).
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
62 \end{methoddesc}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
63
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
64 \begin{methoddesc}[Parser]{parsestr}{text}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
65 Similar to the \method{parse()} method, except it takes a string
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
66 object instead of a file-like object. Calling this method on a string
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
67 is exactly equivalent to wrapping \var{text} in a \class{StringIO}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
68 instance first and calling \method{parse()}.
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
69 \end{methoddesc}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
70
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 Since creating a message object tree from a string or a file object is
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
72 such a common task, two functions are provided as a convenience. They
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
73 are available in the top-level \module{email} package namespace.
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
74
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
75 \begin{funcdesc}{message_from_string}{s\optional{, _class}}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
76 Return a message object tree from a string. This is exactly
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
77 equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} is
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
78 interpreted as with the \class{Parser} class constructor.
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
79 \end{funcdesc}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
80
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
81 \begin{funcdesc}{message_from_file}{fp\optional{, _class}}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
82 Return a message object tree from an open file object. This is exactly
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
83 equivalent to \code{Parser().parse(fp)}. Optional \var{_class} is
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
84 interpreted as with the \class{Parser} class constructor.
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
85 \end{funcdesc}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
86
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
87 Here's an example of how you might use this at an interactive Python
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
88 prompt:
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
89
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
90 \begin{verbatim}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
91 >>> import email
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
92 >>> msg = email.message_from_string(myString)
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 \end{verbatim}
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
94
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
95 \subsubsection{Additional notes}
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
96
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
97 Here are some notes on the parsing semantics:
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
98
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
99 \begin{itemize}
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
100 \item Most non-\mimetype{multipart} type messages are parsed as a single
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
101 message object with a string payload. These objects will return
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
102 0 for \method{is_multipart()}.
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
103 \item One exception is for \mimetype{message/delivery-status} type
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
104 messages. Because the body of such messages consist of
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
105 blocks of headers, \class{Parser} will create a non-multipart
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
106 object containing non-multipart subobjects for each header
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
107 block.
20459
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
108 \item Another exception is for \mimetype{message/*} types (more
20121
f822b356c3d9 Updates do email package documentation for markup, style, and
Barry Warsaw <barry@python.org>
parents: 20098
diff changeset
109 general than \mimetype{message/delivery-status}). These are
20459
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
110 typically \mimetype{message/rfc822} messages, represented as a
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
111 non-multipart object containing a singleton payload which is
c68d1855daa9 Fix a few usage and style-guide conformance issues.
Fred Drake <fdrake@acm.org>
parents: 20374
diff changeset
112 another non-multipart \class{Message} instance.
20098
56bdd90c9fd6 The email package documentation, currently organized the way I think
Barry Warsaw <barry@python.org>
parents:
diff changeset
113 \end{itemize}