
    z%iN                     X   	 g d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ dZdZ ej                  d      Z ej                  d	ej                         Z G d
 d      Z G d de      ZdZ G d de      Z e eej0                  dz
              Zdez  Zej6                  Zy))	GeneratorDecodedGeneratorBytesGenerator    N)deepcopy)StringIOBytesIO)_has_surrogates_
z
\r\n|\r|\nz^From c                       e Zd Z	 ddddZd ZddZd Zd Zd Zd	 Z	d
 Z
d Zd Zd ZeZd Zd Zd Zd Zedd       Zed        Zy)r   Npolicyc                b    	 ||dn|j                   }|| _        || _        || _        || _        y )NT)mangle_from__fp_mangle_from_maxheaderlenr   )selfoutfpr   r   r   s        C/home/uftp/.pyenv/versions/3.12.0/lib/python3.12/email/generator.py__init__zGenerator.__init__#   s>    	. #)>4v7J7JL)(    c                 :    | j                   j                  |       y N)r   writer   ss     r   r   zGenerator.writeC   s    qr   c                    	 | j                   |j                   n| j                   }||j                  |      }| j                  |j                  | j                        }|j                  | _        | j                  | j                        | _        d| _        | j                  | j                        | _        | j                   }|j                   }	 || _         ||_         |rZ|j                         }|s*dt        j                  t        j                               z   }| j                  || j                  z          | j                  |       || _         ||_         y # || _         ||_         w xY w)N)linesepmax_line_length zFrom nobody )r   cloner   r   _NL_encode_encoded_NL_EMPTY_encoded_EMPTYget_unixfromtimectimer   _write)r   msgunixfromr   r   old_gen_policyold_msg_policyufroms           r   flattenzGenerator.flattenG   s*   	(  ${{2\\'\2F(\\$2C2C\DF>><<1"ll4;;7
 	( DKCJ((**TZZ		-DDE

5488+,KK(DK'CJ )DK'CJs   A;E E-c                 V    	 | j                  || j                  d | j                        S Nr   )	__class__r   r   )r   fps     r   r#   zGenerator.clonex   s0    ?~~b"00"%)[[  2 	2r   c                     t               S r   )r   r   s    r   _new_bufferzGenerator._new_buffer   s
    zr   c                     |S r    r   s     r   r%   zGenerator._encode   s    r   c                     |sy t         j                  |      }|d d D ].  }| j                  |       | j                  | j                         0 |d   r| j                  |d          y y )N)NLCREsplitr   r$   )r   lineslines      r   _write_lineszGenerator._write_lines   se    E"#2J 	!DJJtJJtxx 	! 9JJuRy! r   c                     | j                   }	 d | _        | j                         x| _         }| j                  |       || _         | j                  }| `|rOt	        |      }|j                  d      	|d   |d<   n|j                  d|d          |j                  d|d          t        |dd       }|| j                  |       n ||        | j                   j                  |j                                y # || _         | j                  }| `w xY w)Ncontent-transfer-encodingr   Content-Transfer-Encodingcontent-type   _write_headers)r   
_munge_cter9   	_dispatchr   getreplace_headergetattrrH   r   getvalue)r   r-   oldfpsfp	munge_ctemeths         r   r,   zGenerator._write   s     	 "DO!--//DHsNN3DHI3-Cww23;3<Q</0""#>	!M~y|< s,d3<$Js||~&' DHIs   /C6 6Dc                 &   |j                         }|j                         }t        j                  ||f      j	                  dd      }t        | d|z   d       }|0|j	                  dd      }t        | d|z   d       }|| j                  } ||       y )N-r
   _handle_)get_content_maintypeget_content_subtype
UNDERSCOREjoinreplacerM   
_writeBody)r   r-   mainsubspecificrR   generics          r   rJ   zGenerator._dispatch   s    
 '')%%'??D#;/77SAtZ(2D9<ll3,G4g!5t<D|S	r   c                     |j                         D ]0  \  }}| j                  | j                  j                  ||             2 | j                  | j                         y r   )	raw_itemsr   r   foldr$   r   r-   hvs       r   rH   zGenerator._write_headers   sJ    MMO 	/DAqJJt{{''1-.	/ 	

