def _to_stream_when_changed(self, out): ctype = self.content_type if ctype.is_singlepart(): if self._container.body_changed(): charset, encoding, body = encode_body(self) if charset: self.charset = charset self.content_encoding = WithParams(encoding) else: body = self._container.read_body() # RFC allows subparts without headers if self.headers: self.headers.to_stream(out) elif self.is_root(): raise EncodingError("Root message should have headers") out.write(CRLF) out.write(body) else: self.headers.to_stream(out) out.write(CRLF) if ctype.is_multipart(): boundary = ctype.get_boundary_line() for index, part in enumerate(self.parts): out.write((CRLF if index != 0 else "") + boundary + CRLF) part.to_stream(out) out.write(CRLF + ctype.get_boundary_line(final=True) + CRLF) elif ctype.is_message_container(): self.enclosed.to_stream(out)
def to_stream(self, stream): """Takes a stream and serializes headers in a mime format""" for h, v in self.v.iteritems(): try: h = h.encode('ascii') except UnicodeDecodeError: raise EncodingError("Non-ascii header name") stream.write("{0}: {1}\r\n".format(h, to_mime(h, v)))
def to_stream(self, stream, prepends_only=False): """ Takes a stream and serializes headers in a mime format. """ i = 0 for h, v in self.iteritems(raw=True): if prepends_only and i == self.num_prepends: break i += 1 try: h.encode('ascii') except UnicodeDecodeError: raise EncodingError("Non-ascii header name") stream.write("{0}: {1}\r\n".format(h, to_mime(h, v)))