
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    `S                     @   s  d Z ddlmZ ddlmZmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZmZ ed	d
ZeddZeddZdZeddZeddZeddZeddZeddZeddZdZeddZeddZeddZeddZed d!Zed"d#Z ed$d%Z!ed&d'Z"ed(d)Z#ed*d+Z$ed,d-Z%ed.d-Z&ed/d0Z'ed1d0Z(ed2d3Z)ed4dZ*ed5d6Z+ed7d8Z,ed9d:Z-ed;d<Z.ed=dZ/ed>dZ0ed?dZ1ed@dA Z2Z3edBdC Z4Z5edDd!Z6edEdZ7edFdGZ8edHdIZ9edJdKZ:edLdMZ;dNZ<dOZ=G dPdQ dQe>Z?G dRdS dSe@ZAG dTdU dUeZBG dVdW dWeBZCG dXdY dYeCZDG dZd[ d[eCZEG d\d] d]e>ZFG d^d_ d_eFZGd`da ZHG dbdc dceFZIG ddde deeIZJdS )fz? This module contains the error-related constants and classes.     )absolute_import)defaultdict
namedtuple)copydeepcopy)wraps)pformat)PYTHON_VERSIONMutableMapping)compare_paths_ltquote_stringErrorDefinitionz
code, ruleN   document is missing   Zrequired      Zdependencies      Zexcludes!   ''{0}' is not a document, must be a dict"   empty#   Znullable$   type%   schema&   items'   Z	minlength(   Z	maxlengthA   ZregexB   minC   maxD   ZallowedE   F   Z	forbiddenG   H   contains`   a   Zcoerceb   Zrename_handlerc   readonlyd   Zdefault_setter            Z	keysrules   Zvaluesrules         Znoneof   Zoneof   Zanyof   Zallofz0schema definition for field '{0}' must be a dictzvalidation schema missingc                   @   s   e Zd Z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e	dd Ze	dd Ze	dd ZdS )ValidationErrorz:A simple class to store and query basic error information.c                 C   s.   || _ || _|| _|| _|| _|| _|| _d S N)document_pathschema_pathcoderule
constraintvalueinfo)selfr@   rA   rB   rC   rD   rE   rF    rH   @/opt/alt/python38/lib/python3.8/site-packages/cerberus/errors.py__init__^   s    zValidationError.__init__c                 C   s   t | t |kS )z:Assumes the errors relate to the same document and schema.)hashrG   otherrH   rH   rI   __eq__q   s    zValidationError.__eq__c                 C   s   t | jt | jA t | jA S )z>Expects that all other properties are transitively determined.)rK   r@   rA   rB   rG   rH   rH   rI   __hash__u   s    zValidationError.__hash__c                 C   s,   | j |j krt| j |j S t| j|jS d S r?   )r@   r   rA   rL   rH   rH   rI   __lt__y   s    zValidationError.__lt__c              
   C   s>   dj | jjtt| | j| jt| jt| j	t| j
| jdS )Nz{class_name} @ {memptr} ( document_path={document_path},schema_path={schema_path},code={code},constraint={constraint},value={value},info={info} ))
class_nameZmemptrr@   rA   rB   rD   rE   rF   )format	__class____name__hexidr@   rA   rB   r   rD   rE   rF   rO   rH   rH   rI   __repr__   s    
zValidationError.__repr__c                 C   s   | j r| jd S dS )zX
        A list that contains the individual errors of a bulk validation error.
        r   N)is_group_errorrF   rO   rH   rH   rI   child_errors   s    zValidationError.child_errorsc                 C   s@   | j s
dS tt}| jD ]"}|jt| j }|| | q|S )z
        Dictionary with errors of an \*of-rule mapped to the index of the definition it
        occurred in. Returns :obj:`None` if not applicable.
        N)is_logic_errorr   listrZ   rA   lenappend)rG   resulterrorirH   rH   rI   definitions_errors   s    
z"ValidationError.definitions_errorsc                 C   s   | j r| j d S dS dS )z6Field of the contextual mapping, possibly :obj:`None`.N)r@   rO   rH   rH   rI   field   s    
zValidationError.fieldc                 C   s   t | jtj@ S )z(``True`` for errors of bulk validations.)boolrB   ERROR_GROUPrO   rH   rH   rI   rY      s    zValidationError.is_group_errorc                 C   s   t | jtjtj @ S )z[
        ``True`` for validation errors against different schemas with \*of-rules.
        )re   rB   LOGICALrf   rO   rH   rH   rI   r[      s    zValidationError.is_logic_errorc                 C   s   t | jtj@ S )z"``True`` for normalization errors.)re   rB   NORMALIZATIONrO   rH   rH   rI   is_normalization_error   s    z&ValidationError.is_normalization_errorN)rU   
__module____qualname____doc__rJ   rN   rP   rQ   rX   propertyrZ   rb   rd   rY   r[   ri   rH   rH   rH   rI   r>   [   s$   




r>   c                   @   s   e Zd ZdZdd ZdS )	ErrorListz
    A list for :class:`~cerberus.errors.ValidationError` instances that can be queried
    with the ``in`` keyword for a particular :class:`~cerberus.errors.ErrorDefinition`.
    c                    s*   t |tst|j t fdd| D S )Nc                 3   s   | ]}|j  kV  qd S r?   )rB   ).0xZwanted_coderH   rI   	<genexpr>   s     z)ErrorList.__contains__.<locals>.<genexpr>)
isinstancer   	TypeErrorrB   any)rG   Zerror_definitionrH   rq   rI   __contains__   s    
zErrorList.__contains__N)rU   rj   rk   rl   rv   rH   rH   rH   rI   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d Z
dd Zdd Zedd Zedd Zdd Zdd ZdS )ErrorTreeNode)descendantserrorsparent_nodepath	tree_rootc                 C   s8   || _ | j j| _|d | j jd  | _t | _i | _d S Nr   )rz   r|   depthr{   rn   ry   rx   )rG   r{   rz   rH   rH   rI   rJ      s
    
zErrorTreeNode.__init__c                 C   s"   t |tr|| jkS || jkS d S r?   )rs   r   ry   rx   )rG   itemrH   rH   rI   rv      s    

zErrorTreeNode.__contains__c                 C   s   | j |= d S r?   rx   )rG   keyrH   rH   rI   __delitem__   s    zErrorTreeNode.__delitem__c                 C   s
   t | jS r?   )iterry   rO   rH   rH   rI   __iter__   s    zErrorTreeNode.__iter__c                 C   s>   t |tr.| jD ]}|j|jkr|  S qd S | j|S d S r?   )rs   r   ry   rB   rx   get)rG   r   r`   rH   rH   rI   __getitem__   s    


zErrorTreeNode.__getitem__c                 C   s
   t | jS r?   )r]   ry   rO   rH   rH   rI   __len__   s    zErrorTreeNode.__len__c                 C   s   |   S r?   )__str__rO   rH   rH   rI   rX      s    zErrorTreeNode.__repr__c                 C   s   || j |< d S r?   r   )rG   r   rE   rH   rH   rI   __setitem__   s    zErrorTreeNode.__setitem__c                 C   s   t | jd t | j S )N,)strry   rx   rO   rH   rH   rI   r      s    zErrorTreeNode.__str__c                 C   s
   t | jS r?   )r]   r{   rO   rH   rH   rI   r~      s    zErrorTreeNode.depthc                 C   s   | j jS r?   )r|   	tree_typerO   rH   rH   rI   r      s    zErrorTreeNode.tree_typec                 C   s   |  |}|| j }|| jkr,t|| | |< | | }t|| jd kr||j| |j  |jr|j	D ]}| j