488r   c                    |j                         }|y t        |t              st        dt	        |      z        t        |j                        rR|j                  d      }|?t        |      }|d= |j                  ||       |j                         }|d   |d   f| _
        | j                  rt        j                  d|      }| j                  |       y )Nzstring payload expected: %scharsetrD   rF   >From )get_payload
isinstancestr	TypeErrortyper	   _payload	get_paramr   set_payloadrI   r   fcrer]   rB   )r   r-   payloadrg   s       r   _handle_textzGenerator._handle_text   s    //#?'3'9DMIJJ3<<(mmI.G" sm341//+#&'B#C#&~#6#8hhx1G'"r   c                 0   g }|j                         }|g }n5t        |t              r| j                  |       y t        |t              s|g}|D ]`  }| j                         }| j                  |      }|j                  |d| j                         |j                  |j                                b |j                         }|s=| j                  j                  |      }| j                  |      }|j                  |       |j                   e| j"                  r!t$        j'                  d|j                         }	n|j                   }	| j)                  |	       | j                  | j                         | j                  d|z   | j                  z          |r*| j*                  j                  |j-                  d             |D ]K  }
| j                  | j                  dz   |z   | j                  z          | j*                  j                  |
       M | j                  | j                  dz   |z   dz   | j                  z          |j.                  K| j"                  r!t$        j'                  d|j.                        }n|j.                  }| j)                  |       y y )NFr.   r   rh   z--r   )ri   rj   rk   r   listr9   r#   r2   r$   appendrN   get_boundaryr&   rY   _make_boundaryset_boundarypreambler   rq   r]   rB   r   popepilogue)r   r-   msgtextssubpartspartr   gboundaryalltextr{   	body_partr}   s               r   _handle_multipartzGenerator._handle_multipart  s    ??$H#&JJx Hd+ zH 	*D  "A

1AIIdUDHHI=OOAJJL)		* ##% &&++H5G**73HX&<<#!!88Hcll;<<h'JJtxx 

4(?TXX-.HHNN8<<?+ " 	&IJJtxx$1DHH<=HHNN9%		& 	

488d?X-4txx?@<<#!!88Hcll;<<h' $r   c                     | j                   }|j                  d      | _         	 | j                  |       || _         y # || _         w xY w)Nr   r    )r   r#   r   )r   r-   ps      r   _handle_multipart_signedz"Generator._handle_multipart_signed;  sA     KKggag0	""3'DK!DKs	   > 	Ac                    g }|j                         D ]  }| j                         }| j                  |      }|j                  |d| j                         |j                         }|j                  | j                        }|r@|d   | j                  k(  r.|j                  | j                  j                  |d d              |j                  |        | j                  j                  | j                  j                  |             y )NFru   r=   )ri   r9   r#   r2   r$   rN   r?   r&   r(   rw   rY   r   r   )r   r-   blocksr   r   r   textr@   s           r   _handle_message_delivery_statusz)Generator._handle_message_delivery_statusF  s     OO% 
	$D  "A

1AIIdUDHHI=::<DJJt//0Erd&9&99d..33E#2J?@d#
	$ 	t'',,V45r   c                 R   | j                         }| j                  |      }|j                  }t        |t              r>|j                  |j                  d      d| j                         |j                         }n| j                  |      }| j                  j                  |       y )Nr   Fru   )r9   r#   rn   rj   rv   r2   ri   r$   rN   r%   r   r   )r   r-   r   r   rr   s        r   _handle_messagezGenerator._handle_message[  sz    JJqM ,,gt$IIcooa(5$((IKjjlGll7+Gwr   c                 @   t        j                  t        j                        }dt        |z  z   dz   }||S |}d}	 | j                  dt        j                  |      z   dz   t        j                        }|j                  |      s	 |S |dz   t        |      z   }|dz  }d)Nz===============z==r   z^--z(--)?$.rG   )random	randrangesysmaxsize_fmt_compile_rereescape	MULTILINEsearchrk   )clsr   tokenr   bcountercres          r   ry   zGenerator._make_boundaryt  s       -.5<O//%"))A,"6"A2<<PC::d#  3W-AqLG r   c                 .    t        j                  ||      S r   )r   compiler   r   flagss      r   r   zGenerator._compile_re  s    zz!U##r   )NN)FNr   )__name__
