3
[_ð]g(  ã               @   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	m
Z
 G dd„ dejjƒZdS )zDNS Dynamic Update Supporté    Né   )Ústring_typesc                   sx   e Zd Zejjddejjf‡ fdd„	Zddd„Z	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zddd„Zd‡ fdd„	Z‡  ZS )ÚUpdateNc                sš   t t| ƒjƒ  |  jtjjtjjƒO  _t|t	ƒr>tj
j|ƒ}|| _t|t	ƒrZtjj|ƒ}|| _| j| j| j|tjjddd� |dk	r–| j|||d� dS )a¢  Initialize a new DNS Update object.

        See the documentation of the Message class for a complete
        description of the keyring dictionary.

        *zone*, a ``dns.name.Name`` or ``text``, the zone which is being
        updated.

        *rdclass*, an ``int`` or ``text``, the class of the zone.

        *keyring*, a ``dict``, the TSIG keyring to use.  If a
        *keyring* is specified but a *keyname* is not, then the key
        used will be the first key in the *keyring*.  Note that the
        order of keys in a dictionary is not defined, so applications
        should supply a keyname when a keyring is used, unless they
        know the keyring contains only one key.

        *keyname*, a ``dns.name.Name`` or ``None``, the name of the TSIG key
        to use; defaults to ``None``. The key must be defined in the keyring.

        *keyalgorithm*, a ``dns.name.Name``, the TSIG algorithm to use.
        T)ÚcreateZforce_uniqueN)Ú	algorithm)Úsuperr   Ú__init__ÚflagsÚdnsÚopcodeZto_flagsZUPDATEÚ
isinstancer   ÚnameÚ	from_textÚoriginÚ
rdataclassÚzone_rdclassÚ
find_rrsetZquestionÚ	rdatatypeZSOAZuse_tsig)ÚselfÚzoneZrdclassZkeyringZkeynameZkeyalgorithm)Ú	__class__© ú//tmp/pip-build-yz_zf6az/dnspython/dns/update.pyr   !   s    


zUpdate.__init__c          	   C   sB   |dkr| j }|jƒ }| j||| j|j||ddƒ}|j||ƒ dS )z&Add a single RR to the update section.NT)Ú	authorityÚcoversr   r   ÚrdtypeÚadd)r   r   ÚttlÚrdZdeletingÚsectionr   Zrrsetr   r   r   Ú_add_rrF   s    zUpdate._add_rrc       
      G   s:  t |tƒrtjj|dƒ}t |d tjjƒrnx@|D ]8}|rF| j||jƒ x |D ]}| j	||j
||d� qLW q0W nÈt|ƒ}t|jdƒƒ}t |d tjjƒrÎ|r¬| j||d jƒ xˆ|D ]}| j	||||d� q²W nh|jdƒ}t |tƒrîtjj|ƒ}|rþ| j||ƒ x6|D ].}	tjj| j||	| jƒ}| j	||||d� �qW dS )a°  Add records.

        *replace* is the replacement mode.  If ``False``,
        RRs are added to an existing RRset; if ``True``, the RRset
        is replaced with the specified contents.  The second
        argument is the section to add to.  The third argument
        is always a name.  The other arguments can be:

                - rdataset...

                - ttl, rdata...

                - ttl, rdtype, string...
        Nr   )r   )r   r   r
   r   r   ÚrdatasetÚRdatasetÚdeleter   r    r   ÚlistÚintÚpopÚrdataÚRdatar   r   r   )
r   Úreplacer   r   ÚargsÚrdsr   r   r   Úsr   r   r   Ú_addP   s0    






