U
    PN^K                     @   s  d dl mZ d dlZd dlZd dlZ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                   @   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-   7/tmp/pip-install-o1yuzyg2/WTForms/wtforms/validators.pyr'      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#      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)   r5   r*   r-   r-   r.   r'   2   s    zEqualTo.__init__c                 C   s   z|| j  }W n( tk
r6   t|d| j  Y nX |j|jkrt|drV|jjpZ| 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.)	r5   KeyErrorr"   gettextdatahasattrr6   textr*   )r)   formfieldotherdr*   r-   r-   r.   __call__6   s    
zEqualTo.__call__)Nr0   r1   r2   r3   r'   r@   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 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t d|dks0||ks0t d|| _|| _|| _d S )NrB   z1At least one of `min` or `max` must be specified.z `min` cannot be more than `max`.)AssertionErrorminmaxr*   r)   rD   rE   r*   r-   r-   r.   r'   V   s
    zLength.__init__c                 C   s   |j rt|j pd}|| jk s2| jdkr|| jkr| j}|d kr| jdkr\|dd| j}n&| jdkrx|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 between %(min)d and %(max)d characters long.)rD   rE   r   )	r9   lenrD   rE   r*   ngettextr8   r"   dict)r)   r<   r=   lr*   r-   r-   r.   r@   ]   s     
 
 
zLength.__call__)rB   rB   NrA   r-   r-   r-   r.   r   G   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.
    Nc                 C   s   || _ || _|| _d S r%   )rD   rE   r*   rF   r-   r-   r.   r'      s    zNumberRange.__init__c                 C   s   |j }|d ks6| jd k	r"|| jk s6| jd k	r|| jkr| j}|d krz| jd krZ|d}n | jd krp|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   )r9   rD   rE   r*   r8   r"   rI   )r)   r<   r=   r9   r*   r-   r-   r.   r@      s    


zNumberRange.__call__)NNNrA   r-   r-   r-   r.   r   n   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 | _ n
dd | _ d S )Nc                 S   s   |   S r%   )stripsr-   r-   r.   <lambda>       z#Optional.__init__.<locals>.<lambda>c                 S   s   | S r%   r-   rL   r-   r-   r.   rN      rO   )string_check)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
isinstancer   rP   errorsr#   )r)   r<   r=   r-   r-   r.   r@      s    &zOptional.__call__N)Tr0   r1   r2   r3   Zfield_flagsr'   r@   r-   r-   r-   r.   r      s   

r   c                   @   s&   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   s
   || _ d S r%   r*   r)   r*   r-   r-   r.   r'      s    zDataRequired.__init__c                 C   sR   |j rt|j trN|j  sN| jd kr2|d}n| j}g |jd d < t|d S )NThis field is required.)r9   rS   r   rK   r*   r8   rT   r#   r)   r<   r=   r*   r-   r-   r.   r@      s    
zDataRequired.__call__)NrU   r-   r-   r-   r.   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|| 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-   r.   r'      s     zRequired.__init__)r0   r1   r2   r3   r'   __classcell__r-   r-   ra   r.   r      s   r   c                   @   s&   e Zd ZdZdZdddZdd ZdS )	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.
    rV   Nc                 C   s
   || _ d S r%   rW   rX   r-   r-   r.   r'      s    zInputRequired.__init__c                 C   sF   |j r|j d sB| jd kr&|d}n| j}g |jd d < t|d S )Nr   rY   )rR   r*   r8   rT   r#   rZ   r-   r-   r.   r@      s    
zInputRequired.__call__)NrU   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%   )rS   r   recompileregexr*   )r)   rf   flagsr*   r-   r-   r.   r'   
  s    
zRegexp.__init__c                 C   sF   | j |jpd}|sB|d kr:| jd kr4|d}n| j}t||S )Nr$   zInvalid input.)rf   matchr9   r*   r8   r"   r)   r<   r=   r*   rh   r-   r-   r.   r@     s    
zRegexp.__call__)r   N)NrA   r-   r-   r-   r.   r      s   
r   c                   @   s0   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. Note that this uses a very primitive regular
    expression and should only be used in instances where you later verify by
    other means, such as email activation or lookups.

    :param message:
        Error message to raise in case of a validation error.
    z(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*\Z|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"\Z)Nc                 C   s   || _ tdd| _d S )NT)require_tld)r*   HostnameValidationvalidate_hostnamerX   r-   r-   r.   r'   ,  s    zEmail.__init__c                 C   sl   |j }| j}|d kr|d}|r*d|kr2t||dd\}}| j|sVt|| |sht|d S )NzInvalid email address.@   )r9   r*   r8   r"   rsplit
user_regexrh   rl   )r)   r<   r=   valuer*   Z	user_partZdomain_partr-   r-   r.   r@   2  s    

