σ
έ²k^c           @` sG  d  Z  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 d d l	 m
 Z
 d d l m Z m Z d d l m Z m Z d d l m Z d d	 l m Z d d
 l m Z e j e  Z e d    Z e d    Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   s  A specialized map implementation to manage configuration and context information.

Features:
  * Uses YAML configuration files
  * Use environment variables to override configs
  * Can pass a list of required parameters at initialization
  * Works with encrypted files
  * Accepts multiple config files
  * Can query information from consul
  * Does type coercion on strings
  * Composable configs
  * Ordered merge: downstream configs will override upstream
  * Reload from sources on SIGHUP

Available source types:
  * Environment variables
  * Yaml file on file system
  * Yaml file in S3
  * Consul
  * Hashicorp Vault

Notes:
  * Keys are case-insensitive.

i    (   t   absolute_importt   divisiont   print_functionNi   (   t   string_types(   t   memoizet   memoizemethod(   t   AssignmentErrort   NotFoundError(   t   PackageFile(   t   listify(   t   typifyc         C` sA   | j  d d  j  d d  } t d j d   |  | f D   S(   s7   Creates an environment key-equivalent for the given keyt   -t   _t    c         s` s   |  ] } | j    Vq d  S(   N(   t   upper(   t   .0t   x(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pys	   <genexpr>0   s    (   t   replacet   strt   join(   t   app_namet   key(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   make_env_key,   s    c         C` sK   |  j    d } | j |  s7 t d j | |    | t |  j   S(   NR   s#   {0} is not a(n) {1} environment key(   R   t
   startswitht   AssertionErrort   formatt   lent   lower(   R   R   t   app(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   reverse_env_key3   s    't   Configurationc           B` sΧ   e  Z d  Z d d d d  Z d   Z d   Z e d d  Z d   Z	 d   Z
 d   Z e d  Z e d	    Z d
   Z d d  Z d   Z d   Z d   Z e d  Z d   Z d   Z d   Z d   Z RS(   s3  A map implementation to manage configuration and context information. Values
    can be accessed (read, not assigned) as either a dict lookup (e.g. `config[key]`) or as an
    attribute (e.g. `config.key`).

    This class makes the foundational assumption of a yaml configuration file, as values in yaml
    are typed.

    This class allows overriding configuration keys with environment variables. Given an app name
    `foo` and a config parameter `bar: 15`, setting a `FOO_BAR` environment variable to `22` will
    override the value of `bar`. The type of `22` remains `int` because the underlying value of
    `15` is used to infer the type of the `FOO_BAR` environment variable. When an underlying
    parameter does not exist in a config file, the type is intelligently guessed.

    Args:
        app_name (str)
        config_sources (str or list, optional)
        required_parameters (iter, optional)

    Raises:
        InitializationError: on instantiation, when `required_parameters` are not found
        warns: on instantiation, when a given `config_file` cannot be read
        NotFoundError: when requesting a key that does not exist

    Examples:
        >>> for (key, value) in [('FOO_BAR', 22), ('FOO_BAZ', 'yes'), ('FOO_BANG', 'monkey')]:
        ...     os.environ[key] = str(value)

        >>> context = Configuration('foo')
        >>> context.bar, context.bar.__class__
        (22, <... 'int'>)
        >>> context['baz'], type(context['baz'])
        (True, <... 'bool'>)
        >>> context.bang, type(context.bang)
        ('monkey', <... 'str'>)

    c         C` s   | |  _  | p t j |   j |  _ t |  _ t   |  _ t	   |  _
 t   |  _ t t |   |  _ t t d  r |  j   n  |  j   |  j |  d  S(   Nt   SIGHUP(   t   appnamet   inspectt	   getmodulet   __package__t   packaget   Truet   _Configuration__initial_loadt   listt   _Configuration__sourcest   dictt   _config_mapt   sett   _registered_env_keysR	   t   _required_keyst   hasattrt   signalt%   _Configuration__set_up_sighup_handlert%   _Configuration__load_environment_keyst   append_sources(   t   selfR    t   config_sourcest   required_parametersR$   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __init__`   s    		
c         C` s1   t  } x$ t |  D] } |  j | |  q Wd  S(   N(   R%   R	   t   _Configuration__append_source(   R3   R4   t   force_reloadt   source(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR2   w   s    c         C` s   |  j  j t |   d  S(   N(   R-   t   updateR	   (   R3   R5   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   append_required|   s    c         C` s6   |  | _  |  j | |  | | _ |  j j |  d  S(   N(   t   parent_configt   _Configuration__load_sourcet   parent_sourceR(   t   append(   R3   R9   R8   t   _parent_source(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __append_source   s    		c         C` s   |  j    |  S(   N(   t$   _Configuration__ensure_required_keys(   R3   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   verify   s    
c         C` s=   t  |  t j t |  j |  <|  j j |  |  j   d S(   sΞ   Sets environment variables by prepending the app_name to `key`. Also registers the
        environment variable with the instance object preventing an otherwise-required call to
        `reload()`.
        N(   R   t   ost   environR   R    R,   t   addt   _clear_memoization(   R3   R   t   value(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   set_env   s    c         C` s=   t  j j t |  j |  d  |  j j |  |  j   d S(   sS   Removes an environment variable using the prepended app_name convention with `key`.N(	   RD   RE   t   popR   R    t   NoneR,   t   discardRG   (   R3   R   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt	   unset_env   s    c         C` sG   t    |  _ t   |  _ |  j |  |  j   |  j   |  j   d S(   sΰ   Reloads the configuration from the file and environment variables. Useful if using
        `os.environ` instead of this class' `set_env` method, or if the underlying configuration
        file is changed externally.
        N(   R)   R*   R+   R,   t   _Configuration__reload_sourcesR1   RC   RG   (   R3   t   force(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   _reload   s    

c         C` s’   | j    } | |  j k rm t j t |  j |   } |  j j |  } t | | d  k	 rf t
 |  n d   Sy |  j | SWn t k
 r } t |   n Xd  S(   N(   R   R,   RD   t   getenvR   R    R*   t   getR
   RK   t   typet   KeyErrorR   (   R3   R   t   from_envt   from_sourcest   e(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __getitem__€   s    %c         C` s   |  | S(   N(    (   R3   R   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __getattr__±   s    c         C` s%   y |  | SWn t  k
 r  | SXd  S(   N(   RT   (   R3   R   t   default(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyRR   ΄   s    c         G` s   t     d  S(   N(   R   (   R3   t   args(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __setitem__Ί   s    c         c` s0   x) |  j  t |  j j    BD] } | Vq Wd  S(   N(   R,   R+   R*   t   keys(   R3   R   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __iter__½   s    #c         c` s$   x |  D] } | |  | f Vq Wd  S(   N(    (   R3   R   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   itemsΑ   s    c   
      C` s  | r | j  r d  S| j |  } | j rP t | j  j |  rP t    n  | j d d   } t | t	  r | j
 d  } n  |  j t t |   O_ | j d d   } |  j j |  | rxI | D]> } | j   \ } } t   | |   }	 |  j |	 | |  qΝ Wn  d  S(   Nt   additional_requirementst   ,t   additional_sources(   R>   t   dumpt   providesR+   t   issubsett   NotImplementedErrorRJ   RK   t
   isinstanceR   t   splitR-   R	   R*   R:   t   popitemt   globalsR7   (
   R3   R9   R8   R_   R`   Rb   t   srct
   class_namet   kwargst   additional_source(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __load_sourceΕ   s     "c         C` sY   |  j  j   d } x? t j D]4 } | j |  r |  j j t |  j  |   q q Wd  S(   NR   (   R    R   RD   RE   R   R,   RF   R   (   R3   t
   app_prefixt   env_key(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __load_environment_keysέ   s    c         C` sQ   |  j  t |  j j    B} |  j | } | rM t d j t |     n  d  S(   NsX   Required key(s) not found in environment
  or configuration sources.
  Missing Keys: {0}(   R,   R+   R*   R]   R-   t   EnvironmentErrorR   R'   (   R3   t   available_keyst   missing_keys(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __ensure_required_keysζ   s
    	c         C` s   |  j  j d d   d  S(   Nt   _memoized_results(   t   __dict__RJ   RK   (   R3   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyRG   ξ   s    c         ` s;     f d   } t  j t  j    _ t  j  t  j |  d  S(   Nc         ` sF   |  t  j k r d  S  j t  t   j  rB   j |  |  n  d  S(   N(   R/   R   RP   R%   t   callablet'   _Configuration__previous_sighup_handler(   t   signumt   frame(   R3   (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   sighup_handlerς   s
    (   R/   t	   getsignalR   Rz   (   R3   R}   (    (   R3   sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   __set_up_sighup_handlerρ   s    N(   t   __name__t
   __module__t   __doc__RK   R6   R2   R;   t   FalseR7   RC   RI   RM   RP   R   RX   RY   RR   R\   R^   R_   R=   R1   RB   RG   R0   (    (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR   :   s(   $														t   Sourcec           B` s   e  Z d Z d Z d Z e d     Z e d    Z d   Z	 e
 d  Z e d    Z e j d    Z e d    Z e j d    Z RS(	   c         C` s   |  j  S(   N(   t	   _provides(   R3   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyRd     s    c         C` s
   |  j    S(   N(   Rc   (   R3   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR_     s    c         C` s   t     d S(   s   Must return a key, value dictN(   Rf   (   R3   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   load	  s    c         C` s.   |  j  d  k s | r' |  j   |  _  n  |  j  S(   N(   t   _itemsRK   R   (   R3   R8   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyRc     s    c         C` s   |  j  S(   N(   t   _parent_config(   R3   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR<     s    c         C` s   | |  _  d  S(   N(   R   (   R3   R<   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR<     s    c         C` s   |  j  S(   N(   R@   (   R3   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR>     s    c         C` s   | |  _  d  S(   N(   R@   (   R3   R>   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR>     s    N(   R   R   RK   R   R   R@   t   propertyRd   R_   R   R   Rc   R<   t   setterR>   (    (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR   ό   s   	t
   YamlSourcec           B` s   e  Z d d   Z d   Z RS(   c         C` s"   | |  _  | r | n d  |  _ d  S(   N(   t	   _locationRK   R   (   R3   t   locationRd   (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR6   %  s    	c         ` sp   t  |  j |  j j  R } d d  l } | j |    |  j d  k rI   St   f d   |  j D  SWd  QXd  S(   Ni    c         3` s   |  ] } |   | f Vq d  S(   N(    (   R   R   (   t   contents(    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pys	   <genexpr>0  s    (	   R   R   R<   R$   t   yamlR   Rd   RK   R)   (   R3   t   fhR   (    (   R   sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR   )  s    N(   R   R   RK   R6   R   (    (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR   #  s   t   EnvironmentMappedSourcec           B` s    e  Z d  Z d   Z d   Z RS(   sE   Load a full Source object given the value of an environment variable.c         C` s   | |  _  | |  _ d  S(   N(   t   _envvart
   _sourcemap(   R3   t   envvart	   sourcemap(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR6   6  s    	c         C` s3   |  j  |  j |  j } |  j | _ | j   } | S(   N(   R   R<   R   R   (   R3   t   mapped_sourcet   params(    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR   :  s    (   R   R   R   R6   R   (    (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyR   3  s   	(    R   t
   __future__R    R   R   R!   t   loggingRD   R/   t   compatR   t
   decoratorsR   R   t
   exceptionsR   R   t   pathR   t   type_coercionR	   R
   t	   getLoggerR   t   logR   R   t   objectR   R   R   R   (    (    (    sA   lib/python2.7/site-packages/conda/_vendor/auxlib/configuration.pyt   <module>   s$   Β'