U
    <{fm                     @   s  d Z ddlZddlZddlmZmZmZ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ddlZddlZddlZG dd dejjZG dd dejjZdd	 Ze	ejjeejj  eejj  ee e!e"e!e"f Z#d
d Z$G dd dZ%G dd dej&j'Z(G dd dej&j)Z*ddej+j,ej+j,dddejj-df	eee
ejj e.f  ee! ee
ej+j/e.f  e
ej+j/e.f ee
ej0j1e.f  ee
e!e.f  eejj2 ee
ejj e.f  e"eej3j4 dddZ5dS )z
DNS Zones.    N)AnyIterableListOptionalSetTupleUnionc                   @   s   e Zd ZdZdS )UnknownOriginzUnknown originN__name__
__module____qualname____doc__ r   r   K/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/dns/zonefile.pyr	   &   s   r	   c                   @   s   e Zd ZdZdS )CNAMEAndOtherDataz!A node has a CNAME and other dataNr
   r   r   r   r   r   *   s   r   c                 C   sz   t jj|}| |}|d kr$d S | }|t jjjkrR|t jjjkrRtdn$|t jjjkrv|t jjjkrvtdd S )Nz1rdataset type is not compatible with a CNAME nodez9CNAME rdataset is not compatible with a regular data node)	dnsnodeZNodeKindZclassify_rdatasetZget_nodeZclassifyZCNAMEZREGULARr   )txnnamerdatasetZrdataset_kindr   Z	node_kindr   r   r   _check_cname_and_other_data.   s"    

r   c                 C   s   |   } | dsd|  } | S )N$)upper
startswith)sr   r   r   _upper_dollarizeQ   s    
r   c                   @   s   e Zd ZdZdejjejjej	j
eeeee f eejj ee eejj eejj ee d
ddZdd	 Zd
d Zdd Zeeeeeeef dddZdd ZddddZdS )Readerz(Read a DNS zone file into a transaction.FTN)
tokrdclassr   allow_includeallow_directives
force_name	force_ttlforce_rdclassforce_rdtypedefault_ttlc                 C   s   || _ |j \| _| _}| j| _d| _d| _|d k	r:|}
|
d krPd| _d| _	n|
| _d| _	| j| _
|| _|| _g | _d | _|  |dkrdddh| _|r| jd n&|dkrt | _ntdd	 |D | _|| _|| _|| _|	| _| jt d S )
Nr   FT	$GENERATE$ORIGIN$TTL$INCLUDEc                 s   s   | ]}t |V  qd S N)r   ).0dr   r   r   	<genexpr>   s     z"Reader.__init__.<locals>.<genexpr>)r   managerorigin_informationzone_origin
relativizecurrent_originlast_ttllast_ttl_knownr&   default_ttl_known	last_namezone_rdclassr   saved_statecurrent_fileallowed_directivesaddsetr"   r#   r$   r%   Zcheck_put_rdatasetr   )selfr   r   r   r    r!   r"   r#   r$   r%   r&   _r   r   r   __init__[   s<    
zReader.__init__c                 C   s   | j  }| r qq d S r+   )r   getis_eol_or_eofr>   tokenr   r   r   	_eat_line   s    
zReader._eat_linec                 C   s   | j  }| stjj|S r+   )r   rA   is_identifierr   	exceptionSyntaxErrorrC   r   r   r   _get_identifier   s    
zReader._get_identifierc           	   
   C   sV  d}| j dk	r| j }n| jdkr$t| jjdd}| sN| j|| j| _n"| j }| rddS | j	| | j}|
| js|   dS | jr|| j}| jdk	r| j}|| _d| _nV|  }d}z"tj|j}|| _d| _d}W n& tjjk
r   | j	| Y nX | jdk	r(| j}nv|  }ztj|j}W nB tjjk
r^    Y n( tk
r   | j}| j	| Y nX || jkrtjd|dkr|  }d}z"tj|j}|| _d| _d}W nD tjjk
r   | jr| j}n| jr
| j}| j	| Y nX | jdk	r0| j}nD|  }ztj |j}W n( tk
rr   tjd|j Y nX z"tj!||| j| j| j| j}W n` tjjk
r    Y nF tk
r   t"# dd \}}tjd$t%|t%|Y nX | js,|tj j&kr,|j'| _d| _|dkr,|j'}|dkrBtjd| j()||| dS )	z&Process one line from a DNS zone file.NT)Zwant_leadingRR class is not zone's classunknown rdatatype '%s'   zcaught exception {}: {}Missing default TTL value)*r"   r3   r	   r   rA   Zis_whitespaceZas_namer7   rB   ungetis_subdomainr1   rE   r2   r#   r4   r5   rI   r   ttl	from_textvalueBadTTLr$   
rdataclassrG   rH   	Exceptionr8   r6   r&   r%   	rdatatyperdatasysexc_infoformatstrZSOAminimumr   r<   )	r>   rD   r   rP   r   rdtyperdtyvar   r   r   _rr_line   s    







