| 12 | from email.parser import Parser |
| 13 | try: |
| 14 | from email.feedparser import FeedParser |
| 15 | class ParserClass(Parser): |
| 16 | # Copy of Python's Parser's parse patched to properly deal with CRLF |
| 17 | # sequences that happen to fall at an 8192-byte boundary (see |
| 18 | # http://bugs.python.org/issue1555570) |
| 19 | def parse(self, fp, headersonly=False): |
| 20 | """Create a message structure from the data in a file. |
| 21 | |
| 22 | Reads all the data from the file and returns the root of the message |
| 23 | structure. Optional headersonly is a flag specifying whether to stop |
| 24 | parsing after reading the headers or not. The default is False, |
| 25 | meaning it parses the entire contents of the file. |
| 26 | """ |
| 27 | feedparser = FeedParser(self._class) |
| 28 | if headersonly: |
| 29 | feedparser._set_headersonly() |
| 30 | while True: |
| 31 | data = fp.read(8192) |
| 32 | if not data: |
| 33 | break |
| 34 | if data[-1] == '\r': |
| 35 | # Don't split a CRLF. |
| 36 | data += fp.read(1) |
| 37 | feedparser.feed(data) |
| 38 | return feedparser.close() |
| 39 | except ImportError: |
| 40 | ParserClass = Parser |
| 41 | |