ó
¦–Õ\c           @` sé   d  d l  m Z m Z 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 d d „ Z d d „ Z e
 r d e f d	 „  ƒ  YZ n e Z i d
 d 6d d 6d d 6Z d „  Z d „  Z d d „ Z d „  Z d S(   i    (   t   print_functiont   divisiont   absolute_importN(   t   identityi   (   t   PY2t   urlsplitc         C` s«  t  j d |  ƒ r$ i d d 6|  d 6St |  ƒ } | j p< d } | j } | d k r‚ t  j d | ƒ } | r‚ d | j ƒ  } q‚ n  | d k r  i | d 6|  d 6Si | d 6| d 6} | j r_| j j d	 d
 ƒ d j d d
 ƒ d } | d k r| | d | d <n
 | | d <| j r*| j | d <n  | j	 rC| j	 | d <n  | j
 r_| j
 | d <q_n  | j rx| j | d <n  | j r‘| j | d <n  | r§t | | ƒ n  | S(   s¤   Infer storage options from URL path and merge it with existing storage
    options.

    Parameters
    ----------
    urlpath: str or unicode
        Either local absolute file path or URL (hdfs://namenode:8020/file.csv)
    storage_options: dict (optional)
        Its contents will get merged with the inferred information from the
        given path

    Returns
    -------
    Storage options dict.

    Examples
    --------
    >>> infer_storage_options('/mnt/datasets/test.csv')  # doctest: +SKIP
    {"protocol": "file", "path", "/mnt/datasets/test.csv"}
    >>> infer_storage_options(
    ...          'hdfs://username:pwd@node:123/mnt/datasets/test.csv?q=1',
    ...          inherit_storage_options={'extra': 'value'})  # doctest: +SKIP
    {"protocol": "hdfs", "username": "username", "password": "pwd",
    "host": "node", "port": 123, "path": "/mnt/datasets/test.csv",
    "url_query": "q=1", "extra": "value"}
    s   ^[a-zA-Z]:[\\/]t   filet   protocolt   paths   ^/([a-zA-Z])[:|]([\\/].*)$s   %s:%st   httpt   httpst   @i   iÿÿÿÿt   :i    t   s3t   gcst   gst   hostt   portt   usernamet   passwordt	   url_queryt   url_fragment(   R	   R
   (   R   R   R   (   t   ret   matchR   t   schemeR   t   groupst   netloct   rsplitR   R   R   t   queryt   fragmentt   update_storage_options(   t   urlpatht   inherit_storage_optionst   parsed_pathR   R   t   windows_patht   optionsR   (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   infer_storage_options   sB    
	
	)
					c         C` sh   | s i  } n  t  |  ƒ t  | ƒ @} | rW d j d „  | Dƒ ƒ } t d | ƒ ‚ n  |  j | ƒ d  S(   Ns   
c         s` s   |  ] } d  | Vq d S(   s   - %rN(    (   t   .0t   k(    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pys	   <genexpr>e   s    s<   Collision between inferred and specified storage options:
%s(   t   sett   joint   KeyErrort   update(   R#   t	   inheritedt
   collisions(    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyR   `   s    	t   SeekableFilec           B` sG   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` s(   t  | t ƒ r | j } n  | |  _ d  S(   N(   t
   isinstanceR-   R   (   t   selfR   (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   __init__m   s    c         C` s   t  S(   N(   t   True(   R/   (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   seekabler   s    c         C` s6   y |  j  j ƒ  SWn t k
 r1 d |  j  j k SXd  S(   Nt   r(   R   t   readablet   AttributeErrort   mode(   R/   (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyR4   u   s    c         C` s6   y |  j  j ƒ  SWn t k
 r1 d |  j  j k SXd  S(   Nt   w(   R   t   writableR5   R6   (   R/   (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyR8   {   s    c         O` s   |  j  j | | Ž  S(   N(   R   t   read(   R/   t   argst   kwargs(    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   read1   s    c         C` s   |  j  j ƒ  S(   N(   R   t   __iter__(   R/   (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyR=   „   s    c         C` s   t  |  j | ƒ S(   N(   t   getattrR   (   R/   t   key(    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   __getattr__‡   s    (	   t   __name__t
   __module__R0   R2   R4   R8   R<   R=   R@   (    (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyR-   l   s   						t   gzipt   gzt   bz2t   xzc         C` s/   t  j j |  ƒ d j d ƒ } t j | d  ƒ S(   Niÿÿÿÿt   .(   t   osR   t   splitextt   stript   compressionst   gett   None(   t   filenamet	   extension(    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   infer_compression   s    c         C` s½   |  j  ƒ  d k r d Sd } xš t r¸ |  j | ƒ } | s> d S| | } yB | j | ƒ } |  j |  j  ƒ  t | ƒ | t | ƒ ƒ d SWn t t f k
 r£ n X| t | ƒ } q Wd S(   sœ   Seek current file to next byte after a delimiter bytestring

    This seeks the file to the next byte following the delimiter.  It does
    not return anything.  Use ``file.tell()`` to see location afterwards.

    Parameters
    ----------
    file: a file
    delimiter: bytes
        a delimiter like ``b'
'`` or message sentinel
    blocksize: int
        Number of bytes to read from the file at once.
    i    Nt    (   t   tellR1   R9   t   indext   seekt   lent   OSErrort
   ValueError(   R   t	   delimitert	   blocksizet   lastt   currentt   fullt   i(    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   seek_delimiter•   s    	
+c         C` s	  | |  j  ƒ  k r" |  j | ƒ n  | rQ | d k rQ |  j  ƒ  d k rQ |  j ƒ  S| rü t |  | d ƒ |  j  ƒ  } | | | 8} y% |  j | | ƒ t |  | d ƒ Wn' t t f k
 rÏ |  j d d ƒ n X|  j  ƒ  } | } | | } |  j | ƒ n  |  j | ƒ S(   sé   Read a block of bytes from a file

    Parameters
    ----------
    f: File
    offset: int
        Byte offset to start read
    length: int
        Number of bytes to read
    delimiter: bytes (optional)
        Ensure reading starts and stops at delimiter bytestring

    If using the ``delimiter=`` keyword argument we ensure that the read
    starts and stops at delimiter boundaries that follow the locations
    ``offset`` and ``offset + length``.  If ``offset`` is zero then we
    start at zero.  The bytestring returned WILL include the
    terminating delimiter string.

    Examples
    --------

    >>> from io import BytesIO  # doctest: +SKIP
    >>> f = BytesIO(b'Alice, 100\nBob, 200\nCharlie, 300')  # doctest: +SKIP
    >>> read_block(f, 0, 13)  # doctest: +SKIP
    b'Alice, 100\nBo'

    >>> read_block(f, 0, 13, delimiter=b'\n')  # doctest: +SKIP
    b'Alice, 100\nBob, 200\n'

    >>> read_block(f, 10, 10, delimiter=b'\n')  # doctest: +SKIP
    b'Bob, 200\nCharlie, 300'
    i    i   i   Ni   i   (   RR   RT   RM   R9   R^   RV   RW   (   t   ft   offsett   lengthRX   t   startt   end(    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt
   read_block¶   s$    !%

c         ` s;   |  d 7}  t  t j t j |  ƒ ƒ ƒ ‰  ‡  f d †  } | S(   sŽ   Returns a function that receives a single integer
    and returns it as a string padded by enough zero characters
    to align with maximum possible integer

    >>> name_f = build_name_function(57)

    >>> name_f(7)
    '07'
    >>> name_f(31)
    '31'
    >>> build_name_function(1000)(42)
    '0042'
    >>> build_name_function(999)(42)
    '042'
    >>> build_name_function(0)(0)
    '0'
    g:Œ0âŽyE>c         ` s   t  |  ƒ j ˆ  ƒ S(   N(   t   strt   zfill(   R]   (   t
   pad_length(    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   name_function  s    (   t   intt   matht   ceilt   log10(   t   max_intRh   (    (   Rg   s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   build_name_functionñ   s    
(   t
   __future__R    R   R   Rj   RH   R   t   toolzR   t   compatibilityR   R   RM   R$   R   t   objectR-   RK   RP   R^   Rd   Rn   (    (    (    s/   lib/python2.7/site-packages/dask/bytes/utils.pyt   <module>   s   T		!;