o
    tBha                     @   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	 zd dl
Z
dZW n ey5   dZY nw zd dlZdZW n eyI   dZY nw ejdkrVd d	lmZ nd dlZ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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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!G d&d' d'eZ"G d(d) d)e#Z$d*d+ Z%dS ),    )divisionabsolute_importprint_functionN   )util)
exceptionsTF)   r   )abcc                   @   sR   e Zd ZdZefddZdd ZdddZdd
dZdd Z	dddZ
dd ZdS )TemplateaN  A value template for configuration fields.

    The template works like a type and instructs Confuse about how to
    interpret a deserialized YAML value. This includes type conversions,
    providing a default value, and validating for errors. For example, a
    filepath type might expand tildes and check that the file exists.
    c                 C   s
   || _ dS )zCreate a template with a given default value.

        If `default` is the sentinel `REQUIRED` (as it is by default),
        then an error will be raised when a value is missing. Otherwise,
        missing values will instead return `default`.
        Ndefault)selfr    r   h/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/confuse/templates.py__init__*   s   
zTemplate.__init__c                 C   s   |  || S )z_Invoking a template on a view gets the view's value according
        to the template.
        valuer   viewr   r   r   __call__3   s   zTemplate.__call__Nc                 C   s<   z|  \}}| ||W S  tjy   Y nw | |jS )zGet the value for a `ConfigView`.

        May raise a `NotFoundError` if the value is missing (and the
        template requires it) or a `ConfigValueError` for invalid values.
        )firstconvertr   NotFoundErrorget_default_valuenamer   r   templater   _r   r   r   r   9   s   zTemplate.valuer   c                 C   s*   t | dr
| jtu rtd|| jS )zGet the default value to return when the value is missing.

        May raise a `NotFoundError` if the value is required.
        r   {} not found)hasattrr   REQUIREDr   r   format)r   key_namer   r   r   r   H   s   zTemplate.get_default_valuec                 C   s   |S )zConvert the YAML-deserialized value to a value of the desired
        type.

        Subclasses should override this to provide useful conversions.
        May raise a `ConfigValueError` when the configuration is wrong.
        r   r   r   r   r   r   r   r   S   s   zTemplate.convertFc                 C   s"   |rt jnt j}|d|j|)zRaise an exception indicating that a value cannot be
        accepted.

        `type_error` indicates whether the error is due to a type
        mismatch rather than a malformed value. In this case, a more
        specific exception is raised.
        z{0}: {1})r   ConfigTypeErrorConfigValueErrorr!   r   )r   messager   
type_error	exc_classr   r   r   fail]   s   
	zTemplate.failc                 C   s(   d t| j| jtu rdS t| jS )Nz{0}({1}) )r!   type__name__r   r    reprr   r   r   r   __repr__j   s   zTemplate.__repr__Nr   )F)r,   
__module____qualname____doc__r    r   r   r   r   r   r)   r/   r   r   r   r   r
   "   s    	



r
   c                   @      e Zd ZdZdd ZdS )Integerz-An integer configuration value template.
    c                 C   s2   t |tr|S t |trt|S | d|d dS )z@Check that the value is an integer. Floats are rounded.
        zmust be a numberTN)
isinstanceintfloatr)   r#   r   r   r   r   t   s
   

zInteger.convertNr,   r1   r2   r3   r   r   r   r   r   r5   q       r5   c                   @   r4   )NumberzBA numeric type: either an integer or a floating-point number.
    c                 C   s.   t |tjr|S | dt|j|d dS )z3Check that the value is an int or a float.
        zmust be numeric, not {0}TN)r6   r   NUMERIC_TYPESr)   r!   r+   r,   r#   r   r   r   r      s   zNumber.convertNr9   r   r   r   r   r;      r:   r;   c                   @   *   e Zd ZdZdd Zd	ddZdd ZdS )
MappingTemplatezA template that uses a dictionary to specify other types for the
    values for a set of keys and produce a validated `AttrDict`.
    c                 C   s,   i }|  D ]
