django-triager-bot / djangotriager / config.py

"""
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
    NAMESPACE_CONFIG_MAP = {
        "query": ("general", "query"),
        "ticket": ("general", "ticket"),
        "trac": ("general", "trac"),
        "working_copy": ("general", "working_copy"),
    }

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

    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):
                self.parser.add_section(section)
            if not self.parser.has_option(section, argument):
                self.parser.set(section, argument, "")

    def read_config(self, file):
        self.parser.read(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]:
                continue
            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))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.