1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | from ConfigParser import * |
---|
4 | |
---|
5 | class config_parser(SafeConfigParser): |
---|
6 | """ |
---|
7 | A SafeConfig parser with a more forgiving get attribute |
---|
8 | """ |
---|
9 | |
---|
10 | def safe_get(self, sect, opt, method, default=None): |
---|
11 | """ |
---|
12 | If the option is present, return it, otherwise the default. |
---|
13 | """ |
---|
14 | if self.has_option(sect, opt): return method(self, sect, opt) |
---|
15 | else: return default |
---|
16 | |
---|
17 | def get(self, sect, opt, default=None): |
---|
18 | """ |
---|
19 | This returns the requested option or a given default. |
---|
20 | |
---|
21 | It's more like getattr than get. |
---|
22 | """ |
---|
23 | |
---|
24 | return self.safe_get(sect, opt, SafeConfigParser.get, default) |
---|
25 | |
---|
26 | def getint(self, sect, opt, default=0): |
---|
27 | """ |
---|
28 | Returns the selected option as an int or a default. |
---|
29 | """ |
---|
30 | |
---|
31 | return self.safe_get(sect, opt, SafeConfigParser.getint, default) |
---|
32 | |
---|
33 | def getfloat(self, sect, opt, default=0.0): |
---|
34 | """ |
---|
35 | Returns the selected option as an int or a default. |
---|
36 | """ |
---|
37 | |
---|
38 | return self.safe_get(sect, opt, SafeConfigParser.getfloat, default) |
---|
39 | |
---|
40 | def getboolean(self, sect, opt, default=False): |
---|
41 | """ |
---|
42 | Returns the selected option as a boolean or a default. |
---|
43 | """ |
---|
44 | |
---|
45 | return self.safe_get(sect, opt, SafeConfigParser.getboolean, default) |
---|