U
    <{fw1                     @   s   d Z ddlZddlZddlmZmZmZ ddlZddl	Zddl
ZddlZddlZddlZddlZejjhZejjejjejjhZdd ZejG dd dejZG dd	 d	ZejjG d
d deZdS )z)DNS nodes.  A node is a set of rdatasets.    N)AnyDictOptionalc                 C   s   || kp|t jjko|| kS N)dns	rdatatypeZRRSIG)Zrdtypesrdtypecovers r
   G/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/dns/node.py_matches_type_or_its_signature,   s    r   c                   @   sR   e Zd ZdZdZdZdZeej	j
ej	j
d dddZeejjd dd	d
ZdS )NodeKindzRdatasets in nodesr         )r   r	   returnc                 C   s.   t t||rtjS t t||r$tjS tjS d S r   )r   _cname_typesr   CNAME_neutral_typesNEUTRALREGULAR)clsr   r	   r
   r
   r   classify8   s
    zNodeKind.classify)rdatasetr   c                 C   s   |  |j|jS r   )r   r   r	   )r   r   r
   r
   r   classify_rdatasetC   s    zNodeKind.classify_rdatasetN)__name__
__module____qualname____doc__r   r   r   classmethodr   r   	RdataTyper   r   Rdatasetr   r
   r
   r
   r   r   0   s    
r   c                   @   s(  e Zd ZdZdgZdd Zejje	e
ef e
dddZdd	 Zd
d Zdd Zdd Zdd Zdd ZejjdfejjejjejjeejjdddZejjdfejjejjejjeeejj dddZejjfejjejjejjddddZejjdddd Ze d!d"d#Z!ed!d$d%Z"dS )&Nodea  A Node is a set of rdatasets.

    A node is either a CNAME node or an "other data" node.  A CNAME
    node contains only CNAME, KEY, NSEC, and NSEC3 rdatasets along with their
    covering RRSIG rdatasets.  An "other data" node contains any
    rdataset other than a CNAME or RRSIG(CNAME) rdataset.  When
    changes are made to a node, the CNAME or "other data" state is
    always consistent with the update, i.e. the most recent change
    wins.  For example, if you have a node which contains a CNAME
    rdataset, and then add an MX rdataset to it, then the CNAME
    rdataset will be deleted.  Likewise if you have a node containing
    an MX rdataset and add a CNAME rdataset, the MX rdataset will be
    deleted.
    	rdatasetsc                 C   s
   g | _ d S r   r"   selfr
   r
   r   __init__Z   s    zNode.__init__)namekwr   c                 K   sN   t  }| jD ].}t|dkr||j|f| |d q| dd S )a"  Convert a node to text format.

        Each rdataset at the node is printed.  Any keyword arguments
        to this method are passed on to the rdataset's to_text() method.

        *name*, a ``dns.name.Name``, the owner name of the
        rdatasets.

        Returns a ``str``.

        r   
N)ioStringIOr"   lenwriteto_textgetvalue)r%   r'   r(   srdsr
   r
   r   r/   ^   s    
zNode.to_textc                 C   s   dt t|  d S )Nz
<DNS node >)stridr$   r
   r
   r   __repr__r   s    zNode.__repr__c                 C   s<   | j D ]}||j kr dS q|j D ]}|| j kr" dS q"dS )NFTr#   )r%   otherrdr
   r
   r   __eq__u   s    



zNode.__eq__c                 C   s   |  | S r   )r9   )r%   r7   r
   r
   r   __ne__   s    zNode.__ne__c                 C   s
   t | jS r   )r-   r"   r$   r
   r
   r   __len__   s    zNode.__len__c                 C   s
   t | jS r   )iterr"   r$   r
   r
   r   __iter__   s    zNode.__iter__c                 C   sb   t | jdkrRt|}|tjkr6dd | jD | _n|tjkrRdd | jD | _| j| dS )a  Append rdataset to the node with special handling for CNAME and
        other data conditions.

        Specifically, if the rdataset being appended has ``NodeKind.CNAME``,
        then all rdatasets other than KEY, NSEC, NSEC3, and their covering
        RRSIGs are deleted.  If the rdataset being appended has
        ``NodeKind.REGULAR`` then CNAME and RRSIG(CNAME) are deleted.
        r   c                 S   s    g | ]}t |t jkr|qS r
   )r   r   r   .0r2   r
   r
   r   
<listcomp>   s   z)Node._append_rdataset.<locals>.<listcomp>c                 S   s    g | ]}t |t jkr|qS r
   )r   r   r   r>   r
   r
   r   r@      s   N)r-   r"   r   r   r   r   appendr%   r   kindr
   r
   r   _append_rdataset   s    




zNode._append_rdatasetFrdclassr   r	   creater   c                 C   sH   | j D ]}||||r|  S q|s*ttj|||}| | |S )a*  Find an rdataset matching the specified properties in the
        current node.

        *rdclass*, a ``dns.rdataclass.RdataClass``, the class of the rdataset.

        *rdtype*, a ``dns.rdatatype.RdataType``, the type of the rdataset.

        *covers*, a ``dns.rdatatype.RdataType``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.

        *create*, a ``bool``.  If True, create the rdataset if it is not found.

        Raises ``KeyError`` if an rdataset of the desired type and class does
        not exist and *create* is not ``True``.

        Returns a ``dns.rdataset.Rdataset``.
        )r"   matchKeyErrorr   r   r    rD   r%   rF   r   r	   rG   r2   r
   r
   r   find_rdataset   s     


