o
    Uݢg                     @  sz   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZ edeeB ddZG dd	 d	e	e ZdS )
    )annotationsN)Callable)BinaryIOGenericLiteralTextIOTypeVaroverload_T_coT)bound	covariantc                   @  sp   e Zd ZdZe		dd ddZe		dd!ddZdefd"ddZd#ddZd$ddZd%ddZ	d#ddZ
dS )&FileHandleCachezLRU cache for file handles..modeLiteral['w', 'r']	open_func_Callable[[str | bytes | os.PathLike[str] | os.PathLike[bytes], Literal['w', 'r', 'a']], TextIO]c                 C     d S N selfr   r   r   r   [/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/tenkit/cache.py__init__      zFileHandleCache.__init__Literal['wb', 'rb']dCallable[[str | bytes | os.PathLike[str] | os.PathLike[bytes], Literal['wb', 'rb', 'ab']], BinaryIO]c                 C  r   r   r   r   r   r   r   r      r   wLiteral['w', 'r', 'wb', 'rb']KCallable[[str | bytes | os.PathLike[str] | os.PathLike[bytes], str], _T_co]c                 C  s(   || _ || _|   i | _t | _d S r   )r   r   config_max_fileshave_openedcollectionsOrderedDict
open_filesr   r   r   r   r   (   s   returnNonec                 C  s   t t j\}}|d | _d S )Nd   )resource	getrlimitRLIMIT_NOFILEmaxfiles)r   Zsoft_r   r   r   r   =   s   z FileHandleCache.config_max_filesfnstr | bytesr
   c                 C  s   || j v r| j |}|| j |< |S d| jv r$|| jv r$| jdd}n| j}t| j | jd krE| j jdd\}}| | j|< |	  | 
||}d|v r\|| jv r\|| j|  || j |< |S )zGet the cached file object for the given file name, or create one.

        Note:
            While function can take a str or bytes path, they will be considered
            different paths for purposes of caching.
        r   a   F)lastr)r#   popr   r    replacelenr*   popitemtellcloser   seek)r   r,   fhr   Zclose_fnZclose_fhr   r   r   getA   s    


zFileHandleCache.getFileHandleCache[_T_co]c                 C  s   | S r   r   )r   r   r   r   	__enter__d   s   zFileHandleCache.__enter__c                 C  s   | j  D ]}|  qd S r   )r#   valuesr7   )r   exc_type	exc_value	tracebackfr   r   r   __exit__g   s   
zFileHandleCache.__exit__N)..)r   r   r   r   )r   r   r   r   )r   r   r   r   )r$   r%   )r,   r-   r$   r
   )r$   r;   )__name__
__module____qualname____doc__r	   r   openr   r:   r<   rB   r   r   r   r   r      s"    	


#r   )
__future__r   r!   osr'   collections.abcr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   <module>   s    