|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import ConfigParser |
| 3 | +import os |
| 4 | + |
| 5 | +__version__ = '0.0.1' |
| 6 | + |
| 7 | + |
| 8 | +class Configuration(): |
| 9 | + |
| 10 | + def __init__(self, name, path, parse=True, confd_path=None, conf_ext=None, main_defaults={}, section_defaults={}, main_parser=None, section_parser=None): |
| 11 | + self._conf_ext = conf_ext |
| 12 | + self._config_sections = {} |
| 13 | + self._confd_path = confd_path |
| 14 | + self._main_config = {} |
| 15 | + self._main_defaults = main_defaults |
| 16 | + self._main_parser = main_parser |
| 17 | + self._name = name |
| 18 | + self._path = path |
| 19 | + self._section_defaults = section_defaults |
| 20 | + self._section_parser = section_parser |
| 21 | + |
| 22 | + if self._conf_ext: |
| 23 | + self._conf_ext = '.' + conf_ext.strip(".") |
| 24 | + |
| 25 | + if parse: |
| 26 | + self.parse() |
| 27 | + |
| 28 | + def get(self, section, key=None, default=None): |
| 29 | + if section == self._name: |
| 30 | + if key is None: |
| 31 | + return self._main_config |
| 32 | + return self._main_config.get(key, default) |
| 33 | + |
| 34 | + if section in self._config_sections: |
| 35 | + if key is None: |
| 36 | + return self._config_sections[section] |
| 37 | + return self._config_sections[section].get(key, default) |
| 38 | + |
| 39 | + raise KeyError("Invalid section") |
| 40 | + |
| 41 | + def has(self, section, key=None): |
| 42 | + if section in self._config_sections: |
| 43 | + if key is None: |
| 44 | + return True |
| 45 | + |
| 46 | + return key in self._config_sections[section] |
| 47 | + |
| 48 | + if section == self._name: |
| 49 | + if key is None: |
| 50 | + return True |
| 51 | + |
| 52 | + return key in self._main_config |
| 53 | + |
| 54 | + return False |
| 55 | + |
| 56 | + def raw(self, section=None): |
| 57 | + if section: |
| 58 | + if not self.has(section): |
| 59 | + raise KeyError("Invalid section") |
| 60 | + |
| 61 | + if section == self._name: |
| 62 | + return self._main_config |
| 63 | + |
| 64 | + return self._config_sections[section] |
| 65 | + |
| 66 | + return { |
| 67 | + self._name: self._main_config, |
| 68 | + 'sections': self._config_sections |
| 69 | + } |
| 70 | + |
| 71 | + def parse(self): |
| 72 | + configs = self._parse_section(path=self._path, defaults=self._main_defaults, parser=self._main_parser, only_section=self._name) |
| 73 | + self._main_config = configs[self._name] |
| 74 | + |
| 75 | + self._config_sections = self._parse_section(path=self._path, defaults=self._section_defaults, parser=self._section_parser, remove_section=self._name) |
| 76 | + if self._confd_path: |
| 77 | + for f in sorted(os.listdir(self._confd_path)): |
| 78 | + path = os.path.realpath(os.path.join(self._confd_path, f)) |
| 79 | + if not os.path.isfile(path): |
| 80 | + continue |
| 81 | + |
| 82 | + if self._conf_ext and not path.endswith(self._conf_ext): |
| 83 | + continue |
| 84 | + |
| 85 | + configs = self._parse_section(path=path, defaults=self._section_defaults, parser=self._section_parser, remove_section=self._name) |
| 86 | + self._config_sections.update(configs) |
| 87 | + |
| 88 | + def _parse_section(self, path, defaults={}, parser=None, only_section=None, remove_section=None): |
| 89 | + config_parser = ConfigParser.ConfigParser(defaults) |
| 90 | + |
| 91 | + if not path: |
| 92 | + raise IOError('No path specified: "%s"' % path) |
| 93 | + |
| 94 | + path = os.path.realpath(path) |
| 95 | + |
| 96 | + if len(config_parser.read(path)) != 1: |
| 97 | + raise IOError('Could not parse config file "%s"' % path) |
| 98 | + |
| 99 | + configs = {} |
| 100 | + for section in config_parser.sections(): |
| 101 | + if remove_section and remove_section == section: |
| 102 | + continue |
| 103 | + |
| 104 | + if only_section and only_section != section: |
| 105 | + continue |
| 106 | + |
| 107 | + config = dict((x[0], x[1]) for x in config_parser.items(section)) |
| 108 | + if hasattr(parser, '__call__'): |
| 109 | + config = parser(config) |
| 110 | + |
| 111 | + configs[section] = config |
| 112 | + |
| 113 | + return configs |
0 commit comments