def convert_string(raw_data, headers=None): if not raw_data: raw_data = '' if not headers: headers = {} data = util.decode_binary(util.decomp_gzip(raw_data)) if "mime-version:" in data[0:4096].lower(): msg = util.message_from_string(data) for (key, val) in headers.items(): _replace_header(msg, key, val) else: mtype = headers.get(CONTENT_TYPE, NOT_MULTIPART_TYPE) maintype, subtype = mtype.split("/", 1) msg = MIMEBase(maintype, subtype, *headers) msg.set_payload(data) return msg
def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE): if not raw_data: raw_data = '' def create_binmsg(data, content_type): maintype, subtype = content_type.split("/", 1) msg = MIMEBase(maintype, subtype) msg.set_payload(data) return msg try: data = util.decode_binary(util.decomp_gzip(raw_data)) if "mime-version:" in data[0:4096].lower(): msg = util.message_from_string(data) else: msg = create_binmsg(data, content_type) except UnicodeDecodeError: msg = create_binmsg(raw_data, content_type) return msg
def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE): if not raw_data: raw_data = "" def create_binmsg(data, content_type): maintype, subtype = content_type.split("/", 1) msg = MIMEBase(maintype, subtype) msg.set_payload(data) return msg try: data = util.decode_binary(util.decomp_gzip(raw_data)) if "mime-version:" in data[0:4096].lower(): msg = util.message_from_string(data) else: msg = create_binmsg(data, content_type) except UnicodeDecodeError: msg = create_binmsg(raw_data, content_type) return msg
def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE): """convert a string (more likely bytes) or a message into a mime message.""" if not raw_data: raw_data = b'' def create_binmsg(data, content_type): maintype, subtype = content_type.split("/", 1) msg = MIMEBase(maintype, subtype) msg.set_payload(data) return msg if isinstance(raw_data, str): bdata = raw_data.encode('utf-8') else: bdata = raw_data bdata = util.decomp_gzip(bdata, decode=False) if b"mime-version:" in bdata[0:4096].lower(): msg = util.message_from_string(bdata.decode('utf-8')) else: msg = create_binmsg(bdata, content_type) return msg
def test_unicode_not_messed_up(self): roundtripped = util.message_from_string(u'\n').as_string() self.assertNotIn('\x00', roundtripped)
def test_unicode_not_messed_up(self): roundtripped = util.message_from_string(u"\n").as_string() self.assertNotIn("\x00", roundtripped)