
e[\P                 @   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 Wn e	 k
 rj d Z d Z Yn Xd d   Z
 d d   Z d d d  Z d d d d  Z d d d  Z d d d  Z d d   Z d d   Z d d   Z e d k r e   n  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   s\   t  |  t  s t  |  d k r% | S|  d k r5 | S|  t k p[ d |  k oX 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.4/quopri.pyneedsquoting   s    r   c             C   sW   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	 rA |  j   } t  | d | d | } | j |  d S| d d d  } d } x=|  j   } | sr Pn  g  }	 d }
 | d d  d k r | d d  } d }
 n  xm | D]e } t | f  } t | | |  r t |  } n  | r| d	 k r|	 j d
  q |	 j |  q W| d k	 r6| |  n  t j	 |	  } xH t
 |  t k r| | d t d  d d | t d d  } qHW| } q\ | d k	 r| | d |
 n  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.Nr   r   s   
c             S   s   |  rN |  d d   d k rN | j  |  d  d  t |  d d    |  n7 |  d k rt | j  t |   |  n | j  |  |  d  S)Nr   s    	   .r   r   )writer   )soutputlineEndr   r   r   r    ;   s
    2zencode.<locals>.writer   r   r   r   r#   s   =
r   r   )r
   readr    readliner   r   r   appendEMPTYSTRINGjoinr   MAXLINESIZE)inputr"   r   r   dataodatar    ZprevlinelineZoutlineZstrippedr   Zthisliner   r   r   r   ,   s@    	
		c             C   sd   t  d  k	 r" t  |  d | d | Sd d l m } | |   } |   } t | | | |  | j   S)Nr   r   r   )BytesIO)r
   ior.   r   getvalue)r!   r   r   r.   infpoutfpr   r   r   r   j   s    	c             C   s  t  d k	 r; |  j   } t  | d | } | j |  d Sd } x)|  j   } | sZ Pn  d t |  } } | d k r | | d |  d k r d }	 | d } x= | d k r | | d |  d k r | d } q Wn d }	 xd| | k  rI| | | d  }
 |
 d k r/| r/| d	 } | d } q |
 t k rR| |
 } | d } q | d | k rs|	 rsd }	 Pq | d | k  r| | d t k r| t } | d
 } q | d
 | k  r2t | | d | d
   r2t | | d
 | d   r2| t t | | d | d   f  } | d } q | |
 } | d } q W|	 sD | j | d  d } qD qD | r| j |  n  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).Nr   r   r   r   s   
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     & 
)
 
  $
 L+ 
 c             C   s^   t  d  k	 r t  |  d | Sd d l m } | |   } |   } t | | d | | j   S)Nr   r   )r.   )r	   r/   r.   r   r0   )r!   r   r.   r1   r2   r   r   r   r      s    	c             C   se   t  |  t  s t  d |  k o, d k n pd d |  k oH d k n pd d |  k ob d 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   r5      s    r5   c             C   s   d } x |  D] } t  | f  } d | k o9 d k n rM t d  } nz d | k od d k n r| t d  d } nK d	 | k o d
 k n r t d	  d } n d s t d t |    | d t |  | } q W| S)z.Get the integer value of a hexadecimal number.r   r;   r<   0r=   r>   a
   r?   r@   Fznon-hex digit r   )r   r   r   repr)r!   bitsr   r8   r   r   r   r6      s    r6   c           (   C   s;  d d  l  }  d d  l } y) | j |  j d d   d  \ } } Wni | j k
 r } zF |  j |  _ t |  t d  t d  t d  |  j d  WYd  d  } ~ Xn Xd } d } x> | D]6 \ } } | d k r d } n  | d	 k r d } q q W| r,| r,|  j |  _ t d
  |  j d  n  | s>d g } n  d }	 x | D] }
 |
 d k rl|  j j	 } n^ y t
 |
 d  } WnH t k
 r} z( |  j j d |
 | f  d }	 wKWYd  d  } ~ Xn Xz6 | rt | |  j j	  n t | |  j j	 |  Wd  |
 d k r| j   n  XqKW|	 r7|  j |	  n  d  S)Nr   r   Ztdz"usage: quopri [-t | -d] [file] ...z-t: quote tabsz-d: decode; default encoder3   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)rH   rI   ZoptsargsmsgZdecoZtabsorB   stsfilefpr   r   r   main   sT    )



  	 
 r[   __main__)__doc____all__r   r)   r   r'   Zbinasciir	   r
   ImportErrorr   r   r   r   r   r   r5   r6   r[   __name__r   r   r   r   <module>   s*   >+.