B è ;Q·œã@sÌdZdddgZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z d Z d Ze d ¡Ze d ej¡ZGd d„dƒZGdd„deƒZdZGdd„deƒZeeejdƒƒZdeZejZdS)z:Classes to generate plain text from a message object tree.Ú GeneratorÚDecodedGeneratorÚBytesGeneratoréN)Údeepcopy)ÚStringIOÚBytesIO)Ú_has_surrogatesÚ_Ú z \r\n|\r|\nz^From c@s°eZdZdZd'ddœdd„Zdd„Zd(d d „Zd d „Zd d„Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„ZeZdd„Zdd„Zdd „Zd!d"„Zed)d#d$„ƒZed%d&„ƒZdS)*rzŠGenerates output from a Message object tree. This basic generator writes the message to the given file object as plain text. N)ÚpolicycCs6|dkr|dkrdn|j}||_||_||_||_dS)aõCreate the generator for message flattening. outfp is the output file-like object for writing the message to. It must have a write() method. Optional mangle_from_ is a flag that, when True (the default if policy is not set), escapes From_ lines in the body of the message by putting a `>' in front of them. Optional maxheaderlen specifies the longest length for a non-continued header. When a header line is longer (in characters, with tabs expanded to 8 spaces) than maxheaderlen, the header will split as defined in the Header class. Set maxheaderlen to zero to disable header wrapping. The default is 78, as recommended (but not required) by RFC 2822. The policy keyword specifies a policy object that controls a number of aspects of the generator's operation. If no policy is specified, the policy associated with the Message object passed to the flatten method is used. NT)Ú mangle_from_Ú_fpÚ _mangle_from_Ú maxheaderlenr )ÚselfÚoutfpr rr ©rú4/opt/alt/python37/lib64/python3.7/email/generator.pyÚ__init__$s zGenerator.__init__cCs|j |¡dS)N)r Úwrite)rÚsrrrrDszGenerator.writeFcCsØ|jdkr|jn|j}|dk r*|j|d}|jdk rB|j|jd}|j|_| |j¡|_d|_| |j¡|_|j}|j}zL||_||_|r¸|  ¡}|s¨dt   t   ¡¡}|  ||j¡|  |¡Wd||_||_XdS)aÿPrint the message object tree rooted at msg to the output file specified when the Generator instance was created. unixfrom is a flag that forces the printing of a Unix From_ delimiter before the first object in the message tree. If the original message has no From_ delimiter, a `standard' one is crafted. By default, this is False to inhibit the printing of any From_ delimiter. Note that for subobjects, no From_ line is printed. linesep specifies the characters used to indicate a new line in the output. The default value is determined by the policy specified when the Generator instance was created or, if none was specified, from the policy associated with the msg. N)Úlinesep)Úmax_line_lengthÚz From nobody )r ÚclonerrÚ_NLÚ_encodeÚ _encoded_NLZ_EMPTYÚ_encoded_EMPTYZ get_unixfromÚtimeZctimerÚ_write)rÚmsgÚunixfromrr Zold_gen_policyZold_msg_policyZufromrrrÚflattenHs,  zGenerator.flattencCs|j||jd|jdS)z1Clone this generator with the exact same options.N)r )Ú __class__rr )rÚfprrrryszGenerator.clonecCstƒS)N)r)rrrrÚ _new_bufferszGenerator._new_buffercCs|S)Nr)rrrrrr‘szGenerator._encodecCsX|sdSt |¡}x*|dd…D]}| |¡| |j¡q W|drT| |d¡dS)Néÿÿÿÿ)ÚNLCREÚsplitrr)rÚlinesÚlinerrrÚ _write_lines•s  zGenerator._write_linescCs¤|j}z"d|_| ¡|_}| |¡Wd||_|j}|`X|rht|ƒ}| d|d¡| d|d¡t|ddƒ}|dkrˆ| |¡n||ƒ|j |  ¡¡dS)Nzcontent-transfer-encodingrz content-typeéÚ_write_headers) r Ú _munge_cter&Ú _dispatchrZreplace_headerÚgetattrr.rÚgetvalue)rr!ZoldfpZsfpZ munge_cteÚmethrrrr ¥s"   zGenerator._writecCst| ¡}| ¡}t ||f¡ dd¡}t|d|dƒ}|dkrh| dd¡}t|d|dƒ}|dkrh|j}||ƒdS)Nú-r Z_handle_)Úget_content_maintypeÚget_content_subtypeÚ UNDERSCOREÚjoinÚreplacer1Ú _writeBody)rr!ÚmainÚsubZspecificr3Zgenericrrrr0Ès zGenerator._dispatchcCs:x(| ¡D]\}}| |j ||¡¡q W| |j¡dS)N)Ú raw_itemsrr Zfoldr)rr!ÚhÚvrrrr.ÜszGenerator._write_headerscCsž| ¡}|dkrdSt|tƒs.tdt|ƒƒ‚t|jƒr~| d¡}|dk r~t|ƒ}|d=|  ||¡| ¡}|d|df|_ |j rt   d|¡}| |¡dS)Nzstring payload expected: %sÚcharsetzcontent-transfer-encodingz content-typez>From )Ú get_payloadÚ isinstanceÚstrÚ TypeErrorÚtyperÚ_payloadZ get_paramrZ set_payloadr/rÚfcrer<r,)rr!Úpayloadr@rrrÚ _handle_textæs"      zGenerator._handle_textc Cs¢g}| ¡}|dkrg}n(t|tƒr2| |¡dSt|tƒsB|g}x>|D]6}| ¡}| |¡}|j|d|jd|  |  ¡¡qHW|  ¡}|s®|j   |¡}| |¡}| |¡|jdk rê|jrÎt d|j¡} n|j} | | ¡| |j¡| d||j¡|r|j | d¡¡x4|D],} | |jd||j¡|j | ¡qW| |jd|d|j¡|jdk rž|jrŽt d|j¡} n|j} | | ¡dS)NF)r"rz>From z--r)rArBrCrÚlistr&rr#rÚappendr2Z get_boundaryrr8Ú_make_boundaryZ set_boundaryÚpreamblerrGr<r,r ÚpopÚepilogue) rr!ZmsgtextsZsubpartsÚpartrÚgÚboundaryZalltextrMZ body_partrOrrrÚ_handle_multipartþsJ             zGenerator._handle_multipartcCs0|j}|jdd|_z| |¡Wd||_XdS)Nr)r)r rrS)rr!ÚprrrÚ_handle_multipart_signed8s z"Generator._handle_multipart_signedcCsžg}x€| ¡D]t}| ¡}| |¡}|j|d|jd| ¡}| |j¡}|rx|d|jkrx|  |j  |dd…¡¡q|  |¡qW|j   |j  |¡¡dS)NF)r"rr') rAr&rr#rr2r)rrrKr8r r)rr!ZblocksrPrrQÚtextr*rrrÚ_handle_message_delivery_statusCs  z)Generator._handle_message_delivery_statuscCs^| ¡}| |¡}|j}t|tƒrD|j| d¡d|jd| ¡}n |  |¡}|j   |¡dS)NrF)r"r) r&rrFrBrJr#rArr2rr r)rr!rrQrHrrrÚ_handle_messageXs    zGenerator._handle_messagecCszt tj¡}dt|d}|dkr(|S|}d}xD| dt |¡dtj¡}|  |¡sZP|dt |ƒ}|d7}q2W|S)Nz===============z==rz^--z(--)?$Ú.r-) ÚrandomZ randrangeÚsysÚmaxsizeÚ_fmtÚ _compile_reÚreÚescapeÚ MULTILINEÚsearchrC)ÚclsrVÚtokenrRÚbZcounterZcrerrrrLqs   zGenerator._make_boundarycCs t ||¡S)N)r_Úcompile)rcrÚflagsrrrr^ƒszGenerator._compile_re)NN)FN)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr#rr&rr,r r0r.rIr:rSrUrWrXÚ classmethodrLr^rrrrrs* 1# :  csPeZdZdZdd„Zdd„Zdd„Zdd „Z‡fd d „ZeZ e d d „ƒZ ‡Z S)raþGenerates a bytes version of a Message object tree. Functionally identical to the base Generator except that the output is bytes and not string. When surrogates were used in the input to encode bytes, these are decoded back to bytes for output. If the policy has cte_type set to 7bit, then the message is transformed such that the non-ASCII bytes are properly content transfer encoded, using the charset unknown-8bit. The outfp object must accept bytes in its write method. cCs|j | dd¡¡dS)NÚasciiÚsurrogateescape)r rÚencode)rrrrrr•szBytesGenerator.writecCstƒS)N)r)rrrrr&˜szBytesGenerator._new_buffercCs | d¡S)Nrm)ro)rrrrrr›szBytesGenerator._encodecCs<x*| ¡D]\}}|j |j ||¡¡q W| |j¡dS)N)r=r rr Z fold_binaryr)rr!r>r?rrrr.žszBytesGenerator._write_headerscs\|jdkrdSt|jƒrH|jjdksH|jr:t d|j¡|_| |j¡ntt |ƒ  |¡dS)NZ7bitz>From ) rFrr Zcte_typerrGr<r,ÚsuperrrI)rr!)r$rrrI¦s zBytesGenerator._handle_textcCst | d¡|¡S)Nrm)r_rfro)rcrrgrrrr^µszBytesGenerator._compile_re) rhrirjrkrr&rr.rIr:rlr^Ú __classcell__rr)r$rrˆs  zD[Non-text (%(type)s) part of message omitted, filename %(filename)s]c@s(eZdZdZdddœdd„Zdd„ZdS) rz±Generates a text representation of a message. Like the Generator base class, except that non-text parts are substituted with a format string representing the part. N)r cCs.tj|||||d|dkr$t|_n||_dS)aŒLike Generator.__init__() except that an additional optional argument is allowed. Walks through all subparts of a message. If the subpart is of main type `text', then it prints the decoded payload of the subpart. Otherwise, fmt is a format string that is used instead of the message payload. fmt is expanded with the following keywords (in %(keyword)s format): type : Full MIME type of the non-text part maintype : Main MIME type of the non-text part subtype : Sub-MIME type of the non-text part filename : Filename of the non-text part description: Description associated with the non-text part encoding : Content transfer encoding of the non-text part The default value for fmt is None, meaning [Non-text (%(type)s) part of message omitted, filename %(filename)s] )r N)rrÚ_FMTr])rrr rZfmtr rrrrÃs  zDecodedGenerator.__init__c Csˆx‚| ¡D]v}| ¡}|dkr4t|jdd|dq |dkr>q t|j| ¡| ¡| ¡| d¡| dd¡| d d ¡d œ|dq WdS) NrVF)Údecode)ÚfileZ multipartz [no filename]zContent-Descriptionz[no description]zContent-Transfer-Encodingz [no encoding])rEÚmaintypeZsubtypeÚfilenameZ descriptionÚencoding) Úwalkr5ÚprintrAr]Zget_content_typer6Ú get_filenameÚget)rr!rPrurrrr0ás  zDecodedGenerator._dispatch)NNN)rhrirjrkrr0rrrrr½sr-z%%0%dd)rkÚ__all__r_r[rrZÚcopyrÚiorrZ email.utilsrr7ÚNLrfr(rarGrrrrrÚlenÚreprr\Z_widthr]rLrrrrÚs*    p3;