o
    [hQ                     @   s  d dl mZ d dlZd dlZd dlZd dlZzd dlZW n ey'   dZY nw zd dlZW n ey9   dZY nw d dl	m
Z
mZ d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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Z"eZ#eZ$eZ%eZ&eZ'eZ(eZ)eZ*eZ+eZ,eZ-eZ.e Z/dS ))    )unicode_literalsN)string_types	text_type)DataRequireddata_requiredEmailemailEqualToequal_to	IPAddress
ip_addressInputRequiredinput_requiredLengthlengthNumberRangenumber_rangeOptionaloptionalRequiredrequiredRegexpregexpURLurlAnyOfany_ofNoneOfnone_of
MacAddressmac_addressUUIDValidationErrorStopValidationc                   @      e Zd ZdZdddZdS )r"   z>
    Raised when a validator fails to validate its input.
     c                 O      t j| |g|R i | d S N)
ValueError__init__selfmessageargskwargs r/   y/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/wtforms/validators.pyr)   !      zValidationError.__init__Nr%   __name__
__module____qualname____doc__r)   r/   r/   r/   r0   r"      s    r"   c                   @   r$   )r#   z
    Causes the validation chain to stop.

    If StopValidation is raised, no more validators in the validation chain are
    called. If raised with a message, the message will be added to the errors
    list.
    r%   c                 O   r&   r'   )	Exceptionr)   r*   r/   r/   r0   r)   -   r1   zStopValidation.__init__Nr2   r3   r/   r/   r/   r0   r#   %   s    r#   c                   @   "   e Zd ZdZdddZdd ZdS )r	   a9  
    Compares the values of two fields.

    :param fieldname:
        The name of the other field to compare to.
    :param message:
        Error message to raise in case of a validation error. Can be
        interpolated with `%(other_label)s` and `%(other_name)s` to provide a
        more helpful error.
    Nc                 C      || _ || _d S r'   )	fieldnamer,   )r+   r;   r,   r/   r/   r0   r)   <      
zEqualTo.__init__c                 C   s   z|| j  }W n ty   t|d| j  w |j|jkrAt|dr(|jjp*| j | j d}| j}|d u r;|d}t|| d S )NzInvalid field name '%s'.label)Zother_labelZ
other_namez&Field must be equal to %(other_name)s.)	r;   KeyErrorr"   gettextdatahasattrr=   textr,   )r+   formfieldotherdr,   r/   r/   r0   __call__@   s   
zEqualTo.__call__r'   r4   r5   r6   r7   r)   rG   r/   r/   r/   r0   r	   1   s    

