dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

rrset.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2003-2017 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18 """DNS RRsets (an RRset is a named rdataset)"""
19 
20 
21 import dns.name
22 import dns.rdataset
23 import dns.rdataclass
24 import dns.renderer
25 from ._compat import string_types
26 
27 
29 
30  """A DNS RRset (named rdataset).
31 
32  RRset inherits from Rdataset, and RRsets can be treated as
33  Rdatasets in most cases. There are, however, a few notable
34  exceptions. RRsets have different to_wire() and to_text() method
35  arguments, reflecting the fact that RRsets always have an owner
36  name.
37  """
38 
39  __slots__ = ['name', 'deleting']
40 
41  def __init__(self, name, rdclass, rdtype, covers=dns.rdatatype.NONE,
42  deleting=None):
43  """Create a new RRset."""
44 
45  super(RRset, self).__init__(rdclass, rdtype, covers)
46  self.name = name
47  self.deleting = deleting
48 
49  def _clone(self):
50  obj = super(RRset, self)._clone()
51  obj.name = self.name
52  obj.deleting = self.deleting
53  return obj
54 
55  def __repr__(self):
56  if self.covers == 0:
57  ctext = ''
58  else:
59  ctext = '(' + dns.rdatatype.to_text(self.covers) + ')'
60  if self.deleting is not None:
61  dtext = ' delete=' + dns.rdataclass.to_text(self.deleting)
62  else:
63  dtext = ''
64  return '<DNS ' + str(self.name) + ' ' + \
65  dns.rdataclass.to_text(self.rdclass) + ' ' + \
66  dns.rdatatype.to_text(self.rdtype) + ctext + dtext + ' RRset>'
67 
68  def __str__(self):
69  return self.to_text()
70 
71  def __eq__(self, other):
72  if not isinstance(other, RRset):
73  return False
74  if self.name != other.name:
75  return False
76  return super(RRset, self).__eq__(other)
77 
78  def match(self, name, rdclass, rdtype, covers, deleting=None):
79  """Returns ``True`` if this rrset matches the specified class, type,
80  covers, and deletion state.
81  """
82 
83  if not super(RRset, self).match(rdclass, rdtype, covers):
84  return False
85  if self.name != name or self.deleting != deleting:
86  return False
87  return True
88 
89  def to_text(self, origin=None, relativize=True, **kw):
90  """Convert the RRset into DNS master file format.
91 
92  See ``dns.name.Name.choose_relativity`` for more information
93  on how *origin* and *relativize* determine the way names
94  are emitted.
95 
96  Any additional keyword arguments are passed on to the rdata
97  ``to_text()`` method.
98 
99  *origin*, a ``dns.name.Name`` or ``None``, the origin for relative
100  names.
101 
102  *relativize*, a ``bool``. If ``True``, names will be relativized
103  to *origin*.
104  """
105 
106  return super(RRset, self).to_text(self.name, origin, relativize,
107  self.deleting, **kw)
108 
109  def to_wire(self, file, compress=None, origin=None, **kw):
110  """Convert the RRset to wire format.
111 
112  All keyword arguments are passed to ``dns.rdataset.to_wire()``; see
113  that function for details.
114 
115  Returns an ``int``, the number of records emitted.
116  """
117 
118  return super(RRset, self).to_wire(self.name, file, compress, origin,
119  self.deleting, **kw)
120 
121  def to_rdataset(self):
122  """Convert an RRset into an Rdataset.
123 
124  Returns a ``dns.rdataset.Rdataset``.
125  """
126  return dns.rdataset.from_rdata_list(self.ttl, list(self))
127 
128 
129 def from_text_list(name, ttl, rdclass, rdtype, text_rdatas,
130  idna_codec=None):
131  """Create an RRset with the specified name, TTL, class, and type, and with
132  the specified list of rdatas in text format.
133 
134  Returns a ``dns.rrset.RRset`` object.
135  """
136 
137  if isinstance(name, string_types):
138  name = dns.name.from_text(name, None, idna_codec=idna_codec)
139  if isinstance(rdclass, string_types):
140  rdclass = dns.rdataclass.from_text(rdclass)
141  if isinstance(rdtype, string_types):
142  rdtype = dns.rdatatype.from_text(rdtype)
143  r = RRset(name, rdclass, rdtype)
144  r.update_ttl(ttl)
145  for t in text_rdatas:
146  rd = dns.rdata.from_text(r.rdclass, r.rdtype, t)
147  r.add(rd)
148  return r
149 
150 
151 def from_text(name, ttl, rdclass, rdtype, *text_rdatas):
152  """Create an RRset with the specified name, TTL, class, and type and with
153  the specified rdatas in text format.
154 
155  Returns a ``dns.rrset.RRset`` object.
156  """
157 
158  return from_text_list(name, ttl, rdclass, rdtype, text_rdatas)
159 
160 
161 def from_rdata_list(name, ttl, rdatas, idna_codec=None):
162  """Create an RRset with the specified name and TTL, and with
163  the specified list of rdata objects.
164 
165  Returns a ``dns.rrset.RRset`` object.
166  """
167 
168  if isinstance(name, string_types):
169  name = dns.name.from_text(name, None, idna_codec=idna_codec)
170 
171  if len(rdatas) == 0:
172  raise ValueError("rdata list must not be empty")
173  r = None
174  for rd in rdatas:
175  if r is None:
176  r = RRset(name, rd.rdclass, rd.rdtype)
177  r.update_ttl(ttl)
178  r.add(rd)
179  return r
180 
181 
182 def from_rdata(name, ttl, *rdatas):
183  """Create an RRset with the specified name and TTL, and with
184  the specified rdata objects.
185 
186  Returns a ``dns.rrset.RRset`` object.
187  """
188 
189  return from_rdata_list(name, ttl, rdatas)
dns.rdataset.Rdataset
Definition: rdataset.py:44
dns.rrset.RRset.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: rrset.py:89
dns.rrset.RRset.__init__
def __init__(self, name, rdclass, rdtype, covers=dns.rdatatype.NONE, deleting=None)
Definition: rrset.py:41
dns.rdataset.Rdataset.to_text
def to_text(self, name=None, origin=None, relativize=True, override_rdclass=None, **kw)
Definition: rdataset.py:170
dns.name.from_text
def from_text(text, origin=root, idna_codec=None)
Definition: name.py:873
dns.rrset.from_text
def from_text(name, ttl, rdclass, rdtype, *text_rdatas)
Definition: rrset.py:151
dns.rdataset
Definition: rdataset.py:1
dns.rrset.RRset.__repr__
def __repr__(self)
Definition: rrset.py:55
dns.rdataclass
Definition: rdataclass.py:1
dns.rrset.RRset.__str__
def __str__(self)
Definition: rrset.py:68
dns.rdataset.Rdataset.ttl
ttl
Definition: rdataset.py:66
dns.rrset.from_rdata_list
def from_rdata_list(name, ttl, rdatas, idna_codec=None)
Definition: rrset.py:161
dns.renderer
Definition: renderer.py:1
dns.rrset.RRset.to_wire
def to_wire(self, file, compress=None, origin=None, **kw)
Definition: rrset.py:109
dns.rdata.from_text
def from_text(rdclass, rdtype, tok, origin=None, relativize=True)
Definition: rdata.py:344
dns.rrset.RRset._clone
def _clone(self)
Definition: rrset.py:49
dns.rdataset.Rdataset.rdclass
rdclass
Definition: rdataset.py:63
dns.rrset.RRset.match
def match(self, name, rdclass, rdtype, covers, deleting=None)
Definition: rrset.py:78
dns.rdataset.from_rdata_list
def from_rdata_list(ttl, rdatas)
Definition: rdataset.py:322
dns.rrset.RRset.__eq__
def __eq__(self, other)
Definition: rrset.py:71
dns.rdataclass.to_text
def to_text(value)
Definition: rdataclass.py:93
dns.rdataset.Rdataset.covers
covers
Definition: rdataset.py:65
dns.name
Definition: name.py:1
dns.rrset.from_text_list
def from_text_list(name, ttl, rdclass, rdtype, text_rdatas, idna_codec=None)
Definition: rrset.py:129
dns.rdataset.Rdataset.rdtype
rdtype
Definition: rdataset.py:64
dns.rdatatype.to_text
def to_text(value)
Definition: rdatatype.py:219
dns.rdatatype.from_text
def from_text(text)
Definition: rdatatype.py:193
dns.rrset.RRset
Definition: rrset.py:28
dns.rrset.RRset.name
name
Definition: rrset.py:45
dns.rrset.RRset.to_rdataset
def to_rdataset(self)
Definition: rrset.py:121
dns.rdataclass.from_text
def from_text(text)
Definition: rdataclass.py:67
dns.rrset.RRset.deleting
deleting
Definition: rrset.py:46
dns.rrset.from_rdata
def from_rdata(name, ttl, *rdatas)
Definition: rrset.py:182