o
    tBh                     @   sl   d dl mZmZmZ ddlmZmZ ddlmZ d dl	Z	G dd de
ZG dd	 d	eZG d
d deZdS )    )divisionabsolute_importprint_function   )
BASESTRING
build_dict)	yaml_utilNc                       s>   e Zd ZdZ		d
 fdd	Z fddZedd	 Z  ZS )ConfigSourcezTA dictionary augmented with metadata about the source of the
    configuration.
    NFc                    sR   t t| | |durt|tstd|| _|| _|dur$|| _dS d| _dS )a  Create a configuration source from a dictionary.

        :param filename: The file with the data for this configuration source.

        :param default: Indicates whether this source provides the
            application's default configuration settings.

        :param base_for_paths: Indicates whether the source file's directory
        (i.e., the directory component of `self.filename`) should be used as
        the base directory for resolving relative path values provided by this
        source, instead of using the application's configuration directory. If
        no `filename` is provided, `base_for_paths` will be treated as False.
        See `templates.Filename` for details of the relative path resolution
        behavior.
        Nz!filename must be a string or NoneF)	superr	   __init__
isinstancer   	TypeErrorfilenamedefaultbase_for_paths)selfvaluer   r   r   	__class__ f/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/confuse/sources.pyr      s   zConfigSource.__init__c                    s   d tt| | j| j| jS )Nz(ConfigSource({0!r}, {1!r}, {2!r}, {3!r}))formatr
   r	   r   r   r   )r   r   r   r   __repr__%   s   zConfigSource.__repr__c                 C   s(   t |tr|S t |trt|S td)zGiven either a dictionary or a `ConfigSource` object, return
        a `ConfigSource` object. This lets a function accept either type
        of object as an argument.
        zsource value must be a dict)r   r	   dictr   )clsr   r   r   r   of-   s
   

zConfigSource.of)NFF)	__name__
__module____qualname____doc__r   r   classmethodr   __classcell__r   r   r   r   r	      s    r	   c                       s6   e Zd ZdZddddejf fdd	Zdd Z  ZS )
YamlSourcez=A configuration data source that reads from a YAML file.
    NFc                    s:   t j|}tt| i ||| || _|| _|   dS )a  Create a YAML data source by reading data from a file.

        May raise a `ConfigReadError`. However, if `optional` is
        enabled, this exception will not be raised in the case when the
        file does not exist---instead, the source will be silently
        empty.
        N)	ospathabspathr
   r"   r   loaderoptionalload)r   r   r   r   r'   r&   r   r   r   r   ?   s
   	zYamlSource.__init__c                 C   s>   | j rtj| jsi }ntj| j| jdpi }| | dS )z3Load YAML data from the source's filename.
        r&   N)	r'   r#   r$   isfiler   r   	load_yamlr&   update)r   r   r   r   r   r(   N   s   zYamlSource.load)	r   r   r   r   r   Loaderr   r(   r!   r   r   r   r   r"   ;   s    r"   c                       sB   e Zd ZdZddddejf fdd	Zdd Zed	d
 Z	  Z
S )	EnvSourcezCA configuration data source loaded from environment variables.
    __TFc                    sH   t t| ji dddd || _|| _|| _|| _|| _|| _| 	  dS )aq  Create a configuration source from the environment.

        :param prefix: The prefix used to identify the environment variables
            to be loaded into this configuration source.

        :param sep: Separator within variable names to define nested keys.

        :param lower: Indicates whether to convert variable names to lowercase
            after prefix matching.

        :param handle_lists: If variables are split into nested keys, indicates
            whether to search for sub-dicts with keys that are sequential
            integers starting from 0 and convert those dicts to lists.

        :param parse_yaml_docs: Enable parsing the values of environment
            variables as full YAML documents. By default, when False, values
            are parsed only as YAML scalars.

        :param loader: PyYAML Loader class to use to parse YAML values.
        NF)r   r   r   )
r
   r.   r   prefixseplowerhandle_listsparse_yaml_docsr&   r(   )r   r0   r1   r2   r3   r4   r&   r   r   r   r   \   s   zEnvSource.__init__c                 C   s   i }t j D ]5\}}|| jr<|t| jd }| jr!| }| jr0tj	|d| | j
d}ntj|| j
d}|||< q| jrHt|| jdd}| jr[| D ]\}}| |||< qO| | dS )z6Load configuration data from the environment.
        Nzenv variable r)   T)	keep_none)r#   environitems
startswithr0   lenr2   r4   r   load_yaml_stringr&   parse_as_scalarr1   r   r3   _convert_dict_listsr,   )r   config_varsvarr   keykvr   r   r   r(   |   s,   zEnvSource.loadc                    s   t tsS  D ]\}}| ||< qzdd  D  W n ty-    Y S w z fddttD W S  tyH    Y S w )zRecursively search for dicts where all of the keys are integers
        from 0 to the length of the dict, and convert them to lists.
        c                 S   s   i | ]}t ||qS r   )int).0r@   r   r   r   
<dictcomp>   s    z1EnvSource._convert_dict_lists.<locals>.<dictcomp>c                    s   g | ]} |  qS r   r   )rC   i
int_to_keyobjr   r   
<listcomp>   s    z1EnvSource._convert_dict_lists.<locals>.<listcomp>)	r   r   r7   r<   keys
ValueErrorranger9   KeyError)r   rH   r@   rA   r   rF   r   r<      s   
zEnvSource._convert_dict_lists)r   r   r   r   r   r-   r   r(   r    r<   r!   r   r   r   r   r.   Y   s     #r.   )
__future__r   r   r   utilr   r    r   r#   r   r	   r"   r.   r   r   r   r   <module>   s    3