|
@@ -1,341 +0,0 @@
|
1
|
|
-#-*- coding: utf-8 -*-
|
2
|
|
-
|
3
|
|
-import sys
|
4
|
|
-import os.path
|
5
|
|
-import re
|
6
|
|
-import socket
|
7
|
|
-import inspect
|
8
|
|
-import copy
|
9
|
|
-
|
10
|
|
-from lodel.context import LodelContext
|
11
|
|
-LodelContext.expose_modules(globals(), {
|
12
|
|
- 'lodel.exceptions': ['LodelException', 'LodelExceptions',
|
13
|
|
- 'LodelFatalError', 'FieldValidationError']})
|
14
|
|
-
|
15
|
|
-## @package lodel.settings.validator Lodel2 settings validators/cast module
|
16
|
|
-#
|
17
|
|
-# Validator are registered in the Validator class.
|
18
|
|
-# @note to get a list of registered default validators just run
|
19
|
|
-# <pre>$ python scripts/settings_validator.py</pre>
|
20
|
|
-
|
21
|
|
-##@brief Exception class that should be raised when a validation fails
|
22
|
|
-class ValidationError(Exception):
|
23
|
|
- pass
|
24
|
|
-
|
25
|
|
-##@brief Handles settings validators
|
26
|
|
-#
|
27
|
|
-# Class instance are callable objects that takes a value argument (the value to validate). It raises
|
28
|
|
-# a ValidationError if validation fails, else it returns a properly
|
29
|
|
-# casted value.
|
30
|
|
-
|
31
|
|
-class Validator(object):
|
32
|
|
-
|
33
|
|
- _validators = dict()
|
34
|
|
- _description = dict()
|
35
|
|
-
|
36
|
|
- ##@brief Instanciate a validator
|
37
|
|
- #@param name str : validator name
|
38
|
|
- #@param none_is_valid bool : if True None will be validated
|
39
|
|
- #@param **kwargs : more arguement for the validator
|
40
|
|
- def __init__(self, name, none_is_valid = False, **kwargs):
|
41
|
|
- if name is not None and name not in self._validators:
|
42
|
|
- raise LodelFatalError("No validator named '%s'" % name)
|
43
|
|
- self.__none_is_valid = none_is_valid
|
44
|
|
- self.__name = name
|
45
|
|
- self._opt_args = kwargs
|
46
|
|
-
|
47
|
|
- ##@brief Call the validator
|
48
|
|
- # @param value *
|
49
|
|
- # @return properly casted value
|
50
|
|
- # @throw ValidationError
|
51
|
|
- def __call__(self, value):
|
52
|
|
- if self.__none_is_valid and value is None:
|
53
|
|
- return None
|
54
|
|
- try:
|
55
|
|
- ret = self._validators[self.__name](value, **self._opt_args)
|
56
|
|
- return ret
|
57
|
|
- except Exception as e:
|
58
|
|
- raise ValidationError(e)
|
59
|
|
-
|
60
|
|
- ##@brief Register a new validator
|
61
|
|
- # @param name str : validator name
|
62
|
|
- # @param callback callable : the function that will validate a value
|
63
|
|
- # @param description str
|
64
|
|
- @classmethod
|
65
|
|
- def register_validator(cls, name, callback, description=None):
|
66
|
|
- if name in cls._validators:
|
67
|
|
- raise NameError("A validator named '%s' allready exists" % name)
|
68
|
|
- # Broken test for callable
|
69
|
|
- if not inspect.isfunction(callback) and not inspect.ismethod(callback) and not hasattr(callback, '__call__'):
|
70
|
|
- raise TypeError("Callable expected but got %s" % type(callback))
|
71
|
|
- cls._validators[name] = callback
|
72
|
|
- cls._description[name] = description
|
73
|
|
-
|
74
|
|
- ##@brief Get the validator list associated with description
|
75
|
|
- @classmethod
|
76
|
|
- def validators_list(cls):
|
77
|
|
- return copy.copy(cls._description)
|
78
|
|
-
|
79
|
|
- ##@brief Create and register a list validator
|
80
|
|
- # @param elt_validator callable : The validator that will be used for validate each elt value
|
81
|
|
- # @param validator_name str
|
82
|
|
- # @param description None | str
|
83
|
|
- # @param separator str : The element separator
|
84
|
|
- # @return A SettingValidator instance
|
85
|
|
- @classmethod
|
86
|
|
- def create_list_validator(cls, validator_name, elt_validator, description = None, separator = ','):
|
87
|
|
- def list_validator(value):
|
88
|
|
- res = list()
|
89
|
|
- errors = list()
|
90
|
|
- for elt in value.split(separator):
|
91
|
|
- elt = elt_validator(elt)
|
92
|
|
- if len(elt) > 0:
|
93
|
|
- res.append(elt)
|
94
|
|
- return res
|
95
|
|
- description = "Convert value to an array" if description is None else description
|
96
|
|
- cls.register_validator(
|
97
|
|
- validator_name,
|
98
|
|
- list_validator,
|
99
|
|
- description)
|
100
|
|
- return cls(validator_name)
|
101
|
|
-
|
102
|
|
- ##@brief Create and register a list validator which reads an array and returns a string
|
103
|
|
- # @param elt_validator callable : The validator that will be used for validate each elt value
|
104
|
|
- # @param validator_name str
|
105
|
|
- # @param description None | str
|
106
|
|
- # @param separator str : The element separator
|
107
|
|
- # @return A SettingValidator instance
|
108
|
|
- @classmethod
|
109
|
|
- def create_write_list_validator(cls, validator_name, elt_validator, description = None, separator = ','):
|
110
|
|
- def write_list_validator(value):
|
111
|
|
- res = ''
|
112
|
|
- errors = list()
|
113
|
|
- for elt in value:
|
114
|
|
- res += elt_validator(elt) + ','
|
115
|
|
- return res[:len(res)-1]
|
116
|
|
- description = "Convert value to a string" if description is None else description
|
117
|
|
- cls.register_validator(
|
118
|
|
- validator_name,
|
119
|
|
- write_list_validator,
|
120
|
|
- description)
|
121
|
|
- return cls(validator_name)
|
122
|
|
-
|
123
|
|
- ##@brief Create and register a regular expression validator
|
124
|
|
- # @param pattern str : regex pattern
|
125
|
|
- # @param validator_name str : The validator name
|
126
|
|
- # @param description str : Validator description
|
127
|
|
- # @return a SettingValidator instance
|
128
|
|
- @classmethod
|
129
|
|
- def create_re_validator(cls, pattern, validator_name, description = None):
|
130
|
|
- def re_validator(value):
|
131
|
|
- if not re.match(pattern, value):
|
132
|
|
- raise ValidationError("The value '%s' doesn't match the following pattern '%s'" % pattern)
|
133
|
|
- return value
|
134
|
|
- #registering the validator
|
135
|
|
- cls.register_validator(
|
136
|
|
- validator_name,
|
137
|
|
- re_validator,
|
138
|
|
- ("Match value to '%s'" % pattern) if description is None else description)
|
139
|
|
- return cls(validator_name)
|
140
|
|
-
|
141
|
|
-
|
142
|
|
- ## @return a list of registered validators
|
143
|
|
- @classmethod
|
144
|
|
- def validators_list_str(cls):
|
145
|
|
- result = ''
|
146
|
|
- for name in sorted(cls._validators.keys()):
|
147
|
|
- result += "\t%016s" % name
|
148
|
|
- if name in cls._description and cls._description[name] is not None:
|
149
|
|
- result += ": %s" % cls._description[name]
|
150
|
|
- result += "\n"
|
151
|
|
- return result
|
152
|
|
-
|
153
|
|
-##@brief Integer value validator callback
|
154
|
|
-def int_val(value):
|
155
|
|
- return int(value)
|
156
|
|
-
|
157
|
|
-##@brief Output file validator callback
|
158
|
|
-# @return A file object (if filename is '-' return sys.stderr)
|
159
|
|
-def file_err_output(value):
|
160
|
|
- if not isinstance(value, str):
|
161
|
|
- raise ValidationError("A string was expected but got '%s' " % value)
|
162
|
|
- if value == '-':
|
163
|
|
- return None
|
164
|
|
- return value
|
165
|
|
-
|
166
|
|
-##@brief Boolean value validator callback
|
167
|
|
-def boolean_val(value):
|
168
|
|
- if isinstance(value, bool):
|
169
|
|
- return value
|
170
|
|
- if value.strip().lower() == 'true' or value.strip() == '1':
|
171
|
|
- value = True
|
172
|
|
- elif value.strip().lower() == 'false' or value.strip() == '0':
|
173
|
|
- value = False
|
174
|
|
- else:
|
175
|
|
- raise ValidationError("A boolean was expected but got '%s' " % value)
|
176
|
|
- return bool(value)
|
177
|
|
-
|
178
|
|
-##@brief Validate a directory path
|
179
|
|
-def directory_val(value):
|
180
|
|
- res = SettingValidator('strip')(value)
|
181
|
|
- if not os.path.isdir(res):
|
182
|
|
- raise SettingsValidationError("Following path doesn't exist or is not a directory : '%s'"%res)
|
183
|
|
- return res
|
184
|
|
-
|
185
|
|
-##@brief Validate a loglevel value
|
186
|
|
-def loglevel_val(value):
|
187
|
|
- valids = ['DEBUG', 'INFO', 'WARNING', 'SECURITY', 'ERROR', 'CRITICAL']
|
188
|
|
- if value.upper() not in valids:
|
189
|
|
- raise SettingsValidationError(
|
190
|
|
- "The value '%s' is not a valid loglevel" % value)
|
191
|
|
- return value.upper()
|
192
|
|
-
|
193
|
|
-##@brief Validate a path
|
194
|
|
-def path_val(value):
|
195
|
|
- if value is None or not os.path.exists(value):
|
196
|
|
- raise ValidationError(
|
197
|
|
- "path '%s' doesn't exists" % value)
|
198
|
|
- return value
|
199
|
|
-
|
200
|
|
-##@brief Validate None
|
201
|
|
-def none_val(value):
|
202
|
|
- if value is None:
|
203
|
|
- return None
|
204
|
|
- raise ValidationError("None value is required to be validated")
|
205
|
|
-
|
206
|
|
-##@brief Validate a string
|
207
|
|
-def str_val(value):
|
208
|
|
- try:
|
209
|
|
- return str(value)
|
210
|
|
- except Exception as e:
|
211
|
|
- raise ValidationError("Not able to convert value to string : " + str(e))
|
212
|
|
-
|
213
|
|
-##@brief Validate using a regex
|
214
|
|
-def regex_val(value, pattern):
|
215
|
|
- if re.match(pattern, value) is None:
|
216
|
|
- raise ValidationError("The value '%s' is not validated by : \
|
217
|
|
-r\"%s\"" %(value, pattern))
|
218
|
|
- return value
|
219
|
|
-
|
220
|
|
-##@brief Validate a hostname (ipv4 or ipv6)
|
221
|
|
-def host_val(value):
|
222
|
|
- if value == 'localhost':
|
223
|
|
- return value
|
224
|
|
- ok = False
|
225
|
|
- try:
|
226
|
|
- socket.inet_aton(value)
|
227
|
|
- return value
|
228
|
|
- except (TypeError,OSError):
|
229
|
|
- pass
|
230
|
|
- try:
|
231
|
|
- socket.inet_pton(socket.AF_INET6, value)
|
232
|
|
- return value
|
233
|
|
- except (TypeError,OSError):
|
234
|
|
- pass
|
235
|
|
- try:
|
236
|
|
- socket.getaddrinfo(value, 80)
|
237
|
|
- return value
|
238
|
|
- except (TypeError,socket.gaierror):
|
239
|
|
- msg = "The value '%s' is not a valid host"
|
240
|
|
- raise ValidationError(msg % value)
|
241
|
|
-
|
242
|
|
-
|
243
|
|
-def custom_list_validator(value, validator_name, validator_kwargs = None):
|
244
|
|
- validator_kwargs = dict() if validator_kwargs is None else validator_kwargs
|
245
|
|
- validator = Validator(validator_name, **validator_kwargs)
|
246
|
|
- for item in value.split():
|
247
|
|
- validator(item)
|
248
|
|
- return value.split()
|
249
|
|
-
|
250
|
|
-#
|
251
|
|
-# Default validators registration
|
252
|
|
-#
|
253
|
|
-
|
254
|
|
-
|
255
|
|
-Validator.register_validator(
|
256
|
|
- 'custom_list',
|
257
|
|
- custom_list_validator,
|
258
|
|
- 'A list validator that takes a "validator_name" as argument')
|
259
|
|
-
|
260
|
|
-Validator.register_validator(
|
261
|
|
- 'dummy',
|
262
|
|
- lambda value:value,
|
263
|
|
- 'Validate anything')
|
264
|
|
-
|
265
|
|
-Validator.register_validator(
|
266
|
|
- 'none',
|
267
|
|
- none_val,
|
268
|
|
- 'Validate None')
|
269
|
|
-
|
270
|
|
-Validator.register_validator(
|
271
|
|
- 'string',
|
272
|
|
- str_val,
|
273
|
|
- 'Validate string values')
|
274
|
|
-
|
275
|
|
-Validator.register_validator(
|
276
|
|
- 'strip',
|
277
|
|
- str.strip,
|
278
|
|
- 'String trim')
|
279
|
|
-
|
280
|
|
-Validator.register_validator(
|
281
|
|
- 'int',
|
282
|
|
- int_val,
|
283
|
|
- 'Integer value validator')
|
284
|
|
-
|
285
|
|
-Validator.register_validator(
|
286
|
|
- 'bool',
|
287
|
|
- boolean_val,
|
288
|
|
- 'Boolean value validator')
|
289
|
|
-
|
290
|
|
-Validator.register_validator(
|
291
|
|
- 'errfile',
|
292
|
|
- file_err_output,
|
293
|
|
- 'Error output file validator (return stderr if filename is "-")')
|
294
|
|
-
|
295
|
|
-Validator.register_validator(
|
296
|
|
- 'directory',
|
297
|
|
- directory_val,
|
298
|
|
- 'Directory path validator')
|
299
|
|
-
|
300
|
|
-Validator.register_validator(
|
301
|
|
- 'loglevel',
|
302
|
|
- loglevel_val,
|
303
|
|
- 'Loglevel validator')
|
304
|
|
-
|
305
|
|
-Validator.register_validator(
|
306
|
|
- 'path',
|
307
|
|
- path_val,
|
308
|
|
- 'path validator')
|
309
|
|
-
|
310
|
|
-Validator.register_validator(
|
311
|
|
- 'host',
|
312
|
|
- host_val,
|
313
|
|
- 'host validator')
|
314
|
|
-
|
315
|
|
-Validator.register_validator(
|
316
|
|
- 'regex',
|
317
|
|
- regex_val,
|
318
|
|
- 'RegEx name validator (take re as argument)')
|
319
|
|
-
|
320
|
|
-Validator.create_list_validator(
|
321
|
|
- 'list',
|
322
|
|
- Validator('strip'),
|
323
|
|
- description = "Simple list validator. Validate a list of values separated by ','",
|
324
|
|
- separator = ',')
|
325
|
|
-
|
326
|
|
-Validator.create_list_validator(
|
327
|
|
- 'directory_list',
|
328
|
|
- Validator('directory'),
|
329
|
|
- description = "Validator for a list of directory path separated with ','",
|
330
|
|
- separator = ',')
|
331
|
|
-
|
332
|
|
-Validator.create_write_list_validator(
|
333
|
|
- 'write_list',
|
334
|
|
- Validator('directory'),
|
335
|
|
- description = "Validator for an array of values which will be set in a string, separated by ','",
|
336
|
|
- separator = ',')
|
337
|
|
-
|
338
|
|
-Validator.create_re_validator(
|
339
|
|
- r'^https?://[^\./]+.[^\./]+/?.*$',
|
340
|
|
- 'http_url',
|
341
|
|
- 'Url validator')
|