A feed-style parser of email.
| 134 | |
| 135 | |
| 136 | class FeedParser: |
| 137 | """A feed-style parser of email.""" |
| 138 | |
| 139 | def __init__(self, _factory=None, *, policy=compat32): |
| 140 | """_factory is called with no arguments to create a new message obj |
| 141 | |
| 142 | The policy keyword specifies a policy object that controls a number of |
| 143 | aspects of the parser's operation. The default policy maintains |
| 144 | backward compatibility. |
| 145 | |
| 146 | """ |
| 147 | self.policy = policy |
| 148 | self._old_style_factory = False |
| 149 | if _factory is None: |
| 150 | if policy.message_factory is None: |
| 151 | from email.message import Message |
| 152 | self._factory = Message |
| 153 | else: |
| 154 | self._factory = policy.message_factory |
| 155 | else: |
| 156 | self._factory = _factory |
| 157 | try: |
| 158 | _factory(policy=self.policy) |
| 159 | except TypeError: |
| 160 | # Assume this is an old-style factory |
| 161 | self._old_style_factory = True |
| 162 | self._input = BufferedSubFile() |
| 163 | self._msgstack = [] |
| 164 | self._parse = self._parsegen().__next__ |
| 165 | self._cur = None |
| 166 | self._last = None |
| 167 | self._headersonly = False |
| 168 | |
| 169 | # Non-public interface for supporting Parser's headersonly flag |
| 170 | def _set_headersonly(self): |
| 171 | self._headersonly = True |
| 172 | |
| 173 | def feed(self, data): |
| 174 | """Push more data into the parser.""" |
| 175 | self._input.push(data) |
| 176 | self._call_parse() |
| 177 | |
| 178 | def _call_parse(self): |
| 179 | try: |
| 180 | self._parse() |
| 181 | except StopIteration: |
| 182 | pass |
| 183 | |
| 184 | def close(self): |
| 185 | """Parse all remaining data and return the root message object.""" |
| 186 | self._input.close() |
| 187 | self._call_parse() |
| 188 | root = self._pop_message() |
| 189 | assert not self._msgstack |
| 190 | # Look for final set of defects |
| 191 | if root.get_content_maintype() == 'multipart' \ |
| 192 | and not root.is_multipart() and not self._headersonly: |
| 193 | defect = errors.MultipartInvariantViolationDefect() |