
    CjI                    B   S r SSKJr  SSKrSSKrSSKJrJr  SSKJ	r	J
r
  SSKJrJrJr  SSKJr  SSKr\	 " S S	5      5       r\	 " S
 S5      5       r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)z

This module provides a number of classes for validating input.

See [Validating Input](/widgets/input/#validating-input) for details.

    )annotationsN)ABCabstractmethod)	dataclassfield)CallablePatternSequence)urlparsec                      \ rS rSr% Sr\" \S9rS\S'    \	SS j5       r
\	SS j5       r\	SS j5       r\SS	 j5       r\SS
 j5       rSrg)ValidationResult   z4The result of calling a `Validator.validate` method.)default_factorySequence[Failure]failuresc                    [        S U  5       5      nU  VVs/ s H  o"R                    H  o3PM     M     nnnU(       a  [        R                  5       $ [        R	                  U5      $ s  snnf )zMerge multiple ValidationResult objects into one.

Args:
    results: List of ValidationResult objects to merge.

Returns:
    Merged ValidationResult object.
c              3  8   #    U  H  oR                   v   M     g 7fN)is_valid).0results     d/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/textual/validation.py	<genexpr>)ValidationResult.merge.<locals>.<genexpr>&   s     =W6Ws   )allr   r   successfailure)resultsr   r   r   r   s        r   mergeValidationResult.merge   s[     =W==*1Q'gGG'Q#++--#++H55	 Rs   A)c                     [        5       $ )zVConstruct a successful ValidationResult.

Returns:
    A successful ValidationResult.
r        r   r   ValidationResult.success-   s      !!r$   c                    [        U 5      $ )zsConstruct a failure ValidationResult.

Args:
    failures: The failures.

Returns:
    A failure ValidationResult.
r"   r   s    r   r   ValidationResult.failure6   s      ))r$   c                x    U R                    Vs/ s H  nUR                  c  M  UR                  PM      sn$ s  snf )zUtility for extracting failure descriptions as strings.

Useful if you don't care about the additional metadata included in the `Failure` objects.

Returns:
    A list of the string descriptions explaining the failing validations.
)r   descriptionselfr   s     r   failure_descriptions%ValidationResult.failure_descriptionsB   s>      ==
(""  G(
 	
 
s   77c                2    [        U R                  5      S:H  $ )z&True if the validation was successful.r   )lenr   r,   s    r   r   ValidationResult.is_validQ   s     4==!Q&&r$   r#   N)r   zSequence['ValidationResult']returnz'ValidationResult'r3   r   )r   r   r3   r   )r3   z	list[str])r3   bool)__name__
__module____qualname____firstlineno____doc__r   listr   __annotations__staticmethodr   r   r   propertyr-   r   __static_attributes__r#   r$   r   r   r      sz    >"'"=H=J6 6  " " 	* 	* 
 
 ' 'r$   r   c                  Z    \ rS rSr% SrS\S'    SrS\S'    SrS\S'    SS	 jrSS
 jr	Sr
g)FailureW   z'Information about a validation failure.	Validator	validatorN
str | Nonevaluer*   c                    U R                   cT  U R                  R                  b  U R                  R                  U l         g U R                  R                  U 5      U l         g g r   )r*   rD   failure_descriptiondescribe_failurer1   s    r   __post_init__Failure.__post_init__b   sL    #~~11=#'>>#E#E #'>>#B#B4#H 	 $r$   c              #  `   #    U R                   v   U R                  v   U R                  v   g 7fr   )rF   rD   r*   r1   s    r   __rich_repr__Failure.__rich_repr__j   s%     jjnns   ,.)r*   )r3   None)r3   zrich.repr.Result)r6   r7   r8   r9   r:   r<   rF   r*   rJ   rM   r?   r#   r$   r   rA   rA   W   s3    13E:9"K"tIr$   rA   c                  p    \ rS rSrSrS
SS jjr\SS j5       rSS jrSS jr	   S       SS jjr
S	rg)rC   p   a  Base class for the validation of string values.

Commonly used in conjunction with the `Input` widget, which accepts a
list of validators via its constructor. This validation framework can also be used to validate any 'stringly-typed'
values (for example raw command line input from `sys.args`).

To implement your own `Validator`, subclass this class.

Example:
    ```python
    def is_palindrome(value: str) -> bool:
        """Check has string has the same code points left to right, as right to left."""
        return value == value[::-1]

    class Palindrome(Validator):
        def validate(self, value: str) -> ValidationResult:
            if is_palindrome(value):
                return self.success()
            else:
                return self.failure("Not a palindrome!")
    ```
