509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
U
    Žý’`¡G  ã                   @   sþ   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZmZmZm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eƒ eƒ  ZZdS )é    )Úabsolute_import)Úwarn)Úerrors)Ú	_str_typeÚCallableÚHashableÚMappingÚMutableMappingÚSequence)Úget_Validator_classÚvalidator_factoryÚmapping_hashÚTypeDefinitionc                   @   s   e Zd ZdS )Ú_AbortN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú@/opt/alt/python38/lib/python3.8/site-packages/cerberus/schema.pyr      s   r   c                   @   s   e Zd ZdZdS )ÚSchemaErrorz_
    Raised when the validation schema is missing, has the wrong format or contains
    errors.N)r   r   r   Ú__doc__r   r   r   r   r      s   r   c                       sÌ   e Zd ZdZ‡ f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edd„ ƒZedd„ ƒZedd„ ƒZd+dd„Zdd „ Zd!d"„ Zed#d$„ ƒZd%d&„ Zd,d'd(„Zd)d*„ Z‡  ZS )-ÚDefinitionSchemaz1A dict-subclass for caching of validated schemas.c                    sX   dt ƒ krHtdtƒatj ¡ }| tdtfdƒtdt	fdƒdœ¡ |t_t
t| ƒ | ¡S )NÚSchemaValidatorÚcallabler   Úhashable)r   r   )Úglobalsr   ÚSchemaValidatorMixinr   Útypes_mappingÚcopyÚupdater   r   r   Úsuperr   Ú__new__)ÚclsÚargsÚkwargsr   ©Ú	__class__r   r   r!   %   s    


þÿzDefinitionSchema.__new__c                 C   s®   t |tƒ ƒstdƒ‚|| _t |tƒr2|j ||¡}t |tƒsnzt|ƒ}W n$ t	k
rl   t
tj |¡ƒ‚Y nX t|ƒ| _td| jtj||d�| _|  |¡}|  |¡ || _dS )zâ
        :param validator: An instance of Validator-(sub-)class that uses this
                          schema.
        :param schema: A definition-schema as ``dict``. Defaults to an empty
                       one.
        z0validator argument must be a Validator-instance.N)Úallow_unknownZerror_handlerÚtarget_schemaÚtarget_validator)Ú
isinstancer   ÚRuntimeErrorÚ	validatorr   Úschema_registryÚgetr   ÚdictÚ	Exceptionr   r   ÚSCHEMA_ERROR_DEFINITION_TYPEÚformatÚSchemaValidationSchemaÚvalidation_schemar   ZSchemaErrorHandlerÚschema_validatorÚexpandÚvalidateÚschema)Úselfr,   r8   r   r   r   Ú__init__4   s*    


û

zDefinitionSchema.__init__c              
   C   sf   | j  ¡ }z
||= W nD tk
r4   td| ƒ‚Y n. tk
rX } z|‚W 5 d }~X Y n
X | j |= d S )Nz Schema has no field '%s' defined)r8   r   Ú
ValueErrorr   r0   )r9   ÚkeyÚ_new_schemaÚer   r   r   Ú__delitem__U   s    

zDefinitionSchema.__delitem__c                 C   s
   | j | S ©N)r8   )r9   Úitemr   r   r   Ú__getitem__`   s    zDefinitionSchema.__getitem__c                 C   s
   t | jƒS r@   )Úiterr8   ©r9   r   r   r   Ú__iter__c   s    zDefinitionSchema.__iter__c                 C   s
   t | jƒS r@   )Úlenr8   rD   r   r   r   Ú__len__f   s    zDefinitionSchema.__len__c                 C   s   t | ƒS r@   )ÚstrrD   r   r   r   Ú__repr__i   s    zDefinitionSchema.__repr__c                 C   s.   |   d|i¡d }|  ||i¡ || j|< d S ©Nr   )r6   r7   r8   )r9   r<   Úvaluer   r   r   Ú__setitem__l   s    zDefinitionSchema.__setitem__c                 C   s   t | dƒrt| jƒS dS d S )Nr8   zNo schema data is set yet.)ÚhasattrrH   r8   rD   r   r   r   Ú__str__q   s    