\}}t|||< q|| _dS )zCreate a template according to a dict (mapping). The
        mapping's values should themselves either be Types or
        convertible to Types.
        N)itemsas_templatesubtemplates)r   mappingrA   keytypr   r   r   r      s   
zMappingTemplate.__init__Nc                 C   s2   t  }| j D ]\}}||| | ||< q|S )zqGet a dict with the same keys as the template and values
        validated according to the value types.
        )AttrDictrA   r?   r   )r   r   r   outrC   rD   r   r   r   r      s   zMappingTemplate.valuec                 C      d t| jS )NzMappingTemplate({0}))r!   r-   rA   r.   r   r   r   r/         zMappingTemplate.__repr__r0   r,   r1   r2   r3   r   r   r/   r   r   r   r   r>      s
    

	r>   c                   @   r=   )
SequencezZA template used to validate lists of similar items,
    based on a given subtemplate.
    c                 C      t || _dS )zZCreate a template for a list with items validated
        on a given subtemplate.
        Nr@   subtemplater   rM   r   r   r   r         zSequence.__init__Nc                 C   s*   g }|  D ]}|| j||  q|S )z<Get a list of items validated against the template.
        )sequenceappendrM   r   )r   r   r   rF   itemr   r   r   r      s   zSequence.valuec                 C   rG   )NzSequence({0})r!   r-   rM   r.   r   r   r   r/      rH   zSequence.__repr__r0   rI   r   r   r   r   rJ      s
    
rJ   c                   @   r=   )
MappingValuesa  A template used to validate mappings of similar items,
    based on a given subtemplate applied to the values.

    All keys in the mapping are considered valid, but values
    must pass validation by the subtemplate. Similar to the
    Sequence template but for mappings.
    c                 C   rK   )zuCreate a template for a mapping with variable keys
        and item values validated on a given subtemplate.
        NrL   rN   r   r   r   r      rO   zMappingValues.__init__Nc                 C   s,   i }|  D ]\}}| j|| ||< q|S )zxGet a dict with the same keys as the view and the
        value of each item validated against the subtemplate.
        )r?   rM   r   )r   r   r   rF   rC   rR   r   r   r   r      s   zMappingValues.valuec                 C   rG   )NzMappingValues({0})rS   r.   r   r   r   r/      rH   zMappingValues.__repr__r0   rI   r   r   r   r   rT      s
    
	rT   c                       s8   e Zd ZdZeddf fdd	Zdd Zdd	 Z  ZS )
Stringz+A string configuration value template.
    NFc                    s4   t t| | || _|| _|rt|| _dS dS )zCreate a template with the added optional `pattern` argument,
        a regular expression string that the value should match.
        N)superrU   r   patternexpand_varsrecompileregex)r   r   rW   rX   	__class__r   r   r      s   zString.__init__c                 C   sL   g }| j tur|t| j  | jd ur|dt| j  dd|S )Nzpattern=zString({0}), )r   r    rQ   r-   rW   r!   joinr   argsr   r   r   r/      s   

zString.__repr__c                 C   sV   t |tjs| d|d | jr | j|s | d| j| | jr)t	j
|S |S )zBCheck that the value is a string and matches the pattern.
        zmust be a stringTzmust match the pattern {0})r6   r   
BASESTRINGr)   rW   r[   matchr!   rX   ospath
expandvarsr#   r   r   r   r      s   
zString.convert)	r,   r1   r2   r3   r    r   r/   r   __classcell__r   r   r\   r   rU      s
    
rU   c                       s4   e Zd ZdZef fdd	Zdd Zdd Z  ZS )Choicez?A template that permits values from a sequence of choices.
    c                       t t| | || _dS )a3  Create a template that validates any of the values from the
        iterable `choices`.

        If `choices` is a map, then the corresponding value is emitted.
        Otherwise, the value itself is emitted.

        If `choices` is a `Enum`, then the enum entry with the value is
        emitted.
        N)rV   rh   r   choices)r   rj   r   r\   r   r   r     s   

