Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2004 Python Software Foundation |
| 2 | # Author: Barry Warsaw, Thomas Wouters, Anthony Baxter |
| 3 | # Contact: email-sig@python.org |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 5 | """A parser of RFC 2822 and MIME email messages.""" |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 6 | |
Barry Warsaw | 7e21b67 | 2002-05-19 23:51:50 +0000 | [diff] [blame] | 7 | import re |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 8 | from cStringIO import StringIO |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 9 | from email.FeedParser import FeedParser |
| 10 | from email.Message import Message |
Barry Warsaw | e03e8f0 | 2002-09-28 20:44:58 +0000 | [diff] [blame] | 11 | |
Barry Warsaw | 0e4570b | 2003-03-06 05:25:35 +0000 | [diff] [blame] | 12 | NLCRE = re.compile('\r\n|\r|\n') |
Barry Warsaw | 487fe6a | 2002-10-07 17:27:35 +0000 | [diff] [blame] | 13 | |
Barry Warsaw | e03e8f0 | 2002-09-28 20:44:58 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 15 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 16 | class Parser: |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 17 | def __init__(self, _class=Message, strict=False): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 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 preceeded 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. |
Barry Warsaw | f6caeba | 2002-07-09 02:50:02 +0000 | [diff] [blame] | 32 | |
| 33 | Optional strict tells the parser to be strictly RFC compliant or to be |
| 34 | more forgiving in parsing of ill-formatted MIME documents. When |
| 35 | non-strict mode is used, the parser will try to make up for missing or |
| 36 | erroneous boundaries and other peculiarities seen in the wild. |
Barry Warsaw | bb26b45 | 2002-07-19 22:25:34 +0000 | [diff] [blame] | 37 | Default is non-strict parsing. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 38 | """ |
| 39 | self._class = _class |
| 40 | |
Barry Warsaw | e03e8f0 | 2002-09-28 20:44:58 +0000 | [diff] [blame] | 41 | def parse(self, fp, headersonly=False): |
Barry Warsaw | 057b842 | 2002-09-30 20:07:22 +0000 | [diff] [blame] | 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 | """ |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 49 | feedparser = FeedParser(self._class) |
| 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() |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 58 | |
Barry Warsaw | e03e8f0 | 2002-09-28 20:44:58 +0000 | [diff] [blame] | 59 | def parsestr(self, text, headersonly=False): |
Barry Warsaw | 057b842 | 2002-09-30 20:07:22 +0000 | [diff] [blame] | 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 | """ |
Barry Warsaw | f6caeba | 2002-07-09 02:50:02 +0000 | [diff] [blame] | 67 | return self.parse(StringIO(text), headersonly=headersonly) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 68 | |
Barry Warsaw | e552882 | 2001-10-11 15:43:00 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | class HeaderParser(Parser): |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 72 | def parse(self, fp, headersonly=True): |
| 73 | return Parser.parse(self, fp, True) |
Barry Warsaw | e552882 | 2001-10-11 15:43:00 +0000 | [diff] [blame] | 74 | |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 75 | def parsestr(self, text, headersonly=True): |
| 76 | return Parser.parsestr(self, text, True) |