설명 없음
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os.path
  4. import re
  5. import socket
  6. import inspect
  7. import copy
  8. ## @package lodel.settings.validator Lodel2 settings validators/cast module
  9. #
  10. # Validator are registered in the SettingValidator class.
  11. # @note to get a list of registered default validators just run
  12. # <pre>$ python scripts/settings_validator.py</pre>
  13. ##@brief Exception class that should be raised when a validation fails
  14. class SettingsValidationError(Exception):
  15. pass
  16. ##@brief Handles settings validators
  17. #
  18. # Class instance are callable objects that takes a value argument (the value to validate). It raises
  19. # a SettingsValidationError if validation fails, else it returns a properly
  20. # casted value.
  21. class SettingValidator(object):
  22. _validators = dict()
  23. _description = dict()
  24. ##@brief Instanciate a validator
  25. #@param name str : validator name
  26. #@param none_is_valid bool : if True None will be validated
  27. #@param **kwargs : more arguement for the validator
  28. def __init__(self, name, none_is_valid = False, **kwargs):
  29. if name is not None and name not in self._validators:
  30. raise NameError("No validator named '%s'" % name)
  31. self.__none_is_valid = none_is_valid
  32. self.__name = name
  33. self._opt_args = kwargs
  34. ##@brief Call the validator
  35. # @param value *
  36. # @return properly casted value
  37. # @throw SettingsValidationError
  38. def __call__(self, value):
  39. if self.__name is None:
  40. return value
  41. if self.__none_is_valid and value is None:
  42. return None
  43. try:
  44. return self._validators[self.__name](value, **self._opt_args)
  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. @LodelHook('lodel2_dyncode_bootstraped')
  229. def plugin_type_checker(hookname, caller, payload):
  230. from lodel.plugin.plugins import Plugin
  231. from lodel.plugin.exceptions import PluginError
  232. try:
  233. plugin = Plugin.get(value)
  234. except PluginError:
  235. msg = "No plugin named %s found"
  236. msg %= value
  237. raise SettingsValidationError(msg)
  238. if plugin._type_conf_name.lower() != ptype.lower():
  239. msg = "A plugin of type '%s' was expected but found a plugin \
  240. named '%s' that is a '%s' plugin"
  241. msg %= (ptype, value, plugin._type_conf_name)
  242. raise SettingsValidationError(msg)
  243. return value
  244. #
  245. # Default validators registration
  246. #
  247. SettingValidator.register_validator(
  248. 'plugin',
  249. plugin_validator,
  250. 'plugin name & type validator')
  251. SettingValidator.register_validator(
  252. 'dummy',
  253. lambda value:value,
  254. 'Validate anything')
  255. SettingValidator.register_validator(
  256. 'none',
  257. none_val,
  258. 'Validate None')
  259. SettingValidator.register_validator(
  260. 'string',
  261. str_val,
  262. 'Validate string values')
  263. SettingValidator.register_validator(
  264. 'strip',
  265. str.strip,
  266. 'String trim')
  267. SettingValidator.register_validator(
  268. 'int',
  269. int_val,
  270. 'Integer value validator')
  271. SettingValidator.register_validator(
  272. 'bool',
  273. boolean_val,
  274. 'Boolean value validator')
  275. SettingValidator.register_validator(
  276. 'errfile',
  277. file_err_output,
  278. 'Error output file validator (return stderr if filename is "-")')
  279. SettingValidator.register_validator(
  280. 'directory',
  281. directory_val,
  282. 'Directory path validator')
  283. SettingValidator.register_validator(
  284. 'loglevel',
  285. loglevel_val,
  286. 'Loglevel validator')
  287. SettingValidator.register_validator(
  288. 'path',
  289. path_val,
  290. 'path validator')
  291. SettingValidator.register_validator(
  292. 'host',
  293. host_val,
  294. 'host validator')
  295. SettingValidator.register_validator(
  296. 'emfield',
  297. emfield_val,
  298. 'EmField name validator')
  299. SettingValidator.create_list_validator(
  300. 'list',
  301. SettingValidator('strip'),
  302. description = "Simple list validator. Validate a list of values separated by ','",
  303. separator = ',')
  304. SettingValidator.create_list_validator(
  305. 'directory_list',
  306. SettingValidator('directory'),
  307. description = "Validator for a list of directory path separated with ','",
  308. separator = ',')
  309. SettingValidator.create_write_list_validator(
  310. 'write_list',
  311. SettingValidator('directory'),
  312. description = "Validator for an array of values which will be set in a string, separated by ','",
  313. separator = ',')
  314. SettingValidator.create_re_validator(
  315. r'^https?://[^\./]+.[^\./]+/?.*$',
  316. 'http_url',
  317. 'Url validator')
  318. #
  319. # Lodel 2 configuration specification
  320. #
  321. ##@brief Append a piece of confspec
  322. #@note orig is modified during the process
  323. #@param orig dict : the confspec to update
  324. #@param upd dict : the confspec to add
  325. #@return new confspec
  326. def confspec_append(orig, section, key, validator, default):
  327. if section not in orig:
  328. orig[section] = dict()
  329. if key not in orig[section]:
  330. orig[section][key] = (default, validator)
  331. return orig
  332. ##@brief Global specifications for lodel2 settings
  333. LODEL2_CONF_SPECS = {
  334. 'lodel2': {
  335. 'debug': ( True,
  336. SettingValidator('bool')),
  337. 'sitename': ( 'noname',
  338. SettingValidator('strip')),
  339. 'runtest': ( False,
  340. SettingValidator('bool')),
  341. },
  342. 'lodel2.logging.*' : {
  343. 'level': ( 'ERROR',
  344. SettingValidator('loglevel')),
  345. 'context': ( False,
  346. SettingValidator('bool')),
  347. 'filename': ( None,
  348. SettingValidator('errfile', none_is_valid = True)),
  349. 'backupcount': ( None,
  350. SettingValidator('int', none_is_valid = True)),
  351. 'maxbytes': ( None,
  352. SettingValidator('int', none_is_valid = True)),
  353. },
  354. 'lodel2.editorialmodel': {
  355. 'emfile': ( 'em.pickle', SettingValidator('strip')),
  356. 'emtranslator': ( 'picklefile', SettingValidator('strip')),
  357. 'dyncode': ( 'leapi_dyncode.py', SettingValidator('strip')),
  358. 'groups': ( '', SettingValidator('list')),
  359. 'editormode': ( False, SettingValidator('bool')),
  360. },
  361. 'lodel2.datasources.*': {
  362. 'read_only': (False, SettingValidator('bool')),
  363. 'identifier': ( None, SettingValidator('string')),
  364. },
  365. 'lodel2.auth': {
  366. 'login_classfield': ('user.login', SettingValidator('emfield')),
  367. 'pass_classfield': ('user.password', SettingValidator('emfield')),
  368. },
  369. }