o
    i3                     @   s.  d dl Z d dlmZ d dlmZ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	d dlZ	d dlZ	G dd de jZG dd de	jjZe	jjG dd	 d	eZG d
d de	jje	jj ZG dd de	jjZeddG dd dZe	jjG dd de	jj Z!G dd de	j"j#Z#dS )    N)	dataclass)CallableMutableMappingTuplecastc                   @   s   e Zd ZdZdZdZdS )	NodeFlags         N)__name__
__module____qualname__ORIGIN
DELEGATIONGLUE r   r   D/home/app/Keep/.python/lib/python3.10/site-packages/dns/btreezone.pyr      s    r   c                       sP   e Zd ZddgZddedB f fddZdd Zdd	 Zd
d Zdd Z	  Z
S )NodeflagsidNc                    s*   t    |d u rtd}|| _d| _d S Nr   )super__init__r   r   r   )selfr   	__class__r   r   r   &   s
   

zNode.__init__c                 C      | j tj@ dkS r   )r   r   r   r   r   r   r   is_delegation0      zNode.is_delegationc                 C   r   r   )r   r   r   r   r   r   r   is_glue3   r   zNode.is_gluec                 C   r   r   )r   r   r   r   r   r   r   	is_origin6   r   zNode.is_originc                 C   s   | j tjtjB @ dkS r   )r   r   r   r   r   r   r   r   is_origin_or_glue9   s   zNode.is_origin_or_glueN)r   r   r   	__slots__r   r   r   r    r!   r"   __classcell__r   r   r   r   r   #   s    
r   c                       s   e Zd Zde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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 )ImmutableNodenodec                    s4   t    |j| _tdd |jD | _|j| _d S )Nc                 S   s   g | ]}t j|qS r   )dnsrdatasetZImmutableRdataset).0Zrdsr   r   r   
<listcomp>C   s    z*ImmutableNode.__init__.<locals>.<listcomp>)r   r   r   tuple	rdatasetsr   )r   r'   r   r   r   r   ?   s   
zImmutableNode.__init__Frdclassrdtypecoverscreatereturnc                       |rt dt |||dS N	immutableF)	TypeErrorr   find_rdatasetr   r.   r/   r0   r1   r   r   r   r7   G      zImmutableNode.find_rdatasetNc                    r3   r4   )r6   r   get_rdatasetr8   r   r   r   r:   R   r9   zImmutableNode.get_rdatasetc                 C      t dNr5   r6   )r   r.   r/   r0   r   r   r   delete_rdataset]   s   zImmutableNode.delete_rdatasetreplacementc                 C   r;   r<   r=   )r   r?   r   r   r   replace_rdatasete   s   zImmutableNode.replace_rdatasetc                 C   s   dS NTr   r   r   r   r   is_immutableh   s   zImmutableNode.is_immutable)r   r   r   r   r   r(   	rdatatypeNONEZ
rdataclassZ
RdataClass	RdataTypeboolr)   Rdatasetr7   r:   r>   r@   rB   r%   r   r   r   r   r&   =   sP    

r&   c                   @   sH   e Zd ZdejjdeejjdB ef fddZdejjdefddZ	dS )Delegationsnamer2   Nc                 C   sn   |   }|j|dd | }|du rdS | }||\}}}|tjjjk}|s1|tjjj	kr5||fS dS )aF  Get the delegation applicable to *name*, if it exists.

        If there delegation, then return a tuple consisting of the name of
        the delegation point, and a boolean which is `True` if the name is a proper
        subdomain of the delegation point, and `False` if it is equal to the delegation
        point.
        FbeforeNNF)
cursorseekprevkeyfullcomparer(   rI   NameRelationZ	SUBDOMAINEQUAL)r   rI   rM   rO   cutZreln_is_subdomainr   r   r   get_delegationm   s   zDelegations.get_delegationc                 C   s4   |   }|j|dd | |\}}|du rdS |S )z0Is *name* glue, i.e. is it beneath a delegation?FrJ   N)rM   rN   rW   )r   rI   rM   rT   rV   r   r   r   r       s   zDelegations.is_glue)
r   r   r   r(   rI   Namer   rF   rW   r    r   r   r   r   rH   l   s    &rH   c                       s   e Zd Zddejjdef fddZdejj	defdd	Z
dejj	deejjejj	f f fd
dZdejj	deddfddZdejj	ddfddZdejj	dejjddfddZdejj	dejjdejjddfddZ  ZS )WritableVersionFzoner?   c                    s^   t  |d |s)t|tjjsJ |jd }tjj|j	d| _	t
|jd| _d S t
 | _d S )NT)original)r   r   