zEmail.__call__)N)
r0   r1   r2   r3   rd   re   
IGNORECASErp   r'   r@   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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)   rs   rt   r*   r-   r-   r.   r'   P  s
    zIPAddress.__init__c                 C   sV   |j }d}|r.| jr| |p,| jo,| |}|sR| j}|d krJ|d}t|d S )NFzInvalid IP address.)r9   rs   
check_ipv4rt   
check_ipv6r*   r8   r"   )r)   r<   r=   rq   Zvalidr*   r-   r-   r.   r@   W  s     
zIPAddress.__call__c                 C   sP   | d}t|dkrLtdd |D rLtdd |D }tdd |D S dS )N.   c                 s   s   | ]}|  V  qd S r%   )isdigit.0xr-   r-   r.   	<genexpr>f  s     z'IPAddress.check_ipv4.<locals>.<genexpr>c                 s   s   | ]}t |V  qd S r%   )intrz   r-   r-   r.   r}   g  s     c                 s   s   | ]}|d ko|dk V  qdS )r      Nr-   )r{   numr-   r-   r.   r}   h  s     F)splitrG   alllist)clsrq   partsZnumbersr-   r-   r.   ru   c  s
    
zIPAddress.check_ipv4c              	   C   s   | d}t|dkrdS d}|D ]P}|s4|d7 }q"zt|d}W n tk
rZ   Y  dS X |dk sl|dkr" dS q"|dk rd	S |dkr|d s|d sd	S dS )
N:   Fr   rn      i   r[   T)r   rG   r~   r&   )r   rq   r   Z	num_blankpartr-   r-   r.   rv   k  s$    


zIPAddress.check_ipv6)TFN)	r0   r1   r2   r3   r'   r@   classmethodru   rv   r-   r-   r-   r.   r   E  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}$rW   )r]   r   r'   )r)   r*   patternra   r-   r.   r'     s    zMacAddress.__init__c                    s0   | j }|d kr|d}tt| ||| d S )NzInvalid Mac address.)r*   r8   r]   r   r@   rZ   ra   r-   r.   r@     s    
zMacAddress.__call__)Nr0   r1   r2   r3   r'   r@   rc   r-   r-   ra   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 message:
        Error message to raise in case of a validation error.
    TNc                    s,   d}t t| |tj| t|dd| _d S )Nz<^[a-z]+://(?P<host>[^/:]+)(?P<port>:[0-9]+)?(?P<path>\/.*)?$Trj   allow_ip)r]   r   r'   rd   rr   rk   rl   )r)   rj   r*   rf   ra   r-   r.   r'     s    zURL.__init__c                    sH   | j }|d kr|d}tt| |||}| |dsDt|d S )NzInvalid URL.host)r*   r8   r]   r   r@   rl   groupr"   ri   ra   r-   r.   r@     s    
zURL.__call__)TNr   r-   r-   ra   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%   rW   rX   r-   r-   r.   r'     s    zUUID.__init__c                 C   sJ   | j }|d kr|d}zt|j W n tk
rD   t|Y nX d S )NzInvalid UUID.)r*   r8   uuidr!   r9   r&   r"   rZ   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'     s
    zAnyOf.__init__c                 C   sB   |j | jkr>| j}|d kr$|d}t|t| | jd d S )Nz*Invalid value, must be one of: %(values)s.r   r9   r   r*   r8   r"   rI   r   rZ   r-   r-   r.   r@     s
    
zAnyOf.__call__c                 C   s   d dd | D S )N, c                 s   s   | ]}t |V  qd S r%   r   rz   r-   r-   r.   r}     s     z1AnyOf.default_values_formatter.<locals>.<genexpr>joinr   r-   r-   r.   r     s    zAnyOf.default_values_formatter)NNr0   r1   r2   r3   r'   r@   staticmethodr   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   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'     s
    zNoneOf.__init__c                 C   sB   |j | jkr>| j}|d kr$|d}t|t| | jd d S )Nz+Invalid value, can't be any of: %(values)s.r   r   rZ   r-   r-   r.   r@     s
    
zNoneOf.__call__c                 C   s   d dd | D S )Nr   c                 s   s   | ]}t |V  qd S r%   r   rz   r-   r-   r.   r}     s     z2NoneOf.default_values_formatter.<locals>.<genexpr>r   )vr-   r-   r.   r     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 )rk   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)   rj   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   Frw   ?   r[   rB   )r   r   ru   rv   encodeUnicodeErrorrS   r   decoderG   r   hostname_partrh   rj   tld_part)r)   hostnamer   r   r-   r-   r.   r@     s*    


zHostnameValidation.__call__N)TF)r0   r1   r2   r3   rd   re   rr   r   r   r'   r@   r-   r-   r-   r.   rk     s
   
rk   ),
__future__r   rd   r   r^   Zwtforms.compatr   r   __all__r&   r"   r4   r#   objectr	   r   r   r   r   r   r   r   r   r   r   r   r!   r   r   rk   r   r
   r   r    r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   <module>   sJ   
 '(& (@  .