U
    fgT                     @   sv  d dl Z d dlZd dlZd dlZdZG dd deZG dd deZG dd dZ	G d	d
 d
Z
G dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd deZG dd deZG dd dZG dd  d ZG d!d" d"ZG d#d$ d$ZG d%d& d&ZG d'd( d(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'eZ(dS ))    N)!DataRequireddata_requiredEmailemailEqualToequal_to	IPAddress
ip_addressInputRequiredinput_requiredLengthlengthNumberRangenumber_rangeOptionaloptionalRegexpregexpURLurlAnyOfany_ofNoneOfnone_of
MacAddressmac_addressUUIDValidationErrorStopValidationreadonlyReadOnlydisabledDisabledc                   @   s   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   s   t j| |f|| d S N)
ValueError__init__selfmessageargskwargs r,   6/tmp/pip-unpacked-wheel-p06mjha7/wtforms/validators.pyr&   0   s    zValidationError.__init__N)r#   __name__
__module____qualname____doc__r&   r,   r,   r,   r-   r   +   s   r   c                   @   s   e Zd ZdZdddZdS )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   s   t j| |f|| d S r$   )	Exceptionr&   r'   r,   r,   r-   r&   =   s    zStopValidation.__init__N)r#   r.   r,   r,   r,   r-   r   4   s   r   c                   @   s"   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   s   || _ || _d S r$   )	fieldnamer)   )r(   r4   r)   r,   r,   r-   r&   M   s    zEqualTo.__init__c              
   C   s   z|| j  }W n8 tk
rF } zt|d| j  |W 5 d }~X Y nX |j|jkrXd S t|drj|jjpn| j | j d}| j}|d kr|d}t|| d S )NzInvalid field name '%s'.label)Zother_labelZ
other_namez&Field must be equal to %(other_name)s.)	r4   KeyErrorr   gettextdatahasattrr5   textr)   )r(   formfieldotherexcdr)   r,   r,   r-   __call__Q   s(    

zEqualTo.__call__)Nr/   r0   r1   r2   r&   r@   r,   r,   r,   r-   r   A   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.

    When supported, sets the `minlength` and `maxlength` attributes on widgets.
    Nc                 C   sx   |dks|dkst d|dks0||ks0t d|| _|| _|| _i | _| jdkr^| j| jd< | jdkrt| j| jd< d S )NrB   z1At least one of `min` or `max` must be specified.z `min` cannot be more than `max`.Z	minlengthZ	maxlength)AssertionErrorminmaxr)   field_flagsr(   rD   rE   r)   r,   r,   r-   r&   z   s    

