1 # Copyright (C) 2003-2007, 2009, 2010 Nominum, Inc.
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.
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.
16 """DNS RRsets (an RRset is a named rdataset)"""
23 class RRset(dns.rdataset.Rdataset):
24 """A DNS RRset (named rdataset).
26 RRset inherits from Rdataset, and RRsets can be treated as
27 Rdatasets in most cases. There are, however, a few notable
28 exceptions. RRsets have different to_wire() and to_text() method
29 arguments, reflecting the fact that RRsets always have an owner
33 __slots__ = ['name', 'deleting']
35 def __init__(self, name, rdclass, rdtype, covers=dns.rdatatype.NONE,
37 """Create a new RRset."""
39 super(RRset, self).__init__(rdclass, rdtype)
41 self.deleting = deleting
44 obj = super(RRset, self)._clone()
46 obj.deleting = self.deleting
53 ctext = '(' + dns.rdatatype.to_text(self.covers) + ')'
54 if not self.deleting is None:
55 dtext = ' delete=' + dns.rdataclass.to_text(self.deleting)
58 return '<DNS ' + str(self.name) + ' ' + \
59 dns.rdataclass.to_text(self.rdclass) + ' ' + \
60 dns.rdatatype.to_text(self.rdtype) + ctext + dtext + ' RRset>'
65 def __eq__(self, other):
66 """Two RRsets are equal if they have the same name and the same
70 if not isinstance(other, RRset):
72 if self.name != other.name:
74 return super(RRset, self).__eq__(other)
76 def match(self, name, rdclass, rdtype, covers, deleting=None):
77 """Returns True if this rrset matches the specified class, type,
78 covers, and deletion state."""
80 if not super(RRset, self).match(rdclass, rdtype, covers):
82 if self.name != name or self.deleting != deleting:
86 def to_text(self, origin=None, relativize=True, **kw):
87 """Convert the RRset into DNS master file format.
89 @see: L{dns.name.Name.choose_relativity} for more information
90 on how I{origin} and I{relativize} determine the way names
93 Any additional keyword arguments are passed on to the rdata
96 @param origin: The origin for relative names, or None.
97 @type origin: dns.name.Name object
98 @param relativize: True if names should names be relativized
99 @type relativize: bool"""
101 return super(RRset, self).to_text(self.name, origin, relativize,
104 def to_wire(self, file, compress=None, origin=None, **kw):
105 """Convert the RRset to wire format."""
107 return super(RRset, self).to_wire(self.name, file, compress, origin,
110 def to_rdataset(self):
111 """Convert an RRset into an Rdataset.
113 @rtype: dns.rdataset.Rdataset object
115 return dns.rdataset.from_rdata_list(self.ttl, list(self))
118 def from_text_list(name, ttl, rdclass, rdtype, text_rdatas):
119 """Create an RRset with the specified name, TTL, class, and type, and with
120 the specified list of rdatas in text format.
122 @rtype: dns.rrset.RRset object
125 if isinstance(name, (str, unicode)):
126 name = dns.name.from_text(name, None)
127 if isinstance(rdclass, str):
128 rdclass = dns.rdataclass.from_text(rdclass)
129 if isinstance(rdtype, str):
130 rdtype = dns.rdatatype.from_text(rdtype)
131 r = RRset(name, rdclass, rdtype)
133 for t in text_rdatas:
134 rd = dns.rdata.from_text(r.rdclass, r.rdtype, t)
138 def from_text(name, ttl, rdclass, rdtype, *text_rdatas):
139 """Create an RRset with the specified name, TTL, class, and type and with
140 the specified rdatas in text format.
142 @rtype: dns.rrset.RRset object
145 return from_text_list(name, ttl, rdclass, rdtype, text_rdatas)
147 def from_rdata_list(name, ttl, rdatas):
148 """Create an RRset with the specified name and TTL, and with
149 the specified list of rdata objects.
151 @rtype: dns.rrset.RRset object
154 if isinstance(name, (str, unicode)):
155 name = dns.name.from_text(name, None)
158 raise ValueError("rdata list must not be empty")
162 r = RRset(name, rd.rdclass, rd.rdtype)
168 def from_rdata(name, ttl, *rdatas):
169 """Create an RRset with the specified name and TTL, and with
170 the specified rdata objects.
172 @rtype: dns.rrset.RRset object
175 return from_rdata_list(name, ttl, rdatas)