U ,aN@sdZdddgZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z d Z d Zed Zed ejZGd ddZGdddeZdZGdddeZeeejdZdeZejZdS)z:Classes to generate plain text from a message object tree. GeneratorDecodedGeneratorBytesGeneratorN)deepcopy)StringIOBytesIO)_has_surrogates_ z \r\n|\r|\nz^From c@seZdZdZd'ddddZddZd(d d Zd d Zd dZddZ ddZ ddZ ddZ ddZ ddZeZddZddZdd Zd!d"Zed)d#d$Zed%d&ZdS)*rzGenerates output from a Message object tree. This basic generator writes the message to the given file object as plain text. NpolicycCs6|dkr|dkrdn|j}||_||_||_||_dS)aCreate 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 )selfoutfpr rr r4/opt/alt/python38/lib64/python3.8/email/generator.py__init__$s zGenerator.__init__cCs|j|dSN)rwritersrrrrDszGenerator.writeFcCs|jdkr|jn|j}|dk r*|j|d}|jdk rB|j|jd}|j|_||j|_d|_||j|_|j}|j}zL||_||_|r| }|sdt t }| ||j| |W5||_||_XdS)aPrint 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)linesepZmax_line_lengthz From nobody )r clonerr_NL_encode _encoded_NLZ_EMPTY_encoded_EMPTYZ get_unixfromtimectimer_write)rmsgunixfromrr Zold_gen_policyZold_msg_policyZufromrrrflattenHs,  zGenerator.flattencCs|j||jd|jdS)z1Clone this generator with the exact same options.Nr ) __class__rr )rfprrrrys zGenerator.clonecCstSr)rrrrr _new_bufferszGenerator._new_buffercCs|SrrrrrrrszGenerator._encodecCsT|sdSt|}|ddD]}||||jq|drP||ddS)N)NLCREsplitrr)rlineslinerrr _write_liness  zGenerator._write_linescCs|j}z"d|_||_}||W5||_|j}|`X|rt|}|ddkrd|d|d<n|d|d|d|dt|dd}|dkr||n|||j | dS)Ncontent-transfer-encodingrContent-Transfer-Encoding content-type_write_headers) r _munge_cter+ _dispatchrgetZreplace_headergetattrr6rgetvalue)rr%ZoldfpZ munge_cteZsfpmethrrrr$s&   zGenerator._writecCst|}|}t||fdd}t|d|d}|dkrh|dd}t|d|d}|dkrh|j}||dS)N-r Z_handle_)get_content_maintypeget_content_subtype UNDERSCOREjoinreplacer: _writeBody)rr%mainsubZspecificr<Zgenericrrrr8s zGenerator._dispatchcCs6|D]\}}||j||q||jdSr) raw_itemsrr Zfoldrrr%hvrrrr6szGenerator._write_headerscCs|}|dkrdSt|ts.tdt|t|jr~|d}|dk r~t|}|d=| |||}|d|df|_ |j rt d|}||dS)Nzstring payload expected: %scharsetr2r4>From ) get_payload isinstancestr TypeErrortyper_payloadZ get_paramrZ set_payloadr7rfcrerEr1)rr%payloadrJrrr _handle_texts$     zGenerator._handle_textc Csg}|}|dkrg}n(t|tr2||dSt|tsB|g}|D]6}|}||}|j|d|jd| | qF| }|s|j |}||}|||jdk r|jrtd|j} n|j} || ||j|d||j|r|j|d|D],} ||jd||j|j| q||jd|d|j|jdk r|jrtd|j} n|j} || dS)NFr&rrKz--r)rLrMrNrlistr+rr'rappendr;Z get_boundaryr rA_make_boundaryZ set_boundarypreamblerrRrEr1rpopepilogue) rr%ZmsgtextsZsubpartspartrgboundaryZalltextrYZ body_partr[rrr_handle_multipartsJ           zGenerator._handle_multipartcCs0|j}|jdd|_z||W5||_XdS)Nrr)r rr_)rr%prrr_handle_multipart_signed<s z"Generator._handle_multipart_signedcCsg}|D]t}|}||}|j|d|jd|}||j}|rv|d|jkrv| |j |ddq | |q |j |j |dS)NFrUr,) rLr+rr'rr;r.r r!rWrArr)rr%Zblocksr\rr]textr/rrr_handle_message_delivery_statusGs    z)Generator._handle_message_delivery_statuscCs^|}||}|j}t|trD|j|dd|jd|}n | |}|j |dS)NrFrU) r+rrQrMrVr'rLrr;rrr)rr%rr]rSrrr_handle_message\s    zGenerator._handle_messagecCsvttj}dt|d}|dkr(|S|}d}|dt|dtj}| |sXqr|dt |}|d7}q0|S)Nz===============z==rz^--z(--)?$.r5) randomZ randrangesysmaxsize_fmt _compile_rereescape MULTILINEsearchrN)clsrbtokenr^bZcounterZcrerrrrXus   zGenerator._make_boundarycCs t||Sr)rkcompilerorflagsrrrrjszGenerator._compile_re)NN)FN)N)__name__ __module__ __qualname____doc__rrr'rr+rr1r$r8r6rTrCr_rarcrd classmethodrXrjrrrrrs.   1' :  csPeZdZdZddZddZddZdd Zfd d ZeZ e d d Z Z S)raGenerates 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|dddS)Nasciisurrogateescape)rrencoderrrrrszBytesGenerator.writecCstSr)rr*rrrr+szBytesGenerator._new_buffercCs |dSNrz)r|rrrrrszBytesGenerator._encodecCs8|D]\}}|j|j||q||jdSr)rFrrr Z fold_binaryrrGrrrr6szBytesGenerator._write_headerscs\|jdkrdSt|jrH|jjdksH|jr:td|j|_||jntt | |dS)NZ7bitrK) rQrr Zcte_typerrRrEr1superrrT)rr%r(rrrTs zBytesGenerator._handle_textcCst|d|Sr})rkrrr|rsrrrrjszBytesGenerator._compile_re) rurvrwrxrr+rr6rTrCryrj __classcell__rrrrrs  zD[Non-text (%(type)s) part of message omitted, filename %(filename)s]c@s(eZdZdZdddddZddZdS) rzGenerates 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. Nr cCs.tj|||||d|dkr$t|_n||_dS)aLike 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_FMTri)rrr rZfmtr rrrrs  zDecodedGenerator.__init__c Cs|D]v}|}|dkr2t|jdd|dq|dkrprintrLriZget_content_typer? get_filenamer9)rr%r\rrrrr8s(  zDecodedGenerator._dispatch)NNN)rurvrwrxrr8rrrrrs  r5z%%0%dd)rx__all__rkrgr"rfcopyriorrZ email.utilsrr@NLrrr-rmrRrrrrlenreprrhZ_widthrirXrrrrs*    t3;