summaryrefslogtreecommitdiffhomepage
path: root/git_buildpackage/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'git_buildpackage/config.py')
-rw-r--r--git_buildpackage/config.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/git_buildpackage/config.py b/git_buildpackage/config.py
new file mode 100644
index 00000000..3a2c69d3
--- /dev/null
+++ b/git_buildpackage/config.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+#
+# (C) 2006 Guido Guenther <agx@sigxcpu.org>
+"""handles command line and config file option parsing for the git-buildpackage"""
+
+from optparse import OptionParser
+from ConfigParser import SafeConfigParser
+import os.path
+
+class GBPOptionParser(OptionParser):
+ """
+ Handles commandline options and parsing of config files
+ @ivar command: the gbp command we store the options for
+ @type command: string
+ @ivar prefix: prefix to prepend to all commandline options
+ @type prefix: string
+ @ivar config: current configuration parameters
+ @type config: dict
+ @cvar defaults: defaults value of an option if not in the config file or
+ given on the command line
+ @type defaults: dict
+ @cvar config_files: list of config files we parse
+ @type config_files: list
+ """
+ defaults={ 'builder': 'debuild',
+ 'debian-branch' : 'debian',
+ 'upstream-branch' : 'upstream',
+ }
+ config_files=['/etc/git-buildpackage/gbp.conf',
+ os.path.expanduser('~/.gbp.conf'),
+ '.git/gbp.conf' ]
+
+ def __parse_config_files(self):
+ """parse the possible config files and set appropriate values default values"""
+ parser=SafeConfigParser(self.defaults)
+ parser.read(self.config_files)
+ self.config=dict(parser.defaults())
+ if parser.has_section(self.command):
+ self.config=dict(parser.items(self.command))
+
+ def __init__(self, command, prefix='', usage=None):
+ self.command=command
+ self.prefix=prefix
+ self.__parse_config_files()
+ OptionParser.__init__(self, usage=usage)
+
+
+ def add_config_file_option(self, option_name, dest, help):
+ """
+ set a option for the command line parser, the default is read from the config file
+ @var option_name: name of the option
+ @type option_name: string
+ @var dest: where to store this option
+ @type dest: string
+ @var help: help text
+ @type help: string
+ """
+ OptionParser.add_option(self,"--%s%s" % (self.prefix, option_name), dest=dest,
+ default=self.config[option_name], help=help % self.config)
+
+