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