B
    u9aÔ"  ã               @   sl   d dl Z d dlZd dlZd dlmZ dgZdZdZdZdZ	dZ
d	Zeeee	e
egZG d
d„ dƒZdd„ ZdS )é    N)ÚquoteÚTemplateZffz-fzf-z--z.-z-.c               @   sl   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )r   c             C   s   d| _ |  ¡  d S )Nr   )Ú	debuggingÚreset)Úself© r   ú/usr/lib/python3.7/pipes.pyÚ__init__U   s    zTemplate.__init__c             C   s   d| j f S )Nz<Template instance, steps=%r>)Ústeps)r   r   r   r   Ú__repr__Z   s    zTemplate.__repr__c             C   s
   g | _ d S )N)r
   )r   r   r   r   r   ^   s    zTemplate.resetc             C   s"   t ƒ }| jd d … |_| j|_|S )N)r   r
   r   )r   Útr   r   r   Úcloneb   s    zTemplate.clonec             C   s
   || _ d S )N)r   )r   Úflagr   r   r   Údebugj   s    zTemplate.debugc             C   s²   t |ƒt dƒk	rtdƒ‚|tkr.td|f ƒ‚|tkr>tdƒ‚| jr^| jd d tkr^tdƒ‚|d d	kr~t d
|¡s~tdƒ‚|d d	kržt d|¡sžtdƒ‚| j 	||f¡ d S )NÚ z%Template.append: cmd must be a stringzTemplate.append: bad kind %rz-Template.append: SOURCE can only be prependedéÿÿÿÿé   z'Template.append: already ends with SINKr   Úfz\$IN\bz#Template.append: missing $IN in cmdz\$OUT\bz$Template.append: missing $OUT in cmd)
ÚtypeÚ	TypeErrorÚ	stepkindsÚ
ValueErrorÚSOURCEr
   ÚSINKÚreÚsearchÚappend)r   ÚcmdÚkindr   r   r   r   n   s    zTemplate.appendc             C   s´   t |ƒt dƒk	rtdƒ‚|tkr.td|f ƒ‚|tkr>tdƒ‚| jr^| jd d tkr^tdƒ‚|d dkr~t d	|¡s~td
ƒ‚|d dkržt d|¡sžtdƒ‚| j 	d||f¡ d S )Nr   z&Template.prepend: cmd must be a stringzTemplate.prepend: bad kind %rz+Template.prepend: SINK can only be appendedr   r   z,Template.prepend: already begins with SOURCEr   z\$IN\bz$Template.prepend: missing $IN in cmdz\$OUT\bz%Template.prepend: missing $OUT in cmd)
r   r   r   r   r   r
   r   r   r   Úinsert)r   r   r   r   r   r   Úprepend~   s    zTemplate.prependc             C   s6   |dkr|   |¡S |dkr$|  |¡S td|f ƒ‚d S )NÚrÚwz,Template.open: rw must be 'r' or 'w', not %r)Úopen_rÚopen_wr   )r   ÚfileÚrwr   r   r   ÚopenŽ   s    

zTemplate.openc             C   sB   | j st|dƒS | j d d tkr*tdƒ‚|  |d¡}t |d¡S )Nr!   r   r   z)Template.open_r: pipeline ends width SINKr   )r
   r'   r   r   ÚmakepipelineÚosÚpopen)r   r%   r   r   r   r   r#   ˜   s    
zTemplate.open_rc             C   sB   | j st|dƒS | j d d tkr*tdƒ‚|  d|¡}t |d¡S )Nr"   r   r   z,Template.open_w: pipeline begins with SOURCEr   )r
   r'   r   r   r(   r)   r*   )r   r%   r   r   r   r   r$   ¢   s    
zTemplate.open_wc             C   s   t  |  ||¡¡S )N)r)   Úsystemr(   )r   ÚinfileÚoutfiler   r   r   Úcopyª   s    zTemplate.copyc             C   s(   t || j|ƒ}| jr$t|ƒ d| }|S )Nzset -x; )r(   r
   r   Úprint)r   r,   r-   r   r   r   r   r(   ­   s
    zTemplate.makepipelineN)Ú__name__Ú
__module__Ú__qualname__r	   r   r   r   r   r   r    r'   r#   r$   r.   r(   r   r   r   r   r   R   s   

c             C   sÊ  g }x"|D ]\}}|  d||dg¡ q
W |s>|  ddddg¡ |d dd… \}}|d dkrv| sv| dddddg¡ | |d d< |d dd… \}}|d dkr¸|s¸|  ddddg¡ ||d d< g }xˆtdt|ƒƒD ]v}||d  d	 }|| d	 }	|d dks|	d dkrØt ¡ \}
}t |
¡ |  |¡ | ||d  d< || d< qØW x®|D ]¦}|\}}}}|d dkrŠd
t|ƒ d | }|d dkr¬dt|ƒ d | }|d dkrÐ|rÐ|d t|ƒ }|d dkrô|rô|d t|ƒ }||d< qXW |d d }xd|dd … D ]T}|dd… \}}|d dkrbd|krTd| d }|d | }n|d | }qW |rÆd}x|D ]}|d t|ƒ }q„W dt|d ƒ d }|d | d | }|S )Nr   Úcatz--r   r   é   r   r   é   zOUT=z; zIN=ú-z <z >z{ z; }z |
Ú
zrm -fú ztrap z; exitz 1 2 3 13 14 15)	r   r   ÚrangeÚlenÚtempfileZmkstempr)   Úcloser   )r,   r
   r-   Úlistr   r   ZgarbageÚiZlkindZrkindÚfdZtempÚitemÚinfZoutfZcmdlistZrmcmdr%   Ztrapcmdr   r   r   r(   µ   s`    

 


r(   )r   r)   r;   Zshlexr   Ú__all__ZFILEIN_FILEOUTZSTDIN_FILEOUTZFILEIN_STDOUTZSTDIN_STDOUTr   r   r   r   r(   r   r   r   r   Ú<module><   s   c