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 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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=set()
  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 = glob.glob(self.__conf_path+'/*.ini')
  24. for f in l:
  25. config = configparser.ConfigParser(default_section = 'lodel2')
  26. config.read(f)
  27. for s in config:
  28. if s in conf:
  29. for vs in config[s]:
  30. if vs not in conf[s]:
  31. conf[s][vs] = config[s][vs]
  32. if s != 'DEFAULT': self.__conf_sv.add(s + ':' + vs)
  33. else:
  34. raise SettingsError("Key attribute already define : %s" % s + ' '+vs)
  35. else:
  36. opts={}
  37. for key in config[s]:
  38. opts[key] = config[s].get(key)
  39. if s != 'DEFAULT': self.__conf_sv.add(s + ':' + key)
  40. conf.update({s: opts})
  41. os.close(dir_conf)
  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=copy.copy(self.__conf)
  52. sec=conf[section]
  53. if keyname in sec:
  54. optionstr=sec[keyname]
  55. option=validator(sec[keyname])
  56. self.__conf_sv.remove(section + ':' + keyname)
  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. if section_prefix in conf:
  69. return conf[section_prefix]
  70. elif default_section in conf:
  71. return conf[default_section]
  72. return [];
  73. ## @brief Returns the sections which have not been configured
  74. # @return list of missing options
  75. def getremains(self):
  76. return list(self.__conf_sv)