isinstancer(   	versionedZoneZ	_versionsbtree	BTreeDictnodesrH   delegations)r   rZ   r?   versionr   r   r   r      s   
zWritableVersion.__init__rI   r2   c                 C   s    | j jr
|tjjkS || j jkS r#   )rZ   Z
relativizer(   rI   emptyorigin)r   rI   r   r   r   
_is_origin   s   zWritableVersion._is_originc                    s`   t  |\}}tt|}| |r| jtjO  _||fS | j	|r,| jtj
O  _||fS r#   )r   _maybe_cow_with_namer   r   rg   r   r   r   rc   r    r   r   rI   r'   r   r   r   rh      s   

z$WritableVersion._maybe_cow_with_namer    Nc           	      C   s   | j  }||d g }	 | }|d u rnS| }||s!nIttjj	|
 }|| jvrG| j }| j|_|j|j | j| |}t|t	sNJ |rY| jtjO  _n	| jtj M  _|||f q|D ]	\}}|| j |< qld S rL   )rb   rM   rN   nextrP   rV   r   r(   r'   r   valuechangedrZ   node_factoryr   r-   extendaddr]   r   r   r   append)	r   rI   r    rM   ZupdateseltZenamer'   new_noder   r   r   update_glue_flag   s4   



z WritableVersion.update_glue_flagc                 C   sZ   |  |}| j|}|d ur+| r| j| | |d | j|= | j| d S d S rL   )	_validate_namerb   getr   rc   discardrs   rl   ro   ri   r   r   r   delete_node   s   
zWritableVersion.delete_noder)   c                 C   sd   |  |\}}|jtjjkr+| s+| jtjO  _|| j	vr+| j	
| | |d || d S rA   )rh   r/   r(   rC   NSr"   r   r   r   rc   ro   rs   r@   )r   rI   r)   r'   r   r   r   put_rdataset   s   
zWritableVersion.put_rdatasetr/   r0   c                 C   s|   |  |\}}|tjjkr'|| jv r'| jtj M  _| j| | 	|d |
| jj|| t|dkr<| j|= d S d S )NFr   )rh   r(   rC   rx   rc   r   r   r   rv   rs   r>   rZ   r.   lenrb   )r   rI   r/   r0   r'   r   r   r   r>      s   zWritableVersion.delete_rdataset)F)r   r   r   r(   rZ   r_   rF   r   rI   rX   rg   r   r'   r   rh   rs   rw   r)   rG   ry   rC   rE   r>   r%   r   r   r   r   rY      s4    

rY   T)frozenc                   @   sZ   e Zd ZU ejjed< ejjed< ejjdB ed< ejjed< eed< eed< dd	 ZdS )
BoundsrI   leftNrightclosest_encloseris_equalr   c              
   C   sL   | j rd}nd}| jrd}nd}| j d| d| j d| j | d| j 
S )N=<z zonecut  z < z; )r   r   r}   rI   r~   r   )r   opZzonecutr   r   r   __str__   s   "zBounds.__str__)	r   r   r   r(   rI   rX   __annotations__rF   r   r   r   r   r   r|      s   
 r|   c                       s@   e Zd Zdejjf fddZdejje	B de
fddZ  ZS )ImmutableVersionrd   c                    s   t |ts	tdt |jd |j| _|j| _|jD ]}|j	
|}|r-t||j	|< qtttjjtjjf |j	| _	| j	  |j| _| j  d S )NzIa dns.btreezone.ImmutableVersion requires a dns.btreezone.WritableVersionT)r]   rY   
ValueErrorr   r   rZ   r   rf   rl   rb   ru   r&   r   r   r(   rI   rX   r'   r   Zmake_immutablerc   )r   rd   rI   r'   r   r   r   r     s    


zImmutableVersion.__init__rI   r2   c                 C   s8  | j dusJ | j| j }| j|}| j|\}}|dur%|}d}n|}d}ttjj| j	
 }||d | }|dusCJ |  	 | }	|	du sV|	  sWnqH| |}
|	durm|	 }||}ntjjdt|f}d}tj|t|
d |d  d }t|| |||
d tjjjk|S )aT  Return the 'bounds' of *name* in its zone.

        The bounds information is useful when making an authoritative response, as
        it can be used to determine whether the query name is at or beneath a delegation
        point.  The other data in the ``Bounds`` object is useful for making on-the-fly
        DNSSEC signatures.

        The left bound of *name* is *name* itself if it is in the zone, or the greatest
        predecessor which is in the zone.

        The right bound of *name* is the least successor of *name*, or ``None`` if
        no name in the zone is greater than *name*.

        The closest encloser of *name* is *name* itself, if *name* is in the zone;
        otherwise it is the name with the largest number of labels in common with
        *name* that is in the zone, either explicitly or by the implied existence
        of empty non-terminals.

        The bounds *is_equal* field is ``True`` if and only if *name* is equal to
        its left bound.

        The bounds *is_delegation* field is ``True`` if and only if the left bound is a
        delegation point.
        NTFr[   r	   r   )rf   rZ   rt   rc   rW   r   r(   r`   ra   rb   rM   rN   rO   rj   rk   r    rP   rQ   rI   ZNAMERELN_COMMONANCESTORrz   rX   maxr|   rR   rS   )r   rI   rf   rT   rU   targetr   cr}   r~   Zleft_comparisonZ	right_keyZright_comparisonr   r   r   r   bounds  sN   zImmutableVersion.bounds)r   r   r   r(   rZ   Versionr   rI   rX   strr|   r   r%   r   r   r   r   r   
  s    "r   c                   @   s   e Zd ZU eZeg ejjf ed< e	eg e
ejjejjf f ejjejjef Zeg e
ejjejjf f ed< eZeejjegejjf dB ed< eZeejjgejjf dB ed< dS )r_   rm   map_factoryNwritable_version_factoryimmutable_version_factory)r   r   r   r   rm   r   r(   r'   r   r   r   rI   rX   r`   ra   r   rY   r   rZ   r_   rF   r   r   r   r   r   r   r   r_   d  s   
 $$r_   )$enumdataclassesr   typingr   r   r   r   Z	dns.btreer(   Zdns.immutableZdns.nameZdns.nodeZdns.rdataclassZdns.rdatasetZdns.rdatatypeZdns.versionedZdns.zoneIntFlagr   r'   r   r5   r&   r`   ZBTreeSetrI   rX   rH   rZ   rY   r|   r   r   r^   r_   r   r   r   r   <module>   s.   . fY