zDefinitionSchema.__str__c                 C   s   |   | j| j ¡ ¡S r@   )r&   r,   r8   r   rD   r   r   r   r   w   s    zDefinitionSchema.copyc                 C   s<   z|   |¡}|  |¡}W n tk
r,   Y nX |  |¡}|S r@   )Ú_expand_logical_shortcutsÚ_expand_subschemasr0   Ú_rename_deprecated_rulenames)r"   r8   r   r   r   r6   z   s    

zDefinitionSchema.expandc                    sz   dd„ ‰ |  ¡ D ]d\}}‡ fdd„|D ƒD ]H}| dd¡\}}| |g i¡ || D ]}||  ||i¡ qT||= q*q|S )z­
        Expand agglutinated rules in a definition-schema.

        :param schema: The schema-definition to expand.
        :return: The expanded schema-definition.
        c                 S   s   t | tƒo|  d¡S )N)Zallof_Zanyof_Znoneof_Zoneof_)r*   r   Ú
startswith)Úxr   r   r   Ú
is_of_rule�   s    ÿz>DefinitionSchema._expand_logical_shortcuts.<locals>.is_of_rulec                    s   g | ]}ˆ |ƒr|‘qS r   r   ©Ú.0rS   ©rT   r   r   Ú
<listcomp>–   s      z>DefinitionSchema._expand_logical_shortcuts.<locals>.<listcomp>Ú_é   )ÚitemsÚsplitr   Úappend)r"   r8   ÚfieldÚrulesZof_ruleÚoperatorÚrulerK   r   rW   r   rO   ‡   s    	
z*DefinitionSchema._expand_logical_shortcutsc              	      s  ‡ ‡fdd„}‡ ‡fdd„}ˆD ]ì‰ |ƒ s,nD|ƒ rN|   ˆˆ  d ¡ˆˆ  d< n"|   dˆˆ  d i¡d ˆˆ  d< dD ]2}|ˆˆ  krt|   dˆˆ  | i¡d ˆˆ  |< qtdD ]^}|ˆˆ  kr¬tˆˆ  | tƒsÐq¬g }ˆˆ  | D ]}| |   d|i¡d ¡ qà|ˆˆ  |< q¬q ˆS )	Nc                      s   t ˆˆ  tƒodˆˆ  kS )Nr8   ©r*   r   r   ©r^   r8   r   r   Úhas_schema_rule    s    z<DefinitionSchema._expand_subschemas.<locals>.has_schema_rulec                      s<   z t dd„ ˆˆ  d  ¡ D ƒƒW S  tk
r6   Y dS X dS )zw
            Tries to determine heuristically if the schema-constraints are aimed to
            mappings.
            c                 s   s   | ]}t |tƒV  qd S r@   rb   rU   r   r   r   Ú	<genexpr>©   s    zRDefinitionSchema._expand_subschemas.<locals>.has_mapping_schema.<locals>.<genexpr>r8   FN)ÚallÚvaluesÚ	TypeErrorr   rc   r   r   Úhas_mapping_schema£   s    ÿ
z?DefinitionSchema._expand_subschemas.<locals>.has_mapping_schemar8   r   )Ú	keysrulesÚvaluesrulesÚ	keyschemaÚvalueschema)ZallofZanyofr[   ZnoneofZoneof)r6   r*   r
   r]   )r"   r8   rd   ri   ra   Znew_rules_definitionrA   r   rc   r   rP   ž   s(    "$z#DefinitionSchema._expand_subschemasNc                 C   s   | j  ||¡S r@   )r8   r.   )r9   rA   Údefaultr   r   r   r.   Æ   s    zDefinitionSchema.getc                 C   s
   | j  ¡ S r@   )r8   r[   rD   r   r   r   r[   É   s    zDefinitionSchema.itemsc              
   C   s€   z,|   |¡}| j ¡ }| |¡ |  |¡ W nH tk
rP   ttj 	|¡ƒ‚Y n, t
k
rt } z|‚W 5 d }~X Y nX || _d S r@   )r6   r8   r   r   r7   r;   r   r   r1   r2   r0   )r9   r8   r=   r>   r   r   r   r   Ì   s    


