e f@svdZddlZddlZddlZddlZddlmZmZddlm Z ddddd d d d gZ ej ej d j ddZddZGdddeZeZdeedAsrcCs"|jdd}t|gfS)N_ )replace_q_byte_subber)encodedrrrrCsc@s<eZdZdejdejdZddZdS) _QByteMaps-!*+/asciicCs=||jkr"t|||Zszencode_q..)join)bstringrrrrYscCstdd|DS)Ncss|]}tt|VqdS)N)lenr))r*r+rrrr,]szlen_q..)sum)r.rrrr \scCsg}t|d}|rJ|jtj|ddd|}n|}ytj|dd|fSWntjk rtjg}xyd D]e}y%tj|d |dd |fSWqtjk r|dkr|jtjnYqXqWt d YnXdS) Ns===ZvalidateTrr =Fzunexpected binascii.Error)rr r2r3) r/appendrZInvalidBase64PaddingDefectbase64Z b64decodebinasciiErrorZInvalidBase64CharactersDefectAssertionError)rdefectsZpad_errZpadded_encodedirrrrds" % cCstj|jdS)Nr)r6Z b64encoder )r.rrrr~scCs3tt|d\}}|d|r.dndS)Nr3r1r)divmodr/)r.Z groups_of_3Zleftoverrrrr sqbc Cs*|jd\}}}}}|jd\}}}|j}|jdd}t||\}}y|j|}Wntk r|jtj dj ||j|d}YnXt k r|jdd}|jdkr|jtj dj |nYnX||||fS)aDecode encoded word and return (string, charset, lang, defects) tuple. An RFC 2047/2243 encoded word has the form: =?charset*lang?cte?encoded_string?= where '*lang' may be omitted but the other parts may not be. This function expects exactly such a string (that is, it does not check the syntax and may raise errors if the string is not well formed), and returns the encoded_string decoded first from its Content Transfer Encoding and then from the resulting bytes into unicode using the specified charset. If the cte-decoded string does not successfully decode using the specified character set, a defect is added to the defects list and the unknown octets are replaced by the unicode 'unknown' character \uFDFF. The specified charset and language are returned. The default for language, which is rarely if ever encountered, is the empty string. ?*rsurrogateescapez:Encoded word contains bytes not decodable using {} charsetz unknown-8bitzr@z=?{}{}?{}?{}?=)r _cte_encode_length _cte_encodersr)rJrHencodingrIr.ZqlenZblenrrrrr s   )__doc__rer6r7 functoolsrJrrZemailr__all__partialcompilesubrrdictrr)ordrr rrr rEr rMrLr rrrrsH$              *