ó
¦–Õ\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 Z y d  d l Z Wn e	 k
 ru d Z n Xd d l m Z m Z m Z d Z e j d d ƒ e j j e j d d	 ƒ e j j e j j d
 ƒ d d	 ƒ e j j e j j d
 ƒ d ƒ g Z d e j k r0e j d Z e j e ƒ n$ e j j e j j d
 ƒ d d	 ƒ Z i  Z Z e j ƒ  Z g  Z d „  Z d d „ Z d „  Z  e d „ Z! d d „ Z" d e# d „ Z$ d e% f d „  ƒ  YZ& e d d „ Z' e e d „ Z( e e d „ Z) e d „ Z* e e d „ Z+ d „  Z, e( ƒ  e rŽe j j e j j- e. ƒ d ƒ Z/ e$ d e/ ƒ e0 e/ ƒ  Z1 e j2 e1 ƒ Z3 Wd QXe+ e3 ƒ [/ [3 n  d S(   i    (   t   print_functiont   divisiont   absolute_importNi   (   t   makedirst   builtinst   Mappingt   __no_default__t   DASK_ROOT_CONFIGs	   /etc/daskt   etct   daskt   ~s   .configs   .daskt   DASK_CONFIGc         C` sm   y |  | k r |  SWn t  k
 r( |  SXd |  k rG |  j d d ƒ n |  j d d ƒ } | | k ri | S|  S(   s@  Return the canonical name for a key.

    Handles user choice of '-' or '_' conventions by standardizing on whichever
    version was set first. If a key already exists in either hyphen or
    underscore form, the existing version is the canonical name. If neither
    version exists the original key is used as is.
    t   _t   -(   t	   TypeErrort   replace(   t   kt   configt   altk(    (    s*   lib/python2.7/site-packages/dask/config.pyt   canonical_name)   s    0t   newc         C` s§   x  | j  ƒ  D]’ \ } } t | |  ƒ } t | t ƒ rz | |  k sS |  | d k r` i  |  | <n  t |  | | d | ƒq | d k s’ | |  k r | |  | <q q W|  S(   sã   Update a nested dictionary with values from another

    This is like dict.update except that it smoothly merges nested values

    This operates in-place and modifies old

    Parameters
    ----------
    priority: string {'old', 'new'}
        If new (default) then the new dictionary has preference.
        Otherwise the old dictionary does.

    Examples
    --------
    >>> a = {'x': 1, 'y': {'a': 2}}
    >>> b = {'x': 2, 'y': {'b': 3}}
    >>> update(a, b)  # doctest: +SKIP
    {'x': 2, 'y': {'a': 2, 'b': 3}}

    >>> a = {'x': 1, 'y': {'a': 2}}
    >>> b = {'x': 2, 'y': {'b': 3}}
    >>> update(a, b, priority='old')  # doctest: +SKIP
    {'x': 1, 'y': {'a': 2, 'b': 3}}

    See Also
    --------
    dask.config.merge
    t   priorityR   N(   t   itemsR   t
   isinstanceR   t   Nonet   update(   t   oldR   R   R   t   v(    (    s*   lib/python2.7/site-packages/dask/config.pyR   @   s    c          G` s(   i  } x |  D] } t  | | ƒ q W| S(   sT   Update a sequence of nested dictionaries

    This prefers the values in the latter dictionaries to those in the former

    Examples
    --------
    >>> a = {'x': 1, 'y': {'a': 2}}
    >>> b = {'y': {'b': 3}}
    >>> merge(a, b)  # doctest: +SKIP
    {'x': 1, 'y': {'a': 2, 'b': 3}}

    See Also
    --------
    dask.config.update
    (   R   (   t   dictst   resultt   d(    (    s*   lib/python2.7/site-packages/dask/config.pyt   mergek   s    c      	   C` s<  g  } x½ |  D]µ } t  j j | ƒ r t  j j | ƒ r² yd | j t g  t  j | ƒ D]= } t  j j | ƒ d j ƒ  d k rS t  j j	 | | ƒ ^ qS ƒ ƒ Wq¿ t
 k
 r® q¿ XqÂ | j | ƒ q q Wg  } xi | D]a } yA t | ƒ / } t j | j ƒ  ƒ pi  } | j | ƒ Wd QXWqÓ t
 t f k
 r3qÓ XqÓ W| S(   sž    Collect configuration from yaml files

    This searches through a list of paths, expands to find all yaml or json
    files, and then parses each file.
    i   s   .jsons   .yamls   .ymlN(   s   .jsons   .yamls   .yml(   t   ost   patht   existst   isdirt   extendt   sortedt   listdirt   splitextt   lowert   joint   OSErrort   appendt   opent   yamlt	   safe_loadt   readt   IOError(   t   pathst
   file_pathsR!   t   pt   configst   ft   data(    (    s*   lib/python2.7/site-packages/dask/config.pyt   collect_yaml   s*    	Ec         C` s¾   |  d k r t j }  n  i  } xƒ |  j ƒ  D]u \ } } | j d ƒ r+ | d j ƒ  j d d ƒ } y t j | ƒ | | <Wq  t	 t
 f k
 rœ | | | <q  Xq+ q+ Wi  } t | d | ƒ| S(   s‘   Collect config from environment variables

    This grabs environment variables of the form "DASK_FOO__BAR_BAZ=123" and
    turns these into config variables of the form ``{"foo": {"bar-baz": 123}}``
    It transforms the key and value in the following way:

    -  Lower-cases the key text
    -  Treats ``__`` (double-underscore) as nested access
    -  Calls ``ast.literal_eval`` on the value
    t   DASK_i   t   __t   .R   N(   R   R    t   environR   t
   startswithR(   R   t   astt   literal_evalt   SyntaxErrort
   ValueErrort   set(   t   envR   t   namet   valuet   varnameR   (    (    s*   lib/python2.7/site-packages/dask/config.pyt   collect_env§   s    c         C` su  | d k r t } n  t j j | ƒ r+ d S| } t j j | t j j |  ƒ ƒ } yt j j | ƒ s\t | d t	 ƒd | t j
 ƒ  f } t |  ƒ  } t | ƒ } Wd QX| rö g  | D]2 } | j ƒ  rç | j d ƒ rç d | n | ^ q» } n  t | d ƒ  } | j d j | ƒ ƒ Wd QXy t j | | ƒ Wq\t k
 rXt j | ƒ q\Xn  Wn t k
 rpn Xd S(   s   
    Copy file to default location if it does not already exist

    This tries to move a default configuration file to a default location if
    if does not already exist.  It also comments out that file by default.

    This is to be used by downstream modules (like dask.distributed) that may
    have default configuration files that they wish to include in the default
    configuration path.

    Parameters
    ----------
    source : string, filename
        Source configuration file, typically within a source directory.
    destination : string, directory
        Destination directory. Configurable by ``DASK_CONFIG`` environment
        variable, falling back to ~/.config/dask.
    comment : bool, True by default
        Whether or not to comment out the config file when copying.
    Nt   exist_oks	   %s.tmp.%dt   #s   # t   wt    (   R   t   PATHR    R!   t   isfileR)   t   basenameR"   R   t   Truet   getpidR,   t   listt   stripR<   t   writet   renameR*   t   remove(   t   sourcet   destinationt   commentt	   directoryt   tmpR5   t   linest   line(    (    s*   lib/python2.7/site-packages/dask/config.pyt   ensure_fileÃ   s.    	!?RA   c           B` sG   e  Z d  Z d e e d „ Z d „  Z d „  Z e	 d g  d „ ƒ Z
 RS(   sÞ    Temporarily set configuration values within a context manager

    Examples
    --------
    >>> import dask
    >>> with dask.config.set({'foo': 123}):
    ...     pass

    See Also
    --------
    dask.config.get
    c         K` s{   | r | r | } n  | Y | |  _  i  |  _ x? | j ƒ  D]1 \ } } |  j | j d ƒ | | d |  j ƒq< WWd  QXd  S(   NR:   R   (   R   R   R   t   _assignt   split(   t   selft   argR   t   lockt   kwargst   keyRD   (    (    s*   lib/python2.7/site-packages/dask/config.pyt   __init__  s    			c         C` s   |  j  S(   N(   R   (   R_   (    (    s*   lib/python2.7/site-packages/dask/config.pyt	   __enter__  s    c         C` s¦   xŸ |  j  j ƒ  D]Ž \ } } | d k rˆ |  j } y@ x. t | ƒ d k rd | | d } | d } q7 W| | d =Wqž t k
 r„ qž Xq |  j | | |  j ƒ q Wd  S(   Ns
   --delete--i   i    (   R   R   R   t   lent   KeyErrorR]   (   R_   t   typeRD   t	   tracebackt   keysR   (    (    s*   lib/python2.7/site-packages/dask/config.pyt   __exit__  s    	c         C` sò   t  | d | ƒ } t | ƒ d k r{ | d k	 rn t | | g ƒ } | | k ra | | | | <qn d | | <n  | | | <ns | | k rÀ i  | | <| d k	 r· d | t | | g ƒ <n  d } n  |  j | d | | | d | | g d | ƒd S(   s.   Assign value into a nested configuration dictionary

        Optionally record the old values in old

        Parameters
        ----------
        keys: Sequence[str]
            The nested path of keys to assign the value, similar to toolz.put_in
        value: object
        d: dict
            The part of the nested dictionary into which we want to assign the
            value
        old: dict, optional
            If provided this will hold the old values
        path: List[str]
            Used internally to hold the path of old values
        i    i   s
   --delete--R!   R   N(   R   Rf   R   t   tupleR]   (   t   clsRj   RD   R   R   R!   Rc   t   path_key(    (    s*   lib/python2.7/site-packages/dask/config.pyR]   ,  s    
	N(   t   __name__t
   __module__t   __doc__R   R   t   config_lockRd   Re   Rk   t   classmethodR]   (    (    (    s*   lib/python2.7/site-packages/dask/config.pyRA     s   		c         C` s]   | d k r t j } n  g  } t r= | j t d |  ƒ ƒ n  | j t d | ƒ ƒ t | Œ  S(   s}  
    Collect configuration from paths and environment variables

    Parameters
    ----------
    paths : List[str]
        A list of paths to search for yaml config files

    env : dict
        The system environment variables

    Returns
    -------
    config: dict

    See Also
    --------
    dask.config.refresh: collect configuration and update into primary config
    R1   RB   N(	   R   R    R;   R-   R$   R7   R+   RF   R   (   R1   RB   R4   (    (    s*   lib/python2.7/site-packages/dask/config.pyt   collectQ  s    c         K` sE   |  j  ƒ  x! | D] } t |  | d d ƒq Wt |  t |   ƒ d S(   s  
    Update configuration by re-reading yaml files and env variables

    This mutates the global dask.config.config, or the config parameter if
    passed in.

    This goes through the following stages:

    1.  Clearing out all old configuration
    2.  Updating from the stored defaults from downstream libraries
        (see update_defaults)
    3.  Updating from yaml files and environment variables

    Note that some functionality only checks configuration once at startup and
    may not change behavior, even if configuration changes.  It is recommended
    to restart your python process if convenient to ensure that new
    configuration changes take place.

    See Also
    --------
    dask.config.collect: for parameters
    dask.config.update_defaults
    R   R   N(   t   clearR   Rt   (   R   t   defaultsRb   R   (    (    s*   lib/python2.7/site-packages/dask/config.pyt   refreshq  s    
c         C` sw   |  j  d ƒ } | } x[ | D]S } t | | ƒ } y | | } Wq t t t f k
 rn | t k	 rh | S‚  q Xq W| S(   sn  
    Get elements from global config

    Use '.' for nested access

    Examples
    --------
    >>> from dask import config
    >>> config.get('foo')  # doctest: +SKIP
    {'x': 1, 'y': 2}

    >>> config.get('foo.x')  # doctest: +SKIP
    1

    >>> config.get('foo.x.y', default=123)  # doctest: +SKIP
    123

    See Also
    --------
    dask.config.set
    R:   (   R^   R   R   t
   IndexErrorRg   t
   no_default(   Rc   t   defaultR   Rj   R   R   (    (    s*   lib/python2.7/site-packages/dask/config.pyt   get‘  s    c         C` s   g  } i  } xU |  j  ƒ  D]G \ } } t | d d | ƒ} | d k	 r | j | ƒ | | | <q q Wx | D] } | | =qk Wt | d | ƒd S(   s`    Rename old keys to new keys

    This helps migrate older configuration versions over time
    R   N(   R   R{   R   R+   RA   (   t   aliasesR   R   R   t   ot   nRD   R   (    (    s*   lib/python2.7/site-packages/dask/config.pyRS   µ  s    c         C` s$   | j  |  ƒ t | |  d d ƒd S(   s	   Add a new set of defaults to the configuration

    It does two things:

    1.  Add the defaults to a global collection to be used by refresh later
    2.  Updates the global config with the new configuration
        prioritizing older values over newer ones
    R   R   N(   R+   R   (   R   R   Rv   (    (    s*   lib/python2.7/site-packages/dask/config.pyt   update_defaultsÈ  s    	c         C` sŽ   t  |  t ƒ r# d „  |  j ƒ  Dƒ St  |  t ƒ rB t j j |  ƒ St  |  t t t	 j
 f ƒ r† t |  ƒ g  |  D] } t | ƒ ^ qm ƒ S|  Sd S(   sÝ   Expand environment variables in a nested config dictionary

    This function will recursively search through any nested dictionaries
    and/or lists.

    Parameters
    ----------
    config : dict, iterable, or str
        Input object to search for environment variables

    Returns
    -------
    config : same type as input

    Examples
    --------
    >>> expand_environment_variables({'x': [1, 2, '$USER']})  # doctest: +SKIP
    {'x': [1, 2, 'my-username']}
    c         S` s%   i  |  ] \ } } t  | ƒ | “ q S(    (   t   expand_environment_variables(   t   .0R   R   (    (    s*   lib/python2.7/site-packages/dask/config.pys
   <dictcomp>ê  s   	 N(   R   R   R   t   strR    R!   t
   expandvarsRP   Rl   R   RA   Rh   R€   (   R   R   (    (    s*   lib/python2.7/site-packages/dask/config.pyR€   Õ  s    )s	   dask.yamlRU   (4   t
   __future__R    R   R   R=   R    t   syst	   threadingR-   t   ImportErrorR   t   compatibilityR   R   R   Ry   t   getenvR!   R)   t   prefixt
   expanduserR1   R;   RK   R+   t   global_configR   t   LockRr   Rv   R   R   R   R7   RF   RN   R\   t   objectRA   Rt   Rw   R{   RS   R   R€   t   dirnamet   __file__t   fnR,   R5   R.   t	   _defaults(    (    (    s*   lib/python2.7/site-packages/dask/config.pyt   <module>   sV   
!$$
	+	&=N  $	!
