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

Source Code for Module dns.rdtypes.ANY.URI

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