Açıklama Yok
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 12KB

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