Package dns :: Module rrset |
[hide private]
[frames] | no frames]

# Source Code for Module dns.rrset

      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
     19  import dns.name
     20  import dns.rdataset
     21  import dns.rdataclass
     22  import dns.renderer
     23  from ._compat import string_types
     24
     25
     26 -class RRset(dns.rdataset.Rdataset):
     27
     28      """A DNS RRset (named rdataset).
     29
     30      RRset inherits from Rdataset, and RRsets can be treated as
     31      Rdatasets in most cases.  There are, however, a few notable
     32      exceptions.  RRsets have different to_wire() and to_text() method
     33      arguments, reflecting the fact that RRsets always have an owner
     34      name.
     35      """
     36
     37      __slots__ = ['name', 'deleting']
     38
     39 -    def __init__(self, name, rdclass, rdtype, covers=dns.rdatatype.NONE,
     40                   deleting=None):
     41          """Create a new RRset."""
     42
     43          super(RRset, self).__init__(rdclass, rdtype, covers)
     44          self.name = name
     45          self.deleting = deleting
     46
     47 -    def _clone(self):
     48          obj = super(RRset, self)._clone()
     49          obj.name = self.name
     50          obj.deleting = self.deleting
     51          return obj
     52
     53 -    def __repr__(self):
     54          if self.covers == 0:
     55              ctext = ''
     56          else:
     57              ctext = '(' + dns.rdatatype.to_text(self.covers) + ')'
     58          if self.deleting is not None:
     59              dtext = ' delete=' + dns.rdataclass.to_text(self.deleting)
     60          else:
     61              dtext = ''
     62          return '<DNS ' + str(self.name) + ' ' + \
     63                 dns.rdataclass.to_text(self.rdclass) + ' ' + \
     64                 dns.rdatatype.to_text(self.rdtype) + ctext + dtext + ' RRset>'
     65
     66 -    def __str__(self):
     67          return self.to_text()
     68
     69 -    def __eq__(self, other):
     70          """Two RRsets are equal if they have the same name and the same
     71          rdataset
     72
     73          @rtype: bool"""
     74          if not isinstance(other, RRset):
     75              return False
     76          if self.name != other.name:
     77              return False
     78          return super(RRset, self).__eq__(other)
     79
     80 -    def match(self, name, rdclass, rdtype, covers, deleting=None):
     81          """Returns True if this rrset matches the specified class, type,
     82          covers, and deletion state."""
     83
     84          if not super(RRset, self).match(rdclass, rdtype, covers):
     85              return False
     86          if self.name != name or self.deleting != deleting:
     87              return False
     88          return True
     89
     90 -    def to_text(self, origin=None, relativize=True, **kw):
     91          """Convert the RRset into DNS master file format.
     92
     93          @see: L{dns.name.Name.choose_relativity} for more information
     94          on how I{origin} and I{relativize} determine the way names
     95          are emitted.
     96
     97          Any additional keyword arguments are passed on to the rdata
     98          to_text() method.
     99
    100          @param origin: The origin for relative names, or None.
    101          @type origin: dns.name.Name object
    102          @param relativize: True if names should names be relativized
    103          @type relativize: bool"""
    104
    105          return super(RRset, self).to_text(self.name, origin, relativize,
    106                                            self.deleting, **kw)
    107
    108 -    def to_wire(self, file, compress=None, origin=None, **kw):
    109          """Convert the RRset to wire format."""
    110
    111          return super(RRset, self).to_wire(self.name, file, compress, origin,
    112                                            self.deleting, **kw)
    113
    114 -    def to_rdataset(self):
    115          """Convert an RRset into an Rdataset.
    116
    117          @rtype: dns.rdataset.Rdataset object
    118          """
    119          return dns.rdataset.from_rdata_list(self.ttl, list(self))
    120
    121
    122 -def from_text_list(name, ttl, rdclass, rdtype, text_rdatas,
    123                     idna_codec=None):
    124      """Create an RRset with the specified name, TTL, class, and type, and with
    125      the specified list of rdatas in text format.
    126
    127      @rtype: dns.rrset.RRset object
    128      """
    129
    130      if isinstance(name, string_types):
    131          name = dns.name.from_text(name, None, idna_codec=idna_codec)
    132      if isinstance(rdclass, string_types):
    133          rdclass = dns.rdataclass.from_text(rdclass)
    134      if isinstance(rdtype, string_types):
    135          rdtype = dns.rdatatype.from_text(rdtype)
    136      r = RRset(name, rdclass, rdtype)
    137      r.update_ttl(ttl)
    138      for t in text_rdatas:
    139          rd = dns.rdata.from_text(r.rdclass, r.rdtype, t)
    140          r.add(rd)
    141      return r
    142
    143
    144 -def from_text(name, ttl, rdclass, rdtype, *text_rdatas):
    145      """Create an RRset with the specified name, TTL, class, and type and with
    146      the specified rdatas in text format.
    147
    148      @rtype: dns.rrset.RRset object
    149      """
    150
    151      return from_text_list(name, ttl, rdclass, rdtype, text_rdatas)
    152
    153
    154 -def from_rdata_list(name, ttl, rdatas, idna_codec=None):
    155      """Create an RRset with the specified name and TTL, and with
    156      the specified list of rdata objects.
    157
    158      @rtype: dns.rrset.RRset object
    159      """
    160
    161      if isinstance(name, string_types):
    162          name = dns.name.from_text(name, None, idna_codec=idna_codec)
    163
    164      if len(rdatas) == 0:
    165          raise ValueError("rdata list must not be empty")
    166      r = None
    167      for rd in rdatas:
    168          if r is None:
    169              r = RRset(name, rd.rdclass, rd.rdtype)
    170              r.update_ttl(ttl)
    171          r.add(rd)
    172      return r
    173
    174
    175 -def from_rdata(name, ttl, *rdatas):
    176      """Create an RRset with the specified name and TTL, and with
    177      the specified rdata objects.
    178
    179      @rtype: dns.rrset.RRset object
    180      """
    181
    182      return from_rdata_list(name, ttl, rdatas)
    183