r	   c                   @   s"   e Zd ZdZdddZdd ZdS )	r   a  
    Validates the length of a string.

    :param min:
        The minimum required length of the string. If not provided, minimum
        length will not be checked.
    :param max:
        The maximum length of the string. If not provided, maximum length
        will not be checked.
    :param message:
        Error message to raise in case of a validation error. Can be
        interpolated using `%(min)d` and `%(max)d` if desired. Useful defaults
        are provided depending on the existence of min and max.
    Nc                 C   sF   |dks|dksJ d|dks||ksJ d|| _ || _|| _d S )NrI   z1At least one of `min` or `max` must be specified.z `min` cannot be more than `max`.minmaxr,   r+   rK   rL   r,   r/   r/   r0   r)   `   s
   
zLength.__init__c                 C   s   |j rt|j p	d}|| jk s| jdkr]|| jkr_| j}|d u rP| jdkr.|dd| j}n"| jdkr<|dd| j}n| j| jkrK|dd| j}n|d	}t|t| j| j|d
 d S d S )Nr   rI   z.Field must be at least %(min)d character long.z/Field must be at least %(min)d characters long.z.Field cannot be longer than %(max)d character.z/Field cannot be longer than %(max)d characters.z-Field must be exactly %(max)d character long.z.Field must be exactly %(max)d characters long.z:Field must be between %(min)d and %(max)d characters long.)rK   rL   r   )	r@   lenrK   rL   r,   ngettextr?   r"   dict)r+   rC   rD   lr,   r/   r/   r0   rG   g   s&   


zLength.__call__)rI   rI   NrH   r/   r/   r/   r0   r   Q   s    
r   c                   @   r9   )r   a  
    Validates that a number is of a minimum and/or maximum value, inclusive.
    This will work with any comparable number type, such as floats and
    decimals, not just integers.

    :param min:
        The minimum required value of the number. If not provided, minimum
        value will not be checked.
    :param max:
        The maximum value of the number. If not provided, maximum value
        will not be checked.
    :param message:
        Error message to raise in case of a validation error. Can be
        interpolated using `%(min)s` and `%(max)s` if desired. Useful defaults
        are provided depending on the existence of min and max.
    Nc                 C   s   || _ || _|| _d S r'   rJ   rM   r/   r/   r0   r)      s   
zNumberRange.__init__c                 C   s   |j }|d u s t|s | jd ur|| jk s | jd urN|| jkrP| j}|d u rB| jd u r2|d}n| jd u r=|d}n|d}t|t| j| jd d S d S )Nz Number must be at least %(min)s.zNumber must be at most %(max)s.z+Number must be between %(min)s and %(max)s.)rK   rL   )	r@   mathisnanrK   rL   r,   r?   r"   rP   )r+   rC   rD   r@   r,   r/   r/   r0   rG      s   &


zNumberRange.__call__)NNNrH   r/   r/   r/   r0   r   {   s    
r   c                   @   s&   e Zd ZdZdZd	ddZdd ZdS )
r   a=  
    Allows empty input and stops the validation chain from continuing.

    If input is empty, also removes prior errors (such as processing errors)
    from the field.

    :param strip_whitespace:
        If True (the default) also stop the validation chain on input which
        consists of only whitespace.
    )r   Tc                 C   s    |r	dd | _ d S dd | _ d S )Nc                 S   s   |   S r'   )stripsr/   r/   r0   <lambda>   s    z#Optional.__init__.<locals>.<lambda>c                 S   s   | S r'   r/   rU   r/   r/   r0   rW      s    )string_check)r+   Zstrip_whitespacer/   r/   r0   r)      s   zOptional.__init__c                 C   sB   |j rt|j d tr| |j d sg |jd d < t d S d S )Nr   )raw_data
isinstancer   rX   errorsr#   )r+   rC   rD   r/   r/   r0   rG      s   &zOptional.__call__N)Tr4   r5   r6   r7   Zfield_flagsr)   rG   r/   r/   r/   r0   r      s
    

r   c                   @   &   e Zd ZdZdZdddZdd ZdS )	r   a  
    Checks the field's data is 'truthy' otherwise stops the validation chain.

    This validator checks that the ``data`` attribute on the field is a 'true'
    value (effectively, it does ``if field.data``.) Furthermore, if the data
    is a string type, a string containing only whitespace characters is
    considered false.

    If the data is empty, also removes prior errors (such as processing errors)
    from the field.

    **NOTE** this validator used to be called `Required` but the way it behaved
    (requiring coerced data, not input data) meant it functioned in a way
    which was not symmetric to the `Optional` validator and furthermore caused
    confusion with certain fields which coerced data to 'falsey' values like
    ``0``, ``Decimal(0)``, ``time(0)`` etc. Unless a very specific reason
    exists, we recommend using the :class:`InputRequired` instead.

    :param message:
        Error message to raise in case of a validation error.
    r   Nc                 C   
   || _ d S r'   r,   r+   r,   r/   r/   r0   r)         
zDataRequired.__init__c                 C   sV   |j rt|j tr'|j  s)| jd u r|d}n| j}g |jd d < t|d S d S )NThis field is required.)r@   rZ   r   rT   r,   r?   r[   r#   r+   rC   rD   r,   r/   r/   r0   rG      s   
zDataRequired.__call__r'   r\   r/   r/   r/   r0   r      s
    
r   c                       s    e Zd ZdZ fddZ  ZS )r   z
    Legacy alias for DataRequired.

    This is needed over simple aliasing for those who require that the
    class-name of required be 'Required.'

    c                    s*   t t| j|i | tjdtdd d S )Nz7Required is going away in WTForms 3.0, use DataRequired   )
stacklevel)superr   r)   warningswarnDeprecationWarning)r+   r-   r.   	__class__r/   r0   r)      s
   
zRequired.__init__)r4   r5   r6   r7   r)   __classcell__r/   r/   rk   r0   r      s    r   c                   @   r]   )	r   z
    Validates that input was provided for this field.

    Note there is a distinction between this and DataRequired in that
    InputRequired looks that form-input data was provided, and DataRequired
    looks at the post-coercion data.
    r^   Nc                 C   r_   r'   r`   ra   r/   r/   r0   r)      rb   zInputRequired.__init__c                 C   sF   |j r|j d s!| jd u r|d}n| j}g |jd d < t|d S )Nr   rc   )rY   r,   r?   r[   r#   rd   r/   r/   r0   rG      s   
zInputRequired.__call__r'   r\   r/   r/   r/   r0   r      s
    
r   c                   @   s$   e Zd ZdZdddZd	ddZdS )
r   a  
    Validates the field against a user provided regexp.

    :param regex:
        The regular expression string to use. Can also be a compiled regular
        expression pattern.
    :param flags:
        The regexp flags to use, for example re.IGNORECASE. Ignored if
        `regex` is not a string.
    :param message:
        Error message to raise in case of a validation error.
    r   Nc                 C   s&   t |trt||}|| _|| _d S r'   )rZ   r   recompileregexr,   )r+   rp   flagsr,   r/   r/   r0   r)     s   