zReader._rr_line)sidereturnc                 C   s  t d}t d}t d}||}|rJ| \}}}}	}
|dkrJd}||}|rz| \}}}|dkrrd}d}	d}
||}|r| \}}}}	|dkrd}d}
|s|s|sd}d}d}d}	d}
t|}t|	}	|dkrtjd	| |
d
kr
tjd|
 ||||	|
fS )Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$ +r   r-   )re   -zinvalid offset sign %s)r-   oxXnNzinvalid type %s)recompilematchgroupsintr   rG   rH   )r>   rb   Zis_generate1Zis_generate2Zis_generate3g1modsignoffsetwidthbaseg2Zg3r   r   r   _parse_modify  sD    






zReader._parse_modifyc              
      s  | j dkrt| j }z2tj|j\}}}| j }| sHtj	j
W n tk
rf   tj	j
Y nX z$|j}| j }| stj	j
W n tk
r   tj	j
Y nX z8tj|j}|| _d| _| j }| stj	j
W nT tjjk
r6   | js| jstj	
d| jr$| j}n| jr2| j}Y nX z.tj|j}| j }| sdtj	j
W n< tj	j
k
r   tj	j
Y n tk
r   | j}Y nX || jkrtj	
dz.tj|j}| j }| stj	j
W n( tk
r   tj	
d|j Y nX |j}	ttttddd}
ttttd	 fd
d | |\}}}}}| |	\}}}}}t||d |D ]6}|
|||}|
|||} |||} |||}|d| |}|	d| |}tj|| j | jj| _| j}|| js|    dS | j r"| | j}z tj!|||| j | j | j}W n` tj	j
k
r^    Y nF tk
r   t"# dd \}}tj	
dt|t|f Y nX | j$%||| q~dS )zPProcess one line containing the GENERATE statement from a DNS
        zone file.NTrM   rJ   rK   )counteroffset_signrt   rc   c                 S   s   |dkr|d9 }| | S )z0Calculate the index from the counter and offset.rf   r   )ry   rz   rt   r   r   r   _calculate_index  s    z/Reader._generate_line.<locals>._calculate_index)indexrv   ru   rc   c                    sT   |dkrt | ||S  | d|}d|ddd d| }|dkrP| }|S )zVFormat the index with the given base, and zero-fill it
            to the given width.)r-   rg   rh   ri   rh   .Nr{   rk   )rZ   zfilljoinr   )r}   rv   ru   ZhexaZnibbles_format_indexr   r   r     s    z,Reader._generate_line.<locals>._format_index   z$%srL   zcaught exception %s: %s)&r3   r	   r   rA   r   ZgrangerQ   rR   rF   rG   rH   rU   rP   r4   r5   rS   r6   r&   rT   r8   rV   rp   r[   rx   rangereplacer   
idna_codecr7   rO   r1   rE   r2   rW   rX   rY   r   r<   )r>   rD   startstopsteplhsrP   r   r]   rhsr|   ZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseiZlindexrindexZlzfindexZrzfindexr   rW   r^   r_   r`   r   r   r   _generate_line@  s    








  
zReader._generate_line)rc   c           	   
   C   s  zr| j dd}| rr| jdk	r.| j  t| jdkrj| jd\| _ | _| _	| _| _
| _| _| _qqrn| rqn| r| j   qn|jd dkr\t| jdkr\|j }|| jkrtjd| d|dkr,| j  }| s
tjd	tj|j| _d| _| j   q|d
krp| j  | _| j   | jdkr`| j| _| j| j q|dkr2| j  }|j}| j  }| rtj|j| j| j j}| j   n|  stjdn| j}| j!| j | j| j	| j| j
| j| j| jf t"|d| _tj#$| j|| _ || _q|dkrF| %  qtjd| dq| j &| | '  qW nr tjjk
r } zN| j ( \}}|dkrd}tjd|||f }t)* d }|+|dW 5 d}~X Y nX dS )zRead a DNS zone file and build a zone object.

        @raises dns.zone.NoSOA: No SOA RR was found at the zone origin
        @raises dns.zone.NoNS: No NS RRset was found at the zone origin
        TNr   r{   r   zzone file directive 'z' is not allowedr)   zbad $TTLr(   r*   zbad origin in $INCLUDErr'   zUnknown zone file directive ''zsyntax errorz	%s:%d: %srL   ),r   rA   Zis_eofr:   closelenr9   popr3   r7   r4   r5   r&   r6   Zis_eolZ
is_commentZget_eolrR   r;   r   r   rG   rH   rF   rP   rQ   get_namer1   r   _set_originr   r   rB   appendopen	tokenizer	Tokenizerr   rN   ra   whererX   rY   with_traceback)	r>   rD   cfilenameZ
new_origindetailline_numberextbr   r   r   read  s    




 











  




