1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from ConfigParser import ConfigParser
24 import Log
25 import Resource
26 import os
27
28 encoding = "iso-8859-1"
29 config = None
30 prototype = {}
31
33 """A prototype configuration key."""
35 for key, value in args.items():
36 setattr(self, key, value)
37
38 -def define(section, option, type, default = None, text = None, options = None, prototype = prototype):
39 """
40 Define a configuration key.
41
42 @param section: Section name
43 @param option: Option name
44 @param type: Key type (e.g. str, int, ...)
45 @param default: Default value for the key
46 @param text: Text description for the key
47 @param options: Either a mapping of values to text descriptions
48 (e.g. {True: 'Yes', False: 'No'}) or a list of possible values
49 @param prototype: Configuration prototype mapping
50 """
51 if not section in prototype:
52 prototype[section] = {}
53
54 if type == bool and not options:
55 options = [True, False]
56
57 prototype[section][option] = Option(type = type, default = default, text = text, options = options)
58
59 -def load(fileName = None, setAsDefault = False):
66
68 """A configuration registry."""
69 - def __init__(self, prototype, fileName = None):
96
97 - def get(self, section, option):
98 """
99 Read a configuration key.
100
101 @param section: Section name
102 @param option: Option name
103 @return: Key value
104 """
105 try:
106 type = self.prototype[section][option].type
107 default = self.prototype[section][option].default
108 except KeyError:
109 Log.warn("Config key %s.%s not defined while reading." % (section, option))
110 type, default = str, None
111
112 value = self.config.has_option(section, option) and self.config.get(section, option) or default
113 if type == bool:
114 value = str(value).lower()
115 if value in ("1", "true", "yes", "on"):
116 value = True
117 else:
118 value = False
119 else:
120 value = type(value)
121
122
123 return value
124
125 - def set(self, section, option, value):
126 """
127 Set the value of a configuration key.
128
129 @param section: Section name
130 @param option: Option name
131 @param value: Value name
132 """
133 try:
134 prototype[section][option]
135 except KeyError:
136 Log.warn("Config key %s.%s not defined while writing." % (section, option))
137
138 if not self.config.has_section(section):
139 self.config.add_section(section)
140
141 if type(value) == unicode:
142 value = value.encode(encoding)
143 else:
144 value = str(value)
145
146 self.config.set(section, option, value)
147
148 f = open(self.fileName, "w")
149 self.config.write(f)
150 f.close()
151
152 -def get(section, option):
153 """
154 Read the value of a global configuration key.
155
156 @param section: Section name
157 @param option: Option name
158 @return: Key value
159 """
160 global config
161 return config.get(section, option)
162
163 -def set(section, option, value):
164 """
165 Write the value of a global configuration key.
166
167 @param section: Section name
168 @param option: Option name
169 @param value: New key value
170 """
171 global config
172 return config.set(section, option, value)
173