o
    tBhLd                     @   s   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m	Z	 ddl
mZ ddl
mZ ddl
mZ dd	lmZmZmZ dd
lmZmZmZ dZdZdZdZG dd deZG dd deZG dd deZG dd deZG dd deZdS )z%Worry-free YAML configuration files.
    )divisionabsolute_importprint_functionN)OrderedDict   )util)	templates)	yaml_util)ConfigSource	EnvSource
YamlSource)ConfigTypeErrorNotFoundErrorConfigErrorzconfig.yamlzconfig_default.yamlrootREDACTEDc                   @   sF  e Zd ZdZdZ	 dd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdJddZdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- ZdJd.d/Zejfd0d1Zd2d3 Zd4d5 Zd6d7 Z d8d9 Z!dKd;d<Z"dLd=d>Z#d?d@ Z$dAdB Z%e&dCdD Z'e'j(dEdD Z'dFdG Z)dHdI Z*dS )M
ConfigViewal  A configuration "view" is a query into a program's configuration
    data. A view represents a hypothetical location in the configuration
    tree; to extract the data from the location, a client typically
    calls the ``view.get()`` method. The client can access children in
    the tree (subviews) by subscripting the parent view (i.e.,
    ``view[key]``).
    Nc                 C      t )zThe core (internal) data retrieval method. Generates (value,
        source) pairs for each source that contains a value for this
        view. May raise `ConfigTypeError` if a type error occurs while
        traversing a source.
        NotImplementedErrorself r   c/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/confuse/core.pyresolve7   s   zConfigView.resolvec                 C   s4   |   }zt|W S  ty   td| jw )zReturn a (value, source) pair for the first object found for
        this view. This amounts to the first element returned by
        `resolve`. If no values are available, a `NotFoundError` is
        raised.
        z{0} not found)r   r   
iter_first
ValueErrorr   formatname)r   pairsr   r   r   first?   s   zConfigView.firstc                 C   s$   z|    W dS  ty   Y dS w )z@Determine whether the view has a setting in any source.
        FT)r    r   r   r   r   r   existsK   s   
zConfigView.existsc                 C   r   )zSet the *default* value for this configuration view. The
        specified value is added as the lowest-priority configuration
        data source.
        r   r   valuer   r   r   addT      zConfigView.addc                 C   r   )z*Override* the value for this configuration view. The
        specified value is added as the highest-priority configuration
        data source.
        r   r"   r   r   r   set[   r%   zConfigView.setc                 C   r   )z?The RootView object from which this view is descended.
        r   r   r   r   r   r   b   s   zConfigView.rootc                 C   s   d | jj| jS )Nz<{}: {}>)r   	__class____name__r   r   r   r   r   __repr__g   s   zConfigView.__repr__c                 c   s|    z|   D ]}|V  qW dS  ty=   z|  D ]}|V  qW Y dS  ty<   |  \}}td| jt|jw w )z]Iterate over the keys of a dictionary view or the *subviews*
        of a list view.
        z+{0} must be a dictionary or a list, not {1}N)keysr   sequencer    r   r   typer(   )r   keysubviewitem_r   r   r   __iter__j   s&   zConfigView.__iter__c                 C   s
   t | |S )zGet a subview of this view.)Subviewr   r-   r   r   r   __getitem__   s   
zConfigView.__getitem__c                 C   s   |  ||i dS )zPCreate an overlay source to assign a given key under this
        view.
        N)r&   )r   r-   r#   r   r   r   __setitem__      zConfigView.__setitem__c                 C   s   | |   S N)r!   r3   r   r   r   __contains__   s   zConfigView.__contains__Fc                 C   s    |  tj||r	dndd dS )aS  Overlay parsed command-line arguments, generated by a library
        like argparse or optparse, onto this view's value.

        :param namespace: Dictionary or Namespace to overlay this config with.
            Supports nested Dictionaries and Namespaces.
        :type namespace: dict or Namespace
        :param dots: If True, any properties on namespace that contain dots (.)
            will be broken down into child dictionaries.
            :Example:

            {'foo.bar': 'car'}
            # Will be turned into
            {'foo': {'bar': 'car'}}
        :type dots: bool
        . )sepN)r&   r   