zUpdate._addc             G   s   | j d| j|f|žŽ  dS )zÒAdd records.

        The first argument is always a name.  The other
        arguments can be:

                - rdataset...

                - ttl, rdata...

                - ttl, rdtype, string...
        FN)r-   r   )r   r   r*   r   r   r   r   {   s    z
Update.addc          	   G   sf  t |tƒrtjj|dƒ}t|ƒdkrR| j| j|tjj	tj
j	tj
jtj
j	ddƒ �nt |d tjjƒr–xü|D ]&}x |D ]}| j|d|tjjƒ qtW qjW nÌt|ƒ}t |d tjjƒrÔx°|D ]}| j|d|tjjƒ q¶W nŽ|jdƒ}t |tƒrôtj
j|ƒ}t|ƒdk�r(| j| j|| j|tj
jtjj	ddƒ n:x8|D ]0}tjj| j||| jƒ}| j|d|tjjƒ �q.W dS )zèDelete records.

        The first argument is always a name.  The other
        arguments can be:

                - *empty*

                - rdataset...

                - rdata...

                - rdtype, [string...]
        Nr   T)r   r   r
   r   r   Úlenr   r   r   ÚANYr   ÚNONEr!   r"   r    r$   r'   r(   r&   r   r   )r   r   r*   r+   r   r   r,   r   r   r   r#   Š   s6    








zUpdate.deletec             G   s   | j d| j|f|žŽ  dS )ab  Replace records.

        The first argument is always a name.  The other
        arguments can be:

                - rdataset...

                - ttl, rdata...

                - ttl, rdtype, string...

        Note that if you want to replace the entire node, you should do
        a delete of the name followed by one or more calls to add.
        TN)r-   r   )r   r   r*   r   r   r   r)   ¸   s    zUpdate.replacec          	   G   sþ   t |tƒrtjj|dƒ}t|ƒdkrL| j| j|tjj	tj
j	tj
jdddƒ n®t |d tjjƒs|t |d tjjƒs|t|ƒdkrºt |d tjjƒs¢t|ƒ}|jddƒ | jd| j|f|žŽ  n@|d }t |tƒrØtj
j|ƒ}| j| j|tjj	|tj
jdddƒ dS )aS  Require that an owner name (and optionally an rdata type,
        or specific rdataset) exists as a prerequisite to the
        execution of the update.

        The first argument is always a name.
        The other arguments can be:

                - rdataset...

                - rdata...

                - rdtype, string...
        Nr   Tr   F)r   r   r
   r   r   r.   r   Úanswerr   r/   r   r0   r!   r"   r'   r(   r$   Úinsertr-   )r   r   r*   r   r   r   r   ÚpresentÊ   s*    




zUpdate.presentc          	   C   s„   t |tƒrtjj|dƒ}|dkrH| j| j|tjjtj	j
tj	jdddƒ n8t |tƒr^tj	j|ƒ}| j| j|tjj|tj	jdddƒ dS )z‚Require that an owner name (and optionally an rdata type) does
        not exist as a prerequisite to the execution of the update.NT)r   r   r
   r   r   r   r1   r   r0   r   r/   )r   r   r   r   r   r   Úabsentñ   s    




zUpdate.absentéÿÿ  c                s    |dkr| j }tt| ƒj||ƒS )aL  Return a string containing the update in DNS compressed wire
        format.

        *origin*, a ``dns.name.Name`` or ``None``, the origin to be
        appended to any relative names.  If *origin* is ``None``, then
        the origin of the ``dns.update.Update`` message object is used
        (i.e. the *zone* parameter passed when the Update object was
        created).

        *max_size*, an ``int``, the maximum size of the wire format
        output; default is 0, which means "the message's request
        payload, if nonzero, or 65535".

        Returns a ``binary``.
        N)r   r   r   Úto_wire)r   r   Úmax_size)r   r   r   r6     s    zUpdate.to_wire)NN)N)Nr5   )Ú__name__Ú
__module__Ú__qualname__r
   r   ÚINZtsigZdefault_algorithmr   r    r-   r   r#   r)   r3   r4   r6   Ú__classcell__r   r   )r   r   r      s   $

+.'
r   )Ú__doc__Zdns.messager
   Zdns.nameZ
dns.opcodeZ	dns.rdataZdns.rdataclassZdns.rdatasetZdns.tsigÚ_compatr   ÚmessageÚMessager   r   r   r   r   Ú<module>   s   