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