3
öLÖj×S ã @ sì d Z dddgZddlZddlZddlZddlZddlmZ ddlm Z m
Z
ddlmZ dd l
mZ d
ZdZejdƒZejd
ejƒZejdƒZejdƒZG dd„ dƒZG dd„ deƒZdZG dd„ deƒZeeejd ƒƒZde Zej Z dS )z:Classes to generate plain text from a message object tree.Ú GeneratorÚDecodedGeneratorÚBytesGeneratoré N)Údeepcopy)ÚStringIOÚBytesIO)Ú_has_surrogates)ÚHeaderWriteErrorÚ_Ú
z
\r\n|\r|\nz^From z\r\n[^ \t]|\r[^ \n\t]|\n[^ \t]s \r\n[^ \t]|\r[^ \n\t]|\n[^ \t]c @ s° e Zd ZdZd'ddœdd„Zdd„ Zd(d d
„Zdd„ Zd
d„ Zdd„ Z dd„ Z
dd„ Zdd„ Zdd„ Z
dd„ ZeZdd„ Zdd„ Zdd „ Zd!d"„ Zed)d#d$„ƒZed%d&„ ƒZdS )*r zŠGenerates output from a Message object tree.
This basic generator writes the message to the given file object as plain
text.
N)Úpolicyc C s6 |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
r r © r ú'/usr/lib64/python3.6/email/generator.pyÚ__init__' s zGenerator.__init__c C s | j j|ƒ d S )N)r Úwrite)r Úsr r r r G s zGenerator.writeFc C sØ | j dkr|j n| j }|dk r*|j|d�}| jdk rB|j| jd�}|j| _| j| jƒ| _d| _| j| jƒ| _| j }|j }zL|| _ ||_ |r¸|j ƒ }|s¨dt
jt
j
ƒ ƒ }| j|| j ƒ | j
|ƒ W d|| _ ||_ X dS )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Ú zFrom nobody )r Úcloner r Ú_NLÚ_encodeÚ_encoded_NLZ_EMPTYÚ_encoded_EMPTYZget_unixfromÚtimeZctimer Ú_write)r ÚmsgÚunixfromr r Zold_gen_policyZold_msg_policyZufromr r r ÚflattenK s,
zGenerator.flattenc C s | j || jd| jd�S )z1Clone this generator with the exact same options.N)r )Ú __class__r r )r Úfpr r r r | s zGenerator.clonec C s t ƒ S )N)r )r r r r Ú_new_buffer� s zGenerator._new_bufferc C s |S )Nr )r r r r r r ” s zGenerator._encodec C sX |sd S t j|ƒ}x*|d d… D ]}| j|ƒ | j| jƒ q W |d rT| j|d ƒ d S )Né éÿÿÿÿr) r) )ÚNLCREÚsplitr r )r ÚlinesÚliner r r Ú_write_lines˜ s
zGenerator._write_linesc C s¤ | j }z"d | _| jƒ | _ }| j|ƒ W d || _ | j}| `X |rht|ƒ}|jd|d ƒ |jd|d ƒ t|dd ƒ}|d krˆ| j|ƒ n|| ƒ | j j|j ƒ ƒ d S )Nzcontent-transfer-encodingr zcontent-typer( Ú_write_headers)
r Ú
_munge_cter' Ú _dispatchr Zreplace_headerÚgetattrr/ r Úgetvalue)r r" ZoldfpZsfpZ munge_cteÚmethr r r r! ¨ s" zGenerator._writec C st |j ƒ }|jƒ }tj||fƒjddƒ}t| d| d ƒ}|d krh|jddƒ}t| d| d ƒ}|d krh| j}||ƒ d S )Nú-r
Z_handle_)Úget_content_maintypeÚget_content_subtypeÚ
UNDERSCOREÚjoinÚreplacer2 Ú
_writeBody)r r" ÚmainÚsubZspecificr4 Zgenericr r r r1 Ë s zGenerator._dispatchc C s¨ x–|j ƒ D ]Š\}}| jj||ƒ}| jjrŠ| jj}|j| jjƒsRtd|›d|›�ƒ‚|}|j|ƒrr|d t|ƒ … }tj |ƒrŠtd|›�ƒ‚| j
|ƒ q
W | j
| jƒ d S )Nz folded header does not end with z: z folded header contains newline: )Ú raw_itemsr ZfoldÚverify_generated_headersr Úendswithr ÚlenÚNEWLINE_WITHOUT_FWSPÚsearchr r )r r" ÚhÚvÚfoldedr Úfolded_no_linesepr r r r/ ß s
zGenerator._write_headersc C sž |j ƒ }|d krd S t|tƒs.tdt|ƒ ƒ‚t|jƒr~|jdƒ}|d k r~t|ƒ}|d= |j ||ƒ |j ƒ }|d |d f| _
| jr�tj
d|ƒ}| j|ƒ d S )Nzstring payload expected: %sÚcharsetzcontent-transfer-encodingzcontent-typez>From )Úget_payloadÚ
isinstanceÚstrÚ TypeErrorÚtyper Ú_payloadZ get_paramr Zset_payloadr0 r Úfcrer= r. )r r" ÚpayloadrH r r r Ú_handle_textõ s"
zGenerator._handle_textc C s¢ g }|j ƒ }|d krg }n(t|tƒr2| j|ƒ d S t|tƒsB|g}x>|D ]6}| jƒ }| j|ƒ}|j|d| jd� |j |j
ƒ ƒ qHW |jƒ }|s®| jj
|ƒ}| j|ƒ}|j|ƒ |jd k rê| jrÎtjd|jƒ} n|j} | j| ƒ | j| jƒ | jd| | j ƒ |�r| jj|jdƒƒ x4|D ],}
| j| jd | | j ƒ | jj|
ƒ �qW | j| jd | d | j ƒ |jd k �rž| j�rŽtjd|jƒ}n|j}| j|ƒ d S )NF)r# r z>From z--r )rI rJ rK r Úlistr' r r$ r Úappendr3 Zget_boundaryr r9 Ú_make_boundaryZset_boundaryÚpreambler rO r= r. r ÚpopÚepilogue)r r" ZmsgtextsZsubpartsÚpartr ÚgÚboundaryZalltextrU Z body_partrW r r r Ú_handle_multipart
sJ
zGenerator._handle_multipartc C s0 | j }|jdd�| _ z| j|ƒ W d || _ X d S )Nr )r )r r r[ )r r" Úpr r r Ú_handle_multipart_signedG s
z"Generator._handle_multipart_signedc C sž g }x€|j ƒ D ]t}| jƒ }| j|ƒ}|j|d| jd� |jƒ }|j| jƒ}|rx|d | jkrx|j | jj
|d d… ƒƒ q|j |ƒ qW | jj| jj
|ƒƒ d S )NF)r# r r( r) r) )
rI r' r r$ r r3 r+ r r rS r9 r r )r r" ZblocksrX r rY Útextr, r r r Ú_handle_message_delivery_statusR s
z)Generator._handle_message_delivery_statusc C s^ | j ƒ }| j|ƒ}|j}t|tƒrD|j|jdƒd| jd� |jƒ }n
| j |ƒ}| j
j|ƒ d S )Nr F)r# r )r' r rN rJ rR r$ rI r r3 r r r )r r" r rY rP r r r Ú_handle_messageg s
zGenerator._handle_messagec C sz t jtjƒ}d t| d }|d kr(|S |}d}xD| jdtj|ƒ d tjƒ}|j |ƒsZP |d t
|ƒ }|d7 }q2W |S )
Nú=é z==r z^--z(--)?$Ú.r( z===============)ÚrandomZ randrangeÚsysÚmaxsizeÚ_fmtÚ_compile_reÚreÚescapeÚ MULTILINErC rK )Úclsr^ ÚtokenrZ ÚbZcounterZcrer r r rT € s
zGenerator._make_boundaryc C s t j||ƒS )N)ri Úcompile)rl r Úflagsr r r rh ’ s zGenerator._compile_re)NN)FN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r r r$ r r' r r. r! r1 r/ rQ r; r[ r] r_ r` ÚclassmethodrT rh r r r r r s*
1#:c sP e Zd ZdZdd„ Zdd„ Zdd„ Zdd „ Z‡ fd
d„ZeZ e
dd
„ ƒZ‡ ZS )r aþ 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.
c C s | j j|jddƒƒ d S )NÚasciiÚsurrogateescape)r r Úencode)r r r r r r ¤ s zBytesGenerator.writec C s t ƒ S )N)r )r r r r r' § s zBytesGenerator._new_bufferc C s
|j dƒS )Nrv )rx )r r r r r r ª s zBytesGenerator._encodec C sª x˜|j ƒ D ]Œ\}}| jj||ƒ}| jjrŠ| jjjƒ }|j|ƒsRtd|›d|›�ƒ‚|}|j|ƒrr|d t|ƒ … }t j
|ƒrŠtd|›�ƒ‚| jj|ƒ q
W | j| j
ƒ d S )Nz folded header does not end with z: z folded header contains newline: )r> r Zfold_binaryr? r rx r@ r rA ÚNEWLINE_WITHOUT_FWSP_BYTESrC r r r )r r" rD rE rF r rG r r r r/ s
zBytesGenerator._write_headersc s^ |j d krd S t|j ƒrJ| jjdk rJ| jr