B
    u9a   ã               @   sf   d dl Z d dlZd dlZdddgZG dd„ deƒZdddœdd„Zdd	d„Zd
d„ Ze	dkrbeƒ  dS )é    NÚErrorÚencodeÚdecodec               @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r   r   ú/usr/lib/python3.7/uu.pyr   '   s   F)Úbacktickc         	   C   sX  g }z2| dkrt jj} n`t| tƒrz|d kr8tj | ¡}|d krfyt | ¡j	}W n t
k
rd   Y nX t| dƒ} | | ¡ |dkrŒt jj}nt|tƒrªt|dƒ}| |¡ |d kr¶d}|d krÂd}| d|d@ |f  d¡¡ |  d¡}x0t|ƒd	kr| tj||d
¡ |  d¡}qêW |r,| d¡ n
| d¡ W d x|D ]}| ¡  q@W X d S )Nú-ÚrbÚwbi¶  zbegin %o %s
iÿ  Úasciié-   r   )r
   s   `
end
s    
end
)ÚsysÚstdinÚbufferÚ
isinstanceÚstrÚosÚpathÚbasenameÚstatÚst_modeÚAttributeErrorÚopenÚappendÚstdoutÚwriter   ÚreadÚlenÚbinasciiZb2a_uuÚclose)Úin_fileÚout_fileÚnameÚmoder
   Úopened_filesÚdataÚfr   r   r	   r   *   sB    









c          
   C   s  g }| dkrt jj} nt| tƒr4t| dƒ} | | ¡ zÆxr|  ¡ }|sNtdƒ‚| 	d¡sZq:| 
dd¡}t|ƒdkr:|d dkr:yt|d	 d
ƒ P W q: tk
r¦   Y q:X q:W |d krà|d  d¡ d¡}tj |¡ràtd| ƒ‚|d kröt|d	 d
ƒ}|dkr
t jj}n0t|tƒr:t|dƒ}t ||¡ |}| |¡ |  ¡ }x¨|rê| d¡dkrêyt |¡}	W nj tjk
rÒ }
 zH|d d d@ d d d }t |d |… ¡}	|sÂt j d|
 ¡ W d d }
~
X Y nX | |	¡ |  ¡ }qDW |sútdƒ‚W d x|D ]}| ¡  qW X d S )Nr   r   z'No valid begin line found in input files   beginó    é   é   r   é   é   s    	
r   z"Cannot overwrite existing file: %sr   s   endé    é?   é   é   zWarning: %s
zTruncated input file)r   r   r   r   r   r   r   Úreadliner   Ú
startswithÚsplitr    ÚintÚ
ValueErrorÚrstripr   r   r   Úexistsr   ÚchmodÚstripr!   Za2b_uuÚstderrr   r"   )r#   r$   r&   Úquietr'   ZhdrZ	hdrfieldsÚfpÚsr(   ÚvÚnbytesr)   r   r   r	   r   ]   s`    









"

c              C   s4  dd l } | jdd}|jdddddd	d
 |jdddddd	d
 | ¡ \}}t|ƒdkrl| d¡ t d¡ tjj	}tj
j	}t|ƒdkr|d }t|ƒdkr¤|d }|jrì|jràt|tƒrÆt|dƒ}nttjd dƒ t d¡ t||ƒ nD|jr&t|tƒrt|dƒ}nttjd dƒ t d¡ t||ƒ d S )Nr   z'usage: %prog [-d] [-t] [input [output]])Úusagez-dz--decoder   zDecode (instead of encode)?FÚ
store_true)ÚdestÚhelpÚdefaultÚactionz-tz--textÚtextz2data is text, encoded format unix-compatible text?r+   zincorrect number of argumentsr-   r   z: cannot do -t to stdoutr   z: cannot do -t from stdin)ÚoptparseZOptionParserZ
add_optionÚ
parse_argsr    Úerrorr   Úexitr   r   r   r   rH   r   r   r   ÚprintÚargvr   )rI   ÚparserÚoptionsÚargsÚinputÚoutputr   r   r	   Útest    s6    




rT   Ú__main__)NN)NNF)
r!   r   r   Ú__all__Ú	Exceptionr   r   r   rT   r   r   r   r   r	   Ú<module>!   s   
3
C&