Package dns :: Module ttl
[hide private]
[frames] | no frames]

Source Code for Module dns.ttl

 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   
20  import dns.exception 
21  from ._compat import long 
22   
23   
24 -class BadTTL(dns.exception.SyntaxError):
25 """DNS TTL value is not well-formed."""
26 27
28 -def from_text(text):
29 """Convert the text form of a TTL to an integer. 30 31 The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported. 32 33 *text*, a ``text``, the textual TTL. 34 35 Raises ``dns.ttl.BadTTL`` if the TTL is not well-formed. 36 37 Returns an ``int``. 38 """ 39 40 if text.isdigit(): 41 total = long(text) 42 else: 43 if not text[0].isdigit(): 44 raise BadTTL 45 total = long(0) 46 current = long(0) 47 for c in text: 48 if c.isdigit(): 49 current *= 10 50 current += long(c) 51 else: 52 c = c.lower() 53 if c == 'w': 54 total += current * long(604800) 55 elif c == 'd': 56 total += current * long(86400) 57 elif c == 'h': 58 total += current * long(3600) 59 elif c == 'm': 60 total += current * long(60) 61 elif c == 's': 62 total += current 63 else: 64 raise BadTTL("unknown unit '%s'" % c) 65 current = 0 66 if not current == 0: 67 raise BadTTL("trailing integer") 68 if total < long(0) or total > long(2147483647): 69 raise BadTTL("TTL should be between 0 and 2^31 - 1 (inclusive)") 70 return total
71