a
    »ÏOc•  ã                   @   s€   d Z ddlZddlZddlmZ ddlmZ ddlmZm	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dS )zÊ
Attributes that deal with networking specific values such as emails, urls and ip addresses

These can be imported directly from `confu.schema`

## Requirements

- `ipaddress` for ip address validation
é    N)Úurlparse)ÚSoftDependencyError)ÚStrÚValidationErrorc                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚEmailz$
    Describes an email address
    c                    sX   t ƒ j||fi |¤Ž}|dkr(| jr(|S |d u r:| jr:|S t d|¡sTt| ||dƒ‚|S )NÚ z[^@\s]+@[^@\s]+zemail address expected)ÚsuperÚvalidateÚblankÚdefault_is_noneÚreÚmatchr   ©ÚselfÚvalueÚpathÚkwargs©Ú	__class__© úH/var/www/html/gsl.tools/lib/python3.9/site-packages/confu/schema/inet.pyr	      s    zEmail.validate)Ú__name__Ú
__module__Ú__qualname__Ú__doc__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 )ÚUrlz
    Describes a URL
    r   c                    s(   t ƒ jf d|i|¤Ž | dg ¡| _d S )NÚnameÚschemes)r   Ú__init__Úgetr   )r   r   r   r   r   r   r   /   s    zUrl.__init__c                    s¼   t ƒ j||fi |¤Ž}|dkr(| jr(|S |du r:| jr:|S zt|ƒ}W n  tyf   t| ||dƒ‚Y n0 |js|t| ||dƒ‚|js�t| ||dƒ‚| j	r¸|j| j	vr¸t| ||d|j› �ƒ‚|S )aM  
        Currently only validates by running urlparse against it
        and checking that a scheme and netloc is set - and if a list of allowed
        schemes is provide that the scheme is valid against that list

        TODO: may want something more sophisticated than that - could look
        at django's url validator
        r   Nzurl expectedzno url scheme specifiedzno url netloc specifiedzinvalid url scheme: )
r   r	   r
   r   r   Ú
ValueErrorr   ÚschemeÚnetlocr   )r   r   r   r   Úresultr   r   r   r	   3   s$    	ÿzUrl.validate)r   )r   r   r   r   r   r	   r   r   r   r   r   r   )   s   r   c                       s>   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Z‡ fd
d„Z‡  ZS )Ú	IpAddressz*
    Describes a IPv4 or IPv6 address
    r   Nc                    s<   t ƒ jf d|i|¤Ž ts"tdƒ‚|dvr2tdƒ‚|| _dS )á+  
        Initialize attribute

        **Keyword Arguments**

        - name (`str`): describes the attribute name, if not specified
          explicitly will be set through the schema that instantiates
          the attribute.
        - protocol (`int`): ip version, can be 4, 6 or None - if it is none
          the attribute can hold either a v4 or a v6 IP address.
        - default (`mixed`): the default value of this attribute. Once a default
          value is set, schema validation will no longer raise a
          validation error if the attribute is missing from the
          configuration.
        - choices (`list`): if specified on values in this list may be set
          for this attribute
        - help (`str`): help description
        - cli (`bool=True`): enable CLI support for this attribute
        - deprecated (`str`): version id of when this attribute will be deprecated
        - added (`str`): version id of when this attribute was added to the schema
        - removed (`str`): version id of when this attribute will be removed
        r   Ú	ipaddress©Né   é   ú2IpAddress protocol needs to be either 4, 6 or NoneN)r   r   r'   r   r!   Úprotocol©r   r   r,   r   r   r   r   r   ]   s    zIpAddress.__init__c                 K   s(   zt  |¡W S  t jy"   Y dS 0 d S ©NF)r'   ÚIPv4AddressÚAddressValueErrorr   r   r   r   Úvalidate_v4}   s    zIpAddress.validate_v4c                 K   s(   zt  |¡W S  t jy"   Y dS 0 d S r.   )r'   ÚIPv6Addressr0   r   r   r   r   Úvalidate_v6ƒ   s    zIpAddress.validate_v6c                    sÌ   t ƒ j||fi |¤Ž}|d u r(| jr(|S | jr:|dkr:|S |› }| j||fi |¤Ž}| j||fi |¤Ž}| jdkr†|s†t| ||dƒ‚n>| jdkr¤|s¤t| ||dƒ‚n | jd u rÄ|sÄ|sÄt| ||dƒ‚|pÊ|S )Nr   r)   zinvalid ip (v4)r*   zinvalid ip (v6)zinvalid ip (v4 or v6)©r   r	   r   r
   r1   r3   r,   r   ©r   r   r   r   Zvalue_v4Zvalue_v6r   r   r   r	   ‰   s    zIpAddress.validate)r   N©	r   r   r   r   r   r1   r3   r	   r   r   r   r   r   r%   W   s
    r%   c                       s>   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Z‡ fd
d„Z‡  ZS )Ú	IpNetworkz,
    Describes a IPv4 or IPv6 IP prefix
    r   Nc                    s0   t ƒ jf d|i|¤Ž |dvr&tdƒ‚|| _dS )r&   r   r(   r+   N)r   r   r!   r,   r-   r   r   r   r   ¤   s    zIpNetwork.__init__c                 K   s(   zt  |¡W S  t jy"   Y dS 0 d S r.   )r'   ÚIPv4Networkr0   r   r   r   r   r1   Ã   s    zIpNetwork.validate_v4c                 K   s(   zt  |¡W S  t jy"   Y dS 0 d S r.   )r'   ÚIPv6Networkr0   r   r   r   r   r3   É   s    zIpNetwork.validate_v6c                    sÌ   t ƒ j||fi |¤Ž}|d u r(| jr(|S | jr:|dkr:|S |› }| j||fi |¤Ž}| j||fi |¤Ž}| jdkr†|s†t| ||dƒ‚n>| jdkr¤|s¤t| ||dƒ‚n | jd u rÄ|sÄ|sÄt| ||dƒ‚|pÊ|S )Nr   r)   zinvalid network (v4)r*   zinvalid network (v6)zinvalid network (v4 or v6)r4   r5   r   r   r   r	   Ï   s    zIpNetwork.validate)r   Nr6   r   r   r   r   r7   ž   s
   r7   )r   r'   r   Úurllib.parser   Zconfu.exceptionsr   Zconfu.schema.corer   r   r   r   r%   r7   r   r   r   r   Ú<module>   s   
.G