Nc                    Xl         g r   rH   )r,   rH   s     r   __init__Validator.__init__   s    #6 	r$   c                    g)ab  Validate the value and return a ValidationResult describing the outcome of the validation.

Implement this method when defining custom validators.

Args:
    value: The value to validate.

Returns:
    The result of the validation ([`self.success()`][textual.validation.Validator.success) or [`self.failure(...)`][textual.validation.Validator.failure]).
Nr#   r,   rF   s     r   validateValidator.validate   s    r$   c                    U R                   $ )a  Return a string description of the Failure.

Used to provide a more fine-grained description of the failure. A Validator could fail for multiple
reasons, so this method could be used to provide a different reason for different types of failure.

!!! warning

    This method is only called if no other description has been supplied. If you supply a description
    inside a call to `self.failure(description="...")`, or pass a description into the constructor of
    the validator, those will take priority, and this method won't be called.

Args:
    failure: Information about why the validation failed.

Returns:
    A string description of the failure.
rS   r+   s     r   rI   Validator.describe_failure   s    $ '''r$   c                    [        5       $ )zShorthand for `ValidationResult(True)`.

Return `self.success()` from [`validate()`][textual.validation.Validator.validate] to indicated that validation *succeeded*.

Returns:
    A ValidationResult indicating validation succeeded.
r"   r1   s    r   r   Validator.success   s      !!r$   c                p    [        U[        5      (       a  U/n[        U=(       d    [        XUS9/5      nU$ )a  Shorthand for signaling validation failure.

Return `self.failure(...)` from [`validate()`][textual.validation.Validator.validate] to indicated that validation *failed*.

Args:
    description: The failure description that will be used. When used in conjunction with the Input widget,
        this is the description that will ultimately be available inside the handler for `Input.Changed`. If not
        supplied, the `failure_description` from the `Validator` will be used. If that is not supplied either,
        then the `describe_failure` method on `Validator` will be called.
    value: The value that was considered invalid. This is optional, and only needs to be supplied if required
        in your `Input.Changed` handler.
    failures: The reasons the validator failed. If not supplied, a generic `Failure` will be included in the
        ValidationResult returned from this function.

Returns:
    A ValidationResult representing failed validation, and containing the metadata supplied
        to this function.
)rD   rF   r*   )
isinstancerA   r   )r,   r*   rF   r   r   s        r   r   Validator.failure   s;    0 h(( zH!W4+VW
 r$   rS   r   )rH   rE   r3   rO   rF   strr3   r   r   rA   r3   rE   r4   NNN)r*   rE   rF   rE   r   z"Failure | Sequence[Failure] | Noner3   r   )r6   r7   r8   r9   r:   rT   r   rX   rI   r   r   r?   r#   r$   r   rC   rC   p   se    . 
 
((" #' 7;	  5	
 
 r$   rC   c                  p   ^  \ rS rSrSr  S	       S
U 4S jjjr " S S\5      rSS jrSS jr	Sr
U =r$ )Regex   zGA validator that checks the value matches a regex (via `re.fullmatch`).c                :   > [         TU ]  US9  Xl         X l        g NrS   )superrT   regexflags)r,   rk   rl   rH   	__class__s       r   rT   Regex.__init__   s&     	-@A
E
2r$   c                      \ rS rSrSrSrg)Regex.NoResults   zYIndicates validation failed because the regex could not be found within the value string.r#   Nr6   r7   r8   r9   r:   r?   r#   r$   r   	NoResultsrp      s    gr$   rs   c                    U R                   n[        R                  " X!U R                  S9SLnU(       d%  [        R                  X5      /nU R                  US9$ U R                  5       $ )zEnsure that the value matches the regex.

Args:
    value: The value that should match the regex.

Returns:
    The result of the validation.
)rl   Nr'   )rk   re	fullmatchrl   rf   rs   r   r   )r,   rF   rk   	has_matchr   s        r   rX   Regex.validate   sW     

LLTZZ@L	45H<<<22||~r$   c                >    SU R                   < SU R                   S3$ )Describes why the validator failed.

Args:
    failure: Information about why the validation failed.

Returns:
    A string description of the failure.
zMust match regular expression z (flags=z).)rk   rl   r+   s     r   rI   Regex.describe_failure   s"     0

~Xdjj\QSTTr$   )rl   rk   )r   N)rk   zstr | Pattern[str]rl   zint | re.RegexFlagrH   rE   r3   rO   ra   rc   )r6   r7   r8   r9   r:   rT   rA   rs   rX   rI   r?   __classcell__rm   s   @r   rf   rf      s]    Q
 %&*.	
3!
3 "
3 (	
3
 

3 
3hG h 	U 	Ur$   rf   c                     ^  \ rS rSrSr   S       SU 4S jjjr " S S\5      r " S S\5      rSS jr	SS	 jr
SS
 jrSrU =r$ )Numberi  zKValidator that ensures the value is a number, with an optional range check.c                :   > [         TU ]  US9  Xl         X l        g ri   rj   rT   minimummaximumr,   r   r   rH   rm   s       r   rT   Number.__init__  s&     	-@A^^r$   c                      \ rS rSrSrSrg)Number.NotANumberi  ziIndicates a failure due to the value not being a valid number (decimal/integer, inc. scientific notation)r#   Nrr   r#   r$   r   
NotANumberr     s    wr$   r   c                      \ rS rSrSrSrg)Number.NotInRangei  zTIndicates a failure due to the number not being within the range [minimum, maximum].r#   Nrr   r#   r$   r   
NotInRanger     s    br$   r   c                    [        U5      n[        R                  " U5      (       d  [        R                  " U5      (       a)  [        R                  [        R                  X5      /5      $ U R                  U5      (       d)  [        R                  [        R                  X5      /5      $ U R                  5       $ ! [         a,    [        R                  [        R                  X5      /5      s $ f = f)zEnsure that `value` is a valid number, optionally within a range.

Args:
    value: The value to validate.

Returns:
    The result of the validation.
)float
ValueErrorr   r   r   r   mathisnanisinf_validate_ranger   r   )r,   rF   float_values      r   rX   Number.validate!  s    	N,K ::k""djj&=&=#++V->->t-K,LMM##K00#++""4/0  ||~  	N#++V->->t-K,LMM	Ns   B; ;3C10C1c                x    U R                   b  XR                   :  a  gU R                  b  XR                  :  a  gg)z_Return a boolean indicating whether the number is within the range specified in the attributes.FT)r   r   rW   s     r   r   Number._validate_range8  s1    <<#(<<<#(<r$   c                d   [        U[        R                  5      (       a  g[        U[        R                  5      (       aq  U R                  c  U R
                  b  SU R
                   S3$ U R                  b  U R
                  c  SU R                   S3$ SU R                   SU R
                   S3$ g)rz   zMust be a valid number.NMust be less than or equal to .!Must be greater than or equal to Must be between  and )r_   r   r   r   r   r   r+   s     r   rI   Number.describe_failure@  s     gv0011,!2!233||#(@7~QGG)dll.B:4<<.JJ)$,,uT\\N!LLr$   r   r   rd   )r   float | Noner   r   rH   rE   r3   rO   ra   )rF   r   r3   r5   rc   )r6   r7   r8   r9   r:   rT   rA   r   r   rX   r   rI   r?   r|   r}   s   @r   r   r     sw    U !% $*.	
_
_ 
_ (	
_
 

_ 
_xW xcW c. r$   r   c                  P   ^  \ rS rSrSr " S S\5      rSU 4S jjrS	S jrSr	U =r
$ )
IntegeriV  zKValidator which ensures the value is an integer which falls within a range.c                      \ rS rSrSrSrg)Integer.NotAnIntegeriY  z?Indicates a failure due to the value not being a valid integer.r#   Nrr   r#   r$   r   NotAnIntegerr   Y  s    Mr$   r   c                   > [         TU ]  U5      nUR                  (       d  U$  [        U5      nU R                  5       $ ! [         a,    [
        R                  [        R                  X5      /5      s $ f = f)zEnsure that `value` is an integer, optionally within a range.

Args:
    value: The value to validate.

Returns:
    The result of the validation.
)
rj   rX   r   intr   r   r   r   r   r   )r,   rF   number_validation_result	int_valuerm   s       r   rX   Integer.validate\  ss     $)7#3E#: '00++	QE
I ||~  	Q#++W-A-A$-N,OPP	Qs   A   3A65A6c                   [        U[        R                  [        R                  45      (       a  g[        U[        R                  5      (       aq  U R
                  c  U R                  b  SU R                   S3$ U R
                  b  U R                  c  SU R
                   S3$ SU R
                   SU R                   S3$ g)rz   zMust be a valid integer.Nr   r   r   r   r   )r_   r   r   r   r   r   r   r+   s     r   rI   Integer.describe_failureq  s     g 2 2G4H4HIJJ-!3!344||#(@7~QGG)dll.B:4<<.JJ)$,,uT\\N!LLr$   r#   ra   rc   )r6   r7   r8   r9   r:   rA   r   rX   rI   r?   r|   r}   s   @r   r   r   V  s#    UNw N* r$   r   c                  r   ^  \ rS rSrSr   S	       S
U 4S jjjr " S S\5      rSS jrSS jr	Sr
U =r$ )Lengthi  z5Validate that a string is within a range (inclusive).c                :   > [         TU ]  US9  Xl         X l        g ri   r   r   s       r   rT   Length.__init__  s&     	-@ANNr$   c                      \ rS rSrSrSrg)Length.Incorrecti  zKIndicates a failure due to the length of the value being outside the range.r#   Nrr   r#   r$   r   	Incorrectr     s    Yr$   r   c                D   U R                   SL=(       a    [        U5      U R                   :  nU R                  SL=(       a    [        U5      U R                  :  nU(       d  U(       a)  [        R	                  [
        R                  X5      /5      $ U R                  5       $ )zEnsure that value falls within the maximum and minimum length constraints.

Args:
    value: The value to validate.

Returns:
    The result of the validation.
N)r   r0   r   r   r   r   r   r   )r,   rF   	too_shorttoo_longs       r   rX   Length.validate  st     LL,JUdll1J	<<t+IE
T\\0I#++V-=-=d-J,KLL||~r$   c                $   [        U[        R                  5      (       aq  U R                  c  U R                  b  SU R                   S3$ U R                  b  U R                  c  SU R                   S3$ SU R                   SU R                   S3$ g)rz   NzMust be shorter than z characters.zMust be longer than r   r   )r_   r   r   r   r   r+   s     r   rI   Length.describe_failure  s     gv//00||#(@.t||nLII)dll.B-dll^<HH)$,,uT\\N,WWr$   r   rd   )r   
int | Noner   r   rH   rE   r3   rO   ra   rc   )r6   r7   r8   r9   r:   rT   rA   r   rX   rI   r?   r|   r}   s   @r   r   r     sd    ? #"*.	
O
O 
O (	
O
 

O 
OZG Z r$   r   c                  j   ^  \ rS rSrSr S	     S
U 4S jjjr " S S\5      rSS jrSS jr	Sr
U =r$ )Functioni  zIA flexible validator which allows you to provide custom validation logic.c                ,   > [         TU ]  US9  Xl        g ri   )rj   rT   function)r,   r   rH   rm   s      r   rT   Function.__init__  s    
 	-@A hr$   c                      \ rS rSrSrSrg)Function.ReturnedFalsei  zIIndicates validation failed because the supplied function returned False.r#   Nrr   r#   r$   r   ReturnedFalser     s    Wr$   r   c                    U R                  U5      nU(       a  U R                  5       $ U R                  [        R	                  X5      S9$ )zValidate that the supplied function returns True.

Args:
    value: The value to pass into the supplied function.

Returns:
    A ValidationResult indicating success if the function returned True,
        and failure if the function return False.
r'   )r   r   r   r   r   )r,   rF   r   s      r   rX   Function.validate  s=     =='<<>!||X%;%;D%H|IIr$   c                    U R                   $ )rz   rS   r+   s     r   rI   Function.describe_failure  s     '''r$   )r   r   )r   zCallable[[str], bool]rH   rE   r3   rO   ra   rc   )r6   r7   r8   r9   r:   rT   rA   r   rX   rI   r?   r|   r}   s   @r   r   r     sT    S
 +/i'i (i 
	i iX XJ	( 	(r$   r   c                  B    \ rS rSrSr " S S\5      rS	S jrS
S jrSr	g)URLi  zGValidator that checks if a URL is valid (ensuring a scheme is present).c                      \ rS rSrSrSrg)URL.InvalidURLi  z$Indicates that the URL is not valid.r#   Nrr   r#   r$   r   
InvalidURLr     s    2r$   r   c                   [         R                  [        R                  X5      /5      n [	        U5      n[        UR                  UR                  /5      (       d  U$  U R                  5       $ ! [         a    Us $ f = f)zValidates that `value` is a valid URL (contains a scheme).

Args:
    value: The value to validate.

Returns:
    The result of the validation.
)
r   r   r   r   r   r   schemenetlocr   r   )r,   rF   invalid_url
parsed_urls       r   rX   URL.validate  sz     '..t0K/LM	!%J
)):+<+<=>>"" ?
 ||~  		s   2A/ /A>=A>c                    g)rz   zMust be a valid URL.r#   r+   s     r   rI   URL.describe_failure  s     &r$   r#   Nra   rc   )
r6   r7   r8   r9   r:   rA   r   rX   rI   r?   r#   r$   r   r   r     s    Q3W 3&	&r$   r   )r:   
__future__r   r   ru   abcr   r   dataclassesr   r   typingr   r	   r
   urllib.parser   	rich.reprrichr   rA   rC   rf   r   r   r   r   r   r#   r$   r   <module>r      s    #  	 # ( . . !  >' >' >'B   0k k\+UI +U\GY GT.f .b1Y 1h'(y '(T"&) "&r$   