o
    iS1                     @   s   d Z ddlZddlZddl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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Dictc                 C   s   || v p|t jjko|| v S N)dns	rdatatypeRRSIG)rdtypesrdtypecovers r   N/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/dns/node.py_matches_type_or_its_signature+   s   r   c                   @   sX   e Zd ZdZdZdZdZedej	j
dej	j
dd fdd	Zed
ejjdd f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 r   )r   _cname_typesr   CNAME_neutral_typesNEUTRALREGULAR)clsr	   r
   r   r   r   classify7   s
   zNodeKind.classifyrdatasetc                 C   s   |  |j|jS r   )r   r	   r
   )r   r   r   r   r   classify_rdatasetB   s   zNodeKind.classify_rdatasetN)__name__
__module____qualname____doc__r   r   r   classmethodr   r   	RdataTyper   r   Rdatasetr   r   r   r   r   r   /   s    
r   c                   @   sD  e Zd ZdZdgZdd Zdejjde	e
ef de
f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dejjdejjdejjdedejjf
ddZejjdfdejjdejjdejjdedejjdB f
ddZejjfdejjdejjdejjddfd d!Zd"ejjddfd#d$Zdefd%d&Z def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__Y   s   
zNode.__init__namekwr   c                 K   sR   t  }| jD ]}t|dkr ||j|fi | |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   r0   ]   s   

zNode.to_textc                 C   s   dt t|  d S )Nz
<DNS node >)stridr%   r   r   r   __repr__q   s   zNode.__repr__c                 C   s<   | j D ]
}||j vr dS q|j D ]
}|| j vr dS qdS )NFTr$   )r&   otherrdr   r   r   __eq__t   s   



zNode.__eq__c                 C   s   |  | S r   )r:   )r&   r8   r   r   r   __ne__   s   zNode.__ne__c                 C   
   t | jS r   )r.   r#   r%   r   r   r   __len__      
zNode.__len__c                 C   r<   r   )iterr#   r%   r   r   r   __iter__   r>   zNode.__iter__c                 C   sb   t | jdkr)t|}|tjkrdd | jD | _n|tjkr)d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       g | ]}t |t jkr|qS r   )r   r   r   .0r3   r   r   r   
<listcomp>   
    z)Node._append_rdataset.<locals>.<listcomp>c                 S   rA   r   )r   r   r   rB   r   r   r   rD      rE   N)r.   r#   r   r   r   r   appendr&   r   kindr   r   r   _append_rdataset   s   




zNode._append_rdatasetFrdclassr	   r
   createc                 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!   rI   r&   rJ   r	   r
   rK   r3   r   r   r   find_rdataset   s   
 
zNode.find_rdatasetNc                 C   s0   z|  ||||}W |S  ty   d}Y |S w )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)rO   rM   rN   r   r   r   get_rdataset   s   zNode.get_rdatasetc                 C   s*   |  |||}|dur| j| dS 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)rP   r#   remove)r&   rJ   r	   r
   r3   r   r   r   delete_rdataset   s   zNode.delete_rdatasetreplacementc                 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!   
ValueErrorrrsetRRsetto_rdatasetrR   rJ   r	   r
   rI   r&   rS   r   r   r   replace_rdataset	  s   zNode.replace_rdatasetc                 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   rG   r   r   r   r   #  s   

zNode.classifyc                 C      dS )NFr   r%   r   r   r   is_immutable8     zNode.is_immutable)"r   r   r   r   	__slots__r'   r   r(   Namer   r5   r   r0   r7   r:   r;   r=   r@   rI   r   NONE
rdataclass
RdataClassr    boolr   r!   rO   rP   rR   rZ   r   r   r\   r   r   r   r   r"   G   sd    " 
-

)
r"   c                       s   e Zd Z fddZejjdfdejjdejj	dejj	de
dejjf
 fd	d
Zejjdfdejjdejj	dejj	de
dejjdB f
 fddZejjfdejjdejj	dejj	ddfddZdejjddfddZde
f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   ImmutableRdatasetrB   r   r   r   rD   A  s    z*ImmutableNode.__init__.<locals>.<listcomp>)superr'   tupler#   )r&   node	__class__r   r   r'   >  s   

zImmutableNode.__init__FrJ   r	   r
   rK   r   c                       |rt dt |||dS N	immutableF)	TypeErrorrf   rO   r&   rJ   r	   r
   rK   ri   r   r   rO   D     zImmutableNode.find_rdatasetNc                    rk   rl   )rn   rf   rP   ro   ri   r   r   rP   O  rp   zImmutableNode.get_rdatasetc                 C      t dNrm   rn   )r&   rJ   r	   r
   r   r   r   rR   Z  s   zImmutableNode.delete_rdatasetrS   c                 C   rq   rr   rs   rY   r   r   r   rZ   b  s   zImmutableNode.replace_rdatasetc                 C   r[   )NTr   r%   r   r   r   r\   e  r]   zImmutableNode.is_immutable)r   r   r   r'   r   r   r`   ra   rb   r    rc   r   r!   rO   rP   rR   rZ   r\   __classcell__r   r   ri   r   rd   <  sP    


rd   )r   enumr,   typingr   r   dns.immutabler   dns.namedns.rdataclassdns.rdatasetdns.rdatatype	dns.rrsetr   r   r   NSECNSEC3KEYr   r   uniqueEnumr   r"   rm   rd   r   r   r   r   <module>   s.    v