zDefinitionSchema.updatec                 C   s€   |   ¡ D ]r\}}t|tƒrqdD ]X\}}||kr2q ||krBtdƒ‚tdj||d�tƒ | | | | | |< | |  |¡ q q| S )N))rl   rj   )r,   Z
check_with)rm   rk   zTThe rule '{new}' is also present with its old name '{old}' in the same set of rules.zrThe rule '{old}' was renamed to '{new}'. The old name will not be available in the next major release of Cerberus.)ÚoldÚnew)r[   r*   rH   r+   r   r2   ÚDeprecationWarningÚpop)r8   r^   r_   ro   rp   r   r   r   rQ   Ú   s(    
ÿ þüz-DefinitionSchema._rename_deprecated_rulenamesc                 C   s   t | jƒ| _d S r@   )r3   r,   r4   rD   r   r   r   Úregenerate_validation_schemaû   s    z-DefinitionSchema.regenerate_validation_schemac                 C   sJ   |dkr| j }t|ƒt| jjƒf}|| jjkrF|  |¡ | jj |¡ dS )ap  
        Validates a schema that defines rules against supported rules.

        :param schema: The schema to be validated as a legal cerberus schema
                       according to the rules of the assigned Validator object.
                       Raises a :class:`~cerberus.base.SchemaError` when an invalid
                       schema is encountered.
        N)r8   r   r,   r   Ú_valid_schemasÚ	_validateÚadd)r9   r8   Ú_hashr   r   r   r7   þ   s    	
zDefinitionSchema.validatec                 C   s˜   t |tƒr| jj ||¡}i }| ¡ D ]R\}}t |tƒrJt ||¡||< q&i }| ¡ D ]\}}||| dd¡< qV|||< q&| j|dd�s”t	| jj
ƒ‚d S )Nú rY   F©Z	normalize)r*   r   r,   r-   r.   r[   Úrules_set_registryÚreplacer5   r   r   )r9   r8   Ztest_schemar^   r_   Z
test_rulesra   Z
constraintr   r   r   ru     s    


zDefinitionSchema._validate)N)N)r   r   r   r   r!   r:   r?   rB   rE   rG   rI   rL   rN   r   Úclassmethodr6   rO   rP   r.   r[   r   ÚstaticmethodrQ   rs   r7   ru   Ú__classcell__r   r   r%   r   r   "   s2   !


'

 
r   c                   @   s(   e Zd Zi fdd„Zdd„ Zdd„ ZdS )ÚUnvalidatedSchemac                 C   s   t |tƒst|ƒ}|| _d S r@   )r*   r   r/   r8   ©r9   r8   r   r   r   r:   !  s    
zUnvalidatedSchema.__init__c                 C   s   d S r@   r   r€   r   r   r   r7   &  s    zUnvalidatedSchema.validatec                 C   s   |   | j ¡ ¡S r@   )r&   r8   r   rD   r   r   r   r   )  s    zUnvalidatedSchema.copyN)r   r   r   r:   r7   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ ZdS )r3   c                 C   s   d|j ddœ| _d S )NFr/   )r'   r8   Útype)r_   r8   )r9   r,   r   r   r   r:   0  s    ýzSchemaValidationSchema.__init__N)r   r   r   r:   r   r   r   r   r3   /  s   r3   c                       s�   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZedd	„ ƒZe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‡  ZS )r   zi
    This validator mixin provides mechanics to validate schemas passed to a Cerberus
    validator.
    c                    s2   |  dtƒ ¡ |  dtƒ ¡ tt| ƒj||Ž d S )NÚknown_rules_set_refsÚknown_schema_refs)Ú
