Mercurial > treepkg > treepkg
diff treepkg/readconfig.py @ 2:e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Thu, 08 Mar 2007 19:55:44 +0100 |
parents | f78a02e79c84 |
children | fee641fec94e |
line wrap: on
line diff
--- a/treepkg/readconfig.py Tue Mar 06 18:50:53 2007 +0100 +++ b/treepkg/readconfig.py Thu Mar 08 19:55:44 2007 +0100 @@ -8,43 +8,66 @@ """Reads the configuration file""" import sys +import shlex from ConfigParser import SafeConfigParser, NoOptionError - -assembly_line_keys = ["name", "base_dir", "svn_url", "root_cmd", - "deb_email", "deb_fullname"] - defaults = dict(root_cmd="sudo") +packager_desc = [ + "name", "base_dir", "svn_url", + ("root_cmd", shlex.split), + "deb_email", "deb_fullname", + ] + +treepkg_desc = [ + ("check_interval", int), + ] + + +def read_config_section(parser, section, item_desc, defaults=None): + if defaults is None: + defaults = dict() + options = dict() + for item in item_desc: + if isinstance(item, tuple): + key, converter = item + else: + key = item + converter = str + try: + value = parser.get(section, key, vars=defaults) + options[key] = converter(value) + except NoOptionError: + print >>sys.stderr, "%s: Missing option %r in section %r" \ + % (filename, key, section) + sys.exit(1) + return options def read_config(filename): - """Reads the packagemill configuration from filename. + """Reads the tree packager configuration from the file given by filename. - The return value is a list of (name, option) pairs, one for each - assembly line of the mill. + The function returns a tuple with a ('treepkg') and a list of dicts + ('packagers'). The treepkg dict contains the main configuration of + the tree packager. The packagers list contains one dict with the + configuratiin for each packager. """ - assembly_lines = [] parser = SafeConfigParser(defaults) parser.read([filename]) + + # extract packager configurations + packagers = [] for section in parser.sections(): if section.startswith("pkg_"): - pkg_defaults = dict(name=section[4:]) - options = {} - for key in assembly_line_keys: - try: - options[key] = parser.get(section, key, vars=pkg_defaults) - except NoOptionError: - print >>sys.stderr, "%s: Missing option %r in section %r" \ - % (filename, key, section) - sys.exit(1) - assembly_lines.append(options) + packagers.append(read_config_section(parser, section, packager_desc, + dict(name=section[4:]))) - treepkg_opts = dict(check_interval=parser.getint("treepkg", - "check_interval")) + # main config + treepkg = read_config_section(parser, "treepkg", treepkg_desc) - return treepkg_opts, assembly_lines + return treepkg, packagers if __name__ == "__main__": - print read_config(sys.argv[1]) + import pprint + print pprint.pprint(read_config(sys.argv[1]))