1
# -*- coding: utf-8 -*-
8
haveGConfSupport = False
12
import xml.dom.minidom
15
document = xml.dom.minidom.parse(CONFIG_FILE)
18
except xml.parsers.expat.ExpatError:
19
print 'Configuration at ' + CONFIG_FILE + ' is invalid, ignoring'
21
print 'Loading configuration from ' + CONFIG_FILE
24
return string == 'True'
26
valueTypes = {'int': int, 'bool': _bool, 'float': float, 'str': str}
28
if document is not None:
29
elements = document.getElementsByTagName('config')
32
for n in e.getElementsByTagName('value'):
34
name = n.attributes['name'].nodeValue
38
valueType = n.attributes['type'].nodeValue
41
if len(n.childNodes) != 1 or n.childNodes[0].nodeType != n.TEXT_NODE:
43
valueString = n.childNodes[0].nodeValue
46
value = valueTypes[valueType](valueString)
53
haveGConfSupport = True
54
_GCONF_DIR = '/apps/glchess/'
55
_config = gconf.client_get_default()
57
_config.add_dir(_GCONF_DIR[:-1], gconf.CLIENT_PRELOAD_NONE)
58
except gobject.GError:
61
_gconfGetFunction = {gconf.VALUE_BOOL: gconf.Value.get_bool,
62
gconf.VALUE_FLOAT: gconf.Value.get_float,
63
gconf.VALUE_INT: gconf.Value.get_int,
64
gconf.VALUE_STRING: gconf.Value.get_string}
66
_gconfSetFunction = {bool: _config.set_bool,
67
float: _config.set_float,
69
str: _config.set_string,
70
unicode: _config.set_string}
72
# Config default values
73
_defaults = {'show_toolbar': True,
77
'piece_style': 'simple',
79
'show_3d_smooth': False,
80
'show_move_hints': True,
81
'move_format': 'human',
82
'promotion_type': 'queen',
83
'board_view': 'human',
84
'show_comments': False,
85
'show_numbering': False,
86
'enable_networking': True,
89
'new_game_dialog/move_time': 0,
90
'new_game_dialog/white/type': '',
91
'new_game_dialog/white/difficulty': '',
92
'new_game_dialog/black/type': '',
93
'new_game_dialog/black/difficulty': ''}
95
class Error(Exception):
96
"""Exception for configuration use"""
100
"""Get a configuration value.
102
'name' is the name of the value to get (string).
104
Raises an Error exception if the value does not exist.
108
entry = _config.get(_GCONF_DIR + name)
109
except gobject.GError:
114
return _defaults[name]
116
raise Error('No config value: ' + repr(name))
119
function = _gconfGetFunction[entry.type]
121
raise Error('Unknown value type')
123
return function(entry)
130
return _defaults[name]
132
raise Error('No config value: ' + repr(name))
134
def set(name, value):
135
"""Set a configuration value.
137
'name' is the name of the value to set (string).
138
'value' is the value to set to (int, str, float, bool).
142
function = _gconfSetFunction[type(value)]
144
raise TypeError('Only config values of type: int, str, float, bool supported')
147
function(_GCONF_DIR + name, value)
148
except gobject.GError:
154
oldValue = _values[name]
158
if oldValue == value:
161
# Use new value and notify watchers
162
_values[name] = value
164
watchers = _notifiers[name]
168
for func in watchers:
175
set(name, _defaults[name])
177
def _watch(client, _, entry, (function, name)):
179
function(name, value)
181
def watch(name, function):
186
_config.notify_add(_GCONF_DIR + name, _watch, (function, name))
187
except gobject.GError:
191
watchers = _notifiers[name]
193
watchers = _notifiers[name] = []
194
watchers.append(function)
197
"""Save the current configuration"""
201
document = xml.dom.minidom.Document()
203
e = document.createComment('Automatically generated by glChess, do not edit!')
204
document.appendChild(e)
206
root = document.createElement('config')
207
document.appendChild(root)
209
valueNames = {int: 'int', bool: 'bool', float: 'float', str: 'str', unicode: 'str'}
211
names = _values.keys()
214
value = _values[name]
215
e = document.createElement('value')
217
e.setAttribute('name', name)
218
e.setAttribute('type', valueNames[type(value)])
219
valueElement = document.createTextNode(str(value))
220
e.appendChild(valueElement)
223
f = file(CONFIG_FILE, 'w')