Package dns :: Package rdtypes :: Module txtbase
[hide private]
[frames] | no frames]

Source Code for Module dns.rdtypes.txtbase

 1  # Copyright (C) 2006, 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  """TXT-like base class.""" 
17   
18  import dns.exception 
19  import dns.rdata 
20  import dns.tokenizer 
21  import dns.util 
22 23 -class TXTBase(dns.rdata.Rdata):
24 """Base class for rdata that is like a TXT record 25 26 @ivar strings: the text strings 27 @type strings: list of string 28 @see: RFC 1035""" 29 30 __slots__ = ['strings'] 31
32 - def __init__(self, rdclass, rdtype, strings):
33 super(TXTBase, self).__init__(rdclass, rdtype) 34 if isinstance(strings, str): 35 strings = [ strings ] 36 self.strings = strings[:]
37
38 - def to_text(self, origin=None, relativize=True, **kw):
39 txt = '' 40 prefix = '' 41 for s in self.strings: 42 txt += '%s"%s"' % (prefix, dns.rdata._escapify(s)) 43 prefix = ' ' 44 return txt
45 46 @classmethod
47 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
48 strings = [] 49 while 1: 50 token = tok.get().unescape() 51 if token.is_eol_or_eof(): 52 break 53 if not (token.is_quoted_string() or token.is_identifier()): 54 raise dns.exception.SyntaxError("expected a string") 55 if len(token.value) > 255: 56 raise dns.exception.SyntaxError("string too long") 57 strings.append(token.value) 58 if len(strings) == 0: 59 raise dns.exception.UnexpectedEnd 60 return cls(rdclass, rdtype, strings)
61
62 - def to_wire(self, file, compress = None, origin = None):
63 for s in self.strings: 64 l = len(s) 65 assert l < 256 66 dns.util.write_uint8(file, l) 67 file.write(s.encode('latin_1'))
68 69 @classmethod
70 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
71 strings = [] 72 while rdlen > 0: 73 l = wire[current] 74 current += 1 75 rdlen -= 1 76 if l > rdlen: 77 raise dns.exception.FormError 78 s = wire[current : current + l].decode('latin_1') 79 current += l 80 rdlen -= l 81 strings.append(s) 82 return cls(rdclass, rdtype, strings)
83
84 - def _cmp(self, other):
85 return dns.util.cmp(self.strings, other.strings)
86