author | Radek Brich <radek.brich@devl.cz> |
Wed, 07 May 2014 18:37:13 +0200 | |
changeset 12 | d5f749032fa1 |
parent 7 | 776ba4914dfc |
permissions | -rw-r--r-- |
12
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
1 |
import os.path |
3 | 2 |
|
3 |
||
4 |
class ConfigParserError(Exception): |
|
5 |
pass |
|
6 |
||
7 |
||
8 |
class ConfigParser: |
|
12
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
9 |
|
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
10 |
""" |
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
11 |
Simple config file parser utilizing Python's exec() builtin. |
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
12 |
""" |
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
13 |
|
3 | 14 |
def __init__(self): |
4 | 15 |
self.options = {} |
16 |
self.values = {} |
|
3 | 17 |
|
7
776ba4914dfc
Update ConfigParser: rename 'type_' back to 'type'. Rename project in setup.py to make RPM package names more standard.
Radek Brich <brich.radek@ifortuna.cz>
parents:
4
diff
changeset
|
18 |
def add_option(self, name, type=str, default=None): |
4 | 19 |
"""Register option name, type and default value.""" |
7
776ba4914dfc
Update ConfigParser: rename 'type_' back to 'type'. Rename project in setup.py to make RPM package names more standard.
Radek Brich <brich.radek@ifortuna.cz>
parents:
4
diff
changeset
|
20 |
self.options[name] = {'type':type, 'default':default} |
4 | 21 |
self.values[name] = default |
3 | 22 |
|
12
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
23 |
def load(self, fname, must_exist=True): |
4 | 24 |
"""Read config file and check loaded values.""" |
12
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
25 |
if not must_exist and not os.path.isfile(fname): |
d5f749032fa1
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
26 |
return |
4 | 27 |
self.read(fname) |
3 | 28 |
self.check() |
29 |
||
4 | 30 |
def read(self, fname): |
31 |
"""Read config file. Does not check values.""" |
|
3 | 32 |
with open(fname) as f: |
4 | 33 |
exec(f.read(), self.values) |
3 | 34 |
|
35 |
def check(self): |
|
4 | 36 |
"""Check option values against registered types.""" |
37 |
for key in self.values.keys(): |
|
3 | 38 |
if key == '__builtins__': |
39 |
continue |
|
4 | 40 |
if key in self.options: |
41 |
type_ = self.options[key]['type'] |
|
42 |
if not isinstance(self.values[key], type_) and not self.values[key] is None: |
|
3 | 43 |
raise ConfigParserError("Bad value of config parameter '%s': type is %s but should be %s" |
4 | 44 |
% (key, type(self.values[key]), type_)) |
3 | 45 |
else: |
46 |
raise ConfigParserError("Unknown config parameter '%s'." % key) |
|
47 |
||
48 |
def __getattr__(self, name): |
|
49 |
"""Attribute access for config parameters. |
|
50 |
||
4 | 51 |
cfg.my_param <===> cfg.values['myparam'] |
3 | 52 |
|
53 |
""" |
|
4 | 54 |
if name in self.values: |
55 |
return self.values[name] |
|
3 | 56 |
else: |
57 |
raise AttributeError() |
|
58 |