setdefaultÚsetr    r   r:   )r9   r#   r$   r%   r   r   r:   >  s    zSchemaValidatorMixin.__init__c                 C   s
   | j d S )z7The encountered references to rules set registry items.r‚   ©Z_configrD   r   r   r   r‚   C  s    z)SchemaValidatorMixin.known_rules_set_refsc                 C   s
   | j d S )z4The encountered references to schema registry items.rƒ   r†   rD   r   r   r   rƒ   H  s    z&SchemaValidatorMixin.known_schema_refsc                 C   s
   | j d S )z#The schema that is being validated.r(   r†   rD   r   r   r   r(   M  s    z"SchemaValidatorMixin.target_schemac                 C   s
   | j d S )z.The validator whose schema is being validated.r)   r†   rD   r   r   r   r)   R  s    z%SchemaValidatorMixin.target_validatorc                 C   sÀ   t |tƒrR|| jkrd S | j |¡ | jj |¡}|d krN|  |d| ¡ d S |}td|iƒt| jj	ƒf}|| jj
krzd S | j|d| jjd�}||dd� |jr®|  |j¡ n| jj
 |¡ d S )Nz"Rules set definition %s not found.ÚturingF©Údocument_crumbr'   r8   ry   )r*   r   r‚   rv   r)   rz   r.   Ú_errorr   r   rt   Ú_get_child_validatorr_   Ú_errors)r9   r^   rK   Ú
definitionrw   r,   r   r   r   Ú_check_with_bulk_schemaW  s.    



þýz,SchemaValidatorMixin._check_with_bulk_schemac                 C   s€   t |tƒrnpt |tƒrH| j|dddiidd�}||dd�s||  |j¡ n4t |tƒr|tdd	„ |D ƒƒs|| j|f }|  |d
¡ d S )Nrk   r�   ÚlistT©r‰   r8   r'   Fry   c                 s   s   | ]}t |tƒV  qd S r@   )r*   r   rU   r   r   r   re   ƒ  s     z@SchemaValidatorMixin._check_with_dependencies.<locals>.<genexpr>z)All dependencies must be a hashable type.)	r*   r   r   r‹   rŠ   rŒ   r
   rf   Údocument_path)r9   r^   rK   r,   Úpathr   r   r   Ú_check_with_dependenciesw  s    


ý
z-SchemaValidatorMixin._check_with_dependenciesc                 C   s&   t |ƒD ]\}}|  ||f|¡ qd S r@   )Ú	enumeraterŽ   )r9   r^   rK   Úir8   r   r   r   Ú_check_with_items‡  s    z&SchemaValidatorMixin._check_with_itemsc                 C   s–   z|   ||¡}W n tk
r&   Y d S X t|ƒt| jjƒf}|| jjkrLd S | j|d | jd�}||  |¡dd� |j	r„|  
|j	¡ n| jj |¡ d S )Nr�   Fry   )Ú&_handle_schema_reference_for_validatorr   r   r)   r   rt   r‹   Zroot_allow_unknownÚ_expand_rules_set_refsrŒ   rŠ   rv   )r9   r^   rK   rw   r,   r   r   r   Ú_check_with_schema‹  s       ÿz'SchemaValidatorMixin._check_with_schemac                 C   sL   t |tƒrt|fƒnt|ƒ}|t| jjƒ }|rH|  |d d |¡¡¡ d S )NzUnsupported types: {}z, )r*   r   r…   r)   ÚtypesrŠ   r2   Újoin)r9   r^   rK   Zinvalid_constraintsr   r   r   Ú_check_with_typež  s     ÿz%SchemaValidatorMixin._check_with_typec                 C   s@   i }|  ¡ D ].\}}t|tƒr2| jj |¡||< q|||< q|S r@   )r[   r*   r   r)   rz   r.   )r9   r8   ÚresultÚkÚvr   r   r   r˜   ¦  s    

