o ,]@L@s6dZddlmZddlmZddlmZddlmZddlmZddlmZgdZ dd l Z dd l Z dd l Z dd l Z dd lZdd lmZmZdd lmZdd lmZdd lmZdd lmmmZdZdZe de j Z!Gddde"Z#Gddde#Z$dZ%Gddde#Z&e'e(e j)dZ*de*Z+e#j,Z,d S)z:Classes to generate plain text from a message object tree.)print_function)unicode_literals)division)absolute_import)super)str) GeneratorDecodedGeneratorBytesGeneratorN)StringIOBytesIO)compat32)Header)_has_surrogates_ z^From c@seZdZdZd(ddZddZd)d d Zd d Zd ZddZ ddZ ddZ ddZ ddZ ddZddZeZddZddZd d!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. TNcKs:d|vr |d}|d=nd} ||_||_||_||_dS)Npolicy)_fp _mangle_from_ maxheaderlenr)selfoutfp mangle_from_r _3to2kwargsrrB/usr/lib/python3/dist-packages/future/backports/email/generator.py__init__+s zGenerator.__init__cCs|j|dSN)rwritersrrrrHszGenerator.writeFcCs|jdur|jn|j}|dur|j|d}|jdur!|j|jd}|j|_||j|_d|_|d|_|j}|j}z-||_||_|r[| }|sSdt t }| ||j| |W||_||_dS||_||_w)azPrint 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. N)linesepmax_line_lengthz From nobody )rclonerr!_NL_encode _encoded_NL_EMPTY_encoded_EMTPY get_unixfromtimectimer_write)rmsgunixfromr!rold_gen_policyold_msg_policyufromrrrflattenLs0     zGenerator.flattencCs|j||jd|jdS)z1Clone this generator with the exact same options.N)r) __class__rr)rfprrrr%{s zGenerator.cloner$cCtSr)r rrrr _new_bufferszGenerator._new_buffercCs|Srrrrrrr'szGenerator._encodecCs|sdS|d}|ddD]}||d||jq|dd}||t|dt|kr>||jdSdS)NTz ) splitlinesrrstripr&len)rlinesline laststrippedrrr _write_liness  zGenerator._write_linescCsn|j}z||_}||W||_n||_wt|dd}|dur)||n|||j|dS)N_write_headers)rr9 _dispatchgetattrrBrgetvalue)rr/oldfpsfpmethrrrr.s    zGenerator._writecCst|}|}t||fdd}t|d|d}|dur4|dd}t|d|d}|dur4|j}||dS)N-r_handle_)get_content_maintypeget_content_subtype UNDERSCOREjoinreplacerD _writeBody)rr/mainsubspecificrHgenericrrrrCs  zGenerator._dispatchcCs6|D]\}}||j||q||jdSr) raw_itemsrrfoldr&rr/hvrrrrBszGenerator._write_headerscCs|}|dur dSt|tstdt|t|jr2|d}|dur2|d=||||}|j r;t d|}| |dS)Nzstring payload expected: %scharsetzcontent-transfer-encoding>From ) get_payload isinstancer TypeErrortyper_payload get_param set_payloadrfcrerRrA)rr/payloadrZrrr _handle_texts     zGenerator._handle_textc Csg}|}|dur g}nt|tr||dSt|ts!|g}|D]}|}||}|j|d|jd| | q#| }|sU|j |}||}|||jdurs|jretd|j} n|j} || ||j|d||j|r|j|d|D]} ||jd||j|j| q||jd|d|jdur||j|jrtd|j} n|j} || dSdS)NFr0r!r[z--r)r\r]rrlistr9r%r4r&appendrE get_boundaryr(rN_make_boundary set_boundarypreamblerrcrRrArpopepilogue) rr/msgtextssubpartspartr gboundaryalltextrl body_partrnrrr_handle_multipartsN            zGenerator._handle_multipartcCs4|j}|jdd|_z ||W||_dS||_w)Nrr")rr%rv)rr/prrr_handle_multipart_signed/s  z"Generator._handle_multipart_signedcCsg}|D]:}|}||}|j|d|jd|}||j}|r;|d|jkr;| |j |ddq| |q|j |j |dS)NFrfr:) r\r9r%r4r&rEsplitr(_encoded_EMPTYrhrNrr)rr/blocksrqr rrtextr>rrr_handle_message_delivery_status:s    z)Generator._handle_message_delivery_statuscCs^|}||}|j}t|tr"|j|dd|jd|}n| |}|j |dS)NrFrf) r9r%r`r]rgr4r\r&rEr'rr)rr/r rrrdrrr_handle_messageOs    zGenerator._handle_messagecCsxttj}dt|d}|dur|S|}d} |dt|dtj}| |s/ |S|dt |}|d7}q) Nz===============z==rTz^--z(--)?$.) random randrangesysmaxsize_fmt _compile_rereescape MULTILINEsearchr)clsr|tokenrsbcountercrerrrrjhs  zGenerator._make_boundarycCs t||Sr)rcompilerr flagsrrrrzs zGenerator._compile_re)TN)FNr)__name__ __module__ __qualname____doc__rrr4r%rzr9r'rAr.rCrBrerPrvrxr}r~ classmethodrjrrrrrr!s.  /  ;  rcsTeZdZdZdZddZddZddZd d Zfd d Z e Z e d dZ Z S)r aGenerates 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|jt|dddS)Nasciisurrogateescape)rrrencoderrrrrszBytesGenerator.writecCr7r)r r8rrrr9szBytesGenerator._new_buffercCs |dSNr)rrrrrr's zBytesGenerator._encodecCs8|D]\}}|j|j||q||jdSr)rUrrr fold_binaryr&rWrrrrBszBytesGenerator._write_headerscs^|jdurdSt|jr%|jjdks%|jrtd|j|_||jdStt | |dS)N7bitr[) r`rrcte_typerrcrRrArr re)rr/r5rrres zBytesGenerator._handle_textcCst|d|Sr)rrrrrrrrszBytesGenerator._compile_re)rrrrrzrr9r'rBrerPrr __classcell__rrrrr ~s  r zD[Non-text (%(type)s) part of message omitted, filename %(filename)s]c@s"eZdZdZd ddZddZdS) r zGenerates 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,t|||||durt|_dS||_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_FMTr)rrrrfmtrrrrs  zDecodedGenerator.__init__c Cs|D];}|}|dkrt|jdd|dq|dkrqt|j||||d|dd|d d d |dqdS) Nr|F)decode)file multipartz [no filename]zContent-Descriptionz[no description]zContent-Transfer-Encodingz [no encoding])r_maintypesubtypefilename descriptionencoding) walkrKprintr\rget_content_typerL get_filenameget)rr/rqrrrrrCs*  zDecodedGenerator._dispatch)TrN)rrrrrrCrrrrr s  r rz%%0%dd)-r __future__rrrrfuture.builtinsrr__all__rrr,rwarningsior r "future.backports.email._policybaser future.backports.email.headerrfuture.backports.email.utilsrfuture.backports.email.charset backportsemailrZ_charsetrMNLrrrcobjectrr rr r=reprr_widthrrjrrrrs<         _68