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

# Source Code for Module dns.rdtypes.ANY.GPOS

      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  import struct
     17
     18  import dns.exception
     19  import dns.rdata
     20  import dns.tokenizer
     21  from dns._compat import long, text_type
     22
     23
     24 -def _validate_float_string(what):
     25      if what[0] == b'-'[0] or what[0] == b'+'[0]:
     26          what = what[1:]
     27      if what.isdigit():
     28          return
     29      (left, right) = what.split(b'.')
     30      if left == b'' and right == b'':
     31          raise dns.exception.FormError
     32      if not left == b'' and not left.decode().isdigit():
     33          raise dns.exception.FormError
     34      if not right == b'' and not right.decode().isdigit():
     35          raise dns.exception.FormError
     36
     37
     38 -def _sanitize(value):
     39      if isinstance(value, text_type):
     40          return value.encode()
     41      return value
     42
     43
     44 -class GPOS(dns.rdata.Rdata):
     45
     46      """GPOS record
     47
     48      @ivar latitude: latitude
     49      @type latitude: string
     50      @ivar longitude: longitude
     51      @type longitude: string
     52      @ivar altitude: altitude
     53      @type altitude: string
     54      @see: RFC 1712"""
     55
     56      __slots__ = ['latitude', 'longitude', 'altitude']
     57
     58 -    def __init__(self, rdclass, rdtype, latitude, longitude, altitude):
     59          super(GPOS, self).__init__(rdclass, rdtype)
     60          if isinstance(latitude, float) or \
     61             isinstance(latitude, int) or \
     62             isinstance(latitude, long):
     63              latitude = str(latitude)
     64          if isinstance(longitude, float) or \
     65             isinstance(longitude, int) or \
     66             isinstance(longitude, long):
     67              longitude = str(longitude)
     68          if isinstance(altitude, float) or \
     69             isinstance(altitude, int) or \
     70             isinstance(altitude, long):
     71              altitude = str(altitude)
     72          latitude = _sanitize(latitude)
     73          longitude = _sanitize(longitude)
     74          altitude = _sanitize(altitude)
     75          _validate_float_string(latitude)
     76          _validate_float_string(longitude)
     77          _validate_float_string(altitude)
     78          self.latitude = latitude
     79          self.longitude = longitude
     80          self.altitude = altitude
     81
     82 -    def to_text(self, origin=None, relativize=True, **kw):
     83          return '%s %s %s' % (self.latitude.decode(),
     84                               self.longitude.decode(),
     85                               self.altitude.decode())
     86
     87      @classmethod
     88 -    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
     89          latitude = tok.get_string()
     90          longitude = tok.get_string()
     91          altitude = tok.get_string()
     92          tok.get_eol()
     93          return cls(rdclass, rdtype, latitude, longitude, altitude)
     94
     95 -    def to_wire(self, file, compress=None, origin=None):
     96          l = len(self.latitude)
     97          assert l < 256
     98          file.write(struct.pack('!B', l))
     99          file.write(self.latitude)
    100          l = len(self.longitude)
    101          assert l < 256
    102          file.write(struct.pack('!B', l))
    103          file.write(self.longitude)
    104          l = len(self.altitude)
    105          assert l < 256
    106          file.write(struct.pack('!B', l))
    107          file.write(self.altitude)
    108
    109      @classmethod
    110 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
    111          l = wire[current]
    112          current += 1
    113          rdlen -= 1
    114          if l > rdlen:
    115              raise dns.exception.FormError
    116          latitude = wire[current: current + l].unwrap()
    117          current += l
    118          rdlen -= l
    119          l = wire[current]
    120          current += 1
    121          rdlen -= 1
    122          if l > rdlen:
    123              raise dns.exception.FormError
    124          longitude = wire[current: current + l].unwrap()
    125          current += l
    126          rdlen -= l
    127          l = wire[current]
    128          current += 1
    129          rdlen -= 1
    130          if l != rdlen:
    131              raise dns.exception.FormError
    132          altitude = wire[current: current + l].unwrap()
    133          return cls(rdclass, rdtype, latitude, longitude, altitude)
    134
    135 -    def _get_float_latitude(self):
    136          return float(self.latitude)
    137
    138 -    def _set_float_latitude(self, value):
    139          self.latitude = str(value)
    140
    141      float_latitude = property(_get_float_latitude, _set_float_latitude,
    142                                doc="latitude as a floating point value")
    143
    144 -    def _get_float_longitude(self):
    145          return float(self.longitude)
    146
    147 -    def _set_float_longitude(self, value):
    148          self.longitude = str(value)
    149
    150      float_longitude = property(_get_float_longitude, _set_float_longitude,
    151                                 doc="longitude as a floating point value")
    152
    153 -    def _get_float_altitude(self):
    154          return float(self.altitude)
    155
    156 -    def _set_float_altitude(self, value):
    157          self.altitude = str(value)
    158
    159      float_altitude = property(_get_float_altitude, _set_float_altitude,
    160                                doc="altitude as a floating point value")
    161