a
    sd_Þ-  ã                   @   s®   d 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ej
jZG dd„ dejjƒZG dd„ dejjƒZG dd„ dej
jƒZdd	d
„Zdd„ Zdd„ Zdd„ ZdS )zHDNS rdatasets (an rdataset is a set of rdatas of a given type and class)é    Nc                   @   s   e Zd ZdZdS )ÚDifferingCoversz~An attempt was made to add a DNS SIG/RRSIG whose covered type
    is not the same as that of the other rdatas in the rdataset.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú./usr/lib/python3/dist-packages/dns/rdataset.pyr   "   s   r   c                   @   s   e Zd ZdZdS )ÚIncompatibleTypesz?An attempt was made to add DNS RR data of an incompatible type.Nr   r   r   r   r	   r
   '   s   r
   c                       sÀ   e Zd ZdZg d¢Zejjdf‡ fdd„	Z‡ fdd„Z	dd	„ Z
d$‡ fdd„	Z‡ fdd„Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡ fdd„Zdd„ Zd%dd„Zd&d d!„Zd"d#„ Z‡  ZS )'ÚRdatasetzA DNS rdataset.)ÚrdclassÚrdtypeÚcoversÚttlr   c                    s&   t ƒ  ¡  || _|| _|| _|| _dS )zøCreate a new rdataset of the specified class and type.

        *rdclass*, an ``int``, the rdataclass.

        *rdtype*, an ``int``, the rdatatype.

        *covers*, an ``int``, the covered rdatatype.

        *ttl*, an ``int``, the TTL.
        N)ÚsuperÚ__init__r   r   r   r   )Úselfr   r   r   r   ©Ú	__class__r   r	   r   1   s
    
zRdataset.__init__c                    s.   t ƒ  ¡ }| j|_| j|_| j|_| j|_|S ©N)r   Ú_cloner   r   r   r   )r   Úobjr   r   r	   r   C   s    
zRdataset._clonec                 C   s(   t | ƒdkr|| _n|| jk r$|| _dS )zõPerform TTL minimization.

        Set the TTL of the rdataset to be the lesser of the set's current
        TTL or the specified TTL.  If the set contains no rdatas, set the TTL
        to the specified TTL.

        *ttl*, an ``int``.
        r   N)Úlenr   )r   r   r   r   r	   Ú
update_ttlK   s    

zRdataset.update_ttlNc                    s´   | j |j ks| j|jkrt‚|dur.|  |¡ | jtjjksJ| jtjjkr‚| ¡ }t	| ƒdkrt| jtjj
krt|| _n| j|kr‚t‚tj |j¡r¤t	| ƒdkr¤|  ¡  tƒ  |¡ dS )a!  Add the specified rdata to the rdataset.

        If the optional *ttl* parameter is supplied, then
        ``self.update_ttl(ttl)`` will be called prior to adding the rdata.

        *rd*, a ``dns.rdata.Rdata``, the rdata

        *ttl*, an ``int``, the TTL.

        Raises ``dns.rdataset.IncompatibleTypes`` if the type and class
        do not match the type and class of the rdataset.

        Raises ``dns.rdataset.DifferingCovers`` if the type is a signature
        type and the covered type does not match that of the rdataset.
        Nr   )r   r   r
   r   ÚdnsÚ	rdatatypeZRRSIGZSIGr   r   ÚNONEr   Zis_singletonÚclearr   Úadd)r   Úrdr   r   r   r   r	   r   Z   s    
ÿ
zRdataset.addc                    s   |   |j¡ tƒ  |¡ d S r   )r   r   r   Úunion_update©r   Úotherr   r   r	   r    €   s    zRdataset.union_updatec                    s   |   |j¡ tƒ  |¡ d S r   )r   r   r   Úintersection_updater!   r   r   r	   r#   „   s    zRdataset.intersection_updatec                    s   |   |j¡ tƒ  |¡ dS )zƒAdd all rdatas in other to self.

        *other*, a ``dns.rdataset.Rdataset``, the rdataset from which
        to update.
        N)r   r   r   Úupdater!   r   r   r	   r$   ˆ   s    zRdataset.updatec                    s$   dd„ ‰ dd  ‡ fdd„| D ƒ¡ S )Nc                 S   s    t | ƒdkr| d d… d S | S )Néd   z...)r   )Úsr   r   r	   Úmaybe_truncate“   s    z,Rdataset._rdata_repr.<locals>.maybe_truncatez[%s]z, c                 3   s   | ]}d ˆ t |ƒƒ V  qdS )z<%s>N)Ústr)Ú.0Zrr©r'   r   r	   Ú	<genexpr>—   s   ÿz'Rdataset._rdata_repr.<locals>.<genexpr>)Újoin©r   r   r*   r	   Ú_rdata_repr’   s    ÿzRdataset._rdata_reprc                 C   s^   | j dkrd}ndtj | j ¡ d }dtj | j¡ d tj | j¡ | d |  ¡  d S )	Nr   Ú ú(ú)z<DNS ú z rdataset: ú>)r   r   r   Úto_textÚ
rdataclassr   r   r.   )r   Zctextr   r   r	   Ú__repr__š   s    
ÿÿþþþzRdataset.__repr__c                 C   s   |   ¡ S r   )r4   r-   r   r   r	   Ú__str__£   s    zRdataset.__str__c                    sB   t |tƒsdS | j|jks2| j|jks2| j|jkr6dS tƒ  |¡S )NF)Ú
isinstancer   r   r   r   r   Ú__eq__r!   r   r   r	   r9   ¦   s    