zChoice.__init__c              	   C   s   t r/t| jtr/t| jtjr/z| |W S  ty.   | d	dd | jD || Y nw || jvrA| d	t
| j|| t| jtjrM| j| S |S )zeEnsure that the value is among the choices (and remap if the
        choices are a mapping).
        zmust be one of {0!r}, not {1!r}c                 S   s   g | ]}|j qS r   r   ).0cr   r   r   
<listcomp>   s    z"Choice.convert.<locals>.<listcomp>)SUPPORTS_ENUMr6   rj   r+   
issubclassenumEnum
ValueErrorr)   r!   listr	   Mappingr#   r   r   r   r     s.   


zChoice.convertc                 C   s   d | jS )NzChoice({0!r}))r!   rj   r.   r   r   r   r/   2     zChoice.__repr__)	r,   r1   r2   r3   r    r   r   r/   rg   r   r   r\   r   rh     s
    rh   c                       s@   e Zd ZdZef fdd	Zdd Z fddZdd	 Z  Z	S )
OneOfzLA template that permits values complying to one of the given templates.
    c                    s   t t| | t|| _d S r0   )rV   rv   r   rs   allowed)r   rw   r   r\   r   r   r   9  s   zOneOf.__init__c                 C   sL   g }| j d ur|dt| j   | jtur|t| j dd|S )Nzallowed=z
OneOf({0})r^   )rw   rQ   r-   r   r    r!   r_   r`   r   r   r   r/   =  s   

zOneOf.__repr__c                    s   || _ tt| ||S r0   )r   rV   rv   r   )r   r   r   r\   r   r   r   H  s   zOneOf.valuec                 C   s   t | jt}| jD ]Y}z5|r9t |tr&|jr&||| j}t||j|j	< nt|j	|i}|j
||j	 W   S ||W   S  tjyI     tjyR   Y q	 tyb } zt|d}~ww | dt| jt|| dS )z=Ensure that the value follows at least one template.
        Nzmust be one of {0}, not {1})r6   r   r>   rw   Filenamerelative_totemplate_with_relativesr@   rA   rC   parentgetr   ConfigTemplateErrorConfigErrorrr   r)   r!   r-   )r   r   r   
is_mapping	candidatenext_templateexcr   r   r   r   L  s>   


zOneOf.convert)
r,   r1   r2   r3   r    r   r/   r   r   rg   r   r   r\   r   rv   6  s    rv   c                       s6   e Zd ZdZdef fdd	Zdd Zdd Z  ZS )	StrSeqzA template for values that are lists of strings.

    Validates both actual YAML string lists and single strings. Strings
    can optionally be split on whitespace.
    Tc                    ri   )zCreate a new template.

        `split` indicates whether, when the underlying value is a single
        string, it should be split on whitespace. Otherwise, the
        resulting value is a list containing a single string.
        N)rV   r   r   split)r   r   r   r\   r   r   r   y  s   
zStrSeq.__init__c                 C   s8   t |tjr|S t |tr|ddS | d|d d S )Nutf-8ignorezmust be a list of stringsT)r6   r   STRINGbytesdecoder)   )r   xr   r   r   r   _convert_value  s
   
zStrSeq._convert_valuec                    s|   t |tr|dd}t |tjr jr| }n|g}nzt|}W n ty3    dd Y nw  fdd|D S )Nr   r   z/must be a whitespace-separated string or a listTc                    s   g | ]}  |qS r   )r   )rk   vr   r   r   rm     s    z"StrSeq.convert.<locals>.<listcomp>)	r6   r   r   r   r   r   rs   	TypeErrorr)   r#   r   r   r   r     s   

zStrSeq.convert)	r,   r1   r2   r3   r    r   r   r   rg   r   r   r\   r   r   s  s
    
r   c                       s.   e Zd ZdZd fdd	Z fddZ  ZS )Pairsa  A template for ordered key-value pairs.

    This can either be given with the same syntax as for `StrSeq` (i.e. without
    values), or as a list of strings and/or single-element mappings such as::

        - key: value
        - [key, value]
        - key

    The result is a list of two-element tuples. If no value is provided, the
    `default_value` will be returned as the second element.
    Nc                    s   t t| jdd || _dS )zCreate a new template.

        `default` is the dictionary value returned for items that are not
        a mapping, but a single string.
        T)r   N)rV   r   r   default_value)r   r   r\   r   r   r     s   
zPairs.__init__c                    s   zt t| ||| jfW S  tjyi   t|tjr2t	|dkr(| 
d|d t| \}}n#t|tjrJt	|dkrE| 
d|d |\}}n| 
dt| |d t t| ||t t| ||f Y S w )Nr   z must be a single-element mappingT   zmust be a two-element listz+must be a single string, mapping, or a list)rV   r   r   r   r   r$   r6   r	   rt   lenr)   r   
iter_firstr?   rJ   str)r   r   r   kr   r\   r   r   r     s,   
zPairs._convert_valuer0   )r,   r1   r2   r3   r   r   rg   r   r   r\   r   r     s    	r   c                       sF   e Zd ZdZeddddf fdd	Zdd Zdd	 Zdd
dZ  Z	S )rx   a  A template that validates strings as filenames.

    Filenames are returned as absolute, tilde-free paths.

    Relative paths are relative to the template's `cwd` argument
    when it is specified. Otherwise, if the paths come from a file,
    they will be relative to the configuration directory (see the
    `config_dir` method) by default or to the base directory of the
    config file if either the source has `base_for_paths` set to True
    or the template has `in_source_dir` set to True. Paths from sources
    without a file are relative to the current working directory. This
    helps attain the expected behavior when using command-line options.
    NFc                    s,   t t| | || _|| _|| _|| _dS )a  `relative_to` is the name of a sibling value that is
        being validated at the same time.

        `in_app_dir` indicates whether the path should be resolved
        inside the application's config directory (even when the setting
        does not come from a file).

        `in_source_dir` indicates whether the path should be resolved
        relative to the directory containing the source file, if there is
        one, taking precedence over the application's config directory.
        N)rV   rx   r   cwdry   
in_app_dirin_source_dir)r   r   r   ry   r   r   r\   r   r   r     s
   
zFilename.__init__c                 C   s   g }| j tur|t| j  | jd ur|dt| j  | jd ur-|dt| j  | jr5|d | jr=|d dd	|S )Nzcwd=zrelative_to=zin_app_dir=Truezin_source_dir=TruezFilename({0})r^   )
r   r    rQ   r-   r   ry   r   r   r!   r_   r`   r   r   r   r/     s   




