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 | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 7 | import warnings |
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 | |
| 12 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 13 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 14 | class Parser: |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 15 | def __init__(self, *args, **kws): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 16 | """Parser of RFC 2822 and MIME email messages. |
| 17 | |
| 18 | Creates an in-memory object tree representing the email message, which |
| 19 | can then be manipulated and turned over to a Generator to return the |
| 20 | textual representation of the message. |
| 21 | |
| 22 | The string must be formatted as a block of RFC 2822 headers and header |
| 23 | continuation lines, optionally preceeded by a `Unix-from' header. The |
| 24 | header block is terminated either by the end of the string or by a |
| 25 | blank line. |
| 26 | |
| 27 | _class is the class to instantiate for new message objects when they |
| 28 | must be created. This class must have a constructor that can take |
| 29 | zero arguments. Default is Message.Message. |
| 30 | """ |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 31 | if len(args) >= 1: |
| 32 | if '_class' in kws: |
| 33 | raise TypeError("Multiple values for keyword arg '_class'") |
| 34 | kws['_class'] = args[0] |
| 35 | if len(args) == 2: |
| 36 | if 'strict' in kws: |
| 37 | raise TypeError("Multiple values for keyword arg 'strict'") |
| 38 | kws['strict'] = args[1] |
| 39 | if len(args) > 2: |
| 40 | raise TypeError('Too many arguments') |
| 41 | if '_class' in kws: |
| 42 | self._class = kws['_class'] |
| 43 | del kws['_class'] |
| 44 | else: |
| 45 | self._class = Message |
| 46 | if 'strict' in kws: |
| 47 | warnings.warn("'strict' argument is deprecated (and ignored)", |
| 48 | DeprecationWarning, 2) |
| 49 | del kws['strict'] |
| 50 | if kws: |
| 51 | raise TypeError('Unexpected keyword arguments') |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 52 | |
Barry Warsaw | e03e8f0 | 2002-09-28 20:44:58 +0000 | [diff] [blame] | 53 | def parse(self, fp, headersonly=False): |
Barry Warsaw | 057b842 | 2002-09-30 20:07:22 +0000 | [diff] [blame] | 54 | """Create a message structure from the data in a file. |
| 55 | |
| 56 | Reads all the data from the file and returns the root of the message |
| 57 | structure. Optional headersonly is a flag specifying whether to stop |
| 58 | parsing after reading the headers or not. The default is False, |
| 59 | meaning it parses the entire contents of the file. |
| 60 | """ |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 61 | feedparser = FeedParser(self._class) |
| 62 | if headersonly: |
| 63 | feedparser._set_headersonly() |
| 64 | while True: |
| 65 | data = fp.read(8192) |
| 66 | if not data: |
| 67 | break |
| 68 | feedparser.feed(data) |
| 69 | return feedparser.close() |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 70 | |
Barry Warsaw | e03e8f0 | 2002-09-28 20:44:58 +0000 | [diff] [blame] | 71 | def parsestr(self, text, headersonly=False): |
Barry Warsaw | 057b842 | 2002-09-30 20:07:22 +0000 | [diff] [blame] | 72 | """Create a message structure from a string. |
| 73 | |
| 74 | Returns the root of the message structure. Optional headersonly is a |
| 75 | flag specifying whether to stop parsing after reading the headers or |
| 76 | not. The default is False, meaning it parses the entire contents of |
| 77 | the file. |
| 78 | """ |
Barry Warsaw | f6caeba | 2002-07-09 02:50:02 +0000 | [diff] [blame] | 79 | return self.parse(StringIO(text), headersonly=headersonly) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 80 | |
Barry Warsaw | e552882 | 2001-10-11 15:43:00 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | class HeaderParser(Parser): |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 84 | def parse(self, fp, headersonly=True): |
| 85 | return Parser.parse(self, fp, True) |
Barry Warsaw | e552882 | 2001-10-11 15:43:00 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | 235c8eb | 2004-05-09 03:46:42 +0000 | [diff] [blame] | 87 | def parsestr(self, text, headersonly=True): |
| 88 | return Parser.parsestr(self, text, True) |