s4-python: Format to PEP8, simplify tests.
[samba.git] / source4 / scripting / python / samba_external / dnspython / dns / rdtypes / ANY / GPOS.py
1 # Copyright (C) 2003-2007, 2009, 2010 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 dns.exception
17 import dns.rdata
18 import dns.tokenizer
19
20 def _validate_float_string(what):
21     if what[0] == '-' or what[0] == '+':
22         what = what[1:]
23     if what.isdigit():
24         return
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
32
33 class GPOS(dns.rdata.Rdata):
34     """GPOS record
35
36     @ivar latitude: latitude
37     @type latitude: string
38     @ivar longitude: longitude
39     @type longitude: string
40     @ivar altitude: altitude
41     @type altitude: string
42     @see: RFC 1712"""
43
44     __slots__ = ['latitude', 'longitude', 'altitude']
45
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
66
67     def to_text(self, origin=None, relativize=True, **kw):
68         return '%s %s %s' % (self.latitude, self.longitude, self.altitude)
69
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()
74         tok.get_eol()
75         return cls(rdclass, rdtype, latitude, longitude, altitude)
76
77     from_text = classmethod(from_text)
78
79     def to_wire(self, file, compress = None, origin = None):
80         l = len(self.latitude)
81         assert l < 256
82         byte = chr(l)
83         file.write(byte)
84         file.write(self.latitude)
85         l = len(self.longitude)
86         assert l < 256
87         byte = chr(l)
88         file.write(byte)
89         file.write(self.longitude)
90         l = len(self.altitude)
91         assert l < 256
92         byte = chr(l)
93         file.write(byte)
94         file.write(self.altitude)
95
96     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
97         l = ord(wire[current])
98         current += 1
99         rdlen -= 1
100         if l > rdlen:
101             raise dns.exception.FormError
102         latitude = wire[current : current + l]
103         current += l
104         rdlen -= l
105         l = ord(wire[current])
106         current += 1
107         rdlen -= 1
108         if l > rdlen:
109             raise dns.exception.FormError
110         longitude = wire[current : current + l]
111         current += l
112         rdlen -= l
113         l = ord(wire[current])
114         current += 1
115         rdlen -= 1
116         if l != rdlen:
117             raise dns.exception.FormError
118         altitude = wire[current : current + l]
119         return cls(rdclass, rdtype, latitude, longitude, altitude)
120
121     from_wire = classmethod(from_wire)
122
123     def _cmp(self, other):
124         v = cmp(self.latitude, other.latitude)
125         if v == 0:
126             v = cmp(self.longitude, other.longitude)
127             if v == 0:
128                 v = cmp(self.altitude, other.altitude)
129         return v
130
131     def _get_float_latitude(self):
132         return float(self.latitude)
133
134     def _set_float_latitude(self, value):
135         self.latitude = str(value)
136
137     float_latitude = property(_get_float_latitude, _set_float_latitude,
138                               doc="latitude as a floating point value")
139
140     def _get_float_longitude(self):
141         return float(self.longitude)
142
143     def _set_float_longitude(self, value):
144         self.longitude = str(value)
145
146     float_longitude = property(_get_float_longitude, _set_float_longitude,
147                                doc="longitude as a floating point value")
148
149     def _get_float_altitude(self):
150         return float(self.altitude)
151
152     def _set_float_altitude(self, value):
153         self.altitude = str(value)
154
155     float_altitude = property(_get_float_altitude, _set_float_altitude,
156                               doc="altitude as a floating point value")