django-triager-bot / djangotriager /

Config contains all function responsible for merging configuration from all
available sources back together

from ConfigParser import SafeConfigParser

from djangotriager.exceptions import InvalidOptions

class Configuration(object):
    # Specify which commands from namespace maps to what section/arg in configuration
        "query": ("general", "query"),
        "ticket": ("general", "ticket"),
        "trac": ("general", "trac"),
        "working_copy": ("general", "working_copy"),
        "n_patches": ("general", "n_patches"),
        "send_report": ("general", "send_report"),

    def __init__(self):
        super(Configuration, self).__init__()
        self.parser = SafeConfigParser()
        self.command = None

    def _ensure_sections(self):
        for option in Configuration.NAMESPACE_CONFIG_MAP:
            section, argument = Configuration.NAMESPACE_CONFIG_MAP[option]
            if not self.parser.has_section(section):
            if not self.parser.has_option(section, argument):
                self.parser.set(section, argument, "")

    def read_config(self, file):

    def get(self, name):
        if name in Configuration.NAMESPACE_CONFIG_MAP:
            return self.parser.get(*Configuration.NAMESPACE_CONFIG_MAP[name])

    def merge_with_cmd(self, namespace):
        kwargs = dict(namespace._get_kwargs())
        for key in Configuration.NAMESPACE_CONFIG_MAP:
            if not key in kwargs or not kwargs[key]:
            section, option = Configuration.NAMESPACE_CONFIG_MAP[key]
            self.parser.set(section, option, str(kwargs[key]))

    def ensure_one(self, *options):
        options_found = [option for option in options if self.get(option)]
        if len(options_found) != 1:
            raise InvalidOptions("You must specify one of the following "
                                 "options: " + ", ".join(options))

    def ensure_all(self, *options):
        not_found_options = [
            option for option in options if not self.get(option)]
        if not_found_options:
            raise InvalidOptions("The following options are required "
                                 + ", ".join(not_found_options))