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

Source Code for Module dns.rdtypes.ANY.URI

 1  # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. 
 2  # Copyright (C) 2015 Red Hat, Inc. 
 3  # 
 4  # Permission to use, copy, modify, and distribute this software and its 
 5  # documentation for any purpose with or without fee is hereby granted, 
 6  # provided that the above copyright notice and this permission notice 
 7  # appear in all copies. 
 8  # 
 9  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
10  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
11  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
12  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
13  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
14  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
15  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
16   
17  import struct 
18   
19  import dns.exception 
20  import dns.rdata 
21  import dns.name 
22   
23 -class URI(dns.rdata.Rdata):
24 """URI record 25 26 @ivar priority: the priority 27 @type priority: int 28 @ivar weight: the weight 29 @type weight: int 30 @ivar target: the target host 31 @type target: dns.name.Name object 32 @see: draft-faltstrom-uri-13""" 33 34 __slots__ = ['priority', 'weight', 'target'] 35
36 - def __init__(self, rdclass, rdtype, priority, weight, target):
37 super(URI, self).__init__(rdclass, rdtype) 38 self.priority = priority 39 self.weight = weight 40 if len(target) < 1: 41 raise dns.exception.SyntaxError("URI target cannot be empty") 42 self.target = target
43
44 - def to_text(self, origin=None, relativize=True, **kw):
45 return '%d %d "%s"' % (self.priority, self.weight, self.target)
46
47 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
48 priority = tok.get_uint16() 49 weight = tok.get_uint16() 50 target = tok.get().unescape() 51 if not (target.is_quoted_string() or target.is_identifier()): 52 raise dns.exception.SyntaxError("URI target must be a string") 53 tok.get_eol() 54 return cls(rdclass, rdtype, priority, weight, target.value)
55 56 from_text = classmethod(from_text) 57
58 - def to_wire(self, file, compress = None, origin = None):
59 two_ints = struct.pack("!HH", self.priority, self.weight) 60 file.write(two_ints) 61 file.write(self.target)
62
63 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
64 if rdlen < 5: 65 raise dns.exception.FormError('URI RR is shorter than 5 octets') 66 67 (priority, weight) = struct.unpack('!HH', wire[current : current + 4]) 68 current += 4 69 rdlen -= 4 70 target = wire[current : current + rdlen] 71 current += rdlen 72 73 return cls(rdclass, rdtype, priority, weight, target)
74 75 from_wire = classmethod(from_wire)
76