| 14 | |
| 15 | |
| 16 | class Parser: |
| 17 | def __init__(self, _class=None, *, policy=compat32): |
| 18 | """Parser of RFC 5322 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 5322 headers and header |
| 25 | continuation lines, optionally preceded 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. |
| 32 | |
| 33 | The policy keyword specifies a policy object that controls a number of |
| 34 | aspects of the parser's operation. The default policy maintains |
| 35 | backward compatibility. |
| 36 | |
| 37 | """ |
| 38 | self._class = _class |
| 39 | self.policy = policy |
| 40 | |
| 41 | def parse(self, fp, headersonly=False): |
| 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 | """ |
| 49 | feedparser = FeedParser(self._class, policy=self.policy) |
| 50 | if headersonly: |
| 51 | feedparser._set_headersonly() |
| 52 | while data := fp.read(8192): |
| 53 | feedparser.feed(data) |
| 54 | return feedparser.close() |
| 55 | |
| 56 | def parsestr(self, text, headersonly=False): |
| 57 | """Create a message structure from a string. |
| 58 | |
| 59 | Returns the root of the message structure. Optional headersonly is a |
| 60 | flag specifying whether to stop parsing after reading the headers or |
| 61 | not. The default is False, meaning it parses the entire contents of |
| 62 | the file. |
| 63 | """ |
| 64 | return self.parse(StringIO(text), headersonly=headersonly) |
| 65 | |
| 66 | |
| 67 | class HeaderParser(Parser): |
no outgoing calls
searching dependent graphs…