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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ##@brief Constructor
  10. # @param conf_path str : conf.d path
  11. def __init__(self,conf_path):
  12. self.__conf_path=conf_path
  13. self.__conf_sv=dict()
  14. self.__conf=self.__merge()
  15. ##@brief Lists and merges files in settings_loader.conf_path
  16. #
  17. #
  18. # @return dict()
  19. #
  20. def __merge(self):
  21. conf = dict()
  22. dir_conf = os.open(self.__conf_path, os.O_RDONLY)
  23. l_dir = glob.glob(self.__conf_path+'/*.ini')
  24. for f_ini in l_dir:
  25. #To avoid the DEFAULT section whose values are found in all sections, we have to give it an unsual name
  26. config = configparser.ConfigParser(default_section = 'lodel2_default_passaway_tip',interpolation=None)
  27. config.read(f_ini)
  28. for sect in config:
  29. if sect in conf:
  30. for param in config[sect]:
  31. if param not in conf[sect]:
  32. conf[sect][param] = config[sect][param]
  33. if sect != 'DEFAULT': self.__conf_sv[sect + ':' + param]=f_ini
  34. else:
  35. raise SettingsError("Key attribute already defined : %s " % sect + '.' + param + ' dans ' + f_ini + ' et ' + self.__conf_sv[sect + ':' + param])
  36. else:
  37. opts={}
  38. for key in config[sect]:
  39. opts[key] = config[sect].get(key)
  40. if sect != 'lodel2_default_passaway_tip': self.__conf_sv[sect + ':' + key]=f_ini
  41. conf.update({sect: opts})
  42. os.close(dir_conf)
  43. return conf
  44. ##@brief Returns option if exists default_value else and validates
  45. # @param section str : name of the section
  46. # @param keyname str
  47. # @param validator callable : takes one argument value and raises validation fail
  48. # @param default_value *
  49. # @param mandatory bool
  50. # @return the option
  51. def getoption(self,section,keyname,validator,default_value=None,mandatory=False):
  52. conf=copy.copy(self.__conf)
  53. sec=conf[section]
  54. if keyname in sec:
  55. optionstr=sec[keyname]
  56. option=validator(sec[keyname])
  57. del self.__conf_sv[section + ':' + keyname]
  58. return option
  59. elif mandatory:
  60. raise SettingsError("Default value mandatory for option %s" % keyname)
  61. else:
  62. return default_value
  63. ##@brief Returns the section to be configured
  64. # @param section_prefix str
  65. # @param default_section str
  66. # @return the section as dict()
  67. def getsection(self,section_prefix,default_section=None):
  68. conf=copy.copy(self.__conf)
  69. sections=[]
  70. if section_prefix in conf:
  71. sections.append(section_prefix)
  72. for sect_names in conf:
  73. if sect_names in sections:
  74. pass
  75. elif sect_names.startswith(section_prefix + '.'):
  76. sections.append(sect_names)
  77. if sections == [] and default_section:
  78. sections.append(section_prefix + '.' + default_section)
  79. elif sections == []:
  80. raise NameError("Not existing settings section : %s" % section__prefix)
  81. return sections;
  82. ##@brief Returns the sections which have not been configured
  83. # @return list of missing options
  84. def getremains(self):
  85. return list(self.__conf_sv)