1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import glob
19 import os
20 import gobject
21 import gettext
22
23 gettext.textdomain('screenlets')
24 gettext.bindtextdomain('screenlets', '/usr/share/locale')
25
27 return gettext.gettext(s)
28
29
30 try:
31 import gconf
32 except:
33 print _("GConf python module not found. GConf settings backend is disabled.")
34
35
37 """The backend performs the loading/saving of the 'key=value'-strings.
38 Extend this superclass to implement different saving-backends."""
39
42
44 """Delete an instance's configuration by its id."""
45 pass
46
48 """Immediately store all values to disk (in case the backend doesn't
49 save in realtime anyway."""
50 pass
51
53 """Load one option for the instance with the given id."""
54 pass
55
57 """Load all options for the instance with the given id."""
58 pass
59
61 """Save one option for the instance with the given id."""
62 pass
63
64
66 """Backend for storing settings in the GConf registry"""
67
68 gconf_dir = '/apps/screenlets/'
69
74
76 """Delete an instance's configuration by its id."""
77 os.system('gconftool-2 --recursive-unset ' + self.key + id)
78 return True
79
81 """Immediately store all values to disk (in case the backend doesn't
82 save in realtime anyway."""
83 pass
84
86 """Load one option for the instance with the given id."""
87 return self.client.get_string(self.gconf_dir + id + '/' + name)
88
90 """Load all options for the instance with the given id."""
91 keys = []
92 vals = []
93 for i in self.client.all_entries(self.gconf_dir + id):
94 keys.append(i.key.split('/')[4])
95 vals.append(self.client.get_string(i.key))
96 return dict(zip(keys, vals))
97 return None
98
100 """Save one option for the instance with the given id."""
101 self.client.set_string(self.gconf_dir + id + '/' + name, value)
102 print _('Saved option %s%s/%s = %s') % (self.gconf_dir, id, name, value)
103
104
106 """A backend that stores the settings in arrays and saves after a short
107 interval to avoid overhead when multiple values are set within a short time.
108 The data gets saved into $HOME/.config/Screenlets/<Screenletname>/, in a
109 file for each element (named like its id with the extension '.ini')."""
110
111
112 __instances = {}
113 __delay_time = 3000
114 __timeout = None
115 __queue = []
116
117
118 path = ''
119
120
125
127 """Delete an instance from the list and from the filesystem."""
128 if self.__instances.has_key(id):
129 del self.__instances[id]
130 try:
131 import os
132 os.remove(self.path + id + '.ini')
133 except Exception,ex:
134 print ex
135 print _("Temporary file didn't exist - nothing to remove.")
136 return False
137 print _("CachingBackend: <#%s> removed!") % id
138 return True
139
141 """Immediately save all pending data."""
142 self.__save_cache()
143
161
163 """TODO: Load option from the backend (returned as str)."""
164 return self.__instances[id][name]
165
167 """Load all options for the instance with the given id."""
168
169 if self.__instances.has_key(id):
170 return self.__instances[id]
171 return None
172
174 """Load all cached files from path."""
175
176 print _("CachingBackend: Loading instances from cache")
177
178 dirname = self.path
179 dirlst = glob.glob(dirname + '*')
180 tdlen = len(dirname)
181 lst = []
182 for fname in dirlst:
183 dname = fname[tdlen:]
184 if dname.endswith('.ini'):
185 id = dname[:-4]
186 print _("CachingBackend: Loading <%s>") % id
187
188 if self.__instances.has_key(id) == False:
189 self.__instances[id] = {}
190
191 try:
192 f = open(fname, 'r')
193 lines = f.readlines()
194
195 for line in lines:
196
197 parts = line[:-1].split('=', 1)
198 if len(parts) > 1:
199 self.__instances[id][parts[0]] = parts[1]
200 f.close()
201 except Exception, ex:
202 print _("Error while loading options: %s") % str(ex)
203
205 """Save the cache (for all pending instances in queue) to self.path."""
206
207 for id in self.__queue:
208
209 if self.__instances.has_key(id) == False:
210 print _("Queue-element <%s> not found (already removed?)!") % id
211 self.__queue.remove(id)
212 break
213
214
215 lst = []
216 for oname in self.__instances[id]:
217 lst.append([oname, self.__instances[id][oname]])
218
219 if len(lst) > 0:
220 backup = ''
221 try:
222 backup = open(self.path + id + '.ini', 'r')
223 backup.close()
224 except:pass
225 try:
226
227
228 f = open(self.path + id + '.ini', 'w')
229 for el in lst:
230 f.write(el[0] + '=' + el[1] + "\n")
231 f.close()
232 print "OK"
233 except:
234 if backup != '' and os.path.exists(self.path + id + '.ini'):
235 try:
236 backup_restore = open(self.path + id + '.ini', 'w')
237 backup_restore.write(backup)
238 backup_restore.close()
239 except:pass
240 print _("error while saving config: %s %s") % ( oname , self.path)
241
242 self.__queue = []
243
244 return False
245