a
    »ÏOc²`  ã                   @   s<  d 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
mZ ddlmZ G dd„ 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G dd„ deƒZG dd„ deƒZG dd„ 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d'd!d"„Zd#d$„ Zd(d%d&„ZdS ))zS
Fundamental schema attributes

These can be imported directly from `confu.schema`
é    N)Úisclass)ÚApplyDefaultErrorÚValidationErrorÚValidationWarning)Úconfig_parser_dictc                   @   s^   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	edd„ ƒZ
dd„ ZdS )Ú	AttributezZ
    Base confu schema attribute. All other schema attributes
    should extend this

    Ú c                 K   s€   || _ d|v r| d¡| _| d¡| _| d¡| _| d¡| _| dg ¡| _| dd¡| _| dd	¡| _| d
d	¡| _	d| _
dS )a§  
        Initialize attribute

        **Keyword Arguments**

        - name (`str`): describes the attribute name, if not specified
          explicitly will be set through the schema that instantiates
          the attribute.
        - default (`mixed`): the default value of this attribute. Once a default
          value is set, schema validation will no longer raise a
          validation error if the attribute is missing from the
          configuration.
        - choices (`list`): if specified on values in this list may be set
          for this attribute
        - help (`str`): help description
        - cli (`bool=True|function`): enable CLI support for this attribute
        - deprecated (`str`): version id of when this attribute will be deprecated
        - added (`str`): version id of when this attribute was added to the schema
        - removed (`str`): version id of when this attribute will be removed
        ÚdefaultÚ
deprecatedÚaddedÚremovedÚchoicesÚhelpr   ÚcliTÚcli_show_defaultN)ÚnameÚgetÚdefault_handlerr
   r   r   Úchoices_handlerr   Ú
cli_toggler   Ú	container©Úselfr   Úkwargs© r   úH/var/www/html/gsl.tools/lib/python3.9/site-packages/confu/schema/core.pyÚ__init__   s    zAttribute.__init__c                 C   s
   t | dƒS )Nr   )Úhasattr©r   r   r   r   Úhas_defaultT   s    zAttribute.has_defaultc                 C   s   | j o| jd u S ©N)r   r	   r   r   r   r   Údefault_is_noneX   s    zAttribute.default_is_nonec                 C   s    t | ddƒ}t|ƒr|| ƒS |S )z=
        Return the default value for this attribute
        r   N©ÚgetattrÚcallable)r   r	   r   r   r   r	   \   s    zAttribute.defaultc                 C   s"   t | ddƒ}t|ƒr|  | ¡S |S )z¦
        Return a list of possible value choices for this attribute

        Will return an empty list if the attribute is **NOT** limited by
        choices
        r   N)r#   r$   r   )r   r   r   r   r   r   f   s    	
zAttribute.choicesc                 C   s    t | ddƒ}t|ƒr|| ƒS |S )z¥
        Returns whether or not the attribute is available
        in as a cli argument  when confu is used to generated
        argparse or click arguments
        r   Tr"   )r   Ztoggler   r   r   r   u   s    zAttribute.clic                 K   s<   | j s| jst| ||dƒ‚| jr8|| jvr8t| ||dƒ‚|S )a×  
        Validate a value for this attribute

        Will raise a `ValidationError` or `ValidationWarning` exception on
        validation failure

        **Arguments**

        - value (`mixed`): the value to validate
        - path (`list`): current path in the config schema, this is mostly
          used to identify where an error occured when validating
          against config data, you can pass an empty list to it when
          calling this manually
        z-attribute at top level defined without a namezinvalid choice)r   r   r   r   r   ©r   ÚvalueÚpathr   r   r   r   Úvalidate‚   s    ÿ
zAttribute.validateN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r!   r	   r   r   r(   r   r   r   r   r      s   
;


	

r   c                       s:   e Zd ZdZd	‡ fdd„	Zedd„ ƒZ‡ fdd„Z‡  ZS )
ÚStrzƒ
    Attribute that requires a string value

    Keyword Attributes:
        - blank <bool=False>: if True allow "" as a value
    r   c                    s:   t ƒ jf d|i|¤Ž | dd¡| _| js6| jr6d| _d S )Nr   ÚblankFT)Úsuperr   r   r/   Údefault_is_blankr   ©Ú	__class__r   r   r   ¦   s    zStr.__init__c                 C   s
   | j dkS )Nr   )r	   r   r   r   r   r1   ¬   s    zStr.default_is_blankc                    sP   t |tƒs| jst| ||dƒ‚|dkr:| js:t| ||dƒ‚tƒ j||fi |¤ŽS )Nzstring expectedr   zcannot be blank)Ú
isinstanceÚstrr!   r   r/   r0   r(   r%   r2   r   r   r(   °   s
    zStr.validate)r   )	r)   r*   r+   r,   r   r-   r1   r(   Ú__classcell__r   r   r2   r   r.   �   s
   
r.   c                       s.   e Zd ZdZd‡ fdd„	Z‡ fdd„Z‡  ZS )ÚFilez—
    Attribute that requires a file to exist at path

    **Keyword Attributes**

    - require_exist ('bool=True'): if `True` file needs to exist
    r   c                    s(   t ƒ jf d|i|¤Ž | dd¡| _d S )Nr   Úrequire_existT)r0   r   r   r8   r   r2   r   r   r   Ã   s    zFile.__init__c                    s–   t ƒ j||fi |¤Ž}|d u r(| jr(|S |dkr:| jr:|S tj |¡}tj |¡}tj |¡}tj 	|¡sr| j
 o~tj |¡ }|s’t| ||dƒ‚|S )Nr   zfile does not exist)r0   r(   r!   r/   Úosr'   Ú
expandvarsÚ
expanduserÚabspathÚexistsr8   Úisdirr   ©r   r&   r'   r   Zvalidr2   r   r   r(   Ç   s    ÿzFile.validate)r   )r)   r*   r+   r,   r   r(   r6   r   r   r2   r   r7   º   s   r7   c                       s6   e Zd ZdZd	‡ fdd„	Zdd„ Z‡ fdd„Z‡  ZS )
Ú	Directoryad  
    Attribute that requires an existing directory path

    **Keyword Arguments**

    - require_exist ('bool=True'): if `True` directory needs to exist
    - create (`octal`): if set, instead of raising a ValidationError
    on a non-existing directory, attempt to create directory first
    using the value passed as mode (chmod) e.g., create=0o777
    r   c                    s4   t ƒ jf d|i|¤Ž | d¡| _| dd¡| _d S )Nr   Úcreater8   T)r0   r   r   rA   r8   r   r2   r   r   r   ï   s    zDirectory.__init__c              
   C   sP   zt  || j¡ W n8 tyJ } z t| ||d |¡ƒ‚W Y d }~n
d }~0 0 d S )Nz4tried to create directory  but failed with error: {})r9   ÚmakedirsrA   Ú	Exceptionr   Úformat)r   r&   Zconfig_pathÚerrr   r   r   Úmakedirõ   s    üzDirectory.makedirc                    sº   t ƒ j||fi |¤Ž}|d u r(| jr(|S tj |¡}|dkr@|S tj |¡}tj |¡}| jd urztj 	|¡sz|  
||¡ | jrštj 	|¡o–tj |¡}nd}|s¶t| ||d|› �ƒ‚|S )Nr   Tz"valid path to directory expected: )r0   r(   r!   r9   r'   r:   r;   r<   rA   r=   rF   r8   r>   r   r?   r2   r   r   r(      s$    ÿzDirectory.validate)r   )r)   r*   r+   r,   r   rF   r(   r6   r   r   r2   r   r@   â   s   r@   c                       sN   e Zd ZdZg d¢Zg d¢Zd‡ fdd„	Z‡ fdd„Zd	d
„ Zdd„ Z	‡  Z
S )ÚBoolz1
    Attribute that requires a boolean value
    )ÚtrueÚyesÚ1)ÚfalseÚnoÚ0r   c                    s    t ƒ jf d|i|¤Ž d| _d S )Nr   F)r0   r   r   r   r2   r   r   r   -  s    zBool.__init__c                    sZ   t |tƒr@| ¡ | jv rd}n"| ¡ | jv r2d}nt| ||dƒ‚tƒ jt|ƒ|fi |¤ŽS )NTFzboolean expected)	r4   r5   ÚlowerÚtrue_valuesÚfalse_valuesr   r0   r(   Úboolr%   r2   r   r   r(   1  s    
zBool.validatec                 C   s   |d= d  || d¡¡S )NÚtypez
{}/--no-{}ú-)rD   Ústrip©r   Úparamr   r   r   r   Úfinalize_click;  s    zBool.finalize_clickc                 C   sX   |d= |d s|j dd� n8|j dd� | d¡rDd |d ¡|d< d | d	¡¡}|S )
NrR   r	   Ú
store_true)ÚactionÚstore_falser   z
DISABLE {}z--no-{}rS   )Úupdater   rD   rT   rU   r   r   r   Úfinalize_argparse?  s    
zBool.finalize_argparse)r   )r)   r*   r+   r,   rO   rP   r   r(   rW   r\   r6   r   r   r2   r   rG   %  s   
rG   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚIntz2
    Attribute that requires an integer value
    c              	      sZ   |d u r| j r|S zt|ƒ}W n$ ttfyB   t| ||dƒ‚Y n0 tƒ j||fi |¤ŽS )Nzinteger expected)r!   ÚintÚ	TypeErrorÚ
ValueErrorr   r0   r(   r%   r2   r   r   r(   Q  s    zInt.validate©r)   r*   r+   r,   r(   r6   r   r   r2   r   r]   K  s   r]   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚFloatz/
    Attribute that requires a float value
    c              	      sZ   |d u r| j r|S zt|ƒ}W n$ ttfyB   t| ||dƒ‚Y n0 tƒ j||fi |¤ŽS )Nzfloat expected)r!   Úfloatr_   r`   r   r0   r(   r%   r2   r   r   r(   a  s    zFloat.validatera   r   r   r2   r   rb   [  s   rb   c                       s>   e Zd ZdZd	‡ fdd„	Ze‡ fdd„ƒZ‡ fdd„Z‡  ZS )
ÚListz.
    Attribute that requires a list value
    Nc                    s`   t |tƒstdƒ‚d|vr"g |d< t|ƒr2d|d< t |tƒrB| |_tƒ j|fi |¤Ž || _dS )a³  
        Initialize List attribute

        **Keyword Arguments**

        - name (`str`): describes the attribute name, if not specified
          explicitly will be set through the schema that instantiates
          the attribute.
        - item (`Attribute`): allows you to specify an arbitrary attribute
          to use for all values in the list.
        - default (`mixed`): the default value of this attribute. Once a default
          value is set, schema validation will no longer raise a
          validation error if the attribute is missing from the
          configuration.
        - help (`str`): help description
        - cli (`bool=True`): enable CLI support for this attribute
        - deprecated (`str`): version id of when this attribute will be deprecated
        - added (`str`): version id of when this attribute was added to the schema
        - removed (`str`): version id of when this attribute will be removed
        z"item needs to be a confu attributer	   Fr   N)r4   r   r_   r   r   r0   r   Úitem)r   r   re   r   r2   r   r   r   u  s    

zList.__init__c                    s   t | jtƒrdS tƒ jS )NF)r4   re   ÚSchemar0   r   r   r2   r   r   r   ›  s    zList.clic                    s"  t |tƒr| d¡}t |tƒs,t| ||dƒ‚| dtƒ ¡}| dtƒ ¡}g }d}|D ]¶}zTt | jtƒrˆ| 	| jj
|||g ||d�¡ n| 	| j 
|||g ¡¡ |d7 }W qT tyÚ }	 z| |	¡ W Y d }	~	qTd }	~	0  t�y }
 z| |
¡ W Y d }
~
qTd }
~
0 0 qTtƒ j
||fi |¤ŽS )Nú,zlist expectedÚerrorsÚwarningsr   ©rh   ri   é   )r4   r5   ÚsplitÚlistr   r   ÚValidationErrorProcessorre   rf   Úappendr(   Úerrorr   Úwarningr0   )r   r&   r'   r   rh   ri   Z	validatedÚidxre   rp   rq   r2   r   r   r(   ¡  s.    


ÿÿ"zList.validate)NN)	r)   r*   r+   r,   r   r-   r   r(   r6   r   r   r2   r   rd   k  s
   &rd   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )rn   z|
    This the default validation error processor, it will raise an exception
    when a warning or error is encountered
    c                 C   s   |‚d S r    r   ©r   rp   r   r   r   rp   Æ  s    zValidationErrorProcessor.errorc                 C   s   |‚d S r    r   ©r   rq   r   r   r   rq   É  s    z ValidationErrorProcessor.warningN)r)   r*   r+   r,   rp   rq   r   r   r   r   rn   À  s   rn   c                   @   s@   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S )ÚCollectValidationExceptionsz{
    This validation error processor will store all errors and warnings it encounters
    and NOT raise any exceptions
    c                 C   s
   g | _ d S r    ©Ú
exceptionsr   r   r   r   r   Ó  s    z$CollectValidationExceptions.__init__c                 c   s   | j E d H  d S r    rv   r   r   r   r   Ú__iter__Ö  s    z$CollectValidationExceptions.__iter__c                 C   s
   t | jƒS r    )Úlenrw   r   r   r   r   Ú__len__Ù  s    z#CollectValidationExceptions.__len__c                 C   s
   | j | S r    rv   )r   Úkeyr   r   r   Ú__getitem__Ü  s    z'CollectValidationExceptions.__getitem__c                 C   s   | j  |¡ d S r    ©rw   ro   rs   r   r   r   rp   ß  s    z!CollectValidationExceptions.errorc                 C   s   | j  |¡ d S r    r}   rt   r   r   r   rq   â  s    z#CollectValidationExceptions.warningN)
r)   r*   r+   r,   r   rx   rz   r|   rp   rq   r   r   r   r   ru   Í  s   ru   c                       s<   e Zd ZdZ‡ fdd„Zdd„ Zddd„Zdd	d
„Z‡  ZS )rf   a  
    Describes a confu schema.

    Instantiate confu attributes as properties of the schema.

    As the schema itself is a confu attribute, you may nest schemas
    within schemas

    **Example**

    ```
    class MySchema(Schema):
        str_attr = Str()
    ```
    c                    sœ   i | _ t| ƒD ].}t| |ƒ}t|tƒr|| j |< |js||_qd|vrNi |d< d|d< | d¡| _| jrx| j rxtdƒ‚n| jr†| | j_	t
ƒ j|i |¤Ž dS )a  
        Initialize schema

        **Keyword Arguments**

        - item (`Attribute`): allows you to specify an arbitrary attribute
          to use for all values in the schema. This is only allowed if your
          schema does **NOT** explicitly set any attributes in it's definition
        - name (`str`): describes the attribute name, if not specified
          explicitly will be set through the schema that instantiates
          the attribute.
        - default (`mixed`): the default value of this attribute. Once a default
          value is set, schema validation will no longer raise a
          validation error if the attribute is missing from the
          configuration.
        - help (`str`): help description
        - cli (`bool=True`): enable CLI support for this attribute
        - deprecated (`str`): version id of when this attribute will be deprecated
        - added (`str`): version id of when this attribute was added to the schema
        - removed (`str`): version id of when this attribute will be removed
        r	   Fr   re   z^You cannot specify an `item` attribute on a Schema instance that has attributes defined withinN)Ú_attrÚdirr#   r4   r   r   r   re   r`   r   r0   r   )r   Úargsr   r   Úattrr2   r   r   r   ø  s$    


ÿzSchema.__init__c                 c   s   t | j ¡ ƒE d H  d S r    )rm   r~   Úitemsr   r   r   r   Ú
attributes(  s    zSchema.attributesNc                 C   s^   |sg }|   ¡ D ]H\}}t|tƒrH||||g ƒ |j|||g d� q||||g ƒ qd S )N)r'   )rƒ   r4   rf   Úwalk)r   Úcallbackr'   r   Ú	attributer   r   r   r„   ,  s    
zSchema.walkc                 C   sˆ  |du rg }|du rt ƒ }|du r(t ƒ }t|tjjƒrHt|dƒrH|j}nt|tjƒr\t	|ƒ}t|t
ƒs~| t|d ||dƒ¡S t| ¡ ƒD ]¸\}}zP| j || j¡}|du rÄt|||d|› d�ƒ‚n|j|||g ||d�||< W qŠ t�y } z| |¡ W Y d}~qŠd}~0  t�y@ }	 z| |	¡ W Y d}	~	qŠd}	~	0 0 qŠ|  ¡ D ]6\}
}|
|v�rL|j�sL| t|||
g ddƒ¡ �qL|S )	a½  
        Validate config data against this schema

        **Attributes**

        - config (`dict`): config to validate

        **Keyword Attributes**

        - path (`list`): current path in the config data, this can be
          ignored on the initial call and will be set automatically
          on any subsequent calls (nested schemas)
        - errors (`ValidationErrorProcessor`)
        - warnigns (`ValidationErrorProcessor`)
        NÚdataéÿÿÿÿzdictionary expectedzunknown attribute 'ú'rj   Úmissing)rn   r4   ÚcollectionsÚabcÚMutableMappingr   r‡   ÚconfigparserÚConfigParserr   Údictrp   r   rm   r‚   r~   r   re   r   r(   rq   rƒ   r   )r   Úconfigr'   rh   ri   r{   r&   r†   rp   rq   r   r   r   r   r(   6  sD    ÿ