zRegexp.__init__c                 C   sL   | j |jpd}|s$|d u r | jd u r|d}t|| j}t||S )Nr%   zInvalid input.)rp   matchr@   r,   r?   r"   r+   rC   rD   r,   rr   r/   r/   r0   rG     s   

zRegexp.__call__)r   Nr'   rH   r/   r/   r/   r0   r   
  s    
r   c                   @   s:   e Zd ZdZedejZ					d
ddZdd	 Z	dS )r   a  
    Validates an email address. Requires email_validator package to be
    installed. For ex: pip install wtforms[email].

    :param message:
        Error message to raise in case of a validation error.
    :param granular_messsage:
        Use validation failed message from email_validator library
        (Default False).
    :param check_deliverability:
        Perform domain name resolution check (Default False).
    :param allow_smtputf8:
        Fail validation for addresses that would require SMTPUTF8
        (Default True).
    :param allow_empty_local:
        Allow an empty local part (i.e. @example.com), e.g. for validating
        Postfix aliases (Default False).
    z(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*\Z|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"\Z)NFTc                 C   s2   t d u rtd|| _|| _|| _|| _|| _d S )Nz7Install 'email_validator' for email validation support.)email_validatorr8   r,   granular_messagecheck_deliverabilityallow_smtputf8allow_empty_local)r+   r,   ru   rv   rw   rx   r/   r/   r0   r)   C  s   
zEmail.__init__c              
   C   s   z|j d u r
t tj|j | j| j| jd W d S  tjyB } z| j}|d u r:| jr5|	|}t
||	d}t
|d }~ww )N)rv   rw   rx   zInvalid email address.)r@   rt   ZEmailNotValidErrorZvalidate_emailrv   rw   rx   r,   ru   r?   r"   )r+   rC   rD   er,   r/   r/   r0   rG   S  s&   


zEmail.__call__)NFFTF)
r4   r5   r6   r7   rn   ro   
IGNORECASEZ
user_regexr)   rG   r/   r/   r/   r0   r   *  s    
r   c                   @   s:   e Zd ZdZdddZdd Zed	d
 Zedd ZdS )r   aV  
    Validates an IP address. Requires ipaddress package to be instaled for Python 2 support.

    :param ipv4:
        If True, accept IPv4 addresses as valid (default True)
    :param ipv6:
        If True, accept IPv6 addresses as valid (default False)
    :param message:
        Error message to raise in case of a validation error.
    TFNc                 C   s6   t d u rtd|s|std|| _|| _|| _d S )Nz)Install 'ipaddress' for Python 2 support.zDIP Address Validator must have at least one of ipv4 or ipv6 enabled.)	ipaddressr8   r(   ipv4ipv6r,   )r+   r|   r}   r,   r/   r/   r0   r)   r  s   
zIPAddress.__init__c                 C   sV   |j }d}|r| jr| |p| jo| |}|s)| j}|d u r%|d}t|d S )NFzInvalid IP address.)r@   r|   
check_ipv4r}   
check_ipv6r,   r?   r"   )r+   rC   rD   valuevalidr,   r/   r/   r0   rG   {  s    
zIPAddress.__call__c                 C   8   zt |}W n
 ty   Y dS w t|t jsdS dS NFT)r{   r   r(   rZ   IPv4Addressclsr   addressr/   r/   r0   r~        zIPAddress.check_ipv4c                 C   r   r   )r{   r   r(   rZ   IPv6Addressr   r/   r/   r0   r     r   zIPAddress.check_ipv6)TFN)	r4   r5   r6   r7   r)   rG   classmethodr~   r   r/   r/   r/   r0   r   g  s    

	
