a
    ãzeÔ"  ã                   @   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-.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.9/pipes.pyÚ__init__U   s    zTemplate.__init__c                 C   s   d| j f S )Nz<Template instance, steps=%r>©Ústepsr   r	   r	   r
   Ú__repr__Z   s    zTemplate.__repr__c                 C   s
   g | _ d S ©Nr   r   r	   r	   r
   r   ^   s    zTemplate.resetc                 C   s"   t ƒ }| jd d … |_| j|_|S r   )r   r   r   )r   Útr	   r	   r
   Úcloneb   s    zTemplate.clonec                 C   s
   || _ d S r   )r   )r   Úflagr	   r	   r
   Údebugj   s    zTemplate.debugc                 C   s²   t |ƒt dƒurtdƒ‚|tv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   Úfú\$IN\bz#Template.append: missing $IN in cmdú\$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ƒurtdƒ‚|tv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   r   z$Template.prepend: missing $IN in cmdr   z%Template.prepend: missing $OUT in cmd)
r   r   r   r   r   r   r   r    r!   Úinsertr#   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/   r0   r1   r2   r	   r	   r
   r+   ¢   s    
zTemplate.open_wc                 C   s   t  |  ||¡¡S r   )r0   Ú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   r4   r5   r$   r	   r	   r
   r/   ­   s
    zTemplate.makepipelineN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r"   r'   r.   r*   r+   r6   r/   r	   r	   r	   r
   r   R   s   

c                 C   sª  g }|D ]\}}|  d||dg¡ q|s6|  g d¢¡ |d dd… \}}|d dkrj| sj| dg d¢¡ | |d d< |d dd… \}}|d dkr¨|s¨|  g d¢¡ ||d d< g }tdt|ƒƒD ]v}||d  d }|| d }	|d dks |	d dkrÆt ¡ \}
}t |
¡ |  |¡ | ||d  d< || d< qÆ|D ]¦}|\}}}}|d dkrtd	t|ƒ d
 | }|d dkr–dt|ƒ d
 | }|d dkrº|rº|d t|ƒ }|d dkrÞ|rÞ|d t|ƒ }||d< qB|d d }|dd … D ]T}|dd… \}}|d dkrHd|v r:d| d }|d | }n|d | }q|r¦d}|D ]}|d t|ƒ }qfdt|d ƒ d }|d | d | }|S )Nr   )r   Úcatr   r   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ÚtempfileÚmkstempr0   Úcloser   )r4   r   r5   Úlistr$   r%   ZgarbageÚiZlkindZrkindÚfdZtempÚitemÚinfZoutfZcmdlistZrmcmdr,   Ztrapcmdr	   r	   r
   r/   µ   s`    


r/   )r    r0   rC   Zshlexr   Ú__all__ZFILEIN_FILEOUTZSTDIN_FILEOUTZFILEIN_STDOUTZSTDIN_STDOUTr   r   r   r   r/   r	   r	   r	   r
   Ú<module><   s   ÿc