o
    hw1                     @   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   || v p|t jjko|| v S N)dns	rdatatypeZRRSIG)Zrdtypesrdtypecovers r
   ;/var/www/html/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   classify8   s
   zNodeKind.classifyrdatasetc                 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                   @   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ejj 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__Z   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   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 vr dS q|j D ]
}|| j v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   
   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   .0r2   r
   r
   r   
<listcomp>   
    z)Node._append_rdataset.<locals>.<listcomp>c                 S   r@   r
   )r   r   r   rA   r
   r
   r   rC      rD   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    rH   r%   rI   r   r	   rJ   r2   r
   r
   r   find_rdataset   s   
 
zNode.find_rdatasetc                 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)rN   rL   rM   r
   r
   r   get_rdataset   s   zNode.get_rdatasetNc                 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)rO   r"   remove)r%   rI   r   r	   r2   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    
ValueErrorZrrsetZRRsetZto_rdatasetrQ   rI   r   r	   rH   r%   rR   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   rF   r
   r
   r   r   $  s   

zNode.classifyc                 C      dS )NFr
   r$   r
   r
   r   is_immutable9     zNode.is_immutable)#r   r   r   r   	__slots__r&   r   r'   Namer   r4   r   r/   r6   r9   r:   r<   r?   rH   r   NONE
rdataclass
RdataClassr   boolr   r    rN   r   rO   rQ   rV   r   r   rX   r
   r
   r
   r   r!   H   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ejj 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   ZImmutableRdatasetrA   r
   r
   r   rC   B  s    z*ImmutableNode.__init__.<locals>.<listcomp>)superr&   tupler"   )r%   node	__class__r
   r   r&   ?  s   

zImmutableNode.__init__FrI   r   r	   rJ   r   c                       |rt dt |||dS N	immutableF)	TypeErrorra   rN   r%   rI   r   r	   rJ   rd   r
   r   rN   E     zImmutableNode.find_rdatasetc                    rf   rg   )ri   ra   rO   rj   rd   r
   r   rO   P  rk   zImmutableNode.get_rdatasetNc                 C      t dNrh   ri   )r%   rI   r   r	   r
   r
   r   rQ   [  s   zImmutableNode.delete_rdatasetrR   c                 C   rl   rm   rn   rU   r
   r
   r   rV   c  s   zImmutableNode.replace_rdatasetc                 C   rW   )NTr
   r$   r
   r
   r   rX   f  rY   zImmutableNode.is_immutable)r   r   r   r&   r   r   r\   r]   r^   r   r_   r   r    rN   r   rO   rQ   rV   rX   __classcell__r
   r
   rd   r   r`   =  sP    


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!   rh   r`   r
   r
   r
   r   <module>   s0    v