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

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