f6051fea4fc1a6134807c42c1ab796ac7d5f8677
[samba.git] / lib / dnspython / dns / rrset.py
1 # Copyright (C) 2003-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 """DNS RRsets (an RRset is a named rdataset)"""
17
18 import dns.name
19 import dns.rdataset
20 import dns.rdataclass
21 import dns.renderer
22
23 class RRset(dns.rdataset.Rdataset):
24     """A DNS RRset (named rdataset).
25
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
30     name.
31     """
32
33     __slots__ = ['name', 'deleting']
34
35     def __init__(self, name, rdclass, rdtype, covers=dns.rdatatype.NONE,
36                  deleting=None):
37         """Create a new RRset."""
38
39         super(RRset, self).__init__(rdclass, rdtype, covers)
40         self.name = name
41         self.deleting = deleting
42
43     def _clone(self):
44         obj = super(RRset, self)._clone()
45         obj.name = self.name
46         obj.deleting = self.deleting
47         return obj
48
49     def __repr__(self):
50         if self.covers == 0:
51             ctext = ''
52         else:
53             ctext = '(' + dns.rdatatype.to_text(self.covers) + ')'
54         if not self.deleting is None:
55             dtext = ' delete=' + dns.rdataclass.to_text(self.deleting)
56         else:
57             dtext = ''
58         return '<DNS ' + str(self.name) + ' ' + \
59                dns.rdataclass.to_text(self.rdclass) + ' ' + \
60                dns.rdatatype.to_text(self.rdtype) + ctext + dtext + ' RRset>'
61
62     def __str__(self):
63         return self.to_text()
64
65     def __eq__(self, other):
66         """Two RRsets are equal if they have the same name and the same
67         rdataset
68
69         @rtype: bool"""
70         if not isinstance(other, RRset):
71             return False
72         if self.name != other.name:
73             return False
74         return super(RRset, self).__eq__(other)
75
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."""
79
80         if not super(RRset, self).match(rdclass, rdtype, covers):
81             return False
82         if self.name != name or self.deleting != deleting:
83             return False
84         return True
85
86     def to_text(self, origin=None, relativize=True, **kw):
87         """Convert the RRset into DNS master file format.
88
89         @see: L{dns.name.Name.choose_relativity} for more information
90         on how I{origin} and I{relativize} determine the way names
91         are emitted.
92
93         Any additional keyword arguments are passed on to the rdata
94         to_text() method.
95
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"""
100
101         return super(RRset, self).to_text(self.name, origin, relativize,
102                                           self.deleting, **kw)
103
104     def to_wire(self, file, compress=None, origin=None, **kw):
105         """Convert the RRset to wire format."""
106
107         return super(RRset, self).to_wire(self.name, file, compress, origin,
108                                           self.deleting, **kw)
109
110     def to_rdataset(self):
111         """Convert an RRset into an Rdataset.
112
113         @rtype: dns.rdataset.Rdataset object
114         """
115         return dns.rdataset.from_rdata_list(self.ttl, list(self))
116
117
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.
121
122     @rtype: dns.rrset.RRset object
123     """
124
125     if isinstance(name, (str, unicode)):
126         name = dns.name.from_text(name, None)
127     if isinstance(rdclass, (str, unicode)):
128         rdclass = dns.rdataclass.from_text(rdclass)
129     if isinstance(rdtype, (str, unicode)):
130         rdtype = dns.rdatatype.from_text(rdtype)
131     r = RRset(name, rdclass, rdtype)
132     r.update_ttl(ttl)
133     for t in text_rdatas:
134         rd = dns.rdata.from_text(r.rdclass, r.rdtype, t)
135         r.add(rd)
136     return r
137
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.
141
142     @rtype: dns.rrset.RRset object
143     """
144
145     return from_text_list(name, ttl, rdclass, rdtype, text_rdatas)
146
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.
150
151     @rtype: dns.rrset.RRset object
152     """
153
154     if isinstance(name, (str, unicode)):
155         name = dns.name.from_text(name, None)
156
157     if len(rdatas) == 0:
158         raise ValueError("rdata list must not be empty")
159     r = None
160     for rd in rdatas:
161         if r is None:
162             r = RRset(name, rd.rdclass, rd.rdtype)
163             r.update_ttl(ttl)
164             first_time = False
165         r.add(rd)
166     return r
167
168 def from_rdata(name, ttl, *rdatas):
169     """Create an RRset with the specified name and TTL, and with
170     the specified rdata objects.
171
172     @rtype: dns.rrset.RRset object
173     """
174
175     return from_rdata_list(name, ttl, rdatas)