r   c                       s.   e Zd ZdZd fdd	Z fddZ  ZS )r   zu
    Validates a MAC address.

    :param message:
        Error message to raise in case of a validation error.
    Nc                    s   d}t t| j||d d S )Nz&^(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$r`   )rg   r   r)   )r+   r,   patternrk   r/   r0   r)     s   zMacAddress.__init__c                    s0   | j }|d u r|d}tt| ||| d S )NzInvalid Mac address.)r,   r?   rg   r   rG   rd   rk   r/   r0   rG     s   
zMacAddress.__call__r'   r4   r5   r6   r7   r)   rG   rm   r/   r/   rk   r0   r     s    r   c                       s.   e Zd ZdZd fdd	Z fddZ  ZS )	r   a  
    Simple regexp based url validation. Much like the email validator, you
    probably want to validate the url later by other means if the url must
    resolve.

    :param require_tld:
        If true, then the domain-name portion of the URL must contain a .tld
        suffix.  Set this to false if you want to allow domains like
        `localhost`.
    :param message:
        Error message to raise in case of a validation error.
    TNc                    s,   d}t t| |tj| t|dd| _d S )NzP^[a-z]+://(?P<host>[^\/\?:]+)(?P<port>:[0-9]+)?(?P<path>\/.*?)?(?P<query>\?.*)?$Trequire_tldallow_ip)rg   r   r)   rn   rz   HostnameValidationvalidate_hostname)r+   r   r,   rp   rk   r/   r0   r)     s   zURL.__init__c                    sH   | j }|d u r|d}tt| |||}| |ds"t|d S )NzInvalid URL.host)r,   r?   rg   r   rG   r   groupr"   rs   rk   r/   r0   rG     s   
zURL.__call__)TNr   r/   r/   rk   r0   r     s    r   c                   @   r9   )r!   zn
    Validates a UUID.

    :param message:
        Error message to raise in case of a validation error.
    Nc                 C   r_   r'   r`   ra   r/   r/   r0   r)     rb   zUUID.__init__c                 C   sB   | j }|d u r|d}z	t|j W d S  ty    t|w )NzInvalid UUID.)r,   r?   uuidr!   r@   r(   r"   rd   r/   r/   r0   rG     s   
zUUID.__call__r'   rH   r/   r/   r/   r0   r!     s    
r!   c                   @   .   e Zd ZdZd	ddZdd Zedd ZdS )
r   ae  
    Compares the incoming data to a sequence of valid inputs.

    :param values:
        A sequence of valid inputs.
    :param message:
        Error message to raise in case of a validation error. `%(values)s`
        contains the list of values.
    :param values_formatter:
        Function used to format the list of values in the error message.
    Nc                 C   $   || _ || _|d u r| j}|| _d S r'   valuesr,   default_values_formattervalues_formatterr+   r   r,   r   r/   r/   r0   r)     
   
zAnyOf.__init__c                 C   sB   |j | jvr| j}|d u r|d}t|t| | jd d S )Nz*Invalid value, must be one of: %(values)s.r   r@   r   r,   r?   r"   rP   r   rd   r/   r/   r0   rG        
zAnyOf.__call__c                 C      d dd | D S )N, c                 s       | ]}t |V  qd S r'   r   .0xr/   r/   r0   	<genexpr>	      z1AnyOf.default_values_formatter.<locals>.<genexpr>joinr   r/   r/   r0   r        zAnyOf.default_values_formatterNNr4   r5   r6   r7   r)   rG   staticmethodr   r/   r/   r/   r0   r         
r   c                   @   r   )
r   ai  
    Compares the incoming data to a sequence of invalid inputs.

    :param values:
        A sequence of invalid inputs.
    :param message:
        Error message to raise in case of a validation error. `%(values)s`
        contains the list of values.
    :param values_formatter:
        Function used to format the list of values in the error message.
    Nc                 C   r   r'   r   r   r/   r/   r0   r)     r   zNoneOf.__init__c                 C   sB   |j | jv r| j}|d u r|d}t|t| | jd d S )Nz+Invalid value, can't be any of: %(values)s.r   r   rd   r/   r/   r0   rG     r   zNoneOf.__call__c                 C   r   )Nr   c                 s   r   r'   r   r   r/   r/   r0   r   )  r   z2NoneOf.default_values_formatter.<locals>.<genexpr>r   )vr/   r/   r0   r   '  r   zNoneOf.default_values_formatterr   r   r/   r/   r/   r0   r     r   r   c                   @   s>   e Zd ZdZedejZedejZdddZ	dd	 Z
d
S )r   z
    Helper class for checking hostnames for validation.

    This is not a validator in and of itself, and as such is not exported.
    z ^(xn-|[a-z0-9_]+)(-[a-z0-9_]+)*$z*^([a-z]{2,20}|xn--([a-z0-9]+-)*[a-z0-9]+)$TFc                 C   r:   r'   r   )r+   r   r   r/   r/   r0   r)   5  r<   zHostnameValidation.__init__c                 C   s   | j rt|st|rdS z|d}W n	 ty   Y nw t|ts*|d}t	|dkr2dS |
d}|D ]}|rCt	|dkrF dS | j|sO dS q9| jrct	|dk sa| j|d	 scdS dS )
NTidnaascii   F.?   re   rI   )r   r   r~   r   encodeUnicodeErrorrZ   r   decoderN   splithostname_partrr   r   tld_part)r+   hostnamepartspartr/   r/   r0   rG   9  s.   


zHostnameValidation.__call__N)TF)r4   r5   r6   r7   rn   ro   rz   r   r   r)   rG   r/   r/   r/   r0   r   ,  s    
r   )0
__future__r   rR   rn   r   rh   rt   ImportErrorr{   Zwtforms.compatr   r   __all__r(   r"   r8   r#   objectr	   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   r   r   r   r   r/   r/   r/   r0   <module>   sb    
 *(& =9%  .