1 # Copyright (C) 2003-2007, 2009, 2010 Nominum, Inc.
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.
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.
20 def _validate_float_string(what):
21 if what[0] == '-' or what[0] == '+':
25 (left, right) = what.split('.')
26 if left == '' and right == '':
27 raise dns.exception.FormError
28 if not left == '' and not left.isdigit():
29 raise dns.exception.FormError
30 if not right == '' and not right.isdigit():
31 raise dns.exception.FormError
33 class GPOS(dns.rdata.Rdata):
36 @ivar latitude: latitude
37 @type latitude: string
38 @ivar longitude: longitude
39 @type longitude: string
40 @ivar altitude: altitude
41 @type altitude: string
44 __slots__ = ['latitude', 'longitude', 'altitude']
46 def __init__(self, rdclass, rdtype, latitude, longitude, altitude):
47 super(GPOS, self).__init__(rdclass, rdtype)
48 if isinstance(latitude, float) or \
49 isinstance(latitude, int) or \
50 isinstance(latitude, long):
51 latitude = str(latitude)
52 if isinstance(longitude, float) or \
53 isinstance(longitude, int) or \
54 isinstance(longitude, long):
55 longitude = str(longitude)
56 if isinstance(altitude, float) or \
57 isinstance(altitude, int) or \
58 isinstance(altitude, long):
59 altitude = str(altitude)
60 _validate_float_string(latitude)
61 _validate_float_string(longitude)
62 _validate_float_string(altitude)
63 self.latitude = latitude
64 self.longitude = longitude
65 self.altitude = altitude
67 def to_text(self, origin=None, relativize=True, **kw):
68 return '%s %s %s' % (self.latitude, self.longitude, self.altitude)
70 def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
71 latitude = tok.get_string()
72 longitude = tok.get_string()
73 altitude = tok.get_string()
75 return cls(rdclass, rdtype, latitude, longitude, altitude)
77 from_text = classmethod(from_text)
79 def to_wire(self, file, compress = None, origin = None):
80 l = len(self.latitude)
84 file.write(self.latitude)
85 l = len(self.longitude)
89 file.write(self.longitude)
90 l = len(self.altitude)
94 file.write(self.altitude)
96 def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
97 l = ord(wire[current])
101 raise dns.exception.FormError
102 latitude = wire[current : current + l]
105 l = ord(wire[current])
109 raise dns.exception.FormError
110 longitude = wire[current : current + l]
113 l = ord(wire[current])
117 raise dns.exception.FormError
118 altitude = wire[current : current + l]
119 return cls(rdclass, rdtype, latitude, longitude, altitude)
121 from_wire = classmethod(from_wire)
123 def _cmp(self, other):
124 v = cmp(self.latitude, other.latitude)
126 v = cmp(self.longitude, other.longitude)
128 v = cmp(self.altitude, other.altitude)
131 def _get_float_latitude(self):
132 return float(self.latitude)
134 def _set_float_latitude(self, value):
135 self.latitude = str(value)
137 float_latitude = property(_get_float_latitude, _set_float_latitude,
138 doc="latitude as a floating point value")
140 def _get_float_longitude(self):
141 return float(self.longitude)
143 def _set_float_longitude(self, value):
144 self.longitude = str(value)
146 float_longitude = property(_get_float_longitude, _set_float_longitude,
147 doc="longitude as a floating point value")
149 def _get_float_altitude(self):
150 return float(self.altitude)
152 def _set_float_altitude(self, value):
153 self.altitude = str(value)
155 float_altitude = property(_get_float_altitude, _set_float_altitude,
156 doc="altitude as a floating point value")