ÿÿÿ"zSchema.validate)N)NNN)	r)   r*   r+   r,   r   rƒ   r„   r(   r6   r   r   r2   r   rf   æ  s
   0

rf   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚDictzc
    Wrapper for schema with arbitrary keys

    For this the `item` property needs to be set.
    Nc                    s   t ƒ j|||dœ|¤Ž d S )N)r   re   )r0   r   )r   r   re   r€   r   r2   r   r   r   {  s    zDict.__init__)NN)r)   r*   r+   r,   r   r6   r   r   r2   r   r’   t  s   r’   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )ÚProxySchemaz–
    An object that can be used in place of a schema in order to
    dynamically obtain a schema instance from somewhere else during
    validate
    c                 C   s
   t ƒ ‚dS )z*
        return a schema instance
        N)ÚNotImplementedError)r   r‘   r   r   r   Úschema‡  s    zProxySchema.schemaNc                 C   s   |   |¡j||||d�S )zE
        call validate on the schema returned by self.schema
        )r'   rh   ri   )r•   r(   )r   r‘   r'   rh   ri   r   r   r   r(   �  s    
ÿzProxySchema.validate)NNN)r)   r*   r+   r,   r•   r(   r   r   r   r   r“     s   r“   Fc                 K   sÆ   t ƒ }|r*| j|fd|i|¤Ž dg |fS t ƒ }| j|f||dœ|¤Ž t|ƒ}t|ƒ}|dk}	|r¸t|ƒr¸|D ]}
|d|
j› �ƒ qp|D ]}|d|j› �ƒ qŠ|	s¸||› d|› d�ƒ |	||fS d	S )
a¶  
    Helper function that allows schema validation to either collect or raise errors

    **Arguments**

    - schema (`Schema`): schema instance
    - config (`dict|munge.Config`)

    **Keyword Arguments**

    - raise_errors (`bool=False`): if `True` will raise a ValidationError exception
      if it encounters validation errors

      If `False` it will instead collect errors and warnings and return a tuple:

      ```
      success(bool), errors(CollectValidationExceptions), warnings(CollectValidationException)
      ```

    - log (`callable`): function to use to log errors, will be passed
      a str message
    - any additional kwargs will be passed on to `Schema.validate`
    ri   Trj   r   z[Config Error] z[Config Warning] z	 errors, z warnings in configN)ru   r(   ry   r$   Úpretty)r•   r‘   Úraise_errorsÚlogr   ri   rh   Z
