diff options
Diffstat (limited to 'src/util.py')
-rw-r--r-- | src/util.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util.py b/src/util.py new file mode 100644 index 0000000..ff1df56 --- /dev/null +++ b/src/util.py | |||
@@ -0,0 +1,29 @@ | |||
1 | from ConfigParser import SafeConfigParser | ||
2 | |||
3 | class SimpleConfigParser(SafeConfigParser, object): | ||
4 | """SafeConfigParser that auto-loads files | ||
5 | |||
6 | This class also supports a more terse form of get that uses the default | ||
7 | section which can be passed to the constructor. | ||
8 | """ | ||
9 | |||
10 | def __init__(self, filename, default_section=None): | ||
11 | super(SimpleConfigParser, self).__init__() | ||
12 | self.default_section = default_section | ||
13 | |||
14 | with open(filename) as fp: | ||
15 | self.readfp(fp) | ||
16 | |||
17 | def get(self, section, option=None): | ||
18 | """Get that allows skipping section | ||
19 | |||
20 | This can be called with one or two arguments. The two argument form is | ||
21 | the same as ConfigParser. The one argument form allows skipping the | ||
22 | section if the class has a default_section set. | ||
23 | """ | ||
24 | default_get = super(SimpleConfigParser, self).get | ||
25 | |||
26 | if not option and self.default_section: | ||
27 | return default_get(self.default_section, section) | ||
28 | else: | ||
29 | return default_get(section, option) | ||