U
    d                     @   s\   d Z ddlmZ ddlZdZG dd dejjZe	e
ddd	Zee
e	f e
d
ddZdS )zDNS TTL conversion.    )UnionNl    c                   @   s   e Zd ZdZdS )BadTTLz!DNS TTL value is not well-formed.N)__name__
__module____qualname____doc__ r   r   +/tmp/pip-unpacked-wheel-pk5slln3/dns/ttl.pyr       s   r   )textreturnc                 C   s  |   rt| }nt| dkr$tnd}d}d}| D ]}|  rZ|d9 }|t|7 }d}q4|rbt| }|dkr||d 7 }n`|dkr||d 7 }nJ|d	kr||d
 7 }n4|dkr||d 7 }n|dkr||7 }ntd| d}d}q4|dkstd|dk s|tkrtd|S )zConvert the text form of a TTL to an integer.

    The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported.

    *text*, a ``str``, the textual TTL.

    Raises ``dns.ttl.BadTTL`` if the TTL is not well-formed.

    Returns an ``int``.
    r   T
   Fwi:	 diQ hi  m<   szunknown unit '%s'ztrailing integerz1TTL should be between 0 and 2**32 - 1 (inclusive))isdigitintlenr   lowerMAX_TTL)r
   totalcurrentZ
need_digitcr   r   r	   	from_text$   sB    

r   )valuer   c                 C   s0   t | tr| S t | tr$tj| S tdd S )Nzcannot convert value to TTL)
isinstancer   strdnsZttlr   
ValueError)r   r   r   r	   makeV   s
    

r!   )r   typingr   Zdns.exceptionr   r   	exceptionSyntaxErrorr   r   r   r   r!   r   r   r   r	   <module>   s   2