num_errorsZnum_warningsÚsuccessrp   rq   r   r   r   r(   –  s"    
r(   c                 C   sv  | }d}|D ]}|}|  |¡}qt|tƒr˜|rTt|jtƒrT|D ]}t|j|dd� q>|r~t|jtƒr~|D ]}t||jg ƒ qhn|du r–|jr–|j||< nÚt|tƒ�rVt|t	ƒr¸| 
|¡}|rüt|jtƒrüt|jjtƒrüt| ¡ ƒD ]\}}t||j|gƒ qâ|du �rt |j�pi ¡||< |jdu �r6t||| ƒ t|jtƒ�rrt|j|| ƒ n|du �rr|j�rr|j||< dS )zû
    Apply attribute default to config dict at the specified path

    **Arguments**

    - config (`dict`): the config dictonary
    - attribute (`Attribute`): attribute instance
    - path (`list(str)`): full path of the attribute in the schema
    NT©Údebug)r   r4   rd   re   rf   Úapply_defaultsÚapply_defaultr   r	   r“   r•   rm   r‚   ÚcopyÚdeepcopy)r‘   r†   r'   Ú_configÚprevÚsectionre   Úkr   r   r   r�   Ç  sB    


ÿ
þý	
r�   c                    sš   t | tƒr|  ˆ ¡} t | jtƒrJtˆ  ¡ ƒD ]\}}t| j||d� q,dS t | jtƒr€tˆ  ¡ ƒD ]\}}t	ˆ | j|gƒ qbdS ‡ fdd„}|  
|¡ dS )zÉ
    Take a config object and apply a schema's default values to keys that
    are missing.

    **Arguments**

    - schema (`Schema`): schema instance
    - config (`dict`): the config dictonary
    rš   Nc              
      sH   zt ˆ | |ƒ W n2 tyB } zt| |d |ƒ‚W Y d }~n
d }~0 0 d S r    )r�   rC   r   )r†   r'   Úexc©r‘   r   r   r…   ,  s    z apply_defaults.<locals>.callback)r4   r“   r•   re   rf   rm   r‚   rœ   rd   r�   r„   )r•   r‘   r›   r£   Úvr…   r   r¥   r   rœ     s    

rœ   )FN)F)r,   Úcollections.abcr‹   rŽ   rž   r9   Úinspectr   Zconfu.exceptionsr   r   r   Z
confu.utilr   r   r.   r7   r@   rG   r]   rb   rd   rn   ru   rf   r’   r“   r(   r�   rœ   r   r   r   r   Ú<module>   s2    (C&U 
1I