| 73 | |
| 74 | |
| 75 | class BytesParser: |
| 76 | |
| 77 | def __init__(self, *args, **kw): |
| 78 | """Parser of binary RFC 5322 and MIME email messages. |
| 79 | |
| 80 | Creates an in-memory object tree representing the email message, which |
| 81 | can then be manipulated and turned over to a Generator to return the |
| 82 | textual representation of the message. |
| 83 | |
| 84 | The input must be formatted as a block of RFC 5322 headers and header |
| 85 | continuation lines, optionally preceded by a 'Unix-from' header. The |
| 86 | header block is terminated either by the end of the input or by a |
| 87 | blank line. |
| 88 | |
| 89 | _class is the class to instantiate for new message objects when they |
| 90 | must be created. This class must have a constructor that can take |
| 91 | zero arguments. Default is Message.Message. |
| 92 | """ |
| 93 | self.parser = Parser(*args, **kw) |
| 94 | |
| 95 | def parse(self, fp, headersonly=False): |
| 96 | """Create a message structure from the data in a binary file. |
| 97 | |
| 98 | Reads all the data from the file and returns the root of the message |
| 99 | structure. Optional headersonly is a flag specifying whether to stop |
| 100 | parsing after reading the headers or not. The default is False, |
| 101 | meaning it parses the entire contents of the file. |
| 102 | """ |
| 103 | fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape') |
| 104 | try: |
| 105 | return self.parser.parse(fp, headersonly) |
| 106 | finally: |
| 107 | fp.detach() |
| 108 | |
| 109 | |
| 110 | def parsebytes(self, text, headersonly=False): |
| 111 | """Create a message structure from a byte string. |
| 112 | |
| 113 | Returns the root of the message structure. Optional headersonly is a |
| 114 | flag specifying whether to stop parsing after reading the headers or |
| 115 | not. The default is False, meaning it parses the entire contents of |
| 116 | the file. |
| 117 | """ |
| 118 | text = text.decode('ASCII', errors='surrogateescape') |
| 119 | return self.parser.parsestr(text, headersonly) |
| 120 | |
| 121 | |
| 122 | class BytesHeaderParser(BytesParser): |
no outgoing calls
no test coverage detected
searching dependent graphs…