ó
áp7]c           @   sj   d  Z  d d l m Z d d l m Z d e f d     YZ e rN d   Z n	 d   Z d d
 d	  Z	 d
 S(   s$   
Handle file opening for read/write
i˙˙˙˙(   t   _is_string_like(   t   PY3t   EmptyContextManagerc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sw   
    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(   t   _obj(   t   selft   obj(    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyt   __init__   s    c         C   s   |  j  S(   s)   When entering, return the embedded object(   R   (   R   (    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyt	   __enter__   s    c         G   s   t  S(   s   Don't hide anything(   t   False(   R   t   args(    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyt   __exit__   s    c         C   s   t  |  j |  S(   N(   t   getattrR   (   R   t   name(    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyt   __getattr__   s    (   t   __name__t
   __module__t   __doc__R   R   R
   R   (    (    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyR      s
   			c         C   sH   |  j  d  r1 d d  l } | j |  | d | St |  | d | Sd  S(   Ns   .gzi˙˙˙˙t   encoding(   t   endswitht   gzipt   open(   t   fnamet   modeR   R   (    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyt   _open   s    c         C   s<   |  j  d  r+ d d  l } | j |  |  St |  |  Sd  S(   Ns   .gzi˙˙˙˙(   R   R   R   (   R   R   R   R   (    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyR   "   s    t   rc         C   s   t  |   r t |  | |  Sy< d | k r5 |  j n  d | k sM d | k rW |  j n  Wn t k
 rw t d   n Xt |   S(   s7  
    Light wrapper to handle strings and let files (anything else) pass through.

    It also handle '.gz' files.

    Parameters
    ==========
    fname: string or file-like object
        File to open / forward
    mode: string
        Argument passed to the 'open' or 'gzip.open' function
    encoding: string
        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   t   wt   as,   fname must be a string or a file-like object(   R    R   t   readt   writet   AttributeErrort
   ValueErrorR   (   R   R   R   (    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyt   get_file_obj)   s    
N(
   R   t   numpy.lib._iotoolsR    t   statsmodels.compat.pythonR   t   objectR   R   t   NoneR   (    (    (    s9   lib/python2.7/site-packages/statsmodels/iolib/openfile.pyt   <module>   s   	