__module____qualname__r   r   r2   r#   r9   r%   rB   r,   rJ   rH   rs   r[   r   r   r   r   classmethodry   r   r;   r   r   r   r      s    @/(b2(	" %'N(#, J8(t	6* 2  " $ $r   r   c                   L     e Zd Z	 d Zd Zd Zd Z fdZeZe	d        Z
 xZS )r   c                 Z    | j                   j                  |j                  dd             y )Nasciisurrogateescape)r   r   encoder   s     r   r   zBytesGenerator.write  s    qxx):;<r   c                     t               S r   )r   r8   s    r   r9   zBytesGenerator._new_buffer  s
    yr   c                 $    |j                  d      S Nr   )r   r   s     r   r%   zBytesGenerator._encode  s    xx  r   c                     |j                         D ]:  \  }}| j                  j                  | j                  j	                  ||             < | j                  | j
                         y r   )ra   r   r   r   fold_binaryr$   rc   s       r   rH   zBytesGenerator._write_headers  sP     MMO 	:DAqHHNN4;;221a89	: 	

488r   c                 <   |j                   y t        |j                         rf| j                  j                  dk(  sM| j                  r%t
        j                  d|j                         |_         | j                  |j                          y t        t        | +  |       y )N7bitrh   )rn   r	   r   cte_typer   rq   r]   rB   superr   rs   )r   r-   r5   s     r   rs   zBytesGenerator._handle_text  so     <<3<<(1E1Ev1M!!#xx#,,?cll+.3C8r   c                 L    t        j                  |j                  d      |      S r   )r   r   r   r   s      r   r   zBytesGenerator._compile_re  s    zz!((7+U33r   )r   r   r   r   r9   r%   rH   rs   r[   r   r   __classcell__)r5   s   @r   r   r     s:    
=!
9 J4 4r   r   zD[Non-text (%(type)s) part of message omitted, filename %(filename)s]c                   "    e Zd Z	 ddddZd Zy)r   Nr   c                d    	 t         j                  | ||||       |t        | _        y || _        y r4   )r   r   _FMTr   )r   r   r   r   fmtr   s         r   r   zDecodedGenerator.__init__  s:    	* 	4l"( 	 	*;DIDIr   c                    |j                         D ]  }|j                         }|dk(  rt        |j                  d      |        6|dk(  r<t        | j                  |j                         |j                         |j                         |j                  d      |j                  dd      |j                  d	d
      dz  |         y )Nr   F)decode)file	multipartz[no filename]zContent-Descriptionz[no description]rE   z[no encoding])rm   maintypesubtypefilenamedescriptionencoding)	walkrV   printri   r   get_content_typerW   get_filenamerK   )r   r-   r   r   s       r   rJ   zDecodedGenerator._dispatch  s    HHJ 	"D002H6!d&&e&44@[(dii#'#8#8#:#'#<#<#>#'#;#;#=#'#4#4_#E#'88,A,>$@#'88,G,;$=	# 	 !	"	"r   )NNN)r   r   r   r   rJ   r;   r   r   r   r     s    
<"r   r   rG   z%%0%dd)__all__r   r   r*   r   copyr   ior   r   email.utilsr	   rX   NLr   r>   r   rq   r   r   r   r   lenreprr   _widthr   ry   r;   r   r   <module>r      s   
 A
= 	 
      '
	

=!rzz)R\\*o$ o$d/4Y /4d N6"y 6"t 
T#++a- 	!& ))r   