o
    h                     @   s,   d Z ddlmZ ddlZG dd deZdS )zDNS name dictionary    )MutableMappingNc                       sh   e Zd ZdZg dZ fddZdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Z  ZS )NameDictzA dictionary whose keys are dns.name.Name objects.

    In addition to being like a regular Python dictionary, this
    dictionary can also get the deepest match for a given key.
    )	max_depthmax_depth_items__storec                    s6   t    t | _d| _d| _| t|i | d S )Nr   )super__init__dict_NameDict__storer   r   update)selfargskwargs	__class__ ?/var/www/html/venv/lib/python3.10/site-packages/dns/namedict.pyr   -   s
   
zNameDict.__init__c                 C   sD   t || jkr| jd | _d S t || jkr t || _d| _d S d S )N   )lenr   r   r   keyr   r   r   __update_max_depth6   s   

zNameDict.__update_max_depthc                 C   s
   | j | S Nr
   r   r   r   r   __getitem__=      
zNameDict.__getitem__c                 C   s.   t |tjjstd|| j|< | | d S )NzNameDict key must be a name)
isinstancednsnameName
ValueErrorr
   _NameDict__update_max_depth)r   r   valuer   r   r   __setitem__@   s   
zNameDict.__setitem__c                 C   sT   | j | t|| jkr| jd | _| jdkr&d| _| j D ]	}| | qd S d S )Nr   r   )r
   popr   r   r   r!   )r   r   kr   r   r   __delitem__F   s   

zNameDict.__delitem__c                 C   
   t | jS r   )iterr
   r   r   r   r   __iter__O   r   zNameDict.__iter__c                 C   r'   r   )r   r
   r)   r   r   r   __len__R   r   zNameDict.__len__c                 C   s
   || j v S r   r   r   r   r   r   has_keyU   r   zNameDict.has_keyc                 C   sn   t |}|| jkr| j}t| dD ]}tj||d }|| v r*|| | f  S q| tjj }tjj|fS )a  Find the deepest match to *name* in the dictionary.

        The deepest match is the longest name in the dictionary which is
        a superdomain of *name*.  Note that *superdomain* includes matching
        *name* itself.

        *name*, a ``dns.name.Name``, the name to find.

        Returns a ``(key, value)`` where *key* is the deepest
        ``dns.name.Name``, and *value* is the value associated with *key*.
        r   N)r   r   ranger   r   r   empty)r   r   depthinvr   r   r   get_deepest_matchX   s   
zNameDict.get_deepest_match)__name__
__module____qualname____doc__	__slots__r   r!   r   r#   r&   r*   r+   r,   r3   __classcell__r   r   r   r   r   $   s    		r   )r7   collections.abcr   dns.namer   r   r   r   r   r   <module>   s   