| qhn
|| d S r}   )	_path_of_r~   rx   rw   r]   ry   r^   sortrY   rZ   r|   add)rG   r`   Z
error_pathr   nodechild_errorrH   rH   rI   r     s    




zErrorTreeNode.addc                 C   s   t || jd S )N_path)getattrr   rG   r`   rH   rH   rI   r     s    zErrorTreeNode._path_of_N)rU   rj   rk   	__slots__rJ   rv   r   r   r   r   rX   r   r   rm   r~   r   r   r   rH   rH   rH   rI   rw      s    	

rw   c                       s:   e Zd ZdZdddZ fddZdd Zd	d
 Z  ZS )	ErrorTreezw
    Base class for :class:`~cerberus.errors.DocumentErrorTree` and
    :class:`~cerberus.errors.SchemaErrorTree`.
    rH   c                 C   s8   d | _ | | _d| _t | _i | _|D ]}| | q$d S )NrH   )rz   r|   r{   rn   ry   rx   r   rG   ry   r`   rH   rH   rI   rJ      s    zErrorTree.__init__c                    s6   |  |s"| j| | j  ntt| | dS )zl
        Add an error to the tree.

        :param error: :class:`~cerberus.errors.ValidationError`
        N)r   ry   r^   r   superr   r   r   rT   rH   rI   r   )  s    
zErrorTree.addc                 C   s"   |  |}|dk	r|jS t S dS )z
        Returns all errors for a particular path.

        :param path: :class:`tuple` of :term:`hashable` s.
        :rtype: :class:`~cerberus.errors.ErrorList`
        N)fetch_node_fromry   rn   )rG   r{   r   rH   rH   rI   fetch_errors_from5  s    
zErrorTree.fetch_errors_fromc                 C   s&   | }|D ]}|| }|dkr q"q|S )z
        Returns a node for a path.

        :param path: Tuple of :term:`hashable` s.
        :rtype: :class:`~cerberus.errors.ErrorTreeNode` or :obj:`None`
        NrH   )rG   r{   contextr   rH   rH   rI   r   B  s    zErrorTree.fetch_node_from)rH   )	rU   rj   rk   rl   rJ   r   r   r   __classcell__rH   rH   r   rI   r     s
   
	r   c                   @   s   e Zd ZdZdZdS )DocumentErrorTreezv
    Implements a dict-like class to query errors by indexes following the structure of a
    validated document.
    ZdocumentNrU   rj   rk   rl   r   rH   rH   rH   rI   r   Q  s   r   c                   @   s   e Zd ZdZdZdS )SchemaErrorTreezq
    Implements a dict-like class to query errors by indexes following the structure of
    the used schema.
    r   Nr   rH   rH   rH   rI   r   Z  s   r   c                   @   sP   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d Z
dd ZdS )BaseErrorHandlerziBase class for all error handlers.
    Subclasses are identified as error-handlers with an instance-test.c                 O   s   dS )z%Optionally initialize a new instance.NrH   rG   argskwargsrH   rH   rI   rJ   g  s    zBaseErrorHandler.__init__c                 C   s   t dS )a,  
        Returns errors in a handler-specific format.

        :param errors: An object containing the errors.
        :type errors: :term:`iterable` of
                      :class:`~cerberus.errors.ValidationError` instances or a
                      :class:`~cerberus.Validator` instance
        NNotImplementedErrorrG   ry   rH   rH   rI   __call__k  s    	zBaseErrorHandler.__call__c                 C   s   t dS )z5Be a superhero and implement an iterator over errors.Nr   rO   rH   rH   rI   r   v  s    zBaseErrorHandler.__iter__c                 C   s   t dS )z
        Add an error to the errors' container object of a handler.

        :param error: The error to add.
        :type error: :class:`~cerberus.errors.ValidationError`
        Nr   r   rH   rH   rI   r   z  s    zBaseErrorHandler.addc                 C   s   dS )z
        Optionally emits an error in the handler's format to a stream. Or light a LED,
        or even shut down a power plant.

        :param error: The error to emit.
        :type error: :class:`~cerberus.errors.ValidationError`
        NrH   r   rH   rH   rI   emit  s    zBaseErrorHandler.emitc                 C   s   dS )z
        Gets called when a validation ends.

        :param validator: The calling validator.
        :type validator: :class:`~cerberus.Validator`
        NrH   rG   Z	validatorrH   rH   rI   end  s    zBaseErrorHandler.endc                 C   s   |D ]}|  | qdS )z
        Adds all errors to the handler's container object.

        :param errors: The errors to add.
        :type errors: :term:`iterable` of
                      :class:`~cerberus.errors.ValidationError` instances
        N)r   r   rH   rH   rI   extend  s    zBaseErrorHandler.extendc                 C   s   dS )z
        Gets called when a validation starts.

        :param validator: The calling validator.
        :type validator: :class:`~cerberus.Validator`
        NrH   r   rH   rH   rI   start  s    zBaseErrorHandler.startN)rU   rj   rk   rl   rJ   r   r   r   r   r   r   r   rH   rH   rH   rI   r   c  s   	
	r   c                   @   s   e Zd Zdd Zdd ZdS )ToyErrorHandlerc                 O   s   t dd S )NzThis is not supposed to happen.)RuntimeErrorr   rH   rH   rI   r     s    zToyErrorHandler.__call__c                 C   s   d S r?   rH   rO   rH   rH   rI   clear  s    zToyErrorHandler.clearN)rU   rj   rk   r   r   rH   rH   rH   rI   r     s   r   c                    s$   t   fdd}tdk r |S  S )zCerberus error messages expect regular binary strings.
    If unicode is used in a ValidationError message can't be printed.

    This decorator ensures that if legacy Python is used unicode
    strings are encoded before passing to a function.
    c                    sV   dd }t |}||j|_||j|_||j|_||j|_||j|_ | |S )Nc                 S   s   t | tr| dS | S )z1Helper encoding unicode strings into binary utf-8zutf-8)rs   Zunicodeencode)rE   rH   rH   rI   _encode  s    

z0encode_unicode.<locals>.wrapped.<locals>._encode)r   r@   rA   rD   rE   rF   )objr`   r   frH   rI   wrapped  s    zencode_unicode.<locals>.wrappedr   )r   r	   )r   r   rH   r   rI   encode_unicode  s    r   c                %   @   s   e Zd ZdZdddddddd	d
ddddddddddddddddddddddddd d!d"d#$ZdDd%d&Zd'd( Zd)d* Zed+d, Z	e
d-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< ZdEd>d?ZdFd@dAZdGdBdCZd$S )HBasicErrorHandlerz
    Models cerberus' legacy. Returns a :class:`dict`. When mangled through :class:`str`
    a pretty-formatted representation of that tree is returned.
    z{0}r   zrequired fieldzunknown fieldzfield '{0}' is requiredz%depends on these values: {constraint}z&{0} must not be present with '{field}'r   zempty values not allowedznull value not allowedzmust be of {constraint} typezmust be of dict typez'length of list should be {0}, it is {1}zmin length is {constraint}zmax length is {constraint}z)value does not match regex '{constraint}'zmin value is {constraint}zmax value is {constraint}zunallowed value {value}zunallowed values {0}zmissing members {0}z&field '{field}' cannot be coerced: {0}z&field '{field}' cannot be renamed: {0}zfield is read-onlyz.default value for '{field}' cannot be set: {0}z'mapping doesn't validate subschema: {0}z.one or more sequence-items don't validate: {0}z2one or more keys of a mapping  don't validate: {0}z3one or more values in a mapping don't validate: {0}z one or more definitions validatez#none or more than one rule validatezno definitions validatez&one or more definitions don't validate)$r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r%   r'   r(   r)   r*   r+   r.   r/   r0   r2   r4   r5   r6   r7      r:   r;   r<   r=   Nc                 C   s   |d kri n|| _ d S r?   tree)rG   r   rH   rH   rI   rJ     s    zBasicErrorHandler.__init__c                 C   s   |    | | | jS r?   )r   r   pretty_treer   rH   rH   rI   r     s    
