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

Source Code for Module dns.rdtypes.ANY.GPOS

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