From a884ef569828d9d7875427084d7c830f168a655c Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Wed, 21 Mar 2012 23:17:29 +0100 Subject: gbp.config: add list of config file sections to constructor This makes it possible to parse additional mandatory sections from config files to prefill defaults. Git-Dch: Ignore --- gbp/config.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/gbp/config.py b/gbp/config.py index 345330c8..960d4b2e 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -17,7 +17,7 @@ """handles command line and config file option parsing for the gbp commands""" from optparse import OptionParser, OptionGroup, Option, OptionValueError -from ConfigParser import SafeConfigParser +from ConfigParser import SafeConfigParser, NoSectionError from copy import copy import os.path try: @@ -252,8 +252,17 @@ class GbpOptionParser(OptionParser): parser = SafeConfigParser(self.defaults) parser.read(self.config_files) self.config = dict(parser.defaults()) + if parser.has_section(self.command): self.config.update(dict(parser.items(self.command, raw=True))) + + for section in self.sections: + if parser.has_section(section): + self.config.update(dict(parser.items(section, raw=True))) + else: + raise NoSectionError("Mandatory section [%s] does not exist." + % section) + # filter can be either a list or a string, always build a list: if self.config['filter']: if self.config['filter'].startswith('['): @@ -263,7 +272,7 @@ class GbpOptionParser(OptionParser): else: self.config['filter'] = [] - def __init__(self, command, prefix='', usage=None): + def __init__(self, command, prefix='', usage=None, sections=[]): """ @param command: the command to build the config parser for @type command: C{str} @@ -271,8 +280,12 @@ class GbpOptionParser(OptionParser): @type prefix: C{str} @param usage: a usage description @type usage: C{str} + @param sections: additional (non optional) config file sections + to parse + @type sections: C{list} of C{str} """ self.command = command + self.sections = sections self.prefix = prefix self.config = {} self.config_files = self.get_config_files() -- cgit v1.2.3