zReader.read)FTNNNNN)r   r   r   r   r   r   r   rT   
RdataClasstransactionTransactionboolr   r   r[   r   r   Namerp   rV   	RdataTyper@   rE   rI   ra   r   rx   r   r   r   r   r   r   r   X   s6          


2z- r   c                       st   e Z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dd Zdd Zdd Z  ZS )RRsetsReaderTransactionc                    s"   |rt t ||| i | _d S r+   )AssertionErrorsuperr@   	rdatasets)r>   r/   replacementZ	read_only	__class__r   r   r@   +  s    z RRsetsReaderTransaction.__init__c                 C   s   | j |||fS r+   )r   rA   r>   r   r]   coversr   r   r   _get_rdataset0  s    z%RRsetsReaderTransaction._get_rdatasetc                 C   sT   g }| j  D ] \\}}}}||kr|| qt|dkr@d S tj }||_ |S Nr   )r   itemsr   r   r   r   Node)r>   r   r   Zrdataset_namer?   r   r   r   r   r   	_get_node3  s    
z!RRsetsReaderTransaction._get_nodec                 C   s   || j ||j|jf< d S r+   )r   r]   r   )r>   r   r   r   r   r   _put_rdataset>  s    z%RRsetsReaderTransaction._put_rdatasetc                 C   sH   g }| j D ]}|d |kr
|| q
t|dkrD|D ]}| j |= q6d S r   )r   r   r   )r>   r   removekeyr   r   r   _delete_nameA  s    
z$RRsetsReaderTransaction._delete_namec                 C   s,   z| j |||f= W n tk
r&   Y nX d S r+   )r   KeyErrorr   r   r   r   _delete_rdatasetK  s    z(RRsetsReaderTransaction._delete_rdatasetc                 C   s$   | j D ]\}}}||kr dS qdS NTF)r   )r>   r   rj   r?   r   r   r   _name_existsQ  s    z$RRsetsReaderTransaction._name_existsc                 C   s   t | jdkS r   )r   r   r>   r   r   r   _changedW  s    z RRsetsReaderTransaction._changedc                 C   sf   |rb|   rbg }| j D ]:\\}}}}tj||j|j|j}|	| |
| q| j| d S r+   )r   r   r   r   rrsetRRsetr   r]   r   updater   r/   
set_rrsets)r>   commitrrsetsr   r?   r   r   r   r   r   _end_transactionZ  s       
z(RRsetsReaderTransaction._end_transactionc                 C   s   d S r+   r   )r>   originr   r   r   r   e  s    z#RRsetsReaderTransaction._set_originc                 C   s   t d S r+   NotImplementedErrorr   r   r   r   _iterate_rdatasetsh  s    z*RRsetsReaderTransaction._iterate_rdatasetsc                 C   s   t d S r+   r   r   r   r   r   _iterate_namesk  s    z&RRsetsReaderTransaction._iterate_names)r   r   r   r@   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   *  s   
r   c                   @   sN   e Zd ZejjdejjfddZdd Z	dddZ
dd	 Zd
d Zdd ZdS )RRSetsReaderManagerFc                 C   s   || _ || _|| _g | _d S r+   )r   r2   r   r   )r>   r   r2   r   r   r   r   r@   p  s    zRRSetsReaderManager.__init__c                 C   s   t d S r+   r   r   r   r   r   readerx  s    zRRSetsReaderManager.readerc                 C   s   |dkst t| ddS r   )r   r   )r>   r   r   r   r   writer{  s    zRRSetsReaderManager.writerc                 C   s   | j S r+   )r   r   r   r   r   	get_class  s    zRRSetsReaderManager.get_classc                 C   s$   | j rtjj}n| j}| j| j |fS r+   )r2   r   r   emptyr   )r>   Z	effectiver   r   r   r0     s    
z&RRSetsReaderManager.origin_informationc                 C   s
   || _ d S r+   )r   )r>   r   r   r   r   r     s    zRRSetsReaderManager.set_rrsetsN)F)r   r   r   r   r   rootrT   INr@   r   r   r   r0   r   r   r   r   r   r   o  s     