zBasicErrorHandler.__call__c                 C   s
   t | jS r?   )r   r   rO   rH   rH   rI   r     s    zBasicErrorHandler.__str__c                 C   s&   t | j}|D ]}| ||  q|S r?   )r   r   _purge_empty_dicts)rG   Zprettyrd   rH   rH   rI   r     s    
zBasicErrorHandler.pretty_treec                 C   s^   t |}| | |jr$| | n6|jr6| | n$|j| jkrZ| |j	| 
|j| d S r?   )r   _rewrite_error_pathr[   _insert_logic_errorrY   _insert_group_errorrB   messages_insert_errorr@   _format_messagerd   r   rH   rH   rI   r     s    
 zBasicErrorHandler.addc                 C   s
   i | _ d S r?   r   rO   rH   rH   rI   r     s    zBasicErrorHandler.clearc                 C   s   |    d S r?   )r   r   rH   rH   rI   r   !  s    zBasicErrorHandler.startc                 C   s"   | j |j j|j|j||jdS )N)rD   rd   rE   )r   rB   rS   rF   rD   rE   )rG   rd   r`   rH   rH   rI   r   $  s       z!BasicErrorHandler._format_messagec                 C   s   |d }t |dkrT|| jkrD| j|  }| j|  ||g7  < q|i g| j|< nnt |dkr|| jkrvi g| j|< | j| d }|r| jt|d}n|  }||dd | ||j dS )z
        Adds an error or sub-tree to :attr:tree.

        :param path: Path to the error.
        :type path: Tuple of strings and integers.
        :param node: An error message or a sub-tree.
        :type node: String or dictionary.
        r   r   rc   r   N)r]   r   poprT   r   r   update)rG   r{   r   rd   subtreenewrH   rH   rI   r   )  s    	

