ยปCore Development>Code coverage>Lib/email/parser.py

Python code coverage for Lib/email/parser.py

#countcontent
1n/a# Copyright (C) 2001-2007 Python Software Foundation
2n/a# Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
3n/a# Contact: email-sig@python.org
4n/a
5n/a"""A parser of RFC 2822 and MIME email messages."""
6n/a
7n/a__all__ = ['Parser', 'HeaderParser', 'BytesParser', 'BytesHeaderParser',
8n/a 'FeedParser', 'BytesFeedParser']
9n/a
10n/afrom io import StringIO, TextIOWrapper
11n/a
12n/afrom email.feedparser import FeedParser, BytesFeedParser
13n/afrom email._policybase import compat32
14n/a
15n/a
16n/a
17n/aclass Parser:
18n/a def __init__(self, _class=None, *, policy=compat32):
19n/a """Parser of RFC 2822 and MIME email messages.
20n/a
21n/a Creates an in-memory object tree representing the email message, which
22n/a can then be manipulated and turned over to a Generator to return the
23n/a textual representation of the message.
24n/a
25n/a The string must be formatted as a block of RFC 2822 headers and header
26n/a continuation lines, optionally preceded by a `Unix-from' header. The
27n/a header block is terminated either by the end of the string or by a
28n/a blank line.
29n/a
30n/a _class is the class to instantiate for new message objects when they
31n/a must be created. This class must have a constructor that can take
32n/a zero arguments. Default is Message.Message.
33n/a
34n/a The policy keyword specifies a policy object that controls a number of
35n/a aspects of the parser's operation. The default policy maintains
36n/a backward compatibility.
37n/a
38n/a """
39n/a self._class = _class
40n/a self.policy = policy
41n/a
42n/a def parse(self, fp, headersonly=False):
43n/a """Create a message structure from the data in a file.
44n/a
45n/a Reads all the data from the file and returns the root of the message
46n/a structure. Optional headersonly is a flag specifying whether to stop
47n/a parsing after reading the headers or not. The default is False,
48n/a meaning it parses the entire contents of the file.
49n/a """
50n/a feedparser = FeedParser(self._class, policy=self.policy)
51n/a if headersonly:
52n/a feedparser._set_headersonly()
53n/a while True:
54n/a data = fp.read(8192)
55n/a if not data:
56n/a break
57n/a feedparser.feed(data)
58n/a return feedparser.close()
59n/a
60n/a def parsestr(self, text, headersonly=False):
61n/a """Create a message structure from a string.
62n/a
63n/a Returns the root of the message structure. Optional headersonly is a
64n/a flag specifying whether to stop parsing after reading the headers or
65n/a not. The default is False, meaning it parses the entire contents of
66n/a the file.
67n/a """
68n/a return self.parse(StringIO(text), headersonly=headersonly)
69n/a
70n/a
71n/a
72n/aclass HeaderParser(Parser):
73n/a def parse(self, fp, headersonly=True):
74n/a return Parser.parse(self, fp, True)
75n/a
76n/a def parsestr(self, text, headersonly=True):
77n/a return Parser.parsestr(self, text, True)
78n/a
79n/a
80n/aclass BytesParser:
81n/a
82n/a def __init__(self, *args, **kw):
83n/a """Parser of binary RFC 2822 and MIME email messages.
84n/a
85n/a Creates an in-memory object tree representing the email message, which
86n/a can then be manipulated and turned over to a Generator to return the
87n/a textual representation of the message.
88n/a
89n/a The input must be formatted as a block of RFC 2822 headers and header
90n/a continuation lines, optionally preceded by a `Unix-from' header. The
91n/a header block is terminated either by the end of the input or by a
92n/a blank line.
93n/a
94n/a _class is the class to instantiate for new message objects when they
95n/a must be created. This class must have a constructor that can take
96n/a zero arguments. Default is Message.Message.
97n/a """
98n/a self.parser = Parser(*args, **kw)
99n/a
100n/a def parse(self, fp, headersonly=False):
101n/a """Create a message structure from the data in a binary file.
102n/a
103n/a Reads all the data from the file and returns the root of the message
104n/a structure. Optional headersonly is a flag specifying whether to stop
105n/a parsing after reading the headers or not. The default is False,
106n/a meaning it parses the entire contents of the file.
107n/a """
108n/a fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
109n/a try:
110n/a return self.parser.parse(fp, headersonly)
111n/a finally:
112n/a fp.detach()
113n/a
114n/a
115n/a def parsebytes(self, text, headersonly=False):
116n/a """Create a message structure from a byte string.
117n/a
118n/a Returns the root of the message structure. Optional headersonly is a
119n/a flag specifying whether to stop parsing after reading the headers or
120n/a not. The default is False, meaning it parses the entire contents of
121n/a the file.
122n/a """
123n/a text = text.decode('ASCII', errors='surrogateescape')
124n/a return self.parser.parsestr(text, headersonly)
125n/a
126n/a
127n/aclass BytesHeaderParser(BytesParser):
128n/a def parse(self, fp, headersonly=True):
129n/a return BytesParser.parse(self, fp, headersonly=True)
130n/a
131n/a def parsebytes(self, text, headersonly=True):
132n/a return BytesParser.parsebytes(self, text, headersonly=True)