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

Source Code for Module dns.rdtypes.txtbase

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