U
    ÃmœdQ  ã                   @   s@   d Z ddlmZ ddlmZ G dd„ dƒZdd„ Zdd
d„Zd	S )z$
Handle file opening for read/write
é    )ÚPath)Ú_is_string_likec                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚEmptyContextManagerzw
    This class is needed to allow file-like object to be used as
    context manager, but without getting closed.
    c                 C   s
   || _ d S ©N©Ú_obj)ÚselfÚobj© r
   úS/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/iolib/openfile.pyÚ__init__   s    zEmptyContextManager.__init__c                 C   s   | j S )z)When entering, return the embedded objectr   )r   r
   r
   r   Ú	__enter__   s    zEmptyContextManager.__enter__c                 G   s   dS )zDo not hide anythingFr
   )r   Úargsr
   r
   r   Ú__exit__   s    zEmptyContextManager.__exit__c                 C   s   t | j|ƒS r   )Úgetattrr   )r   Únamer
   r
   r   Ú__getattr__   s    zEmptyContextManager.__getattr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r
   r
   r
   r   r   	   s
   r   c                 C   s4   |   d¡r"dd l}|j| ||d�S t| ||d�S d S )Nz.gzr   ©Úencoding)ÚendswithÚgzipÚopen)ÚfnameÚmoder   r   r
   r
   r   Ú_open   s    
r   ÚrNc                 C   s¸   t | ƒrt| ƒ} t| tƒr(| j||d�S t| dƒr@| j||d�S zt| ||d�W S  tk
r²   z(d|krp| j d|ks€d|kr†| j W n tk
r¤   t	dƒ‚Y nX t
| ƒ Y S X dS )	aT  
    Light wrapper to handle strings, path objects and let files (anything else)
    pass through.

    It also handle '.gz' files.

    Parameters
    ----------
    fname : str, path object or file-like object
        File to open / forward
    mode : str
        Argument passed to the 'open' or 'gzip.open' function
    encoding : str
        For Python 3 only, specify the encoding of the file

    Returns
    -------
    A file-like object that is always a context-manager. If the `fname` was
    already a file-like object, the returned context manager *will not
    close the file*.
    )r   r   r   r   r   ÚwÚaz,fname must be a string or a file-like objectN)r   r   Ú
isinstancer   ÚhasattrÚ	TypeErrorÚreadÚwriteÚAttributeErrorÚ
ValueErrorr   )r   r   r   r
   r
   r   Úget_file_obj'   s"    


r)   )r   N)r   Úpathlibr   Znumpy.lib._iotoolsr   r   r   r)   r
   r
   r
   r   Ú<module>   s
   	