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.

settings.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.settings.validator': ['SettingValidator', '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("Only lower case are allowed in section name (thank's ConfigParser...)")
  152. for kname in lodel2_specs[section]:
  153. if kname.lower() != kname:
  154. raise SettingsError("Only lower case are allowed in section name (thank's ConfigParser...)")
  155. # Starting the Plugins class
  156. logger.debug("Starting lodel.plugin.Plugin class")
  157. Plugin.start(plugin_list)
  158. # Fetching conf specs from plugins
  159. specs = [lodel2_specs]
  160. errors = list()
  161. for plugin_name in plugin_list:
  162. try:
  163. specs.append(Plugin.get(plugin_name).confspecs)
  164. except PluginError as e:
  165. errors.append(SettingsError(msg=str(e)))
  166. if len(errors) > 0: #Raise all plugins import errors
  167. raise SettingsErrors(errors)
  168. self.__conf_specs = self.__merge_specs(specs)
  169. self.__populate_from_specs(self.__conf_specs, loader)
  170. self.__started = True
  171. ##@brief Produce a configuration specification dict by merging all specifications
  172. #
  173. # Merges global lodel2 conf spec from @ref lodel.settings.validator.LODEL2_CONF_SPECS
  174. # and configuration specifications from loaded plugins
  175. # @param specs list : list of specifications dict
  176. # @return a specification dict
  177. def __merge_specs(self, specs):
  178. res = copy.copy(specs.pop())
  179. for spec in specs:
  180. for section in spec:
  181. if section.lower() != section:
  182. raise SettingsError("Only lower case are allowed in section name (thank's ConfigParser...)")
  183. if section not in res:
  184. res[section] = dict()
  185. for kname in spec[section]:
  186. if kname.lower() != kname:
  187. raise SettingsError("Only lower case are allowed in section name (thank's ConfigParser...)")
  188. if kname in res[section]:
  189. raise SettingsError("Duplicated key '%s' in section '%s'" % (kname, section))
  190. res[section.lower()][kname] = copy.copy(spec[section][kname])
  191. return res
  192. ##@brief Populate the Settings instance with options values fetched with the loader from merged specs
  193. #
  194. # Populate the __confs attribute
  195. # @param specs dict : Settings specification dictionnary as returned by __merge_specs
  196. # @param loader SettingsLoader : A SettingsLoader instance
  197. def __populate_from_specs(self, specs, loader):
  198. self.__confs = dict()
  199. specs = copy.copy(specs) #Avoid destroying original specs dict (may be useless)
  200. # Construct final specs dict replacing variable sections
  201. # by the actual existing sections
  202. variable_sections = [ section for section in specs if section.endswith('.*') ]
  203. for vsec in variable_sections:
  204. preffix = vsec[:-2]
  205. for section in loader.getsection(preffix, 'default'): #WARNING : hardcoded default section
  206. specs[section] = copy.copy(specs[vsec])
  207. del(specs[vsec])
  208. # Fetching values for sections
  209. for section in specs:
  210. for kname in specs[section]:
  211. validator = specs[section][kname][1]
  212. default = specs[section][kname][0]
  213. if section not in self.__confs:
  214. self.__confs[section] = dict()
  215. self.__confs[section][kname] = loader.getoption(section, kname, validator, default)
  216. # Checking unfectched values
  217. loader.raise_errors()
  218. self.__confs_to_namedtuple()
  219. pass
  220. ##@brief Transform the __confs attribute into imbricated namedtuple
  221. #
  222. # For example an option named "foo" in a section named "hello.world" will
  223. # be acessible with self.__confs.hello.world.foo
  224. def __confs_to_namedtuple(self):
  225. res = None
  226. end = False
  227. splits = list()
  228. for section in self.__confs:
  229. splits.append(section.split('.'))
  230. max_len = max([len(spl) for spl in splits])
  231. # building a tree from sections splits
  232. section_tree = dict()
  233. for spl in splits:
  234. section_name = ""
  235. cur = section_tree
  236. for sec_part in spl:
  237. section_name += sec_part+'.'
  238. if sec_part not in cur:
  239. cur[sec_part] = dict()
  240. cur = cur[sec_part]
  241. section_name = section_name[:-1]
  242. for kname, kval in self.__confs[section_name].items():
  243. if kname in cur:
  244. raise SettingsError("Duplicated key for '%s.%s'" % (section_name, kname))
  245. cur[kname] = kval
  246. path = [ ('root', section_tree) ]
  247. visited = set()
  248. curname = 'root'
  249. nodename = 'Lodel2Settings'
  250. cur = section_tree
  251. while True:
  252. visited.add(nodename)
  253. left = [ (kname, cur[kname])
  254. for kname in cur
  255. if nodename+'.'+kname.title() not in visited and isinstance(cur[kname], dict)
  256. ]
  257. if len(left) == 0:
  258. name, leaf = path.pop()
  259. typename = nodename.replace('.', '')
  260. if len(path) == 0:
  261. # END
  262. self.__confs = self.__tree2namedtuple(leaf,typename)
  263. break
  264. else:
  265. path[-1][1][name] = self.__tree2namedtuple(leaf,typename)
  266. nodename = '.'.join(nodename.split('.')[:-1])
  267. cur = path[-1][1]
  268. else:
  269. curname, cur = left[0]
  270. path.append( (curname, cur) )
  271. nodename += '.' + curname.title()
  272. ##@brief Forge a named tuple given a conftree node
  273. # @param conftree dict : A conftree node
  274. # @param name str
  275. # @return a named tuple with fieldnames corresponding to conftree keys
  276. def __tree2namedtuple(self, conftree, name):
  277. ResNamedTuple = namedtuple(name, conftree.keys())
  278. return ResNamedTuple(**conftree)
  279. class MetaSettingsRO(type):
  280. def __getattr__(self, name):
  281. return getattr(Settings.s, name)
  282. ## @brief A class that provide . notation read only access to configurations
  283. class SettingsRO(object, metaclass=MetaSettingsRO):
  284. pass