zBasicErrorHandler._insert_errorc                 C   sL   |j D ]@}|jr| | q|jr.| | q| |j| |j| qd S r?   )	rZ   r[   r   rY   r   r   r@   r   rd   )rG   r`   r   rH   rH   rI   r   E  s    
z%BasicErrorHandler._insert_group_errorc              	   C   st   |j }| |j| || |j D ]H}|D ]>}|jrD| | q.|jrV| 	| q.| |j| || q.q&d S r?   )
rd   r   r@   r   rb   valuesr[   r   rY   r   )rG   r`   rd   definition_errorsr   rH   rH   rI   r   Q  s    
z%BasicErrorHandler._insert_logic_errorc                 C   s6   |d }|d s|   n|D ]}| ||  qd S )Nrc   )r   r   )rG   Z
error_listr   r   rH   rH   rI   r   a  s
    
z$BasicErrorHandler._purge_empty_dictsr   c                 C   s*   |j r| || n|jr&| || dS )zY
        Recursively rewrites the error path to correctly represent logic errors
        N)r[   _rewrite_logic_error_pathrY   _rewrite_group_error_path)rG   r`   offsetrH   rH   rI   r   i  s    z%BasicErrorHandler._rewrite_error_pathc                 C   sD   t |j| }|jD ]*}|j|d  }|j| |_| || qd S r?   )r]   r@   rZ   r   )rG   r`   r   child_startr   relative_pathrH   rH   rI   r   r  s
    
