aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2012-03-21 23:17:29 +0100
committerGuido Günther <agx@sigxcpu.org>2012-03-21 23:25:54 +0100
commita884ef569828d9d7875427084d7c830f168a655c (patch)
tree1e11a16eff8d95a191c1a82b98bd15089e8aca7b
parentffbff853e59ae5409cd1d170603b036c8e6feb2a (diff)
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
-rw-r--r--gbp/config.py17
1 files 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()