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