eLvdZddlZddlZddlmZddlmZddlmZmZddl m Z m Z m Z m Z ddlmZdd lmZgd Zd Zd Zed Ze dddGddZdZe dddGddZd=dZe dddGddZdZe dddGddZdZe dddGddZd Ze dddGd!d"Z d#Z!e dddGd$d%Z"d>d&Z#e dddGd'd(Z$d>d)Z%e dddGd*d+Z&d,Z'd-Z(d.Z)d/Z*e dddGd0d1Z+d2Z,e dddGd3d4Z-d5Z.e dddGd6d7Z/d8Z0e dddGd9d:Z1de2e3fd;d<Z4y)?z Commonly useful validators. N)contextmanager)Pattern)get_run_validatorsset_run_validators) _AndValidatorand_attribattrs)default_if_none)NotCallableError)r deep_iterable deep_mappingdisabledge get_disabledgtin_ instance_of is_callablelelt matches_remax_lenmin_lennot_optionalprovides set_disabledct| y)a Globally disable or enable running validators. By default, they are run. :param disabled: If ``True``, disable running all validators. :type disabled: bool .. warning:: This function is not thread-safe! .. versionadded:: 21.3.0 Nr)rs 1/usr/lib/python3/dist-packages/attr/validators.pyrr+s8|$ct S)z Return a bool indicating whether validators are currently disabled or not. :return: ``True`` if validators are currently disabled. :rtype: bool .. versionadded:: 21.3.0 )rr#r"rr=s"# ##r#c#bKtd dtdy#tdwxYww)z Context manager that disables running validators within its context. .. warning:: This context manager is not thread-safe! .. versionadded:: 21.3.0 FNTr!r%r#r"rrIs)u! 4 4 s / / ,/FT)reprslotshashc&eZdZeZdZdZy)_InstanceOfValidatorct||jsKdj|j|j|j|}t |||j|y)P We use a callable class to be able to change the ``__repr__``. z?'{name}' must be {type!r} (got {value!r} that is a {actual!r}).)nametypeactualvalueN) isinstancer/formatr. __class__ TypeErrorselfinstattrr1msgs r"__call__z_InstanceOfValidator.__call___sd%+SZZYYYY [C    ,r#c"d|jdS)Nz r/r7s r"__repr__z_InstanceOfValidator.__repr__q1$))a@@r#N__name__ __module__ __qualname__r r/r;r@r%r#r"r+r+[s 8D$Ar#r+ct|S)a A validator that raises a `TypeError` if the initializer is called with a wrong type for this particular attribute (checks are performed using `isinstance` therefore it's also valid to pass a tuple of types). :param type: The type to check for. :type type: type or tuple of type :raises TypeError: With a human readable error message, the attribute (of type `attrs.Attribute`), the expected type, and the value it got. )r+r>s r"rru  %%r#)r'frozenr(c4eZdZeZeZdZdZy)_MatchesReValidatorc|j|sJdj|j|jj|}t |||j|y)r-z9'{name}' must match regex {pattern!r} ({value!r} doesn't))r.patternr1N) match_funcr3r.rL ValueErrorr6s r"r;z_MatchesReValidator.__call__s_u%MTTYY (<(zmatches_re..s!Laq'QZZ161Ls zR'flags' can only be used with a string pattern; pass flags to re.compile() instead)re fullmatchsearchmatchr3joinsortedsetrNr2rr5compilerJ)regexflagsfunc valid_funcsr:rLrMs r"rrs <<ryy"((;K ;)00 IIL3{;KLL   o%! fCC. **UE* rxx]]  ^^ && w 33r#c&eZdZeZdZdZy)_ProvidesValidatorc|jj|s@dj|j|j|}t |||j|y)r-z<'{name}' must provide {interface!r} which {value!r} doesn't.)r. interfacer1N)re providedByr3r.r5r6s r"r;z_ProvidesValidator.__call__s_~~((/PWWYY$..XC   0r#c"d|jdS)Nz"`_). :param interface: The interface to check for. :type interface: ``zope.interface.Interface`` :raises TypeError: With a human readable error message, the attribute (of type `attrs.Attribute`), the expected interface, and the value it got. .. deprecated:: 23.1.0 rNzZattrs's zope-interface support is deprecated and will be removed in, or after, April 2024.) stacklevel)warningswarnDeprecationWarningrc)rerks r"rrs0  MM   i ((r#c&eZdZeZdZdZy)_OptionalValidatorc0|y|j|||yN validatorr7r8r9r1s r"r;z_OptionalValidator.__call__s =  tT5)r#c"d|jdS)Nzrrr?s r"r@z_OptionalValidator.__repr__s)$..);9EEr#N)rCrDrEr rsr;r@r%r#r"rorosI* Fr#roclt|ttfrtt |St|S)a A validator that makes an attribute optional. An optional attribute is one which can be set to ``None`` in addition to satisfying the requirements of the sub-validator. :param Callable | tuple[Callable] | list[Callable] validator: A validator (or validators) that is used for non-``None`` values. .. versionadded:: 15.1.0 .. versionchanged:: 17.1.0 *validator* can be a list of validators. .. versionchanged:: 23.1.0 *validator* can also be a tuple of validators. )r2listtuplerorrrs r"rrs-)dE]+!- ":;; i ((r#c&eZdZeZdZdZy) _InValidatorc ||jv}|s8d|jd|jd|d}t|||j|y#t$rd}YHwxYw)NF'z ' must be in  (got ))optionsr5r.rN)r7r8r9r1 in_optionsr:s r"r;z_InValidator.__call__su $,,.Jdii[ dll-=VE9ANC    J sA AAc"d|jdS)Nzr%r?s r"r@z_IsCallableValidator.__repr__Zs(r#N)rCrDrEr;r@r%r#r"rrHs  )r#rctS)ax A validator that raises a `attrs.exceptions.NotCallableError` if the initializer is called with a value for this particular attribute that is not callable. .. versionadded:: 19.1.0 :raises attrs.exceptions.NotCallableError: With a human readable error message containing the attribute (`attrs.Attribute`) name, and the value it got. )rr%r#r"rr^s  !!r#c^eZdZeeZedeeZdZdZ y) _DeepIterablerrNdefaultrscv|j|j||||D]}|j|||yr-Niterable_validatormember_validator)r7r8r9r1members r"r;z_DeepIterable.__call__tsC  " " .  # #D$ 6 6F  ! !$f 5 6r#cb|jdnd|j}d|d|jdS)N z*?T225Q 8 r#) rCrDrEr rrrrr;r@r%r#r"rrms0 6 76  r#rcVt|ttfrt|}t ||S)a4 A validator that performs deep validation of an iterable. :param member_validator: Validator(s) to apply to iterable members :param iterable_validator: Validator to apply to iterable itself (optional) .. versionadded:: 19.1.0 :raises TypeError: if any sub-validators fail )r2rwrxr rrrs r"rrs."T5M2!12 )+= >>r#czeZdZeeZeeZedeeZdZ dZ y) _DeepMappingrrNrc|j|j||||D]+}|j||||j||||-yr)mapping_validator key_validatorvalue_validator)r7r8r9r1keys r"r;z_DeepMapping.__call__sY  ! ! -  " "4u 5 9C   tT3 /  tU3Z 8 9r#cPdj|j|jS)NzA)rr1)r3rrr?s r"r@z_DeepMapping.__repr__s% O &T''t/C/C& D Er#) rCrDrEr rrrrrr;r@r%r#r"rrs9[]3M{}5Otx 7NO 9Er#rct|||S)a} A validator that performs deep validation of a dictionary. :param key_validator: Validator to apply to dictionary keys :param value_validator: Validator to apply to dictionary values :param mapping_validator: Validator to apply to top-level mapping attribute (optional) .. versionadded:: 19.1.0 :raises TypeError: if any sub-validators fail )r)rrrs r"rrs  8I JJr#cBeZdZeZeZeZdZdZy)_NumberValidatorc|j||js7d|jd|jd|jd|}t |y)r-r|z ' must be r: N) compare_funcboundr. compare_oprNr6s r"r;z_NumberValidator.__call__sR   3dii[ 4??*;1TZZL5'RCS/ !4r#c<d|jd|jdS)Nz=)rrrrs r"rrrr#c8t|dtjS)z A validator that raises `ValueError` if the initializer is called with a number smaller or equal to *val*. :param val: Exclusive lower bound for values .. versionadded:: 21.3.0 r=)rrrrs r"rrrr#c&eZdZeZdZdZy)_MaxLengthValidatorct||jkDr3d|jd|jdt|}t|y)r- Length of 'z ' must be <= rN)len max_lengthr.rNr6s r"r;z_MaxLengthValidator.__call__H u: ' {-7H3u:,WCS/ ! (r#c"d|jdS)Nz= rN)r min_lengthr.rNr6s r"r;z_MinLengthValidator.__call__$rr#c"d|jdS)Nzr?s r"r@z_SubclassOfValidator.__repr__MrAr#NrBr%r#r"rr<s 8D Ar#rct|S)a A validator that raises a `TypeError` if the initializer is called with a wrong type for this particular attribute (checks are performed using `issubclass` therefore it's also valid to pass a tuple of types). :param type: The type to check for. :type type: type or tuple of types :raises TypeError: With a human readable error message, the attribute (of type `attrs.Attribute`), the expected type, and the value it got. )rr>s r" _subclass_ofrQrGr#c~eZdZeZeedZeeee e e Z dZ dZy) _NotValidatorzCnot_ validator child '{validator!r}' did not raise a captured error) converterrrrc |j|||t|jj|j|j||j||j#|j$rYywxYw)N)rs exc_types)rsrNr:r3rrts r"r;z_NotValidator.__call__qsw  NN4u -"nn"nn   ~~   sA''A98A9cPdj|j|jS)Nz;)whatr)r3rsrr?s r"r@z_NotValidator.__repr__s' I &nn   r#N)rCrDrEr rsr r:rr Exceptionrrxrr;r@r%r#r"rrasNI ! -  C ))4*51 I" r#r)r:rcX t|}t|||S#t$r|f}YwxYw)a  A validator that wraps and logically 'inverts' the validator passed to it. It will raise a `ValueError` if the provided validator *doesn't* raise a `ValueError` or `TypeError` (by default), and will suppress the exception if the provided validator *does*. Intended to be used with existing validators to compose logic without needing to create inverted variants, for example, ``not_(in_(...))``. :param validator: A validator to be logically inverted. :param msg: Message to raise if validator fails. Formatted with keys ``exc_types`` and ``validator``. :type msg: str :param exc_types: Exception type(s) to capture. Other types raised by child validators will not be intercepted and pass through. :raises ValueError: With a human readable error message, the attribute (of type `attrs.Attribute`), the validator that failed to raise an exception, the value it got, and the expected exception types. .. versionadded:: 22.2.0 )rxr5r)rsr:rs r"rrs:4!)$  C 33 !L !s  )))rNrq)5__doc__rrV contextlibrr_configrr_makerr r r convertersr exceptionsr __all__rrrr+rrJrrcrrorrzrrrrrrrrrrrrrrrrrrrrNr5rr%r#r"rs}  %;55'( .%$ $!!"ED)AA*A2 & E$d+FF,F.(4VED)HH*H,)6ED) F F* F)&ED)@@*@,!,ET*))+)* "ED)  * 8?"ED)EE*E, K E$d+CC,C" 3 4 4 3E$d+ < <, < 'E$d+ < <, < 'ED)AA*A( & ED)& & *& R J +B4r#