zFilename.__repr__c              	   C   s   t |tjtfstd| j|jkrtd|j	| j|j
 vr.| d| j| i }||j ti }| j}|d urwz||}W n" tyj   ||jv r_td|j	| jtd| j|j	w ||j|< |j}|d usA|j
|| j S )Nz:relative_to may only be used when getting multiple values.z{0} is relative to itselfz1needs sibling value "{0}" to expand relative pathz${0} and {1} are recursively relativez=missing template for {0}, needed to expand {1}'srelative path)r6   r	   rt   r>   r   r}   ry   rC   r!   r   r{   keysr)   updaterA   popKeyErrorr|   )r   r   r   old_templater   next_relativerel_to_templater   r   r   resolve_relative_to   sH   


zFilename.resolve_relative_toc                 C   s  z|  \}}W n tjy   | |j Y S w t|tjs,| d	t
|j|d tjt|}tj|s| jd urItj| j|}n;| jd urZtj| |||}n*|jr`| jsf|jrs| jsstjtj|j|}n|jsy| jrtj|  |}tj|S )Nzmust be a filename, not {0}T)r   r   r   r   r   r6   r   rb   r)   r!   r+   r,   rd   re   
expanduserr   isabsr   r_   ry   r   filenamer   base_for_pathsr   dirnameroot
config_dirabspath)r   r   r   re   sourcer   r   r   r   3  s:   


zFilename.valuer0   )
r,   r1   r2   r3   r    r   r/   r   r   rg   r   r   r\   r   rx     s    3rx   c                       s"   e Zd ZdZd fdd	Z  ZS )Patha  A template that validates strings as `pathlib.Path` objects.

    Filenames are parsed equivalent to the `Filename` template and then
    converted to `pathlib.Path` objects.

    For Python 2 it returns the original path as returned by the `Filename`
    template.
    Nc                    s0   t t| ||}|d u rd S dd l}||S )Nr   )rV   r   r   pathlib)r   r   r   r   r   r\   r   r   r   a  s
   
z
Path.valuer0   )r,   r1   r2   r3   r   rg   r   r   r\   r   r   X  s    r   c                   @   s,   e Zd ZdZd
ddZdddZdd	 ZdS )Optionala_  A template that makes a subtemplate optional.

    If the value is present and not null, it must validate against the
    subtemplate. However, if the value is null or missing, the template will
    still validate, returning a default value. If `allow_missing` is False,
    the template will not allow missing values while still permitting null.
    NTc                 C   sF   t || _|d u rz| j }W n
 tjy   Y nw || _|| _d S r0   )r@   rM   r   r   r   r   allow_missing)r   rM   r   r   r   r   r   r   r  s   

zOptional.__init__c                 C   s`   z|  \}}W n tjy!   | jr| j Y S td|jw |d u r)| jS | j|| S )Nr   )	r   r   r   r   r   r!   r   rM   r   r   r   r   r   r   ~  s   
zOptional.valuec                 C   s   d t| jt| j| jS )Nz%Optional({0}, {1}, allow_missing={2}))r!   r-   rM   r   r   r.   r   r   r   r/     s
   zOptional.__repr__)NTr0   rI   r   r   r   r   r   i  s
    