build_dict)r   	namespacedotsr   r   r   set_args   s    zConfigView.set_argsc                 C   s   t jr|  S t|  S )z5Get the value for this view as a bytestring.
        )r   PY3__unicode__bytesgetr   r   r   r   __str__   s   zConfigView.__str__c                 C   s   t |  S )z9Get the value for this view as a Unicode string.
        )r   STRINGrC   r   r   r   r   rA         zConfigView.__unicode__c                 C   s   |   S )zDGets the value for this view as a boolean. (Python 2 only.)
        )__bool__r   r   r   r   __nonzero__   s   zConfigView.__nonzero__c                 C   s   t |  S )zDGets the value for this view as a boolean. (Python 3 only.)
        )boolrC   r   r   r   r   rG      s   zConfigView.__bool__c              
   C   sj   g }|   D ],\}}z| }W n ty#   td| jt|jw |D ]}||vr1|| q&q|S )a  Returns a list containing all the keys available as subviews
        of the current views. This enumerates all the keys in *all*
        dictionaries matching the current view, in contrast to
        ``view.get(dict).keys()``, which gets all the keys for the
        *first* dict matching the view. If the object for this view in
        any source is not a dict, then a `ConfigTypeError` is raised. The
        keys are ordered according to how they appear in each source.
        z{0} must be a dict, not {1})	r   r*   AttributeErrorr   r   r   r,   r(   append)r   r*   dicr0   cur_keysr-   r   r   r   r*      s"   	
zConfigView.keysc                 c   s"    |   D ]	}|| | fV  qdS )zIterates over (key, subview) pairs contained in dictionaries
        from *all* sources at this view. If the object for this view in
        any source is not a dict, then a `ConfigTypeError` is raised.
        Nr*   r3   r   r   r   items   s   zConfigView.itemsc                 c   s    |   D ]}| | V  qdS )zIterates over all the subviews contained in dictionaries from
        *all* sources at this view. If the object for this view in any
        source is not a dict, then a `ConfigTypeError` is raised.
        NrN   r3   r   r   r   values   s   zConfigView.valuesc                 c   sn    z|   \}}W n
 ty   Y dS w t|ttfs'td| jt|j	t
t|D ]}| | V  q-dS )zIterates over the subviews contained in lists from the *first*
        source at this view. If the object for this view in the first source
        is not a list or tuple, then a `ConfigTypeError` is raised.
        Nz{0} must be a list, not {1})r    r   
isinstancelisttupler   r   r   r,   r(   rangelen)r   
collectionr0   indexr   r   r   r+      s   zConfigView.sequencec              
   c   s\    |   D ]&\}}zt|}W n ty"   td| jt|jw |D ]}|V  q%qdS )a_  Iterates over all subviews from collections at this view from
        *all* sources. If the object for this view in any source is not
        iterable, then a `ConfigTypeError` is raised. This method is
        intended to be used when the view indicates a list; this method
        will concatenate the contents of the list from all sources.
        z {0} must be an iterable, not {1}N)r   iter	TypeErrorr   r   r   r,   r(   )r   rV   r0   itr#   r   r   r   all_contents   s   zConfigView.all_contentsc              	   C   sb   t  }|  D ]'\}}|r|jrt||< qz
|j|d||< W q ty.   | ||< Y qw |S )a  Create a hierarchy of OrderedDicts containing the data from
        this view, recursively reifying all views to get their
        represented values.

        If `redact` is set, then sensitive values are replaced with
        the string "REDACTED".
        redact)r   rO   r]   REDACTED_TOMBSTONEflattenr   rC   )r   r]   odr-   viewr   r   r   r_     s   

zConfigView.flattenc                 C   s   t || |S )a  Retrieve the value for this view according to the template.

        The `template` against which the values are checked can be
        anything convertible to a `Template` using `as_template`. This
        means you can pass in a default integer or string value, for
        example, or a type to just check that something matches the type
        you expect.

        May raise a `ConfigValueError` (or its subclass,
        `ConfigTypeError`) or a `NotFoundError` when the configuration
        doesn't satisfy the template.
        )r   as_templater#   )r   templater   r   r   rC   '  s   zConfigView.getc                 C      |  t S )zBGet the value as a path. Equivalent to `get(Filename())`.
        )rC   r   Filenamer   r   r   r   as_filename8  rF   zConfigView.as_filenamec                 C   rd   )zOGet the value as a `pathlib.Path` object. Equivalent to `get(Path())`.
        )rC   r   Pathr   r   r   r   as_path=  rF   zConfigView.as_pathc                 C   s   |  t|S )z\Get the value from a list of choices. Equivalent to
        `get(Choice(choices))`.
        )rC   r   Choice)r   choicesr   r   r   	as_choiceB  s   zConfigView.as_choicec                 C   rd   )z_Get the value as any number type: int or float. Equivalent to
        `get(Number())`.
        )rC   r   Numberr   r   r   r   	as_numberH     zConfigView.as_numberTc                 C      |  tj|dS )zbGet the value as a sequence of strings. Equivalent to
        `get(StrSeq(split=split))`.
        )split)rC   r   StrSeq)r   rp   r   r   r   
