summaryrefslogtreecommitdiff
path: root/src/util.py
blob: ff1df56e60d5e0b6fb891fc276ed38077cebdb9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from ConfigParser import SafeConfigParser

class SimpleConfigParser(SafeConfigParser, object):
    """SafeConfigParser that auto-loads files

    This class also supports a more terse form of get that uses the default
    section which can be passed to the constructor.
    """

    def __init__(self, filename, default_section=None):
        super(SimpleConfigParser, self).__init__()
        self.default_section = default_section

        with open(filename) as fp:
            self.readfp(fp)

    def get(self, section, option=None):
        """Get that allows skipping section

        This can be called with one or two arguments. The two argument form is
        the same as ConfigParser. The one argument form allows skipping the
        section if the class has a default_section set.
        """
        default_get = super(SimpleConfigParser, self).get

        if not option and self.default_section:
            return default_get(self.default_section, section)
        else:
            return default_get(section, option)