Ingen beskrivning
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_loader.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #-*- coding: utf-8 -*-
  2. import configparser
  3. import os
  4. import glob
  5. import copy
  6. from lodel.settings.utils import *
  7. from lodel.settings.validator import SettingsValidationError
  8. ##@brief Merges and loads configuration files
  9. class SettingsLoader(object):
  10. ## To avoid the DEFAULT section whose values are found in all sections, we
  11. # have to give it an unsual name
  12. DEFAULT_SECTION = 'lodel2_default_passaway_tip'
  13. ##@brief Constructor
  14. # @param conf_path str : conf.d path
  15. def __init__(self,conf_path):
  16. self.__conf_path=conf_path
  17. self.__conf_sv=dict()
  18. self.__conf=self.__merge()
  19. ##@brief Lists and merges files in settings_loader.conf_path
  20. # @return dict()
  21. def __merge(self):
  22. conf = dict()
  23. l_dir = glob.glob(self.__conf_path+'/*.ini')
  24. for f_ini in l_dir:
  25. config = configparser.ConfigParser(default_section = self.DEFAULT_SECTION ,interpolation=None)
  26. config.read(f_ini)
  27. for section in [ s for s in config if s != self.DEFAULT_SECTION ]:
  28. if section not in conf:
  29. conf[section] = dict()
  30. for param in config[section]:
  31. if param not in conf[section]:
  32. conf[section][param]=dict()
  33. conf[section][param]['value'] = config[section][param]
  34. conf[section][param]['file'] = f_ini
  35. self.__conf_sv[section + ':' + param]=f_ini
  36. else:
  37. raise SettingsError("Error redeclaration of key %s in section %s. Found in %s and %s" % (
  38. section,
  39. param,
  40. f_ini,
  41. conf[section][param]['file']))
  42. return conf
  43. ##@brief Returns option if exists default_value else and validates
  44. # @param section str : name of the section
  45. # @param keyname str
  46. # @param validator callable : takes one argument value and raises validation fail
  47. # @param default_value *
  48. # @param mandatory bool
  49. # @return the option
  50. def getoption(self,section,keyname,validator,default_value=None,mandatory=False):
  51. conf=self.__conf
  52. if section in conf:
  53. sec=conf[section]
  54. if keyname in sec:
  55. optionstr=sec[keyname]['value']
  56. try:
  57. option= validator(sec[keyname]['value'])
  58. except Exception as e:
  59. raise SettingsValidationError(
  60. "For %s.%s : %s" %
  61. (section, keyname,e)
  62. )
  63. try:
  64. del self.__conf_sv[section + ':' + keyname]
  65. except KeyError: #allready fetched
  66. pass
  67. return option
  68. elif default_value is None and mandatory:
  69. raise SettingsError("Default value mandatory for option %s" % keyname)
  70. sec[keyname]=dict()
  71. sec[keyname]['value'] = default_value
  72. sec[keyname]['file'] = 'default_value'
  73. return default_value
  74. else:
  75. conf[section]=dict()
  76. conf[section][keyname]=dict()
  77. conf[section][keyname]['value'] = default_value
  78. conf[section][keyname]['file'] = 'default_value'
  79. return default_value
  80. ##@brief Sets option in a config section. Writes in the conf file
  81. # @param section str : name of the section
  82. # @param keyname str
  83. # @param value str
  84. # @param validator callable : takes one argument value and raises validation fail
  85. # @return the option
  86. def setoption(self,section,keyname,value,validator):
  87. f_conf=copy.copy(self.__conf[section][keyname]['file'])
  88. if f_conf == 'default_value':
  89. f_conf = self.__conf_path + '/generated.ini'
  90. conf=self.__conf
  91. conf[section][keyname] = value
  92. config = configparser.ConfigParser()
  93. config.read(f_conf)
  94. if section not in config:
  95. config[section]={}
  96. config[section][keyname] = validator(value)
  97. with open(f_conf, 'w') as configfile:
  98. config.write(configfile)
  99. ##@brief Saves new partial configuration. Writes in the conf files corresponding
  100. # @param sections dict
  101. # @param validators dict of callable : takes one argument value and raises validation fail
  102. def saveconf(self, sections, validators):
  103. for sec in sections:
  104. for kname in sections[sec]:
  105. self.setoption(sec,kname,sections[sec][kname],validators[sec][kname])
  106. ##@brief Returns the section to be configured
  107. # @param section_prefix str
  108. # @param default_section str
  109. # @return the section as dict()
  110. def getsection(self,section_prefix,default_section=None):
  111. conf=copy.copy(self.__conf)
  112. sections=[]
  113. if section_prefix in conf:
  114. sections.append(section_prefix)
  115. for sect_names in conf:
  116. if sect_names in sections:
  117. pass
  118. elif sect_names.startswith(section_prefix + '.'):
  119. sections.append(sect_names)
  120. if sections == [] and default_section:
  121. sections.append(section_prefix + '.' + default_section)
  122. elif sections == []:
  123. raise NameError("Not existing settings section : %s" % section__prefix)
  124. return sections;
  125. ## @brief Returns invalid settings
  126. #
  127. # This method returns all the settings that was not fecthed by
  128. # getsection() method. For the Settings object it allows to know
  129. # the list of invalids settings keys
  130. # @return a dict with SECTION_NAME+":"+KEY_NAME as key and the filename
  131. # where the settings was found as value
  132. def getremains(self):
  133. return self.__conf_sv