r   F)textr   rP   r   default_rdclassr]   r&   r   r   r2   rc   c
                 C   s  t |trtj|tjj|}t |tr8tj|||}t |trNtj|}t |trdtj|}|dk	r|tjj	|}nd}tjj	|}|dk	rtj
j	|}nd}t||	|}
|
d:}tjj| d|d}t|||d|||||d	}|  W 5 Q R X |
jS )a	  Read one or more rrsets from the specified text, possibly subject
    to restrictions.

    *text*, a file object or a string, is the input to process.

    *name*, a string, ``dns.name.Name``, or ``None``, is the owner name of
    the rrset.  If not ``None``, then the owner name is "forced", and the
    input must not specify an owner name.  If ``None``, then any owner names
    are allowed and must be present in the input.

    *ttl*, an ``int``, string, or None.  If not ``None``, the the TTL is
    forced to be the specified value and the input must not specify a TTL.
    If ``None``, then a TTL may be specified in the input.  If it is not
    specified, then the *default_ttl* will be used.

    *rdclass*, a ``dns.rdataclass.RdataClass``, string, or ``None``.  If
    not ``None``, then the class is forced to the specified value, and the
    input must not specify a class.  If ``None``, then the input may specify
    a class that matches *default_rdclass*.  Note that it is not possible to
    return rrsets with differing classes; specifying ``None`` for the class
    simply allows the user to optionally type a class as that may be convenient
    when cutting and pasting.

    *default_rdclass*, a ``dns.rdataclass.RdataClass`` or string.  The class
    of the returned rrsets.

    *rdtype*, a ``dns.rdatatype.RdataType``, string, or ``None``.  If not
    ``None``, then the type is forced to the specified value, and the
    input must not specify a type.  If ``None``, then a type must be present
    for each RR.

    *default_ttl*, an ``int``, string, or ``None``.  If not ``None``, then if
    the TTL is not forced and is not specified, then this value will be used.
    if ``None``, then if the TTL is not forced an error will occur if the TTL
    is not specified.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder.  If ``None``, the default IDNA 2003 encoder/decoder
    is used.  Note that codecs only apply to the owner name; dnspython does
    not do IDNA for names in rdata, as there is no IDNA zonefile format.

    *origin*, a string, ``dns.name.Name``, or ``None``, is the origin for any
    relative names in the input, and also the origin to relativize to if
    *relativize* is ``True``.

    *relativize*, a bool.  If ``True``, names are relativized to the *origin*;
    if ``False`` then any relative names in the input are made absolute by
    appending the *origin*.
    NTz<input>)r   F)r!   r"   r#   r$   r%   r&   )
isinstancer[   r   r   rQ   r   rP   rT   r   makerV   r   r   r   r   r   r   r   r   )r   r   rP   r   r   r]   r&   r   r   r2   r/   r   r   r   r   r   r   read_rrsets  s>    =



r   )6r   rl   rX   typingr   r   r   r   r   r   r   Zdns.exceptionr   Z
dns.grangeZdns.nameZdns.nodeZ	dns.rdataZdns.rdataclassZdns.rdatatypeZdns.rdtypes.ANY.SOAZ	dns.rrsetZdns.tokenizerZdns.transactionZdns.ttlrG   ZDNSExceptionr	   r   r   r   r   r   r   rp   r   ZSavedStateTyper   r   r   r   r   ZTransactionManagerr   rT   r   r   r[   r   rV   r   Z	IDNACodecr   r   r   r   r   r   r   <module>   st   $

   UE 