r   c                       s,   e Zd ZdZef fdd	Zdd Z  ZS )TypeTemplatez\A simple template that checks that a value is an instance of a
    desired Python type.
    c                    ri   )zVCreate a template that checks that the value is an instance
        of `typ`.
        N)rV   r   r   rD   )r   rD   r   r\   r   r   r     s   
zTypeTemplate.__init__c                 C   s0   t || js| d| jjt|j|d |S )Nzmust be a {0}, not {1}T)r6   rD   r)   r!   r,   r+   r#   r   r   r   r     s   zTypeTemplate.convert)r,   r1   r2   r3   r    r   r   rg   r   r   r\   r   r     s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )rE   z^A `dict` subclass that can be accessed via attributes (dot
    notation) for convenience.
    c                 C   s   || v r| | S t |r0   )AttributeError)r   rC   r   r   r   __getattr__  s   zAttrDict.__getattr__c                 C   s   || |< d S r0   r   )r   rC   r   r   r   r   __setattr__  ru   zAttrDict.__setattr__N)r,   r1   r2   r3   r   r   r   r   r   r   rE     s    rE   c                 C   sV  t | tr| S t | tjrt| S | tu rt S t | tr!t| S t | tr/t| t	j
r/t S t | t	j
r9t| S t | trDtt| S trUt | trUt| tjrUt| S t | tr^t| S | tu ret S t | trnt| S trzt | tjrzt| S | du rtdS | tu rt S | tu rttjS | tu rttjS t | trt| S td| )z?Convert a simple "shorthand" Python value to a `Template`.
    Nz!cannot convert to template: {0!r})r6   r
   r	   rt   r>   r7   r5   r+   ro   r   rb   rU   setrh   rs   rn   rp   rq   rv   r8   r;   SUPPORTS_PATHLIBr   PurePathr   r    dictr   rJ   rr   r!   r   r   r   r   r@     sJ   








r@   )&
__future__r   r   r   rd   rY   sysr*   r   r   rp   rn   ImportErrorr   r   version_infocollectionsr	   objectr    r
   r5   r;   r>   rJ   rT   rU   rh   rv   r   r   rx   r   r   r   r   rE   r@   r   r   r   r   <module>   sR    
O*1=*. ,