as_str_seqN  r6   zConfigView.as_str_seqc                 C   ro   )z~Get the value as a sequence of pairs of two strings. Equivalent to
        `get(Pairs(default_value=default_value))`.
        )default_value)rC   r   Pairs)r   rs   r   r   r   as_pairsT  r6   zConfigView.as_pairsc                 C   rd   )zzGet the value as a (Unicode) string. Equivalent to
        `get(unicode)` on Python 2 and `get(str)` on Python 3.
        rC   r   Stringr   r   r   r   as_strZ  rn   zConfigView.as_strc                 C   s   |  tjddS )zgGet the value as a (Unicode) string, with env vars
        expanded by `os.path.expandvars()`.
        T)expand_varsrv   r   r   r   r   as_str_expanded`  r6   zConfigView.as_str_expandedc                 C   s   d|   v S )zdWhether the view contains sensitive information and should be
        redacted from output.
        r   )get_redactionsr   r   r   r   r]   h  s   zConfigView.redactc                 C   s   |  d| d S )Nr   )set_redaction)r   flagr   r   r   r]   o  s   c                 C      t  )z_Add or remove a redaction for a key path, which should be an
        iterable of keys.
        r   r   pathr}   r   r   r   r|   s  s   zConfigView.set_redactionc                 C   r~   )zFGet the set of currently-redacted sub-key-paths at this view.
        r   r   r   r   r   r{   y  s   zConfigView.get_redactionsF)Tr7   )+r(   
__module____qualname____doc__r   r   r    r!   r$   r&   r   r)   r1   r4   r5   r8   r?   rD   rA   rH   rG   r*   rO   rP   r+   r[   r_   r   REQUIREDrC   rf   rh   rk   rm   rr   ru   rx   rz   propertyr]   setterr|   r{   r   r   r   r   r   )   sR    	






r   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )RootViewzmThe base of a view hierarchy. This view keeps track of the
    sources that may be accessed by subviews.
    c                 C   s   t || _t| _t | _dS )zCreate a configuration hierarchy for a list of sources. At
        least one source must be provided. The first source in the list
        has the highest priority.
        N)rR   sources	ROOT_NAMEr   r&   
redactions)r   r   r   r   r   __init__  s   
zRootView.__init__c                 C   s   | j t| d S r7   )r   rK   r
   of)r   objr   r   r   r$        zRootView.addc                 C   s   | j dt| d S Nr   )r   insertr
   r   r"   r   r   r   r&        zRootView.setc                 C   s   dd | j D S )Nc                 s   s    | ]	}t ||fV  qd S r7   )dict.0sr   r   r   	<genexpr>  s    z#RootView.resolve.<locals>.<genexpr>)r   r   r   r   r   r     s   zRootView.resolvec                 C   s   | j dd= | j  dS )zMRemove all sources (and redactions) from this
        configuration.
        N)r   r   clearr   r   r   r   r     s   zRootView.clearc                 C   s   | S r7   r   r   r   r   r   r     s   zRootView.rootc                 C   s2   |r
| j | d S || j v r| j | d S d S r7   )r   r$   remover   r   r   r   r|     s
   
zRootView.set_redactionc                 C   s   | j S r7   )r   r   r   r   r   r{     s   zRootView.get_redactionsN)r(   r   r   r   r   r$   r&   r   r   r   r|   r{   r   r   r   r   r     s    	r   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )r2   z4A subview accessed via a subscript of a parent view.c                 C   s   || _ || _t| j trd| _n| j j| _t| jts"|  jd7  _t| jtr5|  jd| j7  _dS t| jtrH|  j| jd7  _dS t| jt	j
rY|  j| j7  _dS |  jt| j7  _dS )zCMake a subview of a parent view for a given subscript key.
        r:   r9   z#{0}utf-8N)parentr-   rQ   r   r   intr   rB   decoder   rE   repr)r   r   r-   r   r   r   r     s   
zSubview.__init__c              
   c   s|    | j  D ]5\}}z|| j }W n$ ty   Y q ty"   Y q ty5   td| j jt	|j
w ||fV  qd S )Nz!{0} must be a collection, not {1})r   r   r-   
IndexErrorKeyErrorrY   r   r   r   r,   r(   )r   rV   sourcer#   r   r   r   r     s"   zSubview.resolvec                 C      | j | j|i d S r7   )r   r&   r-   r"   r   r   r   r&     r   zSubview.setc                 C   r   r7   )r   r$   r-   r"   r   r   r   r$     r   zSubview.addc                 C   s
   | j  S r7   )r   r   r   r   r   r   r     s   
zSubview.rootc                 C   s   | j | jf| | d S r7   )r   r|   r-   r   r   r   r   r|     s   zSubview.set_redactionc                    s    fdd j  D S )Nc                 3   s.    | ]}|r|d   j kr|dd V  qdS )r   r   N)r-   )r   kpr   r   r   r     s    
z)Subview.get_redactions.<locals>.<genexpr>)r   r{   r   r   r   r   r{     r   zSubview.get_redactionsN)r(   r   r   r   r   r   r&   r$   r   r|   r{   r   r   r   r   r2     s    r2   c                       sv   e Zd Zddejf fdd	Zdd Zdd Zd	d
 ZdddZ	dd Z
dddZdddZdddZdd Z  ZS )ConfigurationNTc                    sd   t t| g  || _|| _|| _| jrt| j| _nd| _d	| j
 | _|r0|   dS dS )aM  Create a configuration object by reading the
        automatically-discovered config files for the application for a
        given name. If `modname` is specified, it should be the import
        name of a module whose package will be searched for a default
        config file. (Otherwise, no defaults are used.) Pass `False` for
        `read` to disable automatic reading of all discovered
        configuration files. Use this when creating a configuration
        object at module load time and then call the `read` method
        later. Specify the Loader class as `loader`.
        Nz{0}DIR)superr   r   appnamemodnameloaderr   find_package_path_package_pathr   upper_env_varread)r   r   r   r   r   r'   r   r   r     s   zConfiguration.__init__c                 C   s   t j|  tS )z[Points to the location of the user configuration.

        The file may not exist.
        )osr   join
config_dirCONFIG_FILENAMEr   r   r   r   user_config_path  s   zConfiguration.user_config_pathc                 C   s"   |   }| t|| jdd dS )zAdd the configuration options from the YAML file in the
        user's configuration directory (given by `config_dir`) if it
        exists.
        T)r   optionalN)r   r$   r   r   r   filenamer   r   r   _add_user_source  s   zConfiguration._add_user_sourcec                 C   s@   | j r| jrtj| jt}| t|| jddd dS dS dS )zAdd the package's default configuration settings. This looks
        for a YAML file located inside the package for the module
        `modname` if it was given.
        T)r   r   defaultN)	r   r   r   r   r   DEFAULT_FILENAMEr$   r   r   r   r   r   r   _add_default_source  s   z!Configuration._add_default_sourcec                 C   s    |r|    |r|   dS dS )zFind and read the files for this configuration and set them
        as the sources for this configuration. To disable either
        discovered user configuration files or the in-package defaults,
        set `user` or `defaults` to `False`.
        N)r   r   r   userdefaultsr   r   r   r     s
   zConfiguration.readc              
   C   s   | j tjv r%tj| j  }tjtj|}tj|r$td| j n(t	
 }|D ]}tj|| j}tjtj|trB nq+tj|d | j}zt| W |S  tyo } z|jtjkrd W Y d}~|S d}~ww )a  Get the path to the user configuration directory. The
        directory is guaranteed to exist as a postcondition (one may be
        created if none exist).

        If the application's ``...DIR`` environment variable is set, it
        is used as the configuration directory. Otherwise,
        platform-specific standard configuration locations are searched
        for a ``config.yaml`` file. If no configuration file is found, a
        fallback path is used.
        z{0} must be a directoryr   N)r   r   environr   abspath
expanduserisfiler   r   r   config_dirsr   r   r   makedirsOSErrorerrnoEEXIST)r   appdir
configdirsconfdirer   r   r   r   *  s2   
zConfiguration.config_dirFc                 C   s   |  t||| jd dS )a  Parses the file as YAML and inserts it into the configuration
        sources with highest priority.

        :param filename: Filename of the YAML file to load.
        :param base_for_paths: Indicates whether the directory containing the
            YAML file will be used as the base directory for resolving relative
            path values stored in the YAML file. Otherwise, by default, the
            directory returned by `config_dir()` will be used as the base.
        )base_for_pathsr   N)r&   r   r   )r   r   r   r   r   r   set_fileR  s   

zConfiguration.set_file__c                 C   s2   |du rd | j }| t||| jd dS )a  Create a configuration overlay at the highest priority from
        environment variables.

        After prefix matching and removal, environment variable names will be
        converted to lowercase for use as keys within the configuration. If
        there are nested keys, list-like dicts (ie, `{0: 'a', 1: 'b'}`) will
        be converted into corresponding lists (ie, `['a', 'b']`). The values
        of all environment variables will be parsed as YAML scalars using the
        `self.loader` Loader class to ensure type conversion is consistent
        with YAML file sources. Use the `EnvSource` class directly to load
        environment variables using non-default behavior and to enable full
        YAML parsing of values.

        :param prefix: The prefix to identify the environment variables to use.
            Defaults to uppercased `self.appname` followed by an underscore.
        :param sep: Separator within variable names to define nested keys.
        Nz{0}_)r;   r   )r   r   r   r&   r   r   )r   prefixr;   r   r   r   set_env_  s   zConfiguration.set_envc                 C   s   |r	| j |d}ndd | jD }t|}| j|_|j |d}tj|tjdddd}d}| jD ]	}|jr8|} nq/|ra|j	rat
|j	d}	|	 }
W d   n1 sSw   Y  t||
d	}|S )
a  Dump the Configuration object to a YAML file.

        The order of the keys is determined from the default
        configuration file. All keys not in the default configuration
        will be appended to the end of the file.

        :param full:      Dump settings that don't differ from the defaults
                          as well
        :param redact:    Remove sensitive information (views with the `redact`
                          flag set) from the output
        r\   c                 S   s   g | ]}|j s|qS r   )r   r   r   r   r   
<listcomp>  s    z&Configuration.dump.<locals>.<listcomp>N   i  )Dumperdefault_flow_styleindentwidthrbr   )r_   r   r   r   yamldumpr	   r   r   r   openr   restore_yaml_commentsr   )r   fullr]   out_dictr   	temp_rootyaml_outdefault_sourcer   fpdefault_datar   r   r   r   u  s0   




zConfiguration.dumpc                 C   s"   | j D ]}t|tr|  qdS )zReload all sources from the file system.

        This only affects sources that come from files (i.e.,
        `YamlSource` objects); other sources, such as dictionaries
        inserted with `add` or `set`, will remain unchanged.
        N)r   rQ   r   load)r   r   r   r   r   reload  s
   

zConfiguration.reloadTTr   )Nr   )TF)r(   r   r   r	   Loaderr   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r     s    

(

'r   c                       s`   e Zd ZdZd fdd	Zd fdd	Z fdd	Z fd
dZ fddZ fddZ	  Z
S )
LazyConfigzA Configuration at reads files on demand when it is first
    accessed. This is appropriate for using as a global config object at
    the module level.
    Nc                    s*   t t| ||d d| _g | _g | _d S )NF)r   r   r   _materialized_lazy_prefix_lazy_suffix)r   r   r   r   r   r   r     s   
zLazyConfig.__init__Tc                    s   d| _ tt| || d S )NT)r   r   r   r   r   r   r   r   r     s   zLazyConfig.readc                    s<   | j s|   |  j| j7  _| j| jd d< tt|  S r   )r   r   r   r   r   r   r   r   r   r   r   r   r     s
   zLazyConfig.resolvec                    s:   t t| | | js|  j| j7  _| jd d = d S d S r7   )r   r   r$   r   r   r   r"   r   r   r   r$     
   zLazyConfig.addc                    s:   t t| | | js| j| jd d< | jd d = d S d S r   )r   r   r&   r   r   r   r"   r   r   r   r&     r   zLazyConfig.setc                    s   t t|   g | _g | _dS )z+Remove all sources from this configuration.N)r   r   r   r   r   r   r   r   r   r     s   
zLazyConfig.clearr7   r   )r(   r   r   r   r   r   r   r$   r&   r   r   r   r   r   r   r     s    r   ) r   
__future__r   r   r   r   r   r   collectionsr   r:   r   r   r	   r   r
   r   r   
exceptionsr   r   r   r   r   r   r^   objectr   r   r2   r   r   r   r   r   r   <module>   s.     X*> B