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

Source Code for Module dns.ttl

 1  # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. 
 2  # 
 3  # Permission to use, copy, modify, and distribute this software and its 
 4  # documentation for any purpose with or without fee is hereby granted, 
 5  # provided that the above copyright notice and this permission notice 
 6  # appear in all copies. 
 7  # 
 8  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
 9  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
10  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
11  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
12  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
13  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
14  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
15   
16  """DNS TTL conversion.""" 
17   
18  import dns.exception 
19  from ._compat import long 
20   
21   
22 -class BadTTL(dns.exception.SyntaxError):
23 24 """DNS TTL value is not well-formed."""
25 26
27 -def from_text(text):
28 """Convert the text form of a TTL to an integer. 29 30 The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported. 31 32 @param text: the textual TTL 33 @type text: string 34 @raises dns.ttl.BadTTL: the TTL is not well-formed 35 @rtype: int 36 """ 37 38 if text.isdigit(): 39 total = long(text) 40 else: 41 if not text[0].isdigit(): 42 raise BadTTL 43 total = long(0) 44 current = long(0) 45 for c in text: 46 if c.isdigit(): 47 current *= 10 48 current += long(c) 49 else: 50 c = c.lower() 51 if c == 'w': 52 total += current * long(604800) 53 elif c == 'd': 54 total += current * long(86400) 55 elif c == 'h': 56 total += current * long(3600) 57 elif c == 'm': 58 total += current * long(60) 59 elif c == 's': 60 total += current 61 else: 62 raise BadTTL("unknown unit '%s'" % c) 63 current = 0 64 if not current == 0: 65 raise BadTTL("trailing integer") 66 if total < long(0) or total > long(2147483647): 67 raise BadTTL("TTL should be between 0 and 2^31 - 1 (inclusive)") 68 return total
69