1 # simple config file parser utilizing Python's exec() builtin |
1 import os.path |
2 |
2 |
3 |
3 |
4 class ConfigParserError(Exception): |
4 class ConfigParserError(Exception): |
5 pass |
5 pass |
6 |
6 |
7 |
7 |
8 class ConfigParser: |
8 class ConfigParser: |
|
9 |
|
10 """ |
|
11 Simple config file parser utilizing Python's exec() builtin. |
|
12 """ |
|
13 |
9 def __init__(self): |
14 def __init__(self): |
10 self.options = {} |
15 self.options = {} |
11 self.values = {} |
16 self.values = {} |
12 |
17 |
13 def add_option(self, name, type=str, default=None): |
18 def add_option(self, name, type=str, default=None): |
14 """Register option name, type and default value.""" |
19 """Register option name, type and default value.""" |
15 self.options[name] = {'type':type, 'default':default} |
20 self.options[name] = {'type':type, 'default':default} |
16 self.values[name] = default |
21 self.values[name] = default |
17 |
22 |
18 def load(self, fname): |
23 def load(self, fname, must_exist=True): |
19 """Read config file and check loaded values.""" |
24 """Read config file and check loaded values.""" |
|
25 if not must_exist and not os.path.isfile(fname): |
|
26 return |
20 self.read(fname) |
27 self.read(fname) |
21 self.check() |
28 self.check() |
22 |
29 |
23 def read(self, fname): |
30 def read(self, fname): |
24 """Read config file. Does not check values.""" |
31 """Read config file. Does not check values.""" |