diff options
Diffstat (limited to 'gitosis/configutil.py')
-rw-r--r-- | gitosis/configutil.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/gitosis/configutil.py b/gitosis/configutil.py new file mode 100644 index 0000000..3215b85 --- /dev/null +++ b/gitosis/configutil.py @@ -0,0 +1,56 @@ +""" +Useful wrapper functions to access ConfigParser structures. +""" +from ConfigParser import NoSectionError, NoOptionError, RawConfigParser +from UserDict import IterableUserDict + +def getboolean_default(config, section, option, default_value): + """ + Return the given section.variable, or return the default if no specific + value is set. + """ + try: + value = config.getboolean(section, option) + except (NoSectionError, NoOptionError): + value = default_value + return value +def get_default(config, section, option, default_value): + """ + Return the given section.variable, or return the default if no specific + value is set. + """ + try: + value = config.get(section, option) + except (NoSectionError, NoOptionError): + value = default_value + return value + +class GitosisConfigDict(IterableUserDict): + def keys(self): + return list(self.__iter__()) + def __iter__(self): + saw = set() + if 'gitosis' in self.data: + saw.add('gitosis') + yield 'gitosis' + sorted_keys = self.data.keys() + sorted_keys.sort() + for _ in sorted_keys: + if _.startswith('group '): + saw.add(_) + yield _ + for _ in sorted_keys: + if _.startswith('repo '): + saw.add(_) + yield _ + for _ in sorted_keys: + if _ not in saw: + saw.add(_) + yield _ + + +class GitosisRawConfigParser(RawConfigParser): + def __init__(self, defaults=None): + RawConfigParser.__init__(self, defaults) + self._sections = GitosisConfigDict(self._sections) + |