z+BasicErrorHandler._rewrite_group_error_pathc           
      C   sx   t |j| }|j D ]Z\}}|s&qd|j|f }|j|f }|D ],}|j|d  }	||	 |_| ||d  qDqd S )Nz%s definition %sr   )r]   r@   rb   r   rC   r   )
rG   r`   r   r   ra   r   nodenamer{   r   Zrel_pathrH   rH   rI   r   {  s    
z+BasicErrorHandler._rewrite_logic_error_path)N)r   )r   )r   )rU   rj   rk   rl   r   rJ   r   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   rH   rH   rI   r     sn   '



	
	r   c                   @   s   e Zd Zej Zded< dS )SchemaErrorHandlerzunknown ruler   N)rU   rj   rk   r   r   r   rH   rH   rH   rI   r     s   
r   )Krl   Z
__future__r   collectionsr   r   r   r   	functoolsr   Zpprintr   Zcerberus.platformr	   r
   Zcerberus.utilsr   r   r   ZCUSTOMZDOCUMENT_MISSINGZREQUIRED_FIELDZUNKNOWN_FIELDZDEPENDENCIES_FIELDZDEPENDENCIES_FIELD_VALUEZEXCLUDES_FIELDZDOCUMENT_FORMATZEMPTY_NOT_ALLOWEDZNOT_NULLABLEZBAD_TYPEZBAD_TYPE_FOR_SCHEMAZITEMS_LENGTHZ
MIN_LENGTHZ
MAX_LENGTHZREGEX_MISMATCHZ	MIN_VALUEZ	MAX_VALUEZUNALLOWED_VALUEZUNALLOWED_VALUESZFORBIDDEN_VALUEZFORBIDDEN_VALUESZMISSING_MEMBERSrh   ZCOERCION_FAILEDZRENAMING_FAILEDZREADONLY_FIELDZSETTING_DEFAULT_FAILEDrf   ZMAPPING_SCHEMAZSEQUENCE_SCHEMAZ	KEYSRULESZ	KEYSCHEMAZVALUESRULESZVALUESCHEMAZ	BAD_ITEMSrg   ZNONEOFZONEOFZANYOFZALLOFZSCHEMA_ERROR_DEFINITION_TYPEZSCHEMA_ERROR_MISSINGobjectr>   r\   rn   rw   r   r   r   r   r   r   r   r   rH   rH   rH   rI   <module>   s~   





































hI7		H ?