Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dns/ttl.py: 13%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

45 statements  

1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 

2 

3# Copyright (C) 2003-2017 Nominum, Inc. 

4# 

5# Permission to use, copy, modify, and distribute this software and its 

6# documentation for any purpose with or without fee is hereby granted, 

7# provided that the above copyright notice and this permission notice 

8# appear in all copies. 

9# 

10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 

11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 

12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 

13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 

14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 

15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 

16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

17 

18"""DNS TTL conversion.""" 

19 

20import dns.exception 

21 

22# Technically TTLs are supposed to be between 0 and 2**31 - 1, with values 

23# greater than that interpreted as 0, but we do not impose this policy here 

24# as values > 2**31 - 1 occur in real world data. 

25# 

26# We leave it to applications to impose tighter bounds if desired. 

27MAX_TTL = 2**32 - 1 

28 

29 

30class BadTTL(dns.exception.SyntaxError): 

31 """DNS TTL value is not well-formed.""" 

32 

33 

34def from_text(text: str) -> int: 

35 """Convert the text form of a TTL to an integer. 

36 

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

38 

39 :param text: The textual TTL. 

40 :type text: str 

41 :raises dns.ttl.BadTTL: If the TTL is not well-formed. 

42 :rtype: int 

43 """ 

44 

45 if text.isdigit(): 

46 total = int(text) 

47 elif len(text) == 0: 

48 raise BadTTL 

49 else: 

50 total = 0 

51 current = 0 

52 need_digit = True 

53 for c in text: 

54 if c.isdigit(): 

55 current *= 10 

56 current += int(c) 

57 need_digit = False 

58 else: 

59 if need_digit: 

60 raise BadTTL 

61 c = c.lower() 

62 if c == "w": 

63 total += current * 604800 

64 elif c == "d": 

65 total += current * 86400 

66 elif c == "h": 

67 total += current * 3600 

68 elif c == "m": 

69 total += current * 60 

70 elif c == "s": 

71 total += current 

72 else: 

73 raise BadTTL(f"unknown unit '{c}'") 

74 current = 0 

75 need_digit = True 

76 if not current == 0: 

77 raise BadTTL("trailing integer") 

78 if total < 0 or total > MAX_TTL: 

79 raise BadTTL("TTL should be between 0 and 2**32 - 1 (inclusive)") 

80 return total 

81 

82 

83def make(value: int | str) -> int: 

84 if isinstance(value, int): 

85 return value 

86 elif isinstance(value, str): 

87 return from_text(value) 

88 else: 

89 raise ValueError("cannot convert value to TTL")