3
ebV                 @   s   d Z ddddgZdZdZdZdZyd	d
lmZmZ W n e	k
rP   dZdZY nX dd Z
dd ZdddZdddZdddZdddZdd Zdd Zdd Zedkre  dS ) zHConversions to/from quoted-printable transport encoding as per RFC 1521.encodedecodeencodestringdecodestring   =L   s   0123456789ABCDEF        )a2b_qpb2a_qpNc             C   sH   t | tst| dkr|S | dkr&|S | tkpFd|   ko@dkn   S )zDecide whether a particular byte ordinal needs to be quoted.

    The 'quotetabs' flag indicates whether embedded tabs and spaces should be
    quoted.  Note that line-ending tabs and spaces are always encoded, as per
    RFC 1521.
    s    	   _       ~)
isinstancebytesAssertionErrorESCAPE)c	quotetabsheader r   /usr/lib/python3.6/quopri.pyneedsquoting   s    r   c             C   sB   t | trt| dkstt| } ttt| d  t| d  f S )zQuote a single character.      )r   r   lenr   ordr   HEX)r   r   r   r   quote$   s    r   Fc             C   s<  t dk	r,| j }t |||d}|j| dS |dfdd}d}x| j }|sNP g }	d}
|dd dkrv|dd }d}
xL|D ]D}t|f}t|||rt|}|r|dkr|	jd	 q||	j| q|W |dk	r|| tj	|	}x:t
|tkr||dtd  d
d |td d }qW |}q@W |dk	r8|||
d dS )av  Read 'input', apply quoted-printable encoding, and write to 'output'.

    'input' and 'output' are binary file objects. The 'quotetabs' flag
    indicates whether embedded tabs and spaces should be quoted. Note that
    line-ending tabs and spaces are always encoded, as per RFC 1521.
    The 'header' flag indicates whether we are encoding spaces as _ as per RFC
    1522.N)r   r      
c             S   sj   | r<| dd  dkr<|j | d d t| dd   |  n*| dkrX|j t| |  n|j | |  d S )Nr   s    	   .r    r    )writer   )soutputlineEndr   r   r   r!   ;   s
    (zencode.<locals>.writer   r   r   r   s   =
)r$   r    r    )r
   readr!   readliner   r   r   appendEMPTYSTRINGjoinr   MAXLINESIZE)inputr#   r   r   dataodatar!   ZprevlinelineZoutlinestrippedr   Zthisliner   r   r   r   ,   s@    	





c             C   sF   t d k	rt | ||dS ddlm} || }| }t|||| |j S )N)r   r   r   )BytesIO)r
   ior0   r   getvalue)r"   r   r   r0   infpoutfpr   r   r   r   j   s    c             C   s  t dk	r*| j }t ||d}|j| dS d}x| j }|s@P dt| }}|dkr||d | dkrd}	|d }x.|dkr||d | dkr|d }qxW nd}	x.||k r|||d  }
|
dkr|r|d	 }|d }q|
tk r||
 }|d }q|d |kr|	 rd}	P q|d |k rV||d |d
  tkrV|t }|d
 }q|d
 |k rt||d |d
  rt||d
 |d  r|tt||d |d  f }|d }q||
 }|d }qW |	s2|j|d  d}q2W |r|j| dS )zRead 'input', apply quoted-printable decoding, and write to 'output'.
    'input' and 'output' are binary file objects.
    If 'header' is true, decode underscore as space (per RFC 1522).N)r   r   r   r   r   s    	r   r         )	r	   r%   r!   r&   r   r   ishexr   unhex)r+   r#   r   r,   r-   newr.   inpartialr   r   r   r   r   u   sR    
   

 
 ( 
B" 
 c             C   sD   t d k	rt | |dS ddlm} || }| }t|||d |j S )N)r   r   )r0   )r	   r1   r0   r   r2   )r"   r   r0   r3   r4   r   r   r   r      s    c             C   sV   t | tstd|   ko dkn  pTd|   ko8dkn  pTd|   koPdkS   S )zDReturn true if the byte ordinal 'c' is a hexadecimal digit in ASCII.   0   9   a   f   A   F)r   r   r   )r   r   r   r   r7      s    r7   c             C   s   d}x| D ]}t |f}d|  ko*dkn  r:td}n`d|  koLdkn  r`tdd }n:d	|  kord
kn  rtd	d }ndstdt| |d t||  }q
W |S )z.Get the integer value of a hexadecimal number.r   r=   r>   0r?   r@   a
   rA   rB   Fznon-hex digit r   )r   r   r   repr)r"   bitsr   r:   r   r   r   r8      s    


r8   c           (   C   s  dd l } dd l}y|j| jdd  d\}}W nV |jk
r } z8| j| _t| td td td | jd W Y d d }~X nX d}d}x(|D ] \}}|dkrd}|d	krd}qW |r|r| j| _td
 | jd |sdg}d}	x|D ]}
|
dkr| jj	}nNyt
|
d}W n> tk
rR } z | jjd|
|f  d}	wW Y d d }~X nX z*|rlt|| jj	 nt|| jj	| W d |
dkr|j  X qW |	r| j|	 d S )Nr   r   Ztdz"usage: quopri [-t | -d] [file] ...z-t: quote tabsz-d: decode; default encoder5   z-tz-dz -t and -d are mutually exclusive-rbz%s: can't open (%s)
)sysgetoptargverrorstderrstdoutprintexitstdinbufferopenOSErrorr!   r   r   close)rJ   rK   ZoptsargsmsgZdecoZtabsorD   stsfilefpr   r   r   main   sT      
 



r]   __main__)F)FF)F)F)__doc____all__r   r*   r   r(   Zbinasciir	   r
   ImportErrorr   r   r   r   r   r   r7   r8   r]   __name__r   r   r   r   <module>   s*   

>

+
.