Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

settings.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. import configparser
  5. import copy
  6. import warnings
  7. import types # for dynamic bindings
  8. from collections import namedtuple
  9. from lodel.context import LodelContext
  10. LodelContext.expose_modules(globals(), {
  11. 'lodel.logger': 'logger',
  12. 'lodel.settings.utils': ['SettingsError', 'SettingsErrors'],
  13. 'lodel.validator.validator': ['Validator', 'LODEL2_CONF_SPECS',
  14. 'confspec_append'],
  15. 'lodel.settings.settings_loader': ['SettingsLoader']})
  16. #  @package lodel.settings.settings Lodel2 settings module
  17. #
  18. # Contains the class that handles the namedtuple tree of settings
  19. # @brief A default python system lib path
  20. PYTHON_SYS_LIB_PATH = '/usr/local/lib/python{major}.{minor}/'.format(
  21. major=sys.version_info.major,
  22. minor=sys.version_info.minor)
  23. class MetaSettings(type):
  24. @property
  25. def s(self):
  26. self.singleton_assert(True)
  27. return self.instance.settings
  28. # @brief Handles configuration load etc.
  29. #
  30. # To see howto bootstrap Settings and use it in lodel instance see
  31. # @ref lodel.settings
  32. #
  33. # @par Basic instance usage
  34. # For example if a file defines confs like :
  35. # <pre>
  36. # [super_section]
  37. # super_conf = super_value
  38. # </pre>
  39. # You can access it with :
  40. # <pre> settings_instance.confs.super_section.super_conf </pre>
  41. #
  42. # @par Init sequence
  43. # The initialization sequence is a bit tricky. In fact, plugins adds allowed
  44. # configuration sections/values, but the list of plugins to load are in... the
  45. # settings.
  46. # Here is the conceptual presentation of Settings class initialization stages :
  47. # -# Preloading (sets values like lodel2 library path or the plugins path)
  48. # -# Ask a @ref lodel.settings.setting_loader.SettingsLoader to load all
  49. # configurations files
  50. # -# Fetch the list of plugins in the loaded settings
  51. # -# Merge plugins settings specification with the global lodel settings
  52. # specs ( see @ref lodel.plugin )
  53. # -# Fetch all settings from the merged settings specs
  54. #
  55. # @par Init sequence in practical
  56. # In practice those steps are done by calling a succession of private methods :
  57. # -# @ref Settings.__bootstrap() ( steps 1 to 3 )
  58. # -# @ref Settings.__merge_specs() ( step 4 )
  59. # -# @ref Settings.__populate_from_specs() (step 5)
  60. # -# And finally @ref Settings.__confs_to_namedtuple()
  61. #
  62. # @todo handles default sections for variable sections (sections ending with
  63. # '.*')
  64. # @todo delete the first stage, the lib path HAVE TO BE HARDCODED. In fact
  65. # when we will run lodel in production the lodel2 lib will be in the python path
  66. #@todo add log messages (now we can)
  67. class Settings(object, metaclass=MetaSettings):
  68. #  @brief Stores the singleton instance
  69. instance = None
  70. #  @brief Instanciate the Settings singleton
  71. # @param conf_dir str : The configuration directory
  72. #@param custom_confspecs None | dict : if given overwrite default lodel2
  73. # confspecs
  74. def __init__(self, conf_dir, custom_confspecs=None):
  75. self.singleton_assert() # check that it is the only instance
  76. Settings.instance = self
  77. #  @brief Configuration specification
  78. #
  79. # Initialized by Settings.__bootstrap() method
  80. self.__conf_specs = custom_confspecs
  81. #  @brief Stores the configurations in namedtuple tree
  82. self.__confs = None
  83. self.__conf_dir = conf_dir
  84. self.__started = False
  85. self.__bootstrap()
  86. #  @brief Get the named tuple representing configuration
  87. @property
  88. def settings(self):
  89. return self.__confs.lodel2
  90. #  @brief Delete the singleton instance
  91. @classmethod
  92. def stop(cls):
  93. del(cls.instance)
  94. cls.instance = None
  95. @classmethod
  96. def started(cls):
  97. return cls.instance is not None and cls.instance.__started
  98. # @brief An utility method that raises if the singleton is not in a good
  99. # state
  100. #@param expect_instanciated bool : if True we expect that the class is
  101. # allready instanciated, else not
  102. # @throw RuntimeError
  103. @classmethod
  104. def singleton_assert(cls, expect_instanciated=False):
  105. if expect_instanciated:
  106. if not cls.started():
  107. raise RuntimeError("The Settings class is not started yet")
  108. else:
  109. if cls.started():
  110. raise RuntimeError("The Settings class is already started")
  111. # @brief Saves a new configuration for section confname
  112. #@param confname is the name of the modified section
  113. #@param confvalue is a dict with variables to save
  114. #@param validator is a dict with adapted validator
  115. @classmethod
  116. def set(cls, confname, confvalue, validator):
  117. loader = SettingsLoader(cls.instance.__conf_dir)
  118. confkey = confname.rpartition('.')
  119. loader.setoption(confkey[0], confkey[2], confvalue, validator)
  120. # @brief This method handles Settings instance bootstraping
  121. def __bootstrap(self):
  122. LodelContext.expose_modules(globals(), {
  123. 'lodel.plugin.plugins': ['Plugin', 'PluginError']})
  124. logger.debug("Settings bootstraping")
  125. if self.__conf_specs is None:
  126. lodel2_specs = LODEL2_CONF_SPECS
  127. else:
  128. lodel2_specs = self.__conf_specs
  129. self.__conf_specs = None
  130. loader = SettingsLoader(self.__conf_dir)
  131. plugin_list = []
  132. for ptype_name, ptype in Plugin.plugin_types().items():
  133. pls = ptype.plist_confspecs()
  134. lodel2_specs = confspec_append(lodel2_specs, **pls)
  135. cur_list = loader.getoption(
  136. pls['section'],
  137. pls['key'],
  138. pls['validator'],
  139. pls['default'])
  140. if cur_list is None:
  141. continue
  142. try:
  143. if isinstance(cur_list, str):
  144. cur_list = [cur_list]
  145. plugin_list += cur_list
  146. except TypeError:
  147. plugin_list += [cur_list]
  148. # Checking confspecs
  149. for section in lodel2_specs:
  150. if section.lower() != section:
  151. raise SettingsError(
  152. "Only lower case are allowed in section name (thank's ConfigParser...)")
  153. for kname in lodel2_specs[section]:
  154. if kname.lower() != kname:
  155. raise SettingsError(
  156. "Only lower case are allowed in section name (thank's ConfigParser...)")
  157. # Starting the Plugins class
  158. logger.debug("Starting lodel.plugin.Plugin class")
  159. Plugin.start(plugin_list)
  160. # Fetching conf specs from plugins
  161. specs = [lodel2_specs]
  162. errors = list()
  163. for plugin_name in plugin_list:
  164. try:
  165. specs.append(Plugin.get(plugin_name).confspecs)
  166. except PluginError as e:
  167. errors.append(SettingsError(msg=str(e)))
  168. if len(errors) > 0: # Raise all plugins import errors
  169. raise SettingsErrors(errors)
  170. self.__conf_specs = self.__merge_specs(specs)
  171. self.__populate_from_specs(self.__conf_specs, loader)
  172. self.__started = True
  173. # @brief Produce a configuration specification dict by merging all specifications
  174. #
  175. # Merges global lodel2 conf spec from @ref lodel.settings.validator.LODEL2_CONF_SPECS
  176. # and configuration specifications from loaded plugins
  177. # @param specs list : list of specifications dict
  178. # @return a specification dict
  179. def __merge_specs(self, specs):
  180. res = copy.copy(specs.pop())
  181. for spec in specs:
  182. for section in spec:
  183. if section.lower() != section:
  184. raise SettingsError(
  185. "Only lower case are allowed in section name (thank's ConfigParser...)")
  186. if section not in res:
  187. res[section] = dict()
  188. for kname in spec[section]:
  189. if kname.lower() != kname:
  190. raise SettingsError(
  191. "Only lower case are allowed in section name (thank's ConfigParser...)")
  192. if kname in res[section]:
  193. raise SettingsError("Duplicated key '%s' in section '%s'" %
  194. (kname, section))
  195. res[section.lower()][kname] = copy.copy(spec[section][kname])
  196. return res
  197. # @brief Populate the Settings instance with options values fetched with the loader from merged specs
  198. #
  199. # Populate the __confs attribute
  200. # @param specs dict : Settings specification dictionnary as returned by __merge_specs
  201. # @param loader SettingsLoader : A SettingsLoader instance
  202. def __populate_from_specs(self, specs, loader):
  203. self.__confs = dict()
  204. specs = copy.copy(specs) # Avoid destroying original specs dict (may be useless)
  205. # Construct final specs dict replacing variable sections
  206. # by the actual existing sections
  207. variable_sections = [section for section in specs if section.endswith('.*')]
  208. for vsec in variable_sections:
  209. preffix = vsec[:-2]
  210. # WARNING : hardcoded default section
  211. for section in loader.getsection(preffix, 'default'):
  212. specs[section] = copy.copy(specs[vsec])
  213. del(specs[vsec])
  214. # Fetching values for sections
  215. for section in specs:
  216. for kname in specs[section]:
  217. validator = specs[section][kname][1]
  218. default = specs[section][kname][0]
  219. if section not in self.__confs:
  220. self.__confs[section] = dict()
  221. self.__confs[section][kname] = loader.getoption(section, kname, validator, default)
  222. # Checking unfectched values
  223. loader.raise_errors()
  224. self.__confs_to_namedtuple()
  225. pass
  226. # @brief Transform the __confs attribute into imbricated namedtuple
  227. #
  228. # For example an option named "foo" in a section named "hello.world" will
  229. # be acessible with self.__confs.hello.world.foo
  230. def __confs_to_namedtuple(self):
  231. res = None
  232. end = False
  233. splits = list()
  234. for section in self.__confs:
  235. splits.append(section.split('.'))
  236. max_len = max([len(spl) for spl in splits])
  237. # building a tree from sections splits
  238. section_tree = dict()
  239. for spl in splits:
  240. section_name = ""
  241. cur = section_tree
  242. for sec_part in spl:
  243. section_name += sec_part + '.'
  244. if sec_part not in cur:
  245. cur[sec_part] = dict()
  246. cur = cur[sec_part]
  247. section_name = section_name[:-1]
  248. for kname, kval in self.__confs[section_name].items():
  249. if kname in cur:
  250. raise SettingsError("Duplicated key for '%s.%s'" % (section_name, kname))
  251. cur[kname] = kval
  252. path = [('root', section_tree)]
  253. visited = set()
  254. curname = 'root'
  255. nodename = 'Lodel2Settings'
  256. cur = section_tree
  257. while True:
  258. visited.add(nodename)
  259. left = [(kname, cur[kname])
  260. for kname in cur
  261. if nodename + '.' + kname.title() not in visited and isinstance(cur[kname], dict)
  262. ]
  263. if len(left) == 0:
  264. name, leaf = path.pop()
  265. typename = nodename.replace('.', '')
  266. if len(path) == 0:
  267. # END
  268. self.__confs = self.__tree2namedtuple(leaf, typename)
  269. break
  270. else:
  271. path[-1][1][name] = self.__tree2namedtuple(leaf, typename)
  272. nodename = '.'.join(nodename.split('.')[:-1])
  273. cur = path[-1][1]
  274. else:
  275. curname, cur = left[0]
  276. path.append((curname, cur))
  277. nodename += '.' + curname.title()
  278. # @brief Forge a named tuple given a conftree node
  279. # @param conftree dict : A conftree node
  280. # @param name str
  281. # @return a named tuple with fieldnames corresponding to conftree keys
  282. def __tree2namedtuple(self, conftree, name):
  283. ResNamedTuple = namedtuple(name, conftree.keys())
  284. return ResNamedTuple(**conftree)
  285. class MetaSettingsRO(type):
  286. def __getattr__(self, name):
  287. return getattr(Settings.s, name)
  288. #  @brief A class that provide . notation read only access to configurations
  289. class SettingsRO(object, metaclass=MetaSettingsRO):
  290. pass