zNode.find_rdatasetc                 C   s2   z|  ||||}W n tk
r,   d}Y nX |S )a  Get an rdataset matching the specified properties in the
        current node.

        None is returned if an rdataset of the specified type and
        class does not exist and *create* is not ``True``.

        *rdclass*, an ``int``, the class of the rdataset.

        *rdtype*, an ``int``, the type of the rdataset.

        *covers*, an ``int``, the covered type.  Usually this value is
        dns.rdatatype.NONE, but if the rdtype is dns.rdatatype.SIG or
        dns.rdatatype.RRSIG, then the covers value will be the rdata
        type the SIG/RRSIG covers.  The library treats the SIG and RRSIG
        types as if they were a family of
        types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).  This makes RRSIGs much
        easier to work with than if RRSIGs covering different rdata
        types were aggregated into a single RRSIG rdataset.

        *create*, a ``bool``.  If True, create the rdataset if it is not found.

        Returns a ``dns.rdataset.Rdataset`` or ``None``.
        N)rK   rI   rJ   r
   r
   r   get_rdataset   s
    
zNode.get_rdatasetNrF   r   r	   r   c                 C   s&   |  |||}|dk	r"| j| dS )aD  Delete the rdataset matching the specified properties in the
        current node.

        If a matching rdataset does not exist, it is not an error.

        *rdclass*, an ``int``, the class of the rdataset.

        *rdtype*, an ``int``, the type of the rdataset.

        *covers*, an ``int``, the covered type.
        N)rL   r"   remove)r%   rF   r   r	   r2   r
   r
   r   delete_rdataset   s    zNode.delete_rdatasetreplacementr   c                 C   sN   t |tjjstdt |tjjr,| }| |j	|j
|j | | dS )a  Replace an rdataset.

        It is not an error if there is no rdataset matching *replacement*.

        Ownership of the *replacement* object is transferred to the node;
        in other words, this method does not store a copy of *replacement*
        at the node, it stores *replacement* itself.

        *replacement*, a ``dns.rdataset.Rdataset``.

        Raises ``ValueError`` if *replacement* is not a
        ``dns.rdataset.Rdataset``.
        zreplacement is not an rdatasetN)
isinstancer   r   r    
ValueErrorZrrsetZRRsetZto_rdatasetrO   rF   r   r	   rD   r%   rQ   r
   r
   r   replace_rdataset
  s      zNode.replace_rdatasetr   c                 C   s4   | j D ]&}t|j|j}|tjkr|  S qtjS )aT  Classify a node.

        A node which contains a CNAME or RRSIG(CNAME) is a
        ``NodeKind.CNAME`` node.

        A node which contains only "neutral" types, i.e. types allowed to
        co-exist with a CNAME, is a ``NodeKind.NEUTRAL`` node.  The neutral
        types are NSEC, NSEC3, KEY, and their associated RRSIGS.  An empty node
        is also considered neutral.

        A node which contains some rdataset which is not a CNAME, RRSIG(CNAME),
        or a neutral type is a a ``NodeKind.REGULAR`` node.  Regular nodes are
        also commonly referred to as "other data".
        )r"   r   r   r   r	   r   rB   r
   r
   r   r   $  s
    


zNode.classifyc                 C   s   dS )NFr
   r$   r
   r
   r   is_immutable9  s    zNode.is_immutable)#r   r   r   r   	__slots__r&   r   r'   Namer   r4   r   r/   r6   r9   r:   r;   r=   rD   r   NONE
rdataclass
RdataClassr   boolr   r    rK   r   rL   rO   rU   r   r   rW   r
   r
   r
   r   r!   H   sL    -
)r!   c                       s   e Zd Z fddZejjdfejjejj	ejj	e
ejjd fddZejjdfejjejj	ejj	e
eejj d fddZejjfejjejj	ejj	d	d
ddZejjd	dddZe
dddZ  ZS )ImmutableNodec                    s$   t    tdd |jD | _d S )Nc                 S   s   g | ]}t j|qS r
   )r   r   ZImmutableRdatasetr>   r
   r
   r   r@   B  s     z*ImmutableNode.__init__.<locals>.<listcomp>)superr&   tupler"   )r%   node	__class__r
   r   r&   ?  s    
zImmutableNode.__init__FrE   c                    s   |rt dt |||dS N	immutableF)	TypeErrorr_   rK   r%   rF   r   r	   rG   rb   r
   r   rK   E  s    zImmutableNode.find_rdatasetc                    s   |rt dt |||dS rd   )rf   r_   rL   rg   rb   r
   r   rL   P  s    zImmutableNode.get_rdatasetNrM   c                 C   s   t dd S Nre   rf   )r%   rF   r   r	   r
   r
   r   rO   [  s    zImmutableNode.delete_rdatasetrP   c                 C   s   t dd S rh   ri   rT   r
   r
   r   rU   c  s    zImmutableNode.replace_rdatasetrV   c                 C   s   dS )NTr
   r$   r
   r
   r   rW   f  s    zImmutableNode.is_immutable)r   r   r   r&   r   r   rZ   r[   r\   r   r]   r   r    rK   r   rL   rO   rU   rW   __classcell__r
   r
   rb   r   r^   =  s8   

r^   )r   enumr+   typingr   r   r   Zdns.immutabler   Zdns.nameZdns.rdataclassZdns.rdatasetZdns.rdatatypeZdns.rendererZ	dns.rrsetr   r   r   ZNSECZNSEC3ZKEYr   r   uniqueEnumr   r!   re   r^   r
   r
   r
   r   <module>   s.    v