暫無描述
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.

settings.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. import configparser
  5. from lodel.plugins import Plugins
  6. from lodel.settings.utils import SettingsError, SettingsErrors
  7. from lodel.settings.validator import SettingValidator
  8. from lodel.settings.settings_loader import SettingsLoader
  9. ## @package lodel.settings Lodel2 settings package
  10. #
  11. # Contains all module that help handling settings
  12. ## @package lodel.settings.settings Lodel2 settings module
  13. #
  14. # Handles configuration load/parse/check.
  15. #
  16. # @subsection Configuration load process
  17. #
  18. # The configuration load process is not trivial. In fact loaded plugins are able to add their own options.
  19. # But the list of plugins to load and the plugins options are in the same file, the instance configuration file.
  20. #
  21. # @subsection Configuration specification
  22. #
  23. # Configuration specification is divided in 2 parts :
  24. # - default values
  25. # - value validation/cast (see @ref Lodel.settings.validator.ConfValidator )
  26. PYTHON_SYS_LIB_PATH = '/usr/local/lib/python{major}.{minor}/'.format(
  27. major = sys.version_info.major,
  28. minor = sys.version_info.minor)
  29. ## @brief Handles configuration load etc.
  30. class Settings(object):
  31. ## @brief global conf specsification (default_value + validator)
  32. _conf_preload = {
  33. 'lib_path': ( PYTHON_SYS_LIB_PATH+'/lodel2/',
  34. SettingValidator('directory')),
  35. 'plugins_path': ( PYTHON_SYS_LIB_PATH+'lodel2/plugins/',
  36. SettingValidator('directory_list')),
  37. }
  38. def __init__(self, conf_file = '/etc/lodel2/lodel2.conf', conf_dir = 'conf.d'):
  39. self.__confs = dict()
  40. self.__load_bootstrap_conf(conf_file)
  41. # now we should have the self.__confs['lodel2']['plugins_paths'] and
  42. # self.__confs['lodel2']['lib_path'] set
  43. self.__bootstrap()
  44. ## @brief This method handlers Settings instance bootstraping
  45. def __bootstrap(self):
  46. #loader = SettingsLoader(self.__conf_dir)
  47. # Starting the Plugins class
  48. Plugins.bootstrap(self.__confs['lodel2']['plugins_path'])
  49. specs = Plugins.get_confspec('dummy')
  50. print("Got specs : %s " % specs)
  51. # then fetch options values from conf specs
  52. ## @brief Load base global configurations keys
  53. #
  54. # Base configurations keys are :
  55. # - lodel2 lib path
  56. # - lodel2 plugins path
  57. #
  58. # @note return nothing but set the __confs attribute
  59. # @see Settings._conf_preload
  60. def __load_bootstrap_conf(self, conf_file):
  61. config = configparser.ConfigParser()
  62. config.read(conf_file)
  63. sections = config.sections()
  64. if len(sections) != 1 or sections[0].lower() != 'lodel2':
  65. raise SettingsError("Global conf error, expected lodel2 section not found")
  66. #Load default values in result
  67. res = dict()
  68. for keyname, (keyvalue, validator) in self._conf_preload.items():
  69. res[keyname] = keyvalue
  70. confs = config[sections[0]]
  71. errors = []
  72. for name in confs:
  73. if name not in res:
  74. errors.append( SettingsError(
  75. "Unknow field",
  76. "lodel2.%s" % name,
  77. conf_file))
  78. try:
  79. res[name] = self._conf_preload[name][1](confs[name])
  80. except Exception as e:
  81. errors.append(SettingsError(str(e), name, conf_file))
  82. if len(errors) > 0:
  83. raise SettingsErrors(errors)
  84. self.__confs['lodel2'] = res