ÿ
þzRdataset.__eq__c                 C   s   |   |¡ S r   )r9   r!   r   r   r	   Ú__ne__¯   s    zRdataset.__ne__Tc                 K   sÖ   |dur"|  ||¡}t|ƒ}d}nd}d}t ¡ }|dur@|}	n| j}	t| ƒdkr|| d ||tj	 
|	¡tj 
| j¡¡¡ nJ| D ]D}
| d||| jtj	 
|	¡tj 
| j¡|
j
f ||dœ|¤Žf ¡ q€| ¡ dd… S )	ad  Convert the rdataset into DNS master file format.

        See ``dns.name.Name.choose_relativity`` for more information
        on how *origin* and *relativize* determine the way names
        are emitted.

        Any additional keyword arguments are passed on to the rdata
        ``to_text()`` method.

        *name*, a ``dns.name.Name``.  If name is not ``None``, emit RRs with
        *name* as the owner name.

        *origin*, a ``dns.name.Name`` or ``None``, the origin for relative
        names.

        *relativize*, a ``bool``.  If ``True``, names will be relativized
        to *origin*.
        Nr2   r/   r   z
{}{}{} {}
z%s%s%d %s %s %s
)ÚoriginÚ
relativizeéÿÿÿÿ)Zchoose_relativityr(   ÚioÚStringIOr   r   ÚwriteÚformatr   r5   r4   r   r   r   Úgetvalue)r   Únamer;   r<   Úoverride_rdclassÚkwZntextZpadr&   r   r   r   r   r	   r4   ²   s2    
þÿþÿzRdataset.to_textc                 C   s"  |dur|}d}n| j }| dd¡ t| ƒdkr`| |||¡ t d| j|dd¡}| |¡ dS |rxt| ƒ}	t	 
|	¡ n| }	|	D ]”}
| |||¡ t d| j|| jd¡}| |¡ | ¡ }|
 |||¡ | ¡ }|| dk sàJ ‚| |d ¡ t d|| ¡}| |¡ | dd¡ q€t| ƒS dS )	at  Convert the rdataset to wire format.

        *name*, a ``dns.name.Name`` is the owner name to use.

        *file* is the file where the name is emitted (typically a
        BytesIO file).

        *compress*, a ``dict``, is the compression table to use.  If
        ``None`` (the default), names will not be compressed.

        *origin* is a ``dns.name.Name`` or ``None``.  If the name is
        relative and origin is not ``None``, then *origin* will be appended
        to it.

        *override_rdclass*, an ``int``, is used as the class instead of the
        class of the rdataset.  This is useful when rendering rdatasets
        associated with dynamic updates.

        *want_shuffle*, a ``bool``.  If ``True``, then the order of the
        Rdatas within the Rdataset will be shuffled before rendering.

        Returns an ``int``, the number of records emitted.
        NFr   é   z!HHIHé   i   z!H)r   Úseekr   Úto_wireÚstructZpackr   r@   ÚlistÚrandomZshuffler   Útell)r   rC   ÚfileÚcompressr;   rD   Zwant_shuffler   ZstuffÚlr   ÚstartÚendr   r   r	   rI   è   s:    
ÿ

zRdataset.to_wirec                 C   s&   | j |kr"| j|kr"| j|kr"dS dS )zaReturns ``True`` if this rdataset matches the specified class,
        type, and covers.
        TF)r   r   r   )r   r   r   r   r   r   r	   Úmatch"  s    
ÿþzRdataset.match)N)NNTN)NNNT)r   r   r   r   Ú	__slots__r   r   r   r   r   r   r   r    r#   r$   r.   r6   r7   r9   r:   r4   rI   rS   Ú__classcell__r   r   r   r	   r   +   s*   &
		  ÿ
6  ÿ
:r   c                 C   s`   t jj | ¡} t jj |¡}t| |ƒ}| |¡ |D ]&}t jj	|j
|j||d}| |¡ q4|S )aG  Create an rdataset with the specified class, type, and TTL, and with
    the specified list of rdatas in text format.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder to use; if ``None``, the default IDNA 2003
    encoder/decoder is used.

    Returns a ``dns.rdataset.Rdataset`` object.
    )Ú
idna_codec)r   r5   Z
RdataClassZmaker   Z	RdataTyper   r   ZrdataÚ	from_textr   r   r   )r   r   r   Útext_rdatasrV   ÚrÚtr   r   r   r	   Úfrom_text_list-  s    

r[   c                 G   s   t | |||ƒS )z£Create an rdataset with the specified class, type, and TTL, and with
    the specified rdatas in text format.

    Returns a ``dns.rdataset.Rdataset`` object.
    )r[   )r   r   r   rX   r   r   r	   rW   B  s    rW   c                 C   sP   t |ƒdkrtdƒ‚d}|D ].}|du r@t|j|jƒ}| | ¡ | |¡ q|S )z’Create an rdataset with the specified TTL, and with
    the specified list of rdata objects.

    Returns a ``dns.rdataset.Rdataset`` object.
    r   zrdata list must not be emptyN)r   Ú
ValueErrorr   r   r   r   r   )r   ÚrdatasrY   r   r   r   r	   Úfrom_rdata_listL  s    
r^   c                 G   s
   t | |ƒS )zŠCreate an rdataset with the specified TTL, and with
    the specified rdata objects.

    Returns a ``dns.rdataset.Rdataset`` object.
    )r^   )r   r]   r   r   r	   Ú
from_rdata^  s    r_   )N)r   r>   rL   rJ   Zdns.exceptionr   Zdns.rdatatypeZdns.rdataclassZ	dns.rdataZdns.setÚsetÚSetZ	SimpleSetZ	exceptionZDNSExceptionr   r
   r   r[   rW   r^   r_   r   r   r   r	   Ú<module>   s$     

