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_loader.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #-*- coding: utf-8 -*-
  2. import configparser
  3. import os
  4. import glob
  5. import copy
  6. from lodel.settings.utils import *
  7. ##@brief Merges and loads configuration files
  8. class SettingsLoader(object):
  9. ## To avoid the DEFAULT section whose values are found in all sections, we
  10. # have to give it an unsual name
  11. DEFAULT_SECTION = 'lodel2_default_passaway_tip'
  12. ##@brief Constructor
  13. # @param conf_path str : conf.d path
  14. def __init__(self,conf_path):
  15. self.__conf_path=conf_path
  16. self.__conf_sv=dict()
  17. self.__conf=self.__merge()
  18. ##@brief Lists and merges files in settings_loader.conf_path
  19. # @return dict()
  20. def __merge(self):
  21. conf = dict()
  22. l_dir = glob.glob(self.__conf_path+'/*.ini')
  23. for f_ini in l_dir:
  24. config = configparser.ConfigParser(default_section = self.DEFAULT_SECTION ,interpolation=None)
  25. config.read(f_ini)
  26. for section in [ s for s in config if s != self.DEFAULT_SECTION ]:
  27. if section not in conf:
  28. conf[section] = dict()
  29. for param in config[section]:
  30. if param not in conf[section]:
  31. conf[section][param] = config[section][param]
  32. self.__conf_sv[section + ':' + param]=f_ini
  33. else:
  34. raise SettingsError("Error redeclaration of key %s in section %s. Found in %s and %s" % (
  35. section,
  36. param,
  37. f_ini,
  38. self.__conf_sv[section + ':' + param]))
  39. return conf
  40. ##@brief Returns option if exists default_value else and validates
  41. # @param section str : name of the section
  42. # @param keyname str
  43. # @param validator callable : takes one argument value and raises validation fail
  44. # @param default_value *
  45. # @param mandatory bool
  46. # @return the option
  47. def getoption(self,section,keyname,validator,default_value=None,mandatory=False):
  48. conf=copy.copy(self.__conf)
  49. sec=conf[section]
  50. if keyname in sec:
  51. optionstr=sec[keyname]
  52. option=validator(sec[keyname])
  53. try:
  54. del self.__conf_sv[section + ':' + keyname]
  55. except KeyError: #allready fetched
  56. pass
  57. return option
  58. elif mandatory:
  59. raise SettingsError("Default value mandatory for option %s" % keyname)
  60. else:
  61. return default_value
  62. ##@brief Returns the section to be configured
  63. # @param section_prefix str
  64. # @param default_section str
  65. # @return the section as dict()
  66. def getsection(self,section_prefix,default_section=None):
  67. conf=copy.copy(self.__conf)
  68. sections=[]
  69. if section_prefix in conf:
  70. sections.append(section_prefix)
  71. for sect_names in conf:
  72. if sect_names in sections:
  73. pass
  74. elif sect_names.startswith(section_prefix + '.'):
  75. sections.append(sect_names)
  76. if sections == [] and default_section:
  77. sections.append(section_prefix + '.' + default_section)
  78. elif sections == []:
  79. raise NameError("Not existing settings section : %s" % section__prefix)
  80. return sections;
  81. ## @brief Returns invalid settings
  82. #
  83. # This method returns all the settings that was not fecthed by
  84. # getsection() method. For the Settings object it allows to know
  85. # the list of invalids settings keys
  86. # @return a dict with SECTION_NAME+":"+KEY_NAME as key and the filename
  87. # where the settings was found as value
  88. def getremains(self):
  89. return self.__conf_sv