
­­­­­­­­­­­­­­­­­­
<!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rt  | tsqg }  | D ]}||  d|id  q|  |< qq 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st	| jj
d S )N rY   F	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)   r{   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)   r{   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,   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)
__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-   r{   r   r   r   r   <module>   s"       	 $E