forked from mcedit/mcedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
299 lines (219 loc) · 7.77 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""Copyright (c) 2010-2012 David Rio Vierra
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
"""
config.py
Configuration settings and storage.
"""
import os
import logging
import collections
import ConfigParser
from cStringIO import StringIO
import mcplatform
from albow import alert
log = logging.getLogger(__name__)
def configFilePath():
return mcplatform.configFilePath
def loadConfig():
class keyDict (collections.MutableMapping):
def __init__(self, *args, **kwargs):
self.dict = dict(*args, **kwargs)
self.keyorder = []
def keys(self):
return list(self.keyorder)
def items(self):
return list(self.__iteritems__())
def __iteritems__(self):
return ((k, self.dict[k]) for k in self.keys())
def __iter__(self):
return self.keys().__iter__()
def __getitem__(self, k):
return self.dict[k]
def __setitem__(self, k, v):
self.dict[k] = v
if not k in self.keyorder:
self.keyorder.append(k)
def __delitem__(self, k):
del self.dict[k]
if k in self.keyorder:
self.keyorder.remove(k)
def __contains__(self, k):
return self.dict.__contains__(k)
def __len__(self):
return self.dict.__len__()
def copy(self):
k = keyDict()
k.dict = self.dict.copy()
k.keyorder = list(self.keyorder)
return k
config = ConfigParser.RawConfigParser([], keyDict)
config.readfp(StringIO(configDefaults))
try:
config.read(configFilePath())
except Exception, e:
log.warn(u"Error while reading configuration file mcedit.ini: {0}".format(e))
return config
def updateConfig():
pass
def saveConfig():
try:
cf = file(configFilePath(), 'w')
config.write(cf)
cf.close()
except Exception, e:
try:
alert(u"Error saving configuration settings to mcedit.ini: {0}".format(e))
except:
pass
configDefaults = """
[Keys]
forward = w
back = s
left = a
right = d
up = q
down = z
brake = space
rotate = e
roll = r
flip = f
mirror = g
swap = x
pan left = j
pan right = l
pan up = i
pan down = k
reset reach = mouse3
increase reach = mouse4
decrease reach = mouse5
confirm construction = return
open level = o
new level = n
delete blocks = delete
toggle fps counter = 0
toggle renderer = m
"""
log.info("Loading config...")
config = loadConfig()
config.observers = {}
def _propertyRef(section, name, dtype=str, default=None):
class PropRef(object):
def get(self):
return _getProperty(section, name, dtype, default)
def set(self, val):
_setProperty(section, name, val)
return PropRef()
def _configProperty(section, name, dtype=str, setter=None, default=None):
assert default is not None
def _getter(self):
return _getProperty(section, name, dtype, default)
def _setter(self, val):
_setProperty(section, name, val)
if setter:
setter(self, val)
return property(_getter, _setter, None)
def _getProperty(section, name, dtype=str, default=None):
try:
if dtype is bool:
return config.getboolean(section, name)
else:
return dtype(config.get(section, name))
except:
if default is None:
raise
_setProperty(section, name, default)
return default
def _setProperty(section, name, value):
log.debug("Property Change: %15s %30s = %s", section, name, value)
config.set(section, name, str(value))
_notifyObservers(section, name, value)
def _notifyObservers(section, name, value):
observers = config.observers.get((section.lower(), name.lower()), {})
newObservers = {}
for targetref, attr in observers:
target = targetref()
if target:
log.debug("Notifying %s", target)
setattr(target, attr, value)
callback = observers[targetref, attr]
if callback:
callback(value)
newObservers[targetref, attr] = callback
config.observers[(section, name)] = newObservers
import weakref
def addObserver(section, name, target, attr=None, dtype=str, callback=None, default=None):
""" Register 'target' for changes in the config var named by section and name.
When the config is changed, calls setattr with target and attr.
attr may be None; it will be created from the name by lowercasing the first
word, uppercasing the rest, and removing spaces.
e.g. "block buffer" becomes "blockBuffer"
"""
observers = config.observers.setdefault((section.lower(), name.lower()), {})
if not attr:
tokens = name.lower().split()
attr = tokens[0] + "".join(t.title() for t in tokens[1:])
log.debug("Subscribing %s.%s", target, attr)
attr = intern(attr)
targetref = weakref.ref(target)
observers.setdefault((targetref, attr), callback)
val = _getProperty(section, name, dtype, default)
setattr(target, attr, val)
if callback:
callback(val)
class Setting(object):
def __init__(self, section, name, dtype, default):
self.section = section
self.name = name
self.dtype = dtype
self.default = default
def __repr__(self):
return "Setting(" + ", ".join(str(s) for s in (self.section, self.name, self.dtype, self.default))
def addObserver(self, target, attr=None, callback=None):
addObserver(self.section, self.name, target, attr, self.dtype, callback, self.default)
def get(self):
return _getProperty(self.section, self.name, self.dtype, self.default)
def set(self, val):
return _setProperty(self.section, self.name, self.dtype(val))
def propertyRef(self):
return _propertyRef(self.section, self.name, self.dtype, self.default)
def configProperty(self, setter=None):
return _configProperty(self.section, self.name, self.dtype, setter, self.default)
def __int__(self):
return int(self.get())
def __float__(self):
return float(self.get())
def __bool__(self):
return bool(self.get())
class Settings(object):
Setting = Setting
def __init__(self, section):
self.section = section
def __call__(self, name, default):
assert default is not None
dtype = type(default)
section = self.section
s = self.Setting(section, name, dtype, default)
if not config.has_section(section):
config.add_section(section)
if not config.has_option(section, name):
s.set(default)
return s
def __setattr__(self, attr, val):
if hasattr(self, attr):
old = getattr(self, attr)
if isinstance(old, Setting):
if isinstance(val, Setting):
raise ValueError("Attempting to reassign setting %s with %s" % (old, val))
log.warn("Setting attr %s via __setattr__ instead of set()!", attr)
return old.set(val)
log.debug("Setting {%s => %s}" % (attr, val))
return object.__setattr__(self, attr, val)