140
141
for k,v in self._prefs.items():
141
142
print k, v.type, v.current
145
class ConfigParserPreferences(object):
146
"""Persistent preferences object that handles preferences via ConfigParser.
148
This preferences implementation is provided as a fallback for gconf-less
149
platforms. The ConfigParser library is included in Python and should be
150
available everywhere. The biggest drawbacks to this backend are lack of
151
access to desktop-wide settings, and lack of external change notification.
154
def __init__(self, rootkey, initial):
155
"""Create a preferences object.
157
Settings are initialised with 'initial' and then overriden
158
from values in the ConfigParser database if available.
160
rootkey : unused (retained for compatibility with existing gconf API)
161
initial : a dictionary of string to Value objects.
163
self.__dict__["_parser"] = ConfigParser.SafeConfigParser()
164
self.__dict__["_listeners"] = []
165
self.__dict__["_prefs"] = initial
166
self.__dict__["_type_mappings"] = {
167
BOOL : self._parser.getboolean,
168
INT : self._parser.getint,
169
STRING : self._parser.get,
170
FLOAT : self._parser.getfloat
173
if sys.platform == "win32":
174
pref_dir = os.path.join(os.getenv("APPDATA"), "Meld")
176
pref_dir = os.path.join(os.path.expanduser("~"), ".meld")
178
if not os.path.exists(pref_dir):
179
os.makedirs(pref_dir)
181
self.__dict__["_file_path"] = os.path.join(pref_dir, "meldrc.ini")
184
config_file = open(self._file_path, "r")
186
self._parser.readfp(config_file)
192
for key, value in self._prefs.items():
193
if self._parser.has_option("DEFAULT", key):
194
value.current = self._type_mappings[value.type]("DEFAULT", key)
196
def __getattr__(self, attr):
197
return self._prefs[attr].current
199
def get_default(self, attr):
200
return self._prefs[attr].default
202
def __setattr__(self, attr, val):
203
value = self._prefs[attr]
204
if value.current != val:
206
self._parser.set(None, attr, str(val))
209
fp = open(self._file_path, "w")
211
self._parser.write(fp)
218
for l in self._listeners:
220
except StopIteration:
223
def notify_add(self, callback):
224
"""Register a callback to be called when a preference changes.
226
callback : a callable object which take two parameters, 'attr' the
227
name of the attribute changed and 'val' the new value.
229
self._listeners.append(callback)
232
"""Print all preferences.
234
for k,v in self._prefs.items():
235
print k, v.type, v.current
237
# Prefer gconf, falling back to ConfigParser
240
Preferences = GConfPreferences
243
Preferences = ConfigParserPreferences