Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validator.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os.path
  4. import re
  5. import inspect
  6. import copy
  7. ## @package lodel.settings.validator Lodel2 settings validators/cast module
  8. #
  9. # Validator are registered in the SettingValidator class.
  10. # @note to get a list of registered default validators just run
  11. # <pre>$ python scripts/settings_validator.py</pre>
  12. ##@brief Exception class that should be raised when a validation fails
  13. class SettingsValidationError(Exception):
  14. pass
  15. ##@brief Handles settings validators
  16. #
  17. # Class instance are callable objects that takes a value argument (the value to validate). It raises
  18. # a SettingsValidationError if validation fails, else it returns a properly
  19. # casted value.
  20. class SettingValidator(object):
  21. _validators = dict()
  22. _description = dict()
  23. ##@brief Instanciate a validator
  24. def __init__(self, name, none_is_valid = False):
  25. if name is not None and name not in self._validators:
  26. raise NameError("No validator named '%s'" % name)
  27. self.__name = name
  28. ##@brief Call the validator
  29. # @param value *
  30. # @return properly casted value
  31. # @throw SettingsValidationError
  32. def __call__(self, value):
  33. if self.__name is None:
  34. return value
  35. try:
  36. return self._validators[self.__name](value)
  37. except Exception as e:
  38. raise SettingsValidationError(e)
  39. ##@brief Register a new validator
  40. # @param name str : validator name
  41. # @param callback callable : the function that will validate a value
  42. @classmethod
  43. def register_validator(cls, name, callback, description=None):
  44. if name in cls._validators:
  45. raise NameError("A validator named '%s' allready exists" % name)
  46. # Broken test for callable
  47. if not inspect.isfunction(callback) and not inspect.ismethod(callback) and not hasattr(callback, '__call__'):
  48. raise TypeError("Callable expected but got %s" % type(callback))
  49. cls._validators[name] = callback
  50. cls._description[name] = description
  51. ##@brief Get the validator list associated with description
  52. @classmethod
  53. def validators_list(cls):
  54. return copy.copy(cls._description)
  55. ##@brief Create and register a list validator
  56. # @param elt_validator callable : The validator that will be used for validate each elt value
  57. # @param validator_name str
  58. # @param description None | str
  59. # @param separator str : The element separator
  60. # @return A SettingValidator instance
  61. @classmethod
  62. def create_list_validator(cls, validator_name, elt_validator, description = None, separator = ','):
  63. def list_validator(value):
  64. res = list()
  65. errors = list()
  66. for elt in value.split(separator):
  67. elt = elt_validator(elt)
  68. if len(elt) > 0:
  69. res.append(elt)
  70. return res
  71. description = "Convert value to an array" if description is None else description
  72. cls.register_validator(
  73. validator_name,
  74. list_validator,
  75. description)
  76. return cls(validator_name)
  77. ##@brief Create and register a list validator which reads an array and returns a string
  78. # @param elt_validator callable : The validator that will be used for validate each elt value
  79. # @param validator_name str
  80. # @param description None | str
  81. # @param separator str : The element separator
  82. # @return A SettingValidator instance
  83. @classmethod
  84. def create_write_list_validator(cls, validator_name, elt_validator, description = None, separator = ','):
  85. def write_list_validator(value):
  86. res = ''
  87. errors = list()
  88. for elt in value:
  89. res += elt_validator(elt) + ','
  90. return res[:len(res)-1]
  91. description = "Convert value to a string" if description is None else description
  92. cls.register_validator(
  93. validator_name,
  94. write_list_validator,
  95. description)
  96. return cls(validator_name)
  97. ##@brief Create and register a regular expression validator
  98. # @param pattern str : regex pattern
  99. # @param validator_name str : The validator name
  100. # @param description str : Validator description
  101. # @return a SettingValidator instance
  102. @classmethod
  103. def create_re_validator(cls, pattern, validator_name, description = None):
  104. def re_validator(value):
  105. if not re.match(pattern, value):
  106. raise SettingsValidationError("The value '%s' doesn't match the following pattern '%s'" % pattern)
  107. return value
  108. #registering the validator
  109. cls.register_validator(
  110. validator_name,
  111. re_validator,
  112. ("Match value to '%s'" % pattern) if description is None else description)
  113. return cls(validator_name)
  114. ## @return a list of registered validators
  115. @classmethod
  116. def validators_list_str(cls):
  117. result = ''
  118. for name in sorted(cls._validators.keys()):
  119. result += "\t%016s" % name
  120. if name in cls._description and cls._description[name] is not None:
  121. result += ": %s" % cls._description[name]
  122. result += "\n"
  123. return result
  124. ##@brief Integer value validator callback
  125. def int_val(value):
  126. return int(value)
  127. ##@brief Output file validator callback
  128. # @return A file object (if filename is '-' return sys.stderr)
  129. def file_err_output(value):
  130. if not isinstance(value, str):
  131. raise SettingsValidationError("A string was expected but got '%s' " % value)
  132. if value == '-':
  133. return None
  134. return value
  135. ##@brief Boolean value validator callback
  136. def boolean_val(value):
  137. if value.strip().lower() == 'true' or value.strip() == '1':
  138. value = True
  139. elif value.strip().lower() == 'false' or value.strip() == '0':
  140. value = False
  141. else:
  142. raise SettingsValidationError("A boolean was expected but got '%s' " % value)
  143. return bool(value)
  144. def directory_val(value):
  145. res = SettingValidator('strip')(value)
  146. if not os.path.isdir(res):
  147. raise SettingsValidationError("Folowing path don't exists or is not a directory : '%s'"%res)
  148. return res
  149. def loglevel_val(value):
  150. valids = ['DEBUG', 'INFO', 'SECURITY', 'ERROR', 'CRITICAL']
  151. if value.upper() not in valids:
  152. raise SettingsValidationError("The value '%s' is not a valid loglevel")
  153. return value.upper()
  154. def path_val(value):
  155. if not os.path.exists(value):
  156. raise SettingsValidationError("The value '%s' is not a valid path")
  157. return value
  158. def dummy_val(value): return value
  159. #
  160. # Default validators registration
  161. #
  162. SettingValidator.register_validator(
  163. 'dummy',
  164. dummy_val,
  165. 'Validate anything')
  166. SettingValidator.register_validator(
  167. 'strip',
  168. str.strip,
  169. 'String trim')
  170. SettingValidator.register_validator(
  171. 'int',
  172. int_val,
  173. 'Integer value validator')
  174. SettingValidator.register_validator(
  175. 'bool',
  176. boolean_val,
  177. 'Boolean value validator')
  178. SettingValidator.register_validator(
  179. 'errfile',
  180. file_err_output,
  181. 'Error output file validator (return stderr if filename is "-")')
  182. SettingValidator.register_validator(
  183. 'directory',
  184. directory_val,
  185. 'Directory path validator')
  186. SettingValidator.register_validator(
  187. 'loglevel',
  188. loglevel_val,
  189. 'Loglevel validator')
  190. SettingValidator.register_validator(
  191. 'path',
  192. path_val,
  193. 'path validator')
  194. SettingValidator.create_list_validator(
  195. 'list',
  196. SettingValidator('strip'),
  197. description = "Simple list validator. Validate a list of values separated by ','",
  198. separator = ',')
  199. SettingValidator.create_list_validator(
  200. 'directory_list',
  201. SettingValidator('directory'),
  202. description = "Validator for a list of directory path separated with ','",
  203. separator = ',')
  204. SettingValidator.create_write_list_validator(
  205. 'write_list',
  206. SettingValidator('directory'),
  207. description = "Validator for an array of values which will be set in a string, separated by ','",
  208. separator = ',')
  209. SettingValidator.create_re_validator(
  210. r'^https?://[^\./]+.[^\./]+/?.*$',
  211. 'http_url',
  212. 'Url validator')
  213. #
  214. # Lodel 2 configuration specification
  215. #
  216. ##@brief Global specifications for lodel2 settings
  217. LODEL2_CONF_SPECS = {
  218. 'lodel2': {
  219. 'debug': ( True,
  220. SettingValidator('bool')),
  221. 'plugins_path': ( None,
  222. SettingValidator('list')),
  223. 'plugins': ( "",
  224. SettingValidator('list')),
  225. 'sitename': ( 'noname',
  226. SettingValidator('strip')),
  227. 'lib_path': ( None,
  228. SettingValidator('path')),
  229. },
  230. 'lodel2.logging.*' : {
  231. 'level': ( 'ERROR',
  232. SettingValidator('loglevel')),
  233. 'context': ( False,
  234. SettingValidator('bool')),
  235. 'filename': ( None,
  236. SettingValidator('errfile', none_is_valid = True)),
  237. 'backupcount': ( None,
  238. SettingValidator('int', none_is_valid = True)),
  239. 'maxbytes': ( None,
  240. SettingValidator('int', none_is_valid = True)),
  241. },
  242. 'lodel2.editorialmodel': {
  243. 'emfile': ( 'em.pickle',
  244. SettingValidator('strip')),
  245. 'emtranslator': ( 'picklefile',
  246. SettingValidator('strip')),
  247. 'dyncode': ( 'leapi_dyncode.py',
  248. SettingValidator('strip')),
  249. 'groups': ( '',
  250. SettingValidator('list')),
  251. 'editormode': ( False,
  252. SettingValidator('bool')),
  253. }
  254. }