
x\c           @   s  d  Z  d d l Z d d l m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 j
 Z
 d d l	 m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z d d l m Z m Z d d l m Z e Z d d	 d
 d d d d d d d d d d d d d d h Z e
 j rd d l m Z m Z e Z  d d l! m" Z# d d l! m$ Z$ m% Z% m& Z& m' Z' m( Z( d d l) m* Z* d d l+ m, Z, n d d l- m Z  d d l. m' Z' m Z d d l" m" Z# d d  l" m$ Z$ m% Z% m& Z& m( Z( d d l- m* Z* d d l/ m, Z, d d! l m Z m Z d d" l0 m1 Z1 e d#    Z e2 e$ e% e&  Z3 e3 j4 d  d$ e5 f d%     YZ6 e
 j sd&   e6 _7 n  d'   Z8 d(   Z9 d)   Z: d*   Z; d+   Z< d,   Z= d d d d-  Z? d.   Z@ i d/ d0 6d1 d2 6d3 d4 6d5 d6 6ZA d7   ZB d d eC eD d8  ZE d9 e jF e f d:     YZG d; e6 f d<     YZH e
 j smd=   eH _7 n  d> e6 f d?     YZI e
 j re jJ d@ dA  ZK e jJ d@ dB  ZL n, dC e6 f dD     YZK dE e5 f dF     YZL d S(G   s   Common IO api utilitiesiN(   t   closingt   contextmanager(   t   BytesIOt   StringIOt   string_typest	   text_type(   t   AbstractMethodErrort   DtypeWarningt   EmptyDataErrort   ParserErrort   ParserWarning(   t   is_file_liket	   is_number(   t   pprint_things   -1.#INDs   1.#QNANs   1.#INDs   -1.#QNANs   #N/A N/As   #N/As   N/As   n/at   NAs   #NAt   NULLt   nullt   NaNs   -NaNt   nans   -nant    (   t   urlopent   pathname2url(   t   urlparse(   t   uses_relativet   uses_netloct   uses_paramst	   urlencodet   urljoin(   t   URLError(   t   HTTPException(   R   (   R   R   (   R   R   R   R   (   R   R    (   t   wrapsc          o   s'   t  t |  |     } | VWd  QXd  S(   N(   R    t   _urlopen(   t   argst   kwargst   f(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR   2   s    t   BaseIteratorc           B   s    e  Z d  Z d   Z d   Z RS(   s   Subclass this and provide a "__next__()" method to obtain an iterator.
    Useful only when the object being iterated is non-reusable (e.g. OK for a
    parser, not for an in-memory table, yes for its iterator).c         C   s   |  S(   N(    (   t   self(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   __iter__A   s    c         C   s   t  |    d  S(   N(   R   (   R$   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   __next__D   s    (   t   __name__t
   __module__t   __doc__R%   R&   (    (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR#   <   s   	c         C   s
   |  j    S(   N(   R&   (   R$   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   <lambda>I   s    c         C   s0   y t  |   j t k SWn t k
 r+ t SXd S(   s   Check to see if a URL has a valid protocol.

    Parameters
    ----------
    url : str or unicode

    Returns
    -------
    isurl : bool
        If `url` has a valid protocol return True otherwise False.
    N(   t	   parse_urlt   schemet   _VALID_URLSt	   Exceptiont   False(   t   url(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   _is_urlL   s    c         C   s#   t  |  t  r t j j |   S|  S(   s[  Return the argument with an initial component of ~ or ~user
       replaced by that user's home directory.

    Parameters
    ----------
    filepath_or_buffer : object to be converted if possible

    Returns
    -------
    expanded_filepath_or_buffer : an expanded filepath or the
                                  input if not expandable
    (   t
   isinstanceR   t   ost   patht
   expanduser(   t   filepath_or_buffer(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   _expand_user^   s    c         C   s"   t  |  t  r t d   n  d  S(   Ns   Passing a bool to header is invalid. Use header=None for no header or header=int or list-like of ints to specify the row(s) making up the column names(   R2   t   boolt	   TypeError(   t   header(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   _validate_header_argp   s    c         C   s   y d d l  } t } Wn t k
 r/ t } n Xy d d l m } t } Wn t k
 rc t } n Xt |  d  r} |  j   S| r t |  | j	  r t
 |   S| r t |  |  r |  j St |   S(   s_  Attempt to convert a path-like object to a string.

    Parameters
    ----------
    filepath_or_buffer : object to be converted

    Returns
    -------
    str_filepath_or_buffer : maybe a string version of the object

    Notes
    -----
    Objects supporting the fspath protocol (python 3.6+) are coerced
    according to its __fspath__ method.

    For backwards compatibility with older pythons, pathlib.Path and
    py.path objects are specially coerced.

    Any other object is passed through unchanged, which includes bytes,
    strings, buffers, or anything else that's not even path-like.
    iN(   t   localt
   __fspath__(   t   pathlibt   Truet   ImportErrorR/   t   py.pathR<   t   hasattrR=   R2   t   PathR   t   strpathR7   (   R6   R>   t   _PATHLIB_INSTALLEDt	   LocalPatht   _PY_PATH_INSTALLED(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   _stringify_pathx   s"    





c         C   s0   y t  |   j d k SWn t k
 r+ t SXd S(   s    Check for an s3, s3n, or s3a urlt   s3t   s3nt   s3aN(   RI   RJ   RK   (   R+   R,   R.   R/   (   R0   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt	   is_s3_url   s    c         C   s0   y t  |   j d k SWn t k
 r+ t SXd S(   s   Check for a gcs urlt   gcst   gsN(   RM   RN   (   R+   R,   R.   R/   (   R0   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt
   is_gcs_url   s    c   
      C   sj  t  |   }  t |   rz t |   } | j j d d  } | d k rN d } n  t | j    } | j   | | | t	 f St
 |   r d d l m } | j |  d | d | d | St |   r d d l m } | j |  d | d | d | St |  t j t j t j f  r't |   d | t f St |   sZd	 }	 t |	 j d
 t |      n  |  d | t f S(   s  
    If the filepath_or_buffer is a url, translate and return the buffer.
    Otherwise passthrough.

    Parameters
    ----------
    filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
                         or buffer
    encoding : the encoding to use to decode py3 bytes, default is 'utf-8'
    mode : str, optional

    Returns
    -------
    tuple of ({a filepath_ or buffer or S3File instance},
              encoding, str,
              compression, str,
              should_close, bool)
    s   Content-Encodingt   gzipi(   RI   t   encodingt   compressiont   mode(   RM   s0   Invalid file path or buffer object type: {_type}t   _typeN(   RH   R1   R   t   headerst   gett   NoneR   t   readt   closeR?   RL   t	   pandas.ioRI   t   get_filepath_or_bufferRO   RM   R2   t   compatR   t   binary_typet   mmapR7   R/   R   t
   ValueErrort   formatt   type(
   R6   RQ   RR   RS   t   reqt   content_encodingt   readerRI   RM   t   msg(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR[      s:    	
!c         C   s   t  d t |    S(   s   
    converts an absolute native path to a FILE URL.

    Parameters
    ----------
    path : a path in native format

    Returns
    -------
    a valid FILE URL
    s   file:(   R   R   (   R4   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   file_path_to_url   s    s   .gzRP   s   .bz2t   bz2s   .zipt   zips   .xzt   xzc         C   s   | d k r d S| d k rr t |   }  t |  t j  s> d Sx- t j   D] \ } } |  j |  rK | SqK Wd S| t k r | Sd j |  } d d g t	 t  } | d j |  7} t
 |   d S(   s  
    Get the compression method for filepath_or_buffer. If compression='infer',
    the inferred compression method is returned. Otherwise, the input
    compression method is returned unchanged, unless it's invalid, in which
    case an error is raised.

    Parameters
    ----------
    filepath_or_buffer :
        a path (str) or buffer
    compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}
        If 'infer' and `filepath_or_buffer` is path-like, then detect
        compression from the following extensions: '.gz', '.bz2', '.zip',
        or '.xz' (otherwise no compression).

    Returns
    -------
    string or None :
        compression method

    Raises
    ------
    ValueError on invalid compression specified
    t   infers!   Unrecognized compression type: {}s   
Valid compression types are {}N(   RW   RH   R2   R\   R   t   _compression_to_extensiont   itemst   endswithR`   t   sortedR_   (   R6   RR   t	   extensionRe   t   valid(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   _infer_compression  s     c         C   s  y  d d l  m } t | f } Wn t k
 r< t f } n Xt   } |  }	 t |   }  t |  t j  }
 |
 r t	 |  |  } n  | rt j
 r |
 r | r d } t |   n  | d k r d d l } |
 r | j |  |  }	 q| j d |   }	 n| d k rqd d l } |
 r.| j |  |  }	 qt j
 r_t | j |  j     }	 |  j   q| j |   }	 n| d k r;t |  |  } | j |  | j d	 k r| }	 q| j d
 k r| j   } t |  d k r| j | j    }	 q8t |  d k r t d j |     q8t d j |    qnH | d k rht j   } | j |  |  }	 n d j |  } t |   | j |	  n |
 r3t j
 r| d	 k rd n | } t |  |  }	 nW | rt |  | d | d d }	 n3 | rt |  | d d d d }	 n t |  |  }	 | j |	  n  t j r| r| sWt |	 |  rd d l m  } | |	 d | d d }	 | j |	  n  | rt! |	 d  ry  t" |	  } |	 j   | }	 Wqt# k
 rqXn  |	 | f S(   s  
    Get file handle for given path/buffer and mode.

    Parameters
    ----------
    path_or_buf :
        a path (str) or buffer
    mode : str
        mode to open path_or_buf with
    encoding : str or None
    compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default None
        If 'infer' and `filepath_or_buffer` is path-like, then detect
        compression from the following extensions: '.gz', '.bz2', '.zip',
        or '.xz' (otherwise no compression).
    memory_map : boolean, default False
        See parsers._parser_params for more information.
    is_text : boolean, default True
        whether file/buffer is in text format (csv, json, etc.), or in binary
        mode (pickle, etc.)

    Returns
    -------
    f : file-like
        A file-like object
    handles : list of file-like objects
        A list of file-like object that were opened in this function.
    i(   t   S3Files:   compression with encoding is not yet supported in Python 2RP   Nt   fileobjRg   Rh   t   wt   ri   i    s   Zero files found in ZIP file {}s;   Multiple files found in ZIP file. Only one file per ZIP: {}Ri   s!   Unrecognized compression type: {}t   wbRQ   t   newlineR   t   errorst   replace(   t   TextIOWrappert   fileno($   t   s3fsRr   R   R@   t   listRH   R2   R\   R   Rq   t   PY2R_   RP   t   opent   GzipFileRg   t   BZ2FileR   t
   decompressRX   RY   t   BytesZipFilet   appendRS   t   namelistt   lent   popR`   t   import_lzmat   LZMAFilet   PY3t   ioRz   RB   t   MMapWrapperR.   (   t   path_or_bufRS   RQ   RR   t
   memory_mapt   is_textRr   t   need_text_wrappingt   handlesR"   t   is_pathRe   RP   Rg   t   zft	   zip_namest   lzmaRz   t   g(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   _get_handle:  s    						

R   c           B   s5   e  Z d  Z e j d  Z d   Z e d    Z RS(   s  
    Wrapper for standard library class ZipFile and allow the returned file-like
    handle to accept byte strings via `write` method.

    BytesIO provides attributes of file-like object and ZipFile.writestr writes
    bytes strings into a member of the archive.
    c         K   sD   | d k r! | j  d d  } n  t t |   j | | | |  d  S(   NRv   t   rbt   bR   (   Rv   R   (   Ry   t   superR   t   __init__(   R$   t   fileRS   RR   R!   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s    c         C   s    t  t |   j |  j |  d  S(   N(   R   R   t   writestrt   filename(   R$   t   data(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   write  s    c         C   s   |  j  d  k S(   N(   t   fpRW   (   R$   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   closed  s    (	   R'   R(   R)   t   zipfilet   ZIP_DEFLATEDR   R   t   propertyR   (    (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s   	R   c           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s  
    Wrapper for the Python's mmap class so that it can be properly read in
    by Python's csv.reader class.

    Parameters
    ----------
    f : file object
        File object to be mapped onto memory. Must support the 'fileno'
        method or have an equivalent attribute

    c         C   s(   t  j  | j   d d t  j |  _  d  S(   Ni    t   access(   R^   R{   t   ACCESS_READ(   R$   R"   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s    c         C   s   t  |  j |  S(   N(   t   getattrR^   (   R$   t   name(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   __getattr__  s    c         C   s   |  S(   N(    (   R$   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR%     s    c         C   sC   |  j  j   } t j r* t j |  } n  | d k r? t  n  | S(   NR   (   R^   t   readlineR\   R   t   bytes_to_strt   StopIteration(   R$   Rw   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR&     s    		(   R'   R(   R)   R   R   R%   R&   (    (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s
   			c         C   s
   |  j    S(   N(   R&   (   R$   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR*     s    t   UTF8Recoderc           B   s5   e  Z d  Z d   Z d d  Z d   Z d   Z RS(   sP   
    Iterator that reads an encoded stream and reencodes the input to UTF-8
    c         C   s   t  j |  |  |  _ d  S(   N(   t   codecst	   getreaderRd   (   R$   R"   RQ   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s    ic         C   s   |  j  j |  j d  S(   Ns   utf-8(   Rd   RX   t   encode(   R$   t   bytes(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyRX     s    c         C   s   |  j  j   j d  S(   Ns   utf-8(   Rd   R   R   (   R$   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s    c         C   s   t  |  j  j d  S(   Ns   utf-8(   t   nextRd   R   (   R$   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s    (   R'   R(   R)   R   RX   R   R   (    (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR     s
   		s   utf-8c         K   s   t  j |  d | | S(   Nt   dialect(   t   csvRd   (   R"   R   RQ   t   kwds(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   UnicodeReader  s    c         K   s   t  j |  d | | S(   NR   (   R   t   writer(   R"   R   RQ   R   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   UnicodeWriter   s    R   c           B   s)   e  Z d  Z e j d d  Z d   Z RS(   s   
        A CSV reader which will iterate over lines in the CSV file "f",
        which is encoded in the given encoding.

        On Python 3, this is replaced (below) by csv.reader, which handles
        unicode.
        s   utf-8c         K   s.   t  | |  } t j | d | | |  _ d  S(   NR   (   R   R   Rd   (   R$   R"   R   RQ   R   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR   -  s    c         C   s2   t  |  j  } g  | D] } t j | d  ^ q S(   Ns   utf-8(   R   Rd   R\   R   (   R$   t   rowt   s(    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR&   1  s    (   R'   R(   R)   R   t   excelR   R&   (    (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR   #  s   R   c           B   s2   e  Z d  Z e j d d  Z d   Z d   Z RS(   su   
        A CSV writer which will write rows to CSV file "f",
        which is encoded in the given encoding.
        s   utf-8c         K   sa   t    |  _ t j |  j d | | |  _ | |  _ t j |    |  _ | j d d   |  _
 d  S(   NR   t   quoting(   R   t   queueR   R   t   streamR   t   getincrementalencodert   encoderRV   RW   R   (   R$   R"   R   RQ   R   (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR   <  s
    	c            s     f d   } g  | D]- } | |  r. | n t  |  j d  ^ q }   j j g  | D] } | ^ qY    j j   } | j d  }   j j |  }   j j	 |    j j
 d  d  S(   Nc            s+     j  t j k r t |   p* t |  t  S(   N(   R   R   t   QUOTE_NONNUMERICR   R2   t   str(   t   x(   R$   (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   _check_as_isE  s    s   utf-8i    (   R   R   R   t   writerowR   t   getvaluet   decodeR   R   R   t   truncate(   R$   R   R   R   R   R   (    (   R$   s/   lib/python2.7/site-packages/pandas/io/common.pyR   D  s    7#c            s     f d   } xX t  |  D]J \ } } g  | D]- } | |  rG | n t |  j d  ^ q/ | | <q W  j j g  | D] } g  | D] } | ^ q ^ qz    j j   } | j d  }   j j |  }   j	 j
 |    j j d  d  S(   Nc            s+     j  t j k r t |   p* t |  t  S(   N(   R   R   R   R   R2   R   (   R   (   R$   (    s/   lib/python2.7/site-packages/pandas/io/common.pyR   X  s    s   utf-8i    (   t	   enumerateR   R   R   t	   writerowsR   R   R   R   R   R   R   (   R$   t   rowsR   t   iR   R   R   R   (    (   R$   s/   lib/python2.7/site-packages/pandas/io/common.pyR   W  s    ?6(   R'   R(   R)   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyR   5  s   	(M   R)   R   t
   contextlibR    R   R   R^   R3   R   t   pandas.compatR\   R   R   R   R   t   pandas.errorsR   R   R   R	   R
   t   pandas.core.dtypes.commonR   R   t   pandas.io.formats.printingR   t   CParserErrort
   _NA_VALUESR   t   urllib.requestR   R   R   t   urllib.parseR   R+   R   R   R   R   R   t   urllib.errorR   t   http.clientR   t   urllib2t   urllibt   httplibt	   functoolsR   t   setR-   t   discardt   objectR#   R   R1   R7   R;   RH   RL   RO   RW   R[   Rf   Rk   Rq   R/   R?   R   t   ZipFileR   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/pandas/io/common.pyt   <module>   s~   "(	("					+		9	
	6'		