B
    u9a!9                 @   s   d dl Z d dlZdddddddd	d
dddgZdad 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G dd dZdd Zd!ddZdd Zedkre  dS )"    Ninputclosenextfilefilenamelineno
filelinenofilenoisfirstlineisstdin	FileInputhook_compressedhook_encodedF rc             C   s(   t rt jrtdt| |||||a t S )Nzinput() already active)_state_fileRuntimeErrorr   )filesinplacebackupbufsizemodeopenhook r   /usr/lib/python3.7/fileinput.pyr   S   s    	
c              C   s   t } d a | r|   d S )N)r   r   )stater   r   r   r   a   s    c               C   s   t stdt  S )Nzno active input())r   r   r   r   r   r   r   r   i   s    
c               C   s   t stdt  S )Nzno active input())r   r   r   r   r   r   r   r   w   s    c               C   s   t stdt  S )Nzno active input())r   r   r   r   r   r   r   r      s    c               C   s   t stdt  S )Nzno active input())r   r   r   r   r   r   r   r      s    c               C   s   t stdt  S )Nzno active input())r   r   r   r   r   r   r   r      s    c               C   s   t stdt  S )Nzno active input())r   r   r	   r   r   r   r   r	      s    c               C   s   t stdt  S )Nzno active input())r   r   r
   r   r   r   r   r
      s    c               @   s   e Zd Zd(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d Zd d! Zd"d# Zd$d% Zd&d' ZdS ))r   NFr   r   r   c             C   s  t |tr|f}nBt |tjr,t|f}n(|d krBtjdd  }|sLd}nt|}|| _|| _	|| _
|rdd l}|jdtdd d | _d | _d | _d| _d| _d | _d| _d | _|dkrtd	d
|krdd l}|dtd || _|r
|rtdt|s
td|| _d S )N   )-r   z!bufsize is deprecated and ignored   )
stacklevelF)r   ZrUUrbz=FileInput opening mode must be one of 'r', 'rU', 'U' and 'rb'r    z'U' mode is deprecatedz4FileInput cannot use an opening hook in inplace modez#FileInput openhook must be callable)
isinstancestrosPathLikefspathsysargvtuple_files_inplace_backupwarningswarnDeprecationWarning_savestdout_output	_filename_startlineno_filelinenor   _isstdin_backupfilename
ValueError_modecallable	_openhook)selfr   r   r   r   r   r   r-   r   r   r   __init__   sJ    


zFileInput.__init__c             C   s   |    d S )N)r   )r;   r   r   r   __del__   s    zFileInput.__del__c             C   s   z|    W d d| _X d S )Nr   )r   r*   )r;   r   r   r   r      s    zFileInput.closec             C   s   | S )Nr   )r;   r   r   r   	__enter__   s    zFileInput.__enter__c             C   s   |    d S )N)r   )r;   typevalue	tracebackr   r   r   __exit__   s    zFileInput.__exit__c             C   s   | S )Nr   )r;   r   r   r   __iter__   s    zFileInput.__iter__c             C   s:   x4|   }|r |  jd7  _|S | js*t|   qW d S )Nr   )	_readliner4   r   StopIterationr   )r;   liner   r   r   __next__   s    zFileInput.__next__c             C   s>   ||   krtdy|  S  tk
r8   tdY nX d S )Nzaccessing lines out of orderzend of input reached)r   r   rG   rE   
IndexError)r;   ir   r   r   __getitem__  s    zFileInput.__getitem__c             C   s   | j }d | _ |r|t_| j}d | _z|r0|  W d | j}d | _y| `W n tk
r\   Y nX z|rr| jsr|  W d | j	}d | _	|r| j
syt| W n tk
r   Y nX d| _X X d S )NF)r0   r'   stdoutr1   r   r   rD   AttributeErrorr5   r6   r,   r$   unlinkOSError)r;   Z
savestdoutoutputfileZbackupfilenamer   r   r   r     s4    

  zFileInput.nextfilec             C   s:   x4|   }|r |  jd7  _|S | js*|S |   qW d S )Nr   )rD   r4   r   r   )r;   rF   r   r   r   readline+  s    zFileInput.readlinec             C   s  | j sd| jkrdS dS | j d | _| j dd  | _ |  | _d| _d | _d| _d| _| jdkrd| _d| jkrt	t
jd	t
j| _nt
j| _d
| _n\| jrt| j| jpd | _yt| j W n tk
r   Y nX t| j| j t| j| j| _yt| j j}W n$ tk
r6   t| jd| _Y n~X tjtjB tjB }ttdr`|tjO }t| j||}t|d| _yttdrt| j| W n tk
r   Y nX t
j| _ | jt
_n,| j!r| !| j| j| _nt| j| j| _| jj"| _#| # S )Nb    r   r   r   Fr   z<stdin>bufferTz.bakwO_BINARYchmod)$r*   r8   r2   r   r3   r4   r   r5   r6   getattrr'   stdinr+   r$   r&   r,   rM   rN   renameopenfstatr   st_moder1   O_CREATO_WRONLYO_TRUNChasattrrV   fdopenrW   rK   r0   r:   rQ   rD   )r;   Zpermr   fdr   r   r   rD   6  s\    







zFileInput._readlinec             C   s   | j S )N)r2   )r;   r   r   r   r   p  s    zFileInput.filenamec             C   s   | j | j S )N)r3   r4   )r;   r   r   r   r   s  s    zFileInput.linenoc             C   s   | j S )N)r4   )r;   r   r   r   r   v  s    zFileInput.filelinenoc             C   s0   | j r(y
| j  S  tk
r$   dS X ndS d S )N)r   r   r7   )r;   r   r   r   r   y  s    
zFileInput.filenoc             C   s
   | j dkS )Nr   )r4   )r;   r   r   r   r	     s    zFileInput.isfirstlinec             C   s   | j S )N)r5   )r;   r   r   r   r
     s    zFileInput.isstdin)NFr   r   r   N)__name__
__module____qualname__r<   r=   r   r>   rB   rC   rG   rJ   r   rQ   rD   r   r   r   r   r	   r
   r   r   r   r   r      s$    
+:	c             C   sV   t j| d }|dkr,dd l}|| |S |dkrHdd l}|| |S t| |S d S )Nr   z.gzr   z.bz2)r$   pathsplitextgzipr[   bz2ZBZ2File)r   r   Zextrj   rk   r   r   r   r     s    c                s    fdd}|S )Nc                s   t | | dS )N)encodingerrors)r[   )r   r   )rl   rm   r   r   r     s    zhook_encoded.<locals>.openhookr   )rl   rm   r   r   )rl   rm   r   r     s    c              C   s   dd l } d}d}|  tjdd  d\}}x(|D ] \}}|dkrDd}|dkr0|}q0W xtt|||dD ]b}|d	d  d
kr|d d	 }|d	d  dkr|d d	 }tdt t t t rdpd|f  qdW tdt t t f  d S )Nr   Fr   zib:z-iTz-b)r   r   rd   
z%d: %s[%d]%s %s*r   z
%d: %s[%d])	getoptr'   r(   r   printr   r   r   r	   )rq   r   r   ZoptsargsoarF   r   r   r   _test  s"        rv   __main__)NFr   r   r   N)N)r'   r$   __all__r   r   r   r   r   r   r   r   r	   r
   r   r   r   rv   re   r   r   r   r   <module>K   s*   
 
	

			 [
