annotate Lib/email/parser.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 9388c671d52d
children 45f6925906bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Copyright (C) 2001-2007 Python Software Foundation
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Contact: email-sig@python.org
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 """A parser of RFC 2822 and MIME email messages."""
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
69334
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
7 __all__ = ['Parser', 'HeaderParser', 'BytesParser', 'BytesHeaderParser']
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import warnings
65180
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
10 from io import StringIO, TextIOWrapper
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 from email.feedparser import FeedParser
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 from email.message import Message
77146
9388c671d52d #14731: refactor email policy framework.
R David Murray <rdmurray@bitdance.com>
parents: 75770
diff changeset
14 from email._policybase import compat32
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 class Parser:
77146
9388c671d52d #14731: refactor email policy framework.
R David Murray <rdmurray@bitdance.com>
parents: 75770
diff changeset
19 def __init__(self, _class=Message, *, policy=compat32):
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 """Parser of RFC 2822 and MIME email messages.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 Creates an in-memory object tree representing the email message, which
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 can then be manipulated and turned over to a Generator to return the
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 textual representation of the message.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 The string must be formatted as a block of RFC 2822 headers and header
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 continuation lines, optionally preceeded by a `Unix-from' header. The
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 header block is terminated either by the end of the string or by a
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 blank line.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 _class is the class to instantiate for new message objects when they
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 must be created. This class must have a constructor that can take
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 zero arguments. Default is Message.Message.
69428
2f3fda9d3906 #11731: simplify/enhance parser/generator API by introducing policy objects.
R David Murray <rdmurray@bitdance.com>
parents: 69334
diff changeset
34
2f3fda9d3906 #11731: simplify/enhance parser/generator API by introducing policy objects.
R David Murray <rdmurray@bitdance.com>
parents: 69334
diff changeset
35 The policy keyword specifies a policy object that controls a number of
2f3fda9d3906 #11731: simplify/enhance parser/generator API by introducing policy objects.
R David Murray <rdmurray@bitdance.com>
parents: 69334
diff changeset
36 aspects of the parser's operation. The default policy maintains
2f3fda9d3906 #11731: simplify/enhance parser/generator API by introducing policy objects.
R David Murray <rdmurray@bitdance.com>
parents: 69334
diff changeset
37 backward compatibility.
2f3fda9d3906 #11731: simplify/enhance parser/generator API by introducing policy objects.
R David Murray <rdmurray@bitdance.com>
parents: 69334
diff changeset
38
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 """
69038
5a71d41ea20a Remove the 'strict' argument to Parser, deprecated since 2.4.
R David Murray <rdmurray@bitdance.com>
parents: 65734
diff changeset
40 self._class = _class
69428
2f3fda9d3906 #11731: simplify/enhance parser/generator API by introducing policy objects.
R David Murray <rdmurray@bitdance.com>
parents: 69334
diff changeset
41 self.policy = policy
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def parse(self, fp, headersonly=False):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 """Create a message structure from the data in a file.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 Reads all the data from the file and returns the root of the message
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 structure. Optional headersonly is a flag specifying whether to stop
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 parsing after reading the headers or not. The default is False,
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 meaning it parses the entire contents of the file.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 """
69428
2f3fda9d3906 #11731: simplify/enhance parser/generator API by introducing policy objects.
R David Murray <rdmurray@bitdance.com>
parents: 69334
diff changeset
51 feedparser = FeedParser(self._class, policy=self.policy)
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 if headersonly:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 feedparser._set_headersonly()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 while True:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 data = fp.read(8192)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 if not data:
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 break
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 43019
diff changeset
58 feedparser.feed(data)
43019
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 return feedparser.close()
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def parsestr(self, text, headersonly=False):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 """Create a message structure from a string.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 Returns the root of the message structure. Optional headersonly is a
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 flag specifying whether to stop parsing after reading the headers or
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 not. The default is False, meaning it parses the entire contents of
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 the file.
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 """
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 return self.parse(StringIO(text), headersonly=headersonly)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 class HeaderParser(Parser):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 def parse(self, fp, headersonly=True):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 return Parser.parse(self, fp, True)
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def parsestr(self, text, headersonly=True):
d5f3c2f416f2 Copying the email package back, despite its failings.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return Parser.parsestr(self, text, True)
65180
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
79
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
80
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
81 class BytesParser:
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
82
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
83 def __init__(self, *args, **kw):
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
84 """Parser of binary RFC 2822 and MIME email messages.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
85
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
86 Creates an in-memory object tree representing the email message, which
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
87 can then be manipulated and turned over to a Generator to return the
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
88 textual representation of the message.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
89
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
90 The input must be formatted as a block of RFC 2822 headers and header
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
91 continuation lines, optionally preceeded by a `Unix-from' header. The
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
92 header block is terminated either by the end of the input or by a
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
93 blank line.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
94
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
95 _class is the class to instantiate for new message objects when they
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
96 must be created. This class must have a constructor that can take
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
97 zero arguments. Default is Message.Message.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
98 """
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
99 self.parser = Parser(*args, **kw)
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
100
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
101 def parse(self, fp, headersonly=False):
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
102 """Create a message structure from the data in a binary file.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
103
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
104 Reads all the data from the file and returns the root of the message
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
105 structure. Optional headersonly is a flag specifying whether to stop
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
106 parsing after reading the headers or not. The default is False,
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
107 meaning it parses the entire contents of the file.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
108 """
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
109 fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
65734
e727cf354720 Properly close a temporary TextIOWrapper in 'email'.
Brett Cannon <bcannon@gmail.com>
parents: 65180
diff changeset
110 with fp:
e727cf354720 Properly close a temporary TextIOWrapper in 'email'.
Brett Cannon <bcannon@gmail.com>
parents: 65180
diff changeset
111 return self.parser.parse(fp, headersonly)
65180
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
112
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
113
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
114 def parsebytes(self, text, headersonly=False):
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
115 """Create a message structure from a byte string.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
116
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
117 Returns the root of the message structure. Optional headersonly is a
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
118 flag specifying whether to stop parsing after reading the headers or
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
119 not. The default is False, meaning it parses the entire contents of
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
120 the file.
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
121 """
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
122 text = text.decode('ASCII', errors='surrogateescape')
1a041f364916 #4661: add bytes parsing and generation to email (email version bump to 5.1.0)
R. David Murray <rdmurray@bitdance.com>
parents: 47635
diff changeset
123 return self.parser.parsestr(text, headersonly)
69334
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
124
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
125
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
126 class BytesHeaderParser(BytesParser):
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
127 def parse(self, fp, headersonly=True):
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
128 return BytesParser.parse(self, fp, headersonly=True)
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
129
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
130 def parsebytes(self, text, headersonly=True):
a95d936ce8eb #11684: Complete parser bytes interface by adding BytesHeaderParser
R David Murray <rdmurray@bitdance.com>
parents: 69038
diff changeset
131 return BytesParser.parsebytes(self, text, headersonly=True)