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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. def __init__(self, name, none_is_valid = False):
  26. if name is not None and name not in self._validators:
  27. raise NameError("No validator named '%s'" % name)
  28. self.__none_is_valid = none_is_valid
  29. self.__name = name
  30. ##@brief Call the validator
  31. # @param value *
  32. # @return properly casted value
  33. # @throw SettingsValidationError
  34. def __call__(self, value):
  35. if self.__name is None:
  36. return value
  37. if self.__none_is_valid and value is None:
  38. return None
  39. try:
  40. return self._validators[self.__name](value)
  41. except Exception as e:
  42. raise SettingsValidationError(e)
  43. ##@brief Register a new validator
  44. # @param name str : validator name
  45. # @param callback callable : the function that will validate a value
  46. # @param description str
  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. from lodel.plugin.hooks import LodelHook
  198. spl = value.split('.')
  199. if len(spl) != 2:
  200. msg = "Expected a value in the form CLASSNAME.FIELDNAME but got : %s"
  201. raise SettingsValidationError(msg % value)
  202. value = tuple(spl)
  203. #Late validation hook
  204. @LodelHook('lodel2_dyncode_bootstraped')
  205. def emfield_conf_check(hookname, caller, payload):
  206. from lodel import dyncode
  207. classnames = { cls.__name__.lower():cls for cls in dyncode.dynclasses}
  208. if value[0].lower() not in classnames:
  209. msg = "Following dynamic class do not exists in current EM : %s"
  210. raise SettingsValidationError(msg % value[0])
  211. ccls = classnames[value[0].lower()]
  212. if value[1].lower() not in ccls.fieldnames(True):
  213. msg = "Following field not found in class %s : %s"
  214. raise SettingsValidationError(msg % value)
  215. return value
  216. def plugin_val(value):
  217. from lodel.plugin.hooks import LodelHook
  218. spl = value.split('.')
  219. if len(spl) != 2:
  220. msg = "Expected a value in the form PLUGIN.TYPE but got : %s"
  221. raise SettingsValidationError(msg % value)
  222. value = tuple(spl)
  223. #Late validation hook
  224. @LodelHook('lodel2_dyncode_bootstraped')
  225. def type_check(hookname, caller, payload):
  226. from lodel import plugin
  227. typesname = { cls.__name__.lower():cls for cls in plugin.PLUGINS_TYPE}
  228. if value[1].lower() not in typesname:
  229. msg = "Following plugin type do not exist in plugin list %s : %s"
  230. raise SettingsValidationError(msg % value)
  231. return value
  232. plug_type_val = plugin_val(value)
  233. return plug_type_val
  234. #
  235. # Default validators registration
  236. #
  237. SettingValidator.register_validator(
  238. 'plugin',
  239. plugin_val,
  240. 'plugin validator')
  241. SettingValidator.register_validator(
  242. 'dummy',
  243. lambda value:value,
  244. 'Validate anything')
  245. SettingValidator.register_validator(
  246. 'none',
  247. none_val,
  248. 'Validate None')
  249. SettingValidator.register_validator(
  250. 'string',
  251. str_val,
  252. 'Validate string values')
  253. SettingValidator.register_validator(
  254. 'strip',
  255. str.strip,
  256. 'String trim')
  257. SettingValidator.register_validator(
  258. 'int',
  259. int_val,
  260. 'Integer value validator')
  261. SettingValidator.register_validator(
  262. 'bool',
  263. boolean_val,
  264. 'Boolean value validator')
  265. SettingValidator.register_validator(
  266. 'errfile',
  267. file_err_output,
  268. 'Error output file validator (return stderr if filename is "-")')
  269. SettingValidator.register_validator(
  270. 'directory',
  271. directory_val,
  272. 'Directory path validator')
  273. SettingValidator.register_validator(
  274. 'loglevel',
  275. loglevel_val,
  276. 'Loglevel validator')
  277. SettingValidator.register_validator(
  278. 'path',
  279. path_val,
  280. 'path validator')
  281. SettingValidator.register_validator(
  282. 'host',
  283. host_val,
  284. 'host validator')
  285. SettingValidator.register_validator(
  286. 'emfield',
  287. emfield_val,
  288. 'EmField name validator')
  289. SettingValidator.create_list_validator(
  290. 'list',
  291. SettingValidator('strip'),
  292. description = "Simple list validator. Validate a list of values separated by ','",
  293. separator = ',')
  294. SettingValidator.create_list_validator(
  295. 'directory_list',
  296. SettingValidator('directory'),
  297. description = "Validator for a list of directory path separated with ','",
  298. separator = ',')
  299. SettingValidator.create_write_list_validator(
  300. 'write_list',
  301. SettingValidator('directory'),
  302. description = "Validator for an array of values which will be set in a string, separated by ','",
  303. separator = ',')
  304. SettingValidator.create_re_validator(
  305. r'^https?://[^\./]+.[^\./]+/?.*$',
  306. 'http_url',
  307. 'Url validator')
  308. #
  309. # Lodel 2 configuration specification
  310. #
  311. ##@brief Append a piece of confspec
  312. #@note orig is modified during the process
  313. #@param orig dict : the confspec to update
  314. #@param upd dict : the confspec to add
  315. #@return new confspec
  316. def confspec_append(orig, section, key, validator, default):
  317. if section not in orig:
  318. orig[section] = dict()
  319. if key not in orig[section]:
  320. orig[section][key] = (default, validator)
  321. return orig
  322. ##@brief Global specifications for lodel2 settings
  323. LODEL2_CONF_SPECS = {
  324. 'lodel2': {
  325. 'debug': ( True,
  326. SettingValidator('bool')),
  327. 'sitename': ( 'noname',
  328. SettingValidator('strip')),
  329. 'runtest': ( False,
  330. SettingValidator('bool')),
  331. },
  332. 'lodel2.logging.*' : {
  333. 'level': ( 'ERROR',
  334. SettingValidator('loglevel')),
  335. 'context': ( False,
  336. SettingValidator('bool')),
  337. 'filename': ( None,
  338. SettingValidator('errfile', none_is_valid = True)),
  339. 'backupcount': ( None,
  340. SettingValidator('int', none_is_valid = True)),
  341. 'maxbytes': ( None,
  342. SettingValidator('int', none_is_valid = True)),
  343. },
  344. 'lodel2.editorialmodel': {
  345. 'emfile': ( 'em.pickle', SettingValidator('strip')),
  346. 'emtranslator': ( 'picklefile', SettingValidator('strip')),
  347. 'dyncode': ( 'leapi_dyncode.py', SettingValidator('strip')),
  348. 'groups': ( '', SettingValidator('list')),
  349. 'editormode': ( False, SettingValidator('bool')),
  350. },
  351. 'lodel2.datasources.*': {
  352. 'read_only': (False, SettingValidator('bool')),
  353. 'identifier': ( None, SettingValidator('string')),
  354. },
  355. 'lodel2.auth': {
  356. 'login_classfield': ('user.login', SettingValidator('emfield')),
  357. 'pass_classfield': ('user.password', SettingValidator('emfield')),
  358. },
  359. }