e fM@sdZdddgZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z d Z d Zejd ejZGd ddZGd ddeZdZGdddeZeeejdZdeZejZdS)z:Classes to generate plain text from a message object tree. GeneratorDecodedGeneratorBytesGeneratorN)deepcopy)StringIOBytesIO)_has_surrogates_ z^From c@seZdZdZddddddZddZd dd d Zd d ZdZddZ ddZ ddZ ddZ ddZ ddZddZeZddZdd Zd!d"Zd#d$Zedd%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. TNpolicycCs(||_||_||_||_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), 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. N)_fp _mangle_from_ maxheaderlenr )selfoutfp mangle_from_rr r4/opt/alt/python34/lib64/python3.4/email/generator.py__init__#s   zGenerator.__init__cCs|jj|dS)N)r write)rsrrrr@szGenerator.writeFc Cs?|jdkr|jn|j}|dk rB|jd|}n|jdk ri|jd|j}n|j|_|j|j|_d|_|jd|_|j}|j}zn||_||_|r|j }|sdt j t j }n|j ||jn|j |Wd||_||_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. Nlinesepmax_line_lengthz From nobody )r clonerr_NL_encode _encoded_NLZ_EMPTYZ_encoded_EMTPYZ get_unixfromtimeZctimer_write)rmsgunixfromrr Zold_gen_policyZold_msg_policyZufromrrrflattenDs,!         zGenerator.flattencCs|j||jdd|jS)z1Clone this generator with the exact same options.Nr ) __class__r r )rfprrrrus zGenerator.clonercCstS)N)r)rrrr _new_bufferszGenerator._new_buffercCs|S)Nr)rrrrrrszGenerator._encodecCs|s dS|jd}x>|ddD],}|j|jd|j|jq*W|djd}|j|t|dt|kr|j|jndS)NTz r'r') splitlinesrrstriprlen)rlineslineZ laststrippedrrr _write_liness zGenerator._write_linesc Cs|j}z-d|_|j|_}|j|Wd||_|j}|`X|rt|}|jd|d|jd|dnt|dd}|dkr|j|n |||jj|j dS)Nzcontent-transfer-encodingrz content-typer&_write_headers) r _munge_cter% _dispatchrZreplace_headergetattrr.rgetvalue)rr ZoldfpZsfpZ munge_ctemethrrrrs"       zGenerator._writecCs|j}|j}tj||fjdd}t|d|d}|dkr|jdd}t|d|d}|dkr|j}qn||dS)N-r Z_handle_)get_content_maintypeget_content_subtype UNDERSCOREjoinreplacer1 _writeBody)rr mainsubZspecificr3Zgenericrrrr0s  !  zGenerator._dispatchcCsMx6|jD](\}}|j|jj||q W|j|jdS)N) raw_itemsrr Zfoldr)rr hvrrrr.s zGenerator._write_headerscCs|j}|dkrdSt|tsDtdt|nt|jr|jd}|dk rt|}|d=|j |||j}|d|df|_ qn|j rt j d|}n|j|dS)Nzstring payload expected: %scharsetzcontent-transfer-encodingz content-typez>From ) get_payload isinstancestr TypeErrortyper_payloadZ get_paramrZ set_payloadr/r fcrer<r-)rr payloadr@rrr _handle_texts"      zGenerator._handle_textc CsCg}|j}|dkr'g}n;t|trG|j|dSt|tsb|g}nxX|D]P}|j}|j|}|j|ddd|j|j |j qiW|j }|s|j j |}|j|}|j|n|jdk rY|jr0tjd|j} n |j} |j| |j|jn|jd||j|r|jj|jdnx=|D]5} |j|jd||j|jj| qW|j|jd|d|j|jdk r?|jr&tjd|j} n |j} |j| ndS)Nr!Frz>From z--r)rArBrCrlistr%rr"rappendr2Z get_boundaryrr8_make_boundaryZ set_boundarypreambler rGr<r-r popepilogue) rr ZmsgtextsZsubpartspartrgboundaryZalltextrMZ body_partrOrrr_handle_multipartsJ            #  zGenerator._handle_multipartc Cs@|j}|jdd|_z|j|Wd||_XdS)Nrr)r rrS)rr prrr_handle_multipart_signed5s  z"Generator._handle_multipart_signedcCsg}x|jD]}|j}|j|}|j|ddd|j|j}|j|j}|r|d|jkr|j |jj |ddq|j |qW|j j |jj |dS)Nr!Frr&r'r') rAr%rr"rr2splitr_encoded_EMPTYrKr8r r)rr ZblocksrPrrQtextr+rrr_handle_message_delivery_status@s  &z)Generator._handle_message_delivery_statuscCs|j}|j|}|j}t|trg|j|jdddd|j|j}n|j |}|j j |dS)Nrr!Fr) r%rrFrBrJr"rArr2rr r)rr rrQrHrrr_handle_messageUs  %zGenerator._handle_messagecCstjtj}d t|d}|dkr4|S|}d}x[|jdtj|dtj}|j |s|Pn|dt |}|d7}qCW|S) N=z==rz^--z(--)?$.r&z===============) randomZ randrangesysmaxsize_fmt _compile_rereescape MULTILINEsearchrC)clsrXtokenrRbZcounterZcrerrrrLns &zGenerator._make_boundarycCstj||S)N)rccompile)rgrflagsrrrrbszGenerator._compile_re)__name__ __module__ __qualname____doc__rrr"rrWr%rr-rr0r.rIr:rSrUrYrZ classmethodrLrbrrrrrs,   1    #   :  csveZdZdZdZddZddZddZd d Zfd d Z e Z e d dZ 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 |jj|jdddS)Nasciisurrogateescape)r rencode)rrrrrrszBytesGenerator.writecCstS)N)r)rrrrr%szBytesGenerator._new_buffercCs |jdS)Nrr)rt)rrrrrrszBytesGenerator._encodecCsPx9|jD]+\}}|jj|jj||q W|j|jdS)N)r=r rr Z fold_binaryr)rr r>r?rrrr.s#zBytesGenerator._write_headerscs|jdkrdSt|jrl|jjdk rl|jrYtjd|j|_n|j|jntt |j |dS)NZ7bitz>From ) rFrr Zcte_typer rGr<r-superrrI)rr )r#rrrIs" zBytesGenerator._handle_textcCstj|jd|S)Nrr)rcrjrt)rgrrkrrrrbszBytesGenerator._compile_re) rlrmrnrorWrr%rr.rIr:rprbrr)r#rrs      zD[Non-text (%(type)s) part of message omitted, filename %(filename)s]c@s7eZdZdZdddddZddZdS) 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. TNNcCs;tj|||||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] N)rr_FMTra)rrrrZfmtrrrrs  zDecodedGenerator.__init__cCsx|jD]}|j}|dkrJt|jddd|q |dkrYq t|ji|jd6|jd6|jd6|jd d 6|jd d d 6|jddd6d|q WdS)NrXdecodeFfileZ multipartrEmaintypesubtypez [no filename]filenamezContent-Descriptionz[no description]Z descriptionzContent-Transfer-Encodingz [no encoding]encoding) walkr5printrAraZget_content_typer6 get_filenameget)rr rPrzrrrr0s           zDecodedGenerator._dispatch)rlrmrnrorr0rrrrrs r&z%%0%dd)ro__all__rcr_rr^copyriorrZ email.utilsrr7NLrjrerGrrrwrr*reprr`Z_widthrarLrrrrs&    m79