Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / node584.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node585.html" />
<link rel="prev" href="node583.html" />
<link rel="parent" href="module-email.Parser.html" />
<link rel="next" href="node585.html" />
<meta name='aesop' content='information' />
<title>12.2.2.2 Parser class API</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.2.2.1 FeedParser API"
href="node583.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="12.2.2 Parsing email messages"
href="module-email.Parser.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="12.2.2.3 Additional notes"
href="node585.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node583.html">12.2.2.1 FeedParser API</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.Parser.html">12.2.2 Parsing email messages</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node585.html">12.2.2.3 Additional notes</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION0014222000000000000000">
12.2.2.2 Parser class API</A>
</H3>
<P>
The <tt class="class">Parser</tt> provides an API that can be used to parse a message when
the complete contents of the message are available in a string or file. The
<tt class="module">email.Parser</tt> module also provides a second class, called
<tt class="class">HeaderParser</tt> which can be used if you're only interested in
the headers of the message. <tt class="class">HeaderParser</tt> can be much faster in
these situations, since it does not attempt to parse the message body,
instead setting the payload to the raw body as a string.
<tt class="class">HeaderParser</tt> has the same API as the <tt class="class">Parser</tt> class.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-3879' xml:id='l2h-3879' class="class">Parser</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>_class</var><big>[</big><var>, strict</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
The constructor for the <tt class="class">Parser</tt> class takes an optional
argument <var>_class</var>. This must be a callable factory (such as a
function or a class), and it is used whenever a sub-message object
needs to be created. It defaults to <tt class="class">Message</tt> (see
<tt class="module"><a href="module-email.Message.html">email.Message</a></tt>). The factory will be called without
arguments.
<P>
The optional <var>strict</var> flag is ignored. <div class="versionnote"><b>Deprecated since release 2.4.</b>
Because the
<tt class="class">Parser</tt> class is a backward compatible API wrapper around the
new-in-Python 2.4 <tt class="class">FeedParser</tt>, <em>all</em> parsing is effectively
non-strict. You should simply stop passing a <var>strict</var> flag to the
<tt class="class">Parser</tt> constructor.</div><p></p>
<P>
<span class="versionnote">Changed in version 2.2.2:
The <var>strict</var> flag was added.</span>
<span class="versionnote">Changed in version 2.4:
The <var>strict</var> flag was deprecated.</span>
</dl>
<P>
The other public <tt class="class">Parser</tt> methods are:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3880' xml:id='l2h-3880' class="method">parse</tt></b>(</nobr></td>
<td><var>fp</var><big>[</big><var>, headersonly</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Read all the data from the file-like object <var>fp</var>, parse the
resulting text, and return the root message object. <var>fp</var> must
support both the <tt class="method">readline()</tt> and the <tt class="method">read()</tt> methods
on file-like objects.
<P>
The text contained in <var>fp</var> must be formatted as a block of <a class="rfc" id='rfcref-90842' xml:id='rfcref-90842'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>
style headers and header continuation lines, optionally preceded by a
envelope header. The header block is terminated either by the
end of the data or by a blank line. Following the header block is the
body of the message (which may contain MIME-encoded subparts).
<P>
Optional <var>headersonly</var> is as with the <tt class="method">parse()</tt> method.
<P>
<span class="versionnote">Changed in version 2.2.2:
The <var>headersonly</var> flag was added.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3881' xml:id='l2h-3881' class="method">parsestr</tt></b>(</nobr></td>
<td><var>text</var><big>[</big><var>, headersonly</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Similar to the <tt class="method">parse()</tt> method, except it takes a string
object instead of a file-like object. Calling this method on a string
is exactly equivalent to wrapping <var>text</var> in a <tt class="class">StringIO</tt>
instance first and calling <tt class="method">parse()</tt>.
<P>
Optional <var>headersonly</var> is a flag specifying whether to stop
parsing after reading the headers or not. The default is <code>False</code>,
meaning it parses the entire contents of the file.
<P>
<span class="versionnote">Changed in version 2.2.2:
The <var>headersonly</var> flag was added.</span>
</dl>
<P>
Since creating a message object structure from a string or a file
object is such a common task, two functions are provided as a
convenience. They are available in the top-level <tt class="module">email</tt>
package namespace.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3882' xml:id='l2h-3882' class="function">message_from_string</tt></b>(</nobr></td>
<td><var>s</var><big>[</big><var>, _class</var><big>[</big><var>, strict</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a message object structure from a string. This is exactly
equivalent to <code>Parser().parsestr(s)</code>. Optional <var>_class</var> and
<var>strict</var> are interpreted as with the <tt class="class">Parser</tt> class constructor.
<P>
<span class="versionnote">Changed in version 2.2.2:
The <var>strict</var> flag was added.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3883' xml:id='l2h-3883' class="function">message_from_file</tt></b>(</nobr></td>
<td><var>fp</var><big>[</big><var>, _class</var><big>[</big><var>, strict</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a message object structure tree from an open file object. This
is exactly equivalent to <code>Parser().parse(fp)</code>. Optional
<var>_class</var> and <var>strict</var> are interpreted as with the
<tt class="class">Parser</tt> class constructor.
<P>
<span class="versionnote">Changed in version 2.2.2:
The <var>strict</var> flag was added.</span>
</dl>
<P>
Here's an example of how you might use this at an interactive Python
prompt:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; import email
&gt;&gt;&gt; msg = email.message_from_string(myString)
</pre></div>
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.2.2.1 FeedParser API"
href="node583.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="12.2.2 Parsing email messages"
href="module-email.Parser.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="12.2.2.3 Additional notes"
href="node585.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node583.html">12.2.2.1 FeedParser API</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.Parser.html">12.2.2 Parsing email messages</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node585.html">12.2.2.3 Additional notes</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>