U
    KvfQ                     @   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
   >/tmp/pip-unpacked-wheel-2v6byqio/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sd|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
   	