zLength.__init__c                 C   s   |j rt|j pd}|| jkr6| jdks2|| jkr6d S | jd k	rH| j}n`| jdkrd|dd| j}nD| jdkr|dd| j}n(| j| jkr|dd| j}n
|d	}t|t| j| j|d
 d S )Nr   rB   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.)rD   rE   r   )	r8   lenrD   rE   r)   ngettextr7   r   dict)r(   r;   r<   r   r)   r,   r,   r-   r@      s6    


zLength.__call__)rB   rB   NrA   r,   r,   r,   r-   r   h   s   
r   c                   @   s"   e Zd ZdZdddZdd ZdS )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.

    When supported, sets the `min` and `max` attributes on widgets.
    Nc                 C   sH   || _ || _|| _i | _| j d k	r.| j | jd< | jd k	rD| j| jd< d S )NrD   rE   )rD   rE   r)   rF   rG   r,   r,   r-   r&      s    

zNumberRange.__init__c                 C   s   |j }|d k	rDt|sD| jd ks,|| jkrD| jd ks@|| jkrDd S | jd k	rV| j}n6| jd krl|d}n | jd kr|d}n
|d}t|t| j| jd 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.)rD   rE   )	r8   mathisnanrD   rE   r)   r7   r   rJ   )r(   r;   r<   r8   r)   r,   r,   r-   r@      s,    



zNumberRange.__call__)NNNrA   r,   r,   r,   r-   r      s   

r   c                   @   s"   e Zd ZdZdddZdd ZdS )	r   al  
    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.

    Sets the `optional` attribute on widgets.
    Tc                 C   s(   |rdd | _ n
dd | _ ddi| _d S )Nc                 S   s   |   S r$   )stripsr,   r,   r-   <lambda>       z#Optional.__init__.<locals>.<lambda>c                 S   s   | S r$   r,   rN   r,   r,   r-   rP      rQ   r   T)string_checkrF   )r(   strip_whitespacer,   r,   r-   r&      s    
zOptional.__init__c                 C   s>   |j r&t|j d tr:| |j d s:g |jd d < t d S )Nr   )raw_data
isinstancestrrR   errorsr   r(   r;   r<   r,   r,   r-   r@      s    zOptional.__call__N)TrA   r,   r,   r,   r-   r      s   
r   c                   @   s"   e 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.

    Sets the `required` attribute on widgets.
    Nc                 C   s   || _ ddi| _d S NrequiredTr)   rF   r(   r)   r,   r,   r-   r&     s    zDataRequired.__init__c                 C   sV   |j r t|j tr|j  r d S | jd kr6|d}n| j}g |jd d < t|d S )NThis field is required.)r8   rU   rV   rM   r)   r7   rW   r   r(   r;   r<   r)   r,   r,   r-   r@      s    
zDataRequired.__call__)NrA   r,   r,   r,   r-   r     s   
r   c                   @   s"   e Zd ZdZdddZdd ZdS )r
   a  
    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. This means that this validator only checks
    whether non-empty data was sent, not whether non-empty data was coerced
    from that data. Initially populated data is not considered sent.

    Sets the `required` attribute on widgets.
    Nc                 C   s   || _ ddi| _d S rY   r[   r\   r,   r,   r-   r&   :  s    zInputRequired.__init__c                 C   sJ   |j r|j d rd S | jd kr*|d}n| j}g |jd d < t|d S )Nr   r]   )rT   r)   r7   rW   r   r^   r,   r,   r-   r@   >  s    
zInputRequired.__call__)NrA   r,   r,   r,   r-   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$   )rU   rV   recompileregexr)   )r(   ra   flagsr)   r,   r,   r-   r&   Y  s    
zRegexp.__init__c                 C   sJ   | j |jpd}|r|S |d kr>| jd kr8|d}n| j}t|d S )Nr#   zInvalid input.)ra   matchr8   r)   r7   r   r(   r;   r<   r)   rc   r,   r,   r-   r@   _  s    
zRegexp.__call__)r   N)NrA   r,   r,   r,   r-   r   K  s   
r   c                   @   s"   e Zd Zd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_message:
        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).
    NFTc                 C   s"   || _ || _|| _|| _|| _d S r$   )r)   granular_messagecheck_deliverabilityallow_smtputf8allow_empty_local)r(   r)   re   rf   rg   rh   r,   r,   r-   r&     s
    zEmail.__init__c              
   C   s   zdd l }W n, tk
r8 } ztd|W 5 d }~X Y nX z0|jd krN| |j|j| j| j| jd W nX |jk
r } z8| j	}|d kr| j
r||}n
|d}t||W 5 d }~X Y nX d S )Nr   z7Install 'email_validator' for email validation support.)rf   rg   rh   zInvalid email address.)email_validatorImportErrorr3   r8   ZEmailNotValidErrorZvalidate_emailrf   rg   rh   r)   re   r7   r   )r(   r;   r<   ri   r>   er)   r,   r,   r-   r@     s0    


zEmail.__call__)NFFTFrA   r,   r,   r,   r-   r   m  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   a  
    Validates an IP address.

    :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   s&   |s|st d|| _|| _|| _d S )NzDIP Address Validator must have at least one of ipv4 or ipv6 enabled.)r%   ipv4ipv6r)   )r(   rl   rm   r)   r,   r,   r-   r&     s    zIPAddress.__init__c                 C   sZ   |j }d}|r.| jr| |p,| jo,| |}|r6d S | j}|d krN|d}t|d S )NFzInvalid IP address.)r8   rl   
check_ipv4rm   
check_ipv6r)   r7   r   )r(   r;   r<   valuevalidr)   r,   r,   r-   r@     s    
zIPAddress.__call__c                 C   s:   zt |}W n tk
r$   Y dS X t|t js6dS dS NFT)	ipaddressr	   r%   rU   IPv4Addressclsrp   addressr,   r,   r-   rn     s    zIPAddress.check_ipv4c                 C   s:   zt |}W n tk
r$   Y dS X t|t js6dS dS rr   )rs   r	   r%   rU   IPv6Addressru   r,   r,   r-   ro     s    zIPAddress.check_ipv6)TFN)	r/   r0   r1   r2   r&   r@   classmethodrn   ro   r,   r,   r,   r-   r     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  j||d d S )Nz&^(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$r)   )superr&   )r(   r)   pattern	__class__r,   r-   r&     s    zMacAddress.__init__c                    s,   | j }|d kr|d}t ||| d S )NzInvalid Mac address.)r)   r7   r{   r@   r^   r}   r,   r-   r@     s    
zMacAddress.__call__)Nr/   r0   r1   r2   r&   r@   __classcell__r,   r,   r}   r-   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 allow_ip:
        If false, then give ip as host will fail validation
    :param message:
        Error message to raise in case of a validation error.
    TNc                    s(   d}t  |tj| t||d| _d S )NzP^[a-z]+://(?P<host>[^\/\?:]+)(?P<port>:[0-9]+)?(?P<path>\/.*?)?(?P<query>\?.*)?$require_tldallow_ip)r{   r&   r_   
IGNORECASEHostnameValidationvalidate_hostname)r(   r   r   r)   ra   r}   r,   r-   r&     s     zURL.__init__c                    sD   | j }|d kr|d}t |||}| |ds@t|d S )NzInvalid URL.host)r)   r7   r{   r@   r   groupr   rd   r}   r,   r-   r@     s    
zURL.__call__)TTNr   r,   r,   r}   r-   r     s   r   c                   @   s"   e Zd ZdZdddZdd ZdS )r   zn
    Validates a UUID.

    :param message:
        Error message to raise in case of a validation error.
    Nc                 C   s
   || _ d S r$   rz   r\   r,   r,   r-   r&   +  s    zUUID.__init__c              
   C   sZ   | j }|d kr|d}zt|j W n, tk
rT } zt||W 5 d }~X Y nX d S )NzInvalid UUID.)r)   r7   uuidr   r8   r%   r   )r(   r;   r<   r)   r>   r,   r,   r-   r@   .  s    
zUUID.__call__)NrA   r,   r,   r,   r-   r   #  s   
r   c                   @   s.   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   s$   || _ || _|d kr| j}|| _d S r$   valuesr)   default_values_formattervalues_formatterr(   r   r)   r   r,   r,   r-   r&   E  s
    zAnyOf.__init__c                 C   sF   |j | jkrd S | j}|d kr(|d}t|t| | jd d S )Nz*Invalid value, must be one of: %(values)s.r   r8   r   r)   r7   r   rJ   r   r^   r,   r,   r-   r@   L  s    
zAnyOf.__call__c                 C   s   d dd | D S )N, c                 s   s   | ]}t |V  qd S r$   rV   .0xr,   r,   r-   	<genexpr>X  s     z1AnyOf.default_values_formatter.<locals>.<genexpr>joinr   r,   r,   r-   r   V  s    zAnyOf.default_values_formatter)NNr/   r0   r1   r2   r&   r@   staticmethodr   r,   r,   r,   r-   r   8  s
   

r   c                   @   s.   e Zd ZdZd	ddZdd Zedd ZdS )
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   s$   || _ || _|d kr| j}|| _d S r$   r   r   r,   r,   r-   r&   h  s
    zNoneOf.__init__c                 C   sF   |j | jkrd S | j}|d kr(|d}t|t| | jd d S )Nz+Invalid value, can't be any of: %(values)s.r   r   r^   r,   r,   r-   r@   o  s    
zNoneOf.__call__c                 C   s   d dd | D S )Nr   c                 s   s   | ]}t |V  qd S r$   r   r   r,   r,   r-   r   {  s     z2NoneOf.default_values_formatter.<locals>.<genexpr>r   )vr,   r,   r-   r   y  s    zNoneOf.default_values_formatter)NNr   r,   r,   r,   r-   r   [  s
   

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   s   || _ || _d S r$   r   )r(   r   r   r,   r,   r-   r&     s    zHostnameValidation.__init__c                 C   s   | j rt|st|rdS z|d}W n tk
r@   Y nX t|tsV|d}t	|dkrfdS |
d}|D ],}|rt	|dkr dS | j|st dS qt| jrt	|dk s| j|d	 sdS dS )
NTidnaascii   F.?      rB   )r   r   rn   ro   encodeUnicodeErrorrU   rV   decoderH   splithostname_partrc   r   tld_part)r(   hostnamepartspartr,   r,   r-   r@     s.    


"zHostnameValidation.__call__N)TF)r/   r0   r1   r2   r_   r`   r   r   r   r&   r@   r,   r,   r,   r-   r   ~  s
   
r   c                   @   s    e Zd ZdZdd Zdd ZdS )r    z
    Set a field readonly.

    Validation fails if the form data is different than the
    field object data, or if unset, from the field default data.
    c                 C   s   ddi| _ d S )Nr   TrF   r(   r,   r,   r-   r&     s    zReadOnly.__init__c                 C   s   |j |jkrt|dd S )NzThis field cannot be edited)r8   Zobject_datar   r7   rX   r,   r,   r-   r@     s    zReadOnly.__call__NrA   r,   r,   r,   r-   r      s   r    c                   @   s    e Zd ZdZdd Zdd ZdS )r"   zU
    Set a field disabled.

    Validation fails if the form data has any value.
    c                 C   s   ddi| _ d S )Nr!   Tr   r   r,   r,   r-   r&     s    zDisabled.__init__c                 C   s   |j d k	rt|dd S )Nz.This field is disabled and cannot have a value)rT   r   r7   rX   r,   r,   r-   r@     s    
zDisabled.__call__NrA   r,   r,   r,   r-   r"     s   r"   ))rs   rK   r_   r   __all__r%   r   r3   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   r!   r,   r,   r,   r-   <module>   sL   %	'B9 *"=>'##/