a
    ze9                     @   s   d dl Z d dlZd dlmZ g dZdad#dd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)GenericAlias)inputclosenextfilefilenamelineno
filelinenofilenoisfirstlineisstdin	FileInputhook_compressedhook_encodedF rmodeopenhookc                C   s(   t rt jrtdt| ||||da t S )Nzinput() already activer   )_state_fileRuntimeErrorr   )filesinplacebackupr   r    r   /usr/lib/python3.9/fileinput.pyr   T   s    
r   c                  C   s   t } d a | r|   d S N)r   r   )stater   r   r   r   a   s    r   c                   C   s   t stdt  S Nzno active input())r   r   r   r   r   r   r   r   i   s    
r   c                   C   s   t stdt  S r   )r   r   r   r   r   r   r   r   w   s    r   c                   C   s   t stdt  S r   )r   r   r   r   r   r   r   r      s    r   c                   C   s   t stdt  S r   )r   r   r   r   r   r   r   r      s    r   c                   C   s   t stdt  S r   )r   r   r	   r   r   r   r   r	      s    r	   c                   C   s   t stdt  S r   )r   r   r
   r   r   r   r   r
      s    r
   c                   C   s   t stdt  S r   )r   r   r   r   r   r   r   r      s    r   c                   @   s   e Zd Zd(dd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eeZdS ))r   NFr   r   r   c                C   s  t |tr|f}nBt |tjr,t|f}n(|d u rBtjdd  }|sLd}nt|}|| _|| _	|| _
d | _d | _d | _d| _d| _d | _d| _d | _|dvrtdd|v rdd l}|dtd	 || _d|vr|d
dnd| _|r|rtdt|std|| _d S )N   )-r   F)r   ZrUUrbz=FileInput opening mode must be one of 'r', 'rU', 'U' and 'rb'r!   z'U' mode is deprecated   r   wz4FileInput cannot use an opening hook in inplace modez#FileInput openhook must be callable)
isinstancestrosPathLikefspathsysargvtuple_files_inplace_backup_savestdout_output	_filename_startlineno_filelinenor   _isstdin_backupfilename
ValueErrorwarningswarnDeprecationWarning_modereplace_write_modecallable	_openhook)selfr   r   r   r   r   r8   r   r   r   __init__   sF    

zFileInput.__init__c                 C   s   |    d S r   r   r@   r   r   r   __del__   s    zFileInput.__del__c                 C   s    z|    W d| _nd| _0 d S )Nr   )r   r-   rC   r   r   r   r      s    
zFileInput.closec                 C   s   | S r   r   rC   r   r   r   	__enter__   s    zFileInput.__enter__c                 C   s   |    d S r   rB   )r@   typevalue	tracebackr   r   r   __exit__   s    zFileInput.__exit__c                 C   s   | S r   r   rC   r   r   r   __iter__   s    zFileInput.__iter__c                 C   s6   |   }|r|  jd7  _|S | js(t|   q d S Nr   )	_readliner4   r   StopIterationr   r@   liner   r   r   __next__   s    zFileInput.__next__c                 C   sV   dd l }|jdtdd ||  kr,tdz
|  W S  tyP   tdY n0 d S )Nr   zTSupport for indexing FileInput objects is deprecated. Use iterator protocol instead.r#   )
stacklevelzaccessing lines out of orderzend of input reached)r8   r9   r:   r   r   rP   rM   
IndexError)r@   ir8   r   r   r   __getitem__  s    
zFileInput.__getitem__c                 C   s  | j }d | _ |r|t_| j}d | _z|r0|  W | j}d | _z| `W n tyX   Y n0 zT|rn| jsn|  W | j	}d | _	|r| j
szt| W n ty   Y n0 d| _n@| j	}d | _	|r| j
szt| W n ty   Y n0 d| _0 n| j}d | _z| `W n ty   Y n0 z^|r4| js4|  W | j	}d | _	|rt| j
stzt| W n tyr   Y n0 d| _nF| j	}d | _	|r| j
szt| W n ty   Y n0 d| _0 0 d S )NF)r0   r*   stdoutr1   r   r   rL   AttributeErrorr5   r6   r/   r'   unlinkOSError)r@   Z
savestdoutoutputfileZbackupfilenamer   r   r   r     sf    






zFileInput.nextfilec                 C   s6   |   }|r|  jd7  _|S | js(|S |   q d S rK   )rL   r4   r   r   rN   r   r   r   readline/  s    zFileInput.readlinec                 C   s  | j sd| jv rdS dS | j d | _| j dd  | _ |  | _d| _d | _d| _d| _| jdkrd| _d| jv rt	t
jd	t
j| _nt
j| _d
| _nN| jrt| j| jpd | _zt| j W n ty   Y n0 t| j| j t| j| j| _zt| j j}W n$ ty4   t| j| j| _Y nr0 tjtjB tjB }ttdr^|tjO }t| j||}t|| j| _zt| j| W n ty   Y n0 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O_BINARY)%r-   r;   r2   r   r3   r4   r   r5   r6   getattrr*   stdinr.   r'   r)   r/   rW   rX   renameopenfstatr	   st_moder=   r1   O_CREATO_WRONLYO_TRUNChasattrr_   fdopenchmodrU   r0   r?   r[   rL   )r@   permr   fdr   r   r   rL   :  s\    







zFileInput._readlinec                 C   s   | j S r   )r2   rC   r   r   r   r   s  s    zFileInput.filenamec                 C   s   | j | j S r   )r3   r4   rC   r   r   r   r   v  s    zFileInput.linenoc                 C   s   | j S r   r4   rC   r   r   r   r   y  s    zFileInput.filelinenoc                 C   s2   | j r*z| j  W S  ty&   Y dS 0 ndS d S )N)r   r	   r7   rC   r   r   r   r	   |  s    
zFileInput.filenoc                 C   s
   | j dkS rK   rn   rC   r   r   r   r
     s    zFileInput.isfirstlinec                 C   s   | j S r   )r5   rC   r   r   r   r     s    zFileInput.isstdin)NFr   )__name__
__module____qualname__rA   rD   r   rE   rI   rJ   rP   rT   r   r[   rL   r   r   r   r	   r
   r   classmethodr   __class_getitem__r   r   r   r   r      s(   )9	r   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gziprc   bz2BZ2File)r   r   extrw   rx   r   r   r   r     s    r   c                    s    fdd}|S )Nc                    s   t | | dS )Nencodingerrors)rc   )r   r   r{   r   r   r     s    zhook_encoded.<locals>.openhookr   )r|   r}   r   r   r{   r   r     s    r   c                  C   s   dd l } d}d}|  tjdd  d\}}|D ] \}}|dkrBd}|dkr.|}q.t|||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  q^tdt t t f  d S )Nr   Fr   zib:z-iTz-b)r   r   ro   
z%d: %s[%d]%s %s*r   z
%d: %s[%d])	getoptr*   r+   r   printr   r   r   r
   )r   r   r   ZoptsargsoarO   r   r   r   _test  s    
r   __main__)NFr   )N)r*   r'   typesr   __all__r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   rp   r   r   r   r   <module>K   s&   	

			 `
