No Description
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 14KB

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