z+SchemaValidatorMixin._expand_rules_set_refsc                 C   sd   t |tƒs|S || jkrt‚| j |¡ | jj |¡}|d kr`| j|f }|  	|d 
|¡¡ t‚|S )NzSchema definition {} not found.)r*   r   rƒ   r   rv   r)   r-   r.   r‘   rŠ   r2   )r9   r^   rK   r�   r’   r   r   r   r—   ¯  s    

z;SchemaValidatorMixin._handle_schema_reference_for_validatorc                 C   s’   t |tƒs|  |tj¡ dS | j|d| jjd�}|D ]X}td|iƒt| jj	ƒf}|| jj
kr^q4||dd� |jr~|  |j¡ q4| jj
 |¡ q4dS )z2{'allowed': ('allof', 'anyof', 'noneof', 'oneof')}NFrˆ   r‡   ry   )r*   r
   rŠ   r   ZBAD_TYPEr‹   r)   Zvalidation_rulesr   r   rt   rŒ   rv   )r9   ra   r^   rK   r,   Zconstraintsrw   r   r   r   Ú_validate_logical½  s$    
ý

þz&SchemaValidatorMixin._validate_logical)r   r   r   r   r:   Úpropertyr‚   rƒ   r(   r)   rŽ   r“   r–   r™   rœ   r˜   r—   r    r~   r   r   r%   r   r   8  s$   



 	r   c                   @   sN   e Zd ZdZi fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dd„ Z
dS )ÚRegistryzÛ
    A registry to store and retrieve schemas and parts of it by a name that can be used
    in validation schemas.

    :param definitions: Optional, initial definitions.
    :type definitions: any :term:`mapping`
    c                 C   s   i | _ |  |¡ d S r@   )Ú_storageÚextend)r9   Údefinitionsr   r   r   r:   ä  s    zRegistry.__init__c                 C   s   |   |¡| j|< dS )aQ  
        Register a definition to the registry. Existing definitions are replaced
        silently.

        :param name: The name which can be used as reference in a validation
                     schema.
        :type name: :class:`str`
        :param definition: The definition.
        :type definition: any :term:`mapping`
        N)Ú_expand_definitionr£   )r9   Únamer�   r   r   r   rv   è  s    zRegistry.addc                 C   s   | j S )z_
        Returns a :class:`dict` with all registered definitions mapped to their name.
        )r£   rD   r   r   r   rf   õ  s    zRegistry.allc                 C   s   | j  ¡  dS )z&Purge all definitions in the registry.N)r£   ÚclearrD   r   r   r   r¨   û  s    zRegistry.clearc                 C   s&   t |ƒ ¡ D ]\}}|  ||¡ qdS )a  
        Add several definitions at once. Existing definitions are
        replaced silently.

        :param definitions: The names and definitions.
        :type definitions: a :term:`mapping` or an :term:`iterable` with
                           two-value :class:`tuple` s
        N)r/   r[   rv   )r9   r¥   r§   r�   r   r   r   r¤   ÿ  s    	zRegistry.extendNc                 C   s   | j  ||¡S )zæ
        Retrieve a definition from the registry.

        :param name: The reference that points to the definition.
        :type name: :class:`str`
        :param default: Return value if the reference isn't registered.
        )r£   r.   )r9   r§   rn   r   r   r   r.     s    zRegistry.getc                 G   s   |D ]}| j  |d¡ qdS )z¢
        Unregister definitions from the registry.

        :param names: The names of the definitions that are to be
                      unregistered.
        N)r£   rr   )r9   Únamesr§   r   r   r   Úremove  s    zRegistry.remove)N)r   r   r   r   r:   rv   rf   r¨   r¤   r.   rª   r   r   r   r   r¢   Û  s   

r¢   c                   @   s   e Zd Zedd„ ƒZdS )ÚSchemaRegistryc                 C   s
   t  |¡S r@   ©r   r6   ©r"   r�   r   r   r   r¦   !  s    z!SchemaRegistry._expand_definitionN©r   r   r   r|   r¦   r   r   r   r   r«      s   r«   c                   @   s   e Zd Zedd„ ƒZdS )ÚRulesSetRegistryc                 C   s   t  d|i¡d S rJ   r¬   r­   r   r   r   r¦   '  s    z#RulesSetRegistry._expand_definitionNr®   r   r   r   r   r¯   &  s   r¯   N)Z
__future__r   Úwarningsr   Zcerberusr   Zcerberus.platformr   r   r   r   r	   r
   Zcerberus.utilsr   r   r   r   r0   r   r   r   r   r3   Úobjectr   r¢   r«   r¯   r-   rz   r   r   r   r   Ú<module>   s"       	 $E