-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.py
408 lines (312 loc) · 11.7 KB
/
decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# ------------------------------------------------------------------------------
#
# Project: pyows <http://eoxserver.org>
# Authors: Fabian Schindler <[email protected]>
#
# ------------------------------------------------------------------------------
# Copyright (C) 2019 EOX IT Services GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies of this Software or works derived from this Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ------------------------------------------------------------------------------
""" This module provides base functionality for any other decoder class.
"""
ZERO_OR_ONE = "?"
ONE_OR_MORE = "+"
ANY = "*"
SINGLE_VALUES = (ZERO_OR_ONE, 1)
class DecodingException(Exception):
""" Base Exception class to be thrown whenever a decoding failed.
"""
def __init__(self, message, locator=None):
super().__init__(message)
self.locator = locator
class WrongMultiplicityException(DecodingException):
""" Decoding Exception to be thrown when the multiplicity of a parameter did
not match the expected count.
"""
code = "InvalidParameterValue"
def __init__(self, locator, expected, result):
super().__init__(
"Parameter '%s': expected %s got %d" % (locator, expected, result),
locator
)
class MissingParameterException(DecodingException):
""" Exception to be thrown, when a decoder could not read one parameter,
where exactly one was required.
"""
code = "MissingParameterValue"
def __init__(self, locator):
super().__init__(
"Missing required parameter '%s'" % locator, locator
)
class MissingParameterMultipleException(DecodingException):
""" Exception to be thrown, when a decoder could not read at least one
parameter, where one ore more were required.
"""
code = "MissingParameterValue"
def __init__(self, locator):
super().__init__(
"Missing at least one required parameter '%s'" % locator, locator
)
class NoChoiceResultException(DecodingException):
pass
class ExclusiveException(DecodingException):
pass
# NOTE: The following exceptions may get propagated as OWS exceptions
# therefore it is necessary to set the proper OWS exception code.
class InvalidParameterException(DecodingException):
code = "InvalidParameterValue"
# Compound fields
class Choice:
""" Tries all given choices until one does return something.
"""
def __init__(self, *choices):
self.choices = choices
def __get__(self, decoder, decoder_class=None):
for choice in self.choices:
try:
return choice.__get__(decoder, decoder_class)
except Exception:
continue
raise NoChoiceResultException
class Exclusive:
""" For mutual exclusive Parameters.
"""
def __init__(self, *choices):
self.choices = choices
def __get__(self, decoder, decoder_class=None):
result = None
num = 0
for choice in self.choices:
try:
result = choice.__get__(decoder, decoder_class)
num += 1
except Exception:
continue
if num != 1:
raise ExclusiveException
return result
class Concatenate:
""" Helper to concatenate the results of all sub-parameters to one.
"""
def __init__(self, *choices, **kwargs):
self.choices = choices
self.allow_errors = kwargs.get("allow_errors", True)
def __get__(self, decoder, decoder_class=None):
result = []
for choice in self.choices:
try:
value = choice.__get__(decoder, decoder_class)
if isinstance(value, (list, tuple)):
result.extend(value)
else:
result.append(value)
except Exception as exc:
if self.allow_errors:
# swallow exception
continue
raise exc
return result
# Type conversion helpers
class typelist:
""" Helper for XMLDecoder schemas that expect a string that represents a
list of a type separated by some separator.
"""
def __init__(self, typ=None, separator=" "):
self.typ = typ
self.separator = separator
def __call__(self, value):
split = value.split(self.separator)
if self.typ:
return [self.typ(v) for v in split]
return split
class fixed:
""" Helper for parameters that are expected to be have a fixed value and
raises a ValueError if not.
"""
def __init__(self, value, case_sensitive=True):
self.value = value if case_sensitive else value.lower()
self.case_sensitive = case_sensitive
def __call__(self, value):
compare = value if self.case_sensitive else value.lower()
if self.value != compare:
raise ValueError(
"Value mismatch, expected %s, got %s." %
(self.value, value)
)
return value
class enum:
""" Helper for parameters that are expected to be in a certain enumeration.
A ValueError is raised if not.
"""
def __init__(self, values, case_sensitive=True):
self.values = values
self.compare_values = values if case_sensitive else [
lower(v) for v in values
]
self.case_sensitive = case_sensitive
def __call__(self, value):
compare = value if self.case_sensitive else value.lower()
if compare not in self.compare_values:
raise ValueError(
"Unexpected value '%s'. Expected one of: %s." %
(value, ", ".join(map(lambda s: "'%s'" % s, self.values)))
)
return value
def lower(value):
""" Functor to return a lower-case string.
"""
return value.lower()
def upper(value):
""" Functor to return a upper-case string.
"""
return value.upper()
def strip(value):
""" Functor to return a whitespace stripped string.
"""
return value.strip()
class value_range:
""" Helper to assert that a given parameter is within a specified range.
"""
def __init__(self, min, max, type=float):
self._min = min
self._max = max
self._type = type
def __call__(self, raw):
value = self._type(raw)
if value < self._min or value > self._max:
raise ValueError(
"Given value '%s' exceeds expected bounds (%s, %s)"
% (value, self._min, self._max)
)
return value
def boolean(raw):
""" Functor to convert "true"/"false" to a boolean.
"""
raw = raw.lower()
if raw not in ("true", "false"):
raise ValueError("Could not parse a boolean value from '%s'." % raw)
return raw == "true"
def to_dict(decoder, dict_class=dict):
""" Utility function to get a dictionary representation of the given decoder.
This function invokes all decoder parameters and sets the dictionary
fields accordingly
"""
return dict_class(
(name, getattr(decoder, name))
for name in dir(decoder)
if not name.startswith("_") and name != "namespaces"
)
class NO_DEFAULT:
pass
class BaseParameter(property):
""" Abstract base class for XML, KVP or any other kind of parameter.
"""
def __init__(self, type=None, num=1, default=NO_DEFAULT,
default_factory=None):
super().__init__(self.fget)
self.type = type or str
self.num = num
self.default = default
self.default_factory = default_factory
def select(self, decoder):
""" Interface method.
"""
raise NotImplementedError
@property
def locator(self):
return ""
def fget(self, decoder):
""" Property getter function.
"""
results = self.select(decoder)
count = len(results)
locator = self.locator
multiple = self.num not in SINGLE_VALUES
# check the correct count of the result
if not multiple and count > 1:
raise WrongMultiplicityException(locator, "at most one", count)
elif self.num == 1 and count == 0:
raise MissingParameterException(locator)
elif self.num == ONE_OR_MORE and count == 0:
raise MissingParameterMultipleException(locator)
elif isinstance(self.num, int) and count != self.num:
raise WrongMultiplicityException(locator, self.num, count)
# parse the value/values, or return the defaults
if multiple:
if count == 0 and self.num == ANY:
if self.default_factory:
return self.default_factory()
elif self.default is not NO_DEFAULT:
return self.default
try:
return [self.type(v) for v in results]
except Exception as e:
# let some more sophisticated exceptions pass
if hasattr(e, "locator") or hasattr(e, "code"):
raise
raise InvalidParameterException(str(e), locator)
elif self.num == ZERO_OR_ONE and count == 0:
if self.default_factory:
return self.default_factory()
elif self.default is not NO_DEFAULT:
return self.default
else:
return None
elif self.type:
try:
return self.type(results[0])
except Exception as e:
# let some more sophisticated exceptions pass
if hasattr(e, "locator") or hasattr(e, "code"):
raise
raise InvalidParameterException(str(e), locator)
return results[0]
class BaseDecoder:
object_class = None
def create_object(self, params: dict):
""" Create the associated object for that decoder using the
passed parameters.
"""
if self.object_class is not None:
return self.object_class(**params)
raise NotImplementedError
def map_params(self, params):
""" Map parameters, if necessary. Default implementation is
a no-op.
"""
return params
def collect_params(self):
""" Collect all parameters. This will collect all values
which are computed using properties.
"""
cls = type(self)
return {
name: getattr(self, name)
for name in dir(self)
if isinstance(getattr(cls, name, None), property)
}
def decode(self):
""" Collect all decoder parameters and construct the object.
"""
return self.create_object(
self.map_params(
self.collect_params()
)
)