view treepkg/readconfig.py @ 258:bb98e728f25b

Allow default values for individual options to be passed to read_config_section. The default value can now be passed as a third item in a tuple used in the section description passed to read_config_section. The predefined option descriptions have been updated to use this new mechanism and the global defaults variable is not needed anymore. Also, indivdual PackageTrack classes can now use this mechanism to specify defaults for their additional configuration options.
author Bernhard Herzog <bh@intevation.de>
date Fri, 17 Apr 2009 18:48:58 +0000
parents eaa696629a91
children faeeac2c4c71
line wrap: on
line source
# Copyright (C) 2007, 2008, 2009 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

"""Reads the configuration file"""

import sys
import shlex
from ConfigParser import SafeConfigParser, NoOptionError

import util
import packager

def convert_bool(s):
    s = s.lower()
    if s in ("true", "yes", "1"):
        return True
    if s in ("false", "no", "0"):
        return False
    raise ValueError("cannot determine boolean value of %r" % (s,))


packager_desc = [
    "name", "base_dir", "svn_url", ("rules_svn_url", str, ""), "packager_class",
    ("root_cmd", shlex.split, "sudo"), "pbuilderrc",
    "deb_email", "deb_fullname", "debrevision_prefix",
    ("handle_dependencies", convert_bool),
    ("signing_key_id", str, ""),
    ]

treepkg_desc = [
    ("check_interval", int),
    "instructions_file",
    ]


def read_config_section(parser, section, item_desc, defaults=None):
    if defaults is None:
        defaults = dict()
    options = dict()
    for item in item_desc:
        has_default_value = False
        if isinstance(item, tuple):
            key, converter = item[:2]
            if len(item) == 3:
                default_value = item[-1]
                has_default_value = True
        else:
            key = item
            converter = str
        try:
            value = parser.get(section, key, vars=defaults)
        except NoOptionError:
            if has_default_value:
                value = default_value
            else:
                print >>sys.stderr, "Missing option %r in section %r" \
                      % (key, section)
                sys.exit(1)
        options[key] = converter(value)
    return options


def read_config(filename):
    """Reads the tree packager configuration from the file given by filename.

    The function returns a tuple with a dict ('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.
    """
    parser = SafeConfigParser()
    parser.read([filename])

    # extract packager configurations
    packagers = []
    for section in parser.sections():
        if section.startswith("pkg_"):
            vars = dict(name=section[4:])
            packager_class = parser.get(section, "packager_class", vars=vars)
            module = packager.import_packager_module(packager_class)
            desc = packager_desc + module.PackageTrack.extra_config_desc
            packagers.append(read_config_section(parser, section, desc,
                                                 defaults=vars))

    # main config
    treepkg = read_config_section(parser, "treepkg", treepkg_desc)

    return treepkg, packagers


if __name__ == "__main__":
    import pprint
    print pprint.pprint(read_config(sys.argv[1]))
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)