Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/email/parser.py: 24%
41 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
1# Copyright (C) 2001-2007 Python Software Foundation
2# Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
3# Contact: email-sig@python.org
5"""A parser of RFC 2822 and MIME email messages."""
7__all__ = ['Parser', 'HeaderParser', 'BytesParser', 'BytesHeaderParser',
8 'FeedParser', 'BytesFeedParser']
10from io import StringIO, TextIOWrapper
12from email.feedparser import FeedParser, BytesFeedParser
13from email._policybase import compat32
16class Parser:
17 def __init__(self, _class=None, *, policy=compat32):
18 """Parser of RFC 2822 and MIME email messages.
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.
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.
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.
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.
37 """
38 self._class = _class
39 self.policy = policy
41 def parse(self, fp, headersonly=False):
42 """Create a message structure from the data in a file.
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()
59 def parsestr(self, text, headersonly=False):
60 """Create a message structure from a string.
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)
71class HeaderParser(Parser):
72 def parse(self, fp, headersonly=True):
73 return Parser.parse(self, fp, True)
75 def parsestr(self, text, headersonly=True):
76 return Parser.parsestr(self, text, True)
79class BytesParser:
81 def __init__(self, *args, **kw):
82 """Parser of binary RFC 2822 and MIME email messages.
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.
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.
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)
99 def parse(self, fp, headersonly=False):
100 """Create a message structure from the data in a binary file.
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()
114 def parsebytes(self, text, headersonly=False):
115 """Create a message structure from a byte string.
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)
126class BytesHeaderParser(BytesParser):
127 def parse(self, fp, headersonly=True):
128 return BytesParser.parse(self, fp, headersonly=True)
130 def parsebytes(self, text, headersonly=True):
131 return BytesParser.parsebytes(self, text, headersonly=True)