Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
ttl.py93 linesDownload Raw Back to dns
1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license2 3# Copyright (C) 2003-2017 Nominum, Inc.4#5# Permission to use, copy, modify, and distribute this software and its6# documentation for any purpose with or without fee is hereby granted,7# provided that the above copyright notice and this permission notice8# appear in all copies.9#10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17 18"""DNS TTL conversion."""19 20from typing import Union21 22import dns.exception23 24# Technically TTLs are supposed to be between 0 and 2**31 - 1, with values25# greater than that interpreted as 0, but we do not impose this policy here26# as values > 2**31 - 1 occur in real world data.27#28# We leave it to applications to impose tighter bounds if desired.29MAX_TTL = 2**32 - 130 31 32class BadTTL(dns.exception.SyntaxError):33    """DNS TTL value is not well-formed."""34 35 36def from_text(text: str) -> int:37    """Convert the text form of a TTL to an integer.38 39    The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported.40 41    *text*, a ``str``, the textual TTL.42 43    Raises ``dns.ttl.BadTTL`` if the TTL is not well-formed.44 45    Returns an ``int``.46    """47 48    if text.isdigit():49        total = int(text)50    elif len(text) == 0:51        raise BadTTL52    else:53        total = 054        current = 055        need_digit = True56        for c in text:57            if c.isdigit():58                current *= 1059                current += int(c)60                need_digit = False61            else:62                if need_digit:63                    raise BadTTL64                c = c.lower()65                if c == "w":66                    total += current * 60480067                elif c == "d":68                    total += current * 8640069                elif c == "h":70                    total += current * 360071                elif c == "m":72                    total += current * 6073                elif c == "s":74                    total += current75                else:76                    raise BadTTL("unknown unit '%s'" % c)77                current = 078                need_digit = True79        if not current == 0:80            raise BadTTL("trailing integer")81    if total < 0 or total > MAX_TTL:82        raise BadTTL("TTL should be between 0 and 2**32 - 1 (inclusive)")83    return total84 85 86def make(value: Union[int, str]) -> int:87    if isinstance(value, int):88        return value89    elif isinstance(value, str):90        return dns.ttl.from_text(value)91    else:92        raise ValueError("cannot convert value to TTL")93 
codekingpro/portable-devtools · Team Ai