我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用quopri.decodestring()。
def decode_param(param): name, v = param.split('=', 1) values = v.split('\n') value_results = [] for value in values: match = re.search(r'=\?((?:\w|-)+)\?(Q|B)\?(.+)\?=', value) if match: encoding, type_, code = match.groups() if type_ == 'Q': value = quopri.decodestring(code) elif type_ == 'B': value = base64.decodestring(code) value = str_encode(value, encoding) value_results.append(value) if value_results: v = ''.join(value_results) logger.debug("Decoded parameter {} - {}".format(name, v)) return name, v
def decode(message_part): content_transfer_encoding = message_part.part["Content-Transfer-Encoding"] content_type = message_part.part["Content-Type"] payload = message_part.part.get_payload() if content_transfer_encoding == "base64": payload = base64.b64decode(payload) elif content_transfer_encoding == "quoted-printable": payload = quopri.decodestring(payload) # payload is already properly decoded, usually happens in plain text emails if isinstance(payload, str): return payload # payload is text, decode with proper charset if is_text(content_type): return payload.decode(get_charset(content_type)) # payload is probably binary, don't do anything else return payload
def decodeFileName(self, name): # http://code.google.com/p/googleappengine/issues/detail?id=2749 # Open since Sept. 2010, claimed to be fixed in Version 1.7.2 (September 18, 2012) # and still totally broken try: if name.startswith("=?"): #RFC 2047 return unicode( email.Header.make_header(email.Header.decode_header(name + "\n"))) elif "=" in name and not name.endswith("="): #Quoted Printable return decodestring(name.encode("ascii")).decode("UTF-8") else: #Maybe base64 encoded return b64decode(name.encode("ascii")).decode("UTF-8") except: #Sorry - I cant guess whats happend here if isinstance(name, str) and not isinstance(name, unicode): try: return name.decode("UTF-8", "ignore") except: pass return name
def decode(input_str): result = '' search_result = re.search('=\?([^\?]*)\?([^\?]*)\?([^\?]*)\?=', input_str) while search_result is not None: charset, tp, text = search_result.groups() s = search_result.start(0) e = search_result.end(0) text = text.encode('cp866', 'ignore').decode('cp866', 'ignore') result += input_str[:s] input_str = input_str[e:].lstrip() if tp.lower() != 'q': result += base64.b64decode(text.encode('cp866')).decode(charset, 'ignore') else: result += quopri.decodestring(text).decode(charset, 'ignore') search_result = re.search('=\?([^\?]*)\?([^\?]*)\?([^\?]*)\?=', input_str) else: result += input_str return result
def decode_transfer_encoding(encoding, body): if encoding == 'base64': return _base64_decode(body) elif encoding == 'quoted-printable': return quopri.decodestring(body) else: return body
def format_mail(loop, msg, to_text=True, ignore_tables=True): """Format the mail to markdown Parameter --------- msg: email.message to_text: bool, optional Convert text/html mails to text/plain with markdown formatting Returns ------- text: str """ h = html2text.HTML2Text() h.ignore_tables = ignore_tables body = None for part in msg.walk(): if to_text and part.get_content_type() == "text/html": body = h.handle(quopri.decodestring(part.get_payload()).decode()) break elif part.get_content_type() == "text/plain": body = quopri.decodestring(part.get_payload()) break if not body: log.error("Could not find text body mail") body = quopri.decodestring(msg.as_string()) text = f"### {msg['Subject']} \n {body}" return text
def test_decodestring(self): for p, e in self.STRINGS: self.assertEqual(quopri.decodestring(e), p)
def test_idempotent_string(self): for p, e in self.STRINGS: self.assertEqual(quopri.decodestring(quopri.encodestring(e)), e)
def test_embedded_ws(self): for p, e in self.ESTRINGS: self.assertEqual(quopri.encodestring(p, quotetabs=True), e) self.assertEqual(quopri.decodestring(e), p)
def test_decode_header(self): for p, e in self.HSTRINGS: self.assertEqual(quopri.decodestring(e, header=True), p)
def test_decodestring(self): for p, e in self.STRINGS: self.assertTrue(quopri.decodestring(e) == p)
def test_idempotent_string(self): for p, e in self.STRINGS: self.assertTrue(quopri.decodestring(quopri.encodestring(e)) == e)
def test_embedded_ws(self): for p, e in self.ESTRINGS: self.assertTrue(quopri.encodestring(p, quotetabs=True) == e) self.assertTrue(quopri.decodestring(e) == p)
def test_decode_header(self): for p, e in self.HSTRINGS: self.assertTrue(quopri.decodestring(e, header=True) == p)
def test_decodestring_double_equals(self): # Issue 21511 - Ensure that byte string is compared to byte string # instead of int byte value decoded_value, encoded_value = (b"123=four", b"123==four") self.assertEqual(quopri.decodestring(encoded_value), decoded_value)
def printBody( self, message, body, cstr ): if message.has_key('Content-Transfer-Encoding') and message.get('Content-Transfer-Encoding')=='base64': try: body = base64.b64decode(body) #cstr.write('decoded base64 successfully' + '\n') except: cstr.write('WARNING - could not decode base64' + '\n') #pj suggested improvement by vragosta to get rid of occasional " =20" at end of lines. #cstr.write(body + '\n') cstr.write(quopri.decodestring(body) + '\n') # ------------------------------------------------- # Get and print to STDOUT the mail message # -------------------------------------------------
def _infer_text_fragment_inner(self, title, body, post_id): # dead code? If not needs to be refactored with langstrings # and moved within text_fragment, maybe? body = sanitize_html(body, []) quote = self.quote.replace("\r", "") try: # for historical reasons quote = quopri.decodestring(quote) except: pass quote = sanitize_html(quote, []) if quote != self.body: self.body = quote quote = quote.replace("\n", "") start = body.find(quote) lookin = 'message-body' if start < 0: xpath = "//div[@id='%s']/div[class='post_title']" % (post_id) start = title.find(quote) if start < 0: return None lookin = 'message-subject' xpath = "//div[@id='message-%s']//div[@class='%s']" % ( Post.uri_generic(post_id), lookin) tfi = self.db.query(TextFragmentIdentifier).filter_by( extract=self).first() if not tfi: tfi = TextFragmentIdentifier(extract=self) tfi.xpath_start = tfi.xpath_end = xpath tfi.offset_start = start tfi.offset_end = start+len(quote) return tfi
def __init__(self, filename): mail = mailbox.mbox(filename, create=False)[0] # Simply name it commit_hash, otherwise we would have to refactor # tons of code. self.commit_hash = mail['Message-ID'] self.mail_subject = mail['Subject'] # we need timezone aware datetimes due to the fact, that most of all # emails contain timezone aware timestamps. There's an issue with # timezone unaware timestamps: they can't be compared to timezone aware # timestamps. To cope with that, we simple shift those mails to UTC # (which is also true in most cases). # # E.g. python converts this timestamp to an timezone unaware one, # while it is GMT: # 'Fri, 23 Feb 2007 13:35:50 -0000 (GMT)' try: date = email.utils.parsedate_to_datetime(mail['Date']) except: # assume epoch log.debug(' Message %s: unable to parse date %s' % (self.commit_hash, mail['Date'])) date = datetime.datetime.utcfromtimestamp(0) if date.tzinfo is None: date = date.replace(tzinfo=datetime.timezone.utc) payload = mail.get_payload() # Check encoding and decode cte = mail['Content-Transfer-Encoding'] if cte == 'QUOTED-PRINTABLE': charset = mail.get_content_charset() if charset not in CHARSETS: charset = 'ascii' payload = quopri.decodestring(payload) payload = payload.decode(charset, errors='ignore') # MAY RAISE AN ERROR, FORBID RETURN NULL msg, diff = parse_payload(payload) # reconstruct commit message subject = self.mail_subject match = PATCH_SUBJECT_REGEX.match(self.mail_subject) if match: subject = match.group(1) msg = [subject, ''] + msg author_name = mail['From'] author_email = '' match = MAIL_FROM_REGEX.match(author_name) if match: author_name = match.group(1) author_email = match.group(2) super(PatchMail, self).__init__(msg, diff, author_name, author_email, date, snip_header=True)
def import_mail(ctx, tag, category): content = "" for l in click.get_text_stream('stdin'): content = content + l msg = email.message_from_string(content) # title subject, encoding = email.header.decode_header(msg['Subject'])[0] if encoding is None: encoding = "utf-8" title = subject.decode(encoding) # content content = msg.get_payload(decode=False) content = quopri.decodestring(content) content = "# " + title + '\n\n' + content date = datetime.datetime.now() coll = db.get_document_collection(ctx) config = ctx.obj["config"] item = { "title": title, "content": content, "tags": list(tag), "categories": list(category), "created": date, "updated": date, "encrypted": False, } # insert item if its valid if validate(item): coll = db.get_document_collection(ctx) docid = coll.insert_one(item).inserted_id transaction.log(ctx, str(docid), "import", title) utils.log_info("Document \"%s\" created." % title) else: utils.log_error("Validation of the updated object did not succeed")