Merge lp:~izidor/gtg/default_preferences_plugins into lp:~gtg/gtg/old-trunk

Proposed by Izidor Matušov on 2013-01-15
Status: Merged
Merged at revision: 1277
Proposed branch: lp:~izidor/gtg/default_preferences_plugins
Merge into: lp:~gtg/gtg/old-trunk
Diff against target: 194 lines (+39/-56)
8 files modified
CHANGELOG (+1/-0)
GTG/core/plugins/api.py (+11/-4)
GTG/plugins/export/export.py (+3/-6)
GTG/plugins/hamster/hamster.py (+3/-7)
GTG/plugins/notification_area/notification_area.py (+3/-7)
GTG/plugins/task_reaper/reaper.py (+3/-6)
GTG/plugins/untouched_tasks/untouchedTasks.py (+3/-6)
GTG/plugins/urgency_color/urgency_color.py (+12/-20)
To merge this branch: bzr merge lp:~izidor/gtg/default_preferences_plugins
Reviewer Review Type Date Requested Status
Nimit Shah Approve on 2013-01-18
Gtg developers 2013-01-15 Pending
Review via email: mp+143403@code.launchpad.net

Description of the change

Plugin API now takes care about default values of preferences, thus simplify lives of plugin creators (by removing couple lines to write :)

To post a comment you must log in.
Nimit Shah (nimit-svnit) wrote :

Hi Izidor,
  The code looks proper to me.
  Just one small thing: Would "config" be a more intuitive variable name then "result"? Not an important question, you can always ignore it :)

1276. By Izidor Matušov on 2013-01-18

Rename result into config

Nimit Shah (nimit-svnit) wrote :

It is proper according to me :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CHANGELOG'
2--- CHANGELOG 2013-01-13 20:14:19 +0000
3+++ CHANGELOG 2013-01-18 16:20:28 +0000
4@@ -11,6 +11,7 @@
5 * Fix for bug #1096622: Damaged image in the help, by Parin Porecha
6 * Fix for bug #1095390: Quick Add bar incorrectly parses tags, by Parin Porecha
7 * Fix for bug #1079143: Adding the same custom color in tag editor multiple times raises an Exception, by Parin Porecha
8+ * Better handling of preferences loading (bug #1094307)
9
10 2012-11-06 Getting Things GNOME! 0.3
11 * Hide tasks with due date someday, #931376
12
13=== modified file 'GTG/core/plugins/api.py'
14--- GTG/core/plugins/api.py 2012-12-18 13:01:11 +0000
15+++ GTG/core/plugins/api.py 2013-01-18 16:20:28 +0000
16@@ -209,8 +209,13 @@
17 pane.basetree.get_basetree().refresh_all()
18
19 #=== file saving/loading ======================================================
20- def load_configuration_object(self, plugin_name, filename, \
21- basedir=xdg_config_home):
22+ def load_configuration_object(self, plugin_name, filename,
23+ basedir=xdg_config_home, default_values=None):
24+ if default_values is not None:
25+ config = dict(default_values)
26+ else:
27+ config = dict()
28+
29 dirname = os.path.join(basedir, 'gtg/plugins', plugin_name)
30 path = os.path.join(dirname, filename)
31 if os.path.isdir(dirname):
32@@ -218,12 +223,14 @@
33 try:
34 with open(path, 'r') as file:
35 item = pickle.load(file)
36+ config.update(item)
37 except:
38- return None
39- return item
40+ pass
41 else:
42 os.makedirs(dirname)
43
44+ return config
45+
46 def save_configuration_object(self, plugin_name, filename, item, \
47 basedir=xdg_config_home):
48 dirname = os.path.join(basedir, 'gtg/plugins', plugin_name)
49
50=== modified file 'GTG/plugins/export/export.py'
51--- GTG/plugins/export/export.py 2012-08-11 12:24:49 +0000
52+++ GTG/plugins/export/export.py 2013-01-18 16:20:28 +0000
53@@ -366,12 +366,9 @@
54
55 def _preferences_load(self):
56 """ Restore user preferences """
57- data = self.plugin_api.load_configuration_object(
58- self.PLUGIN_NAME, "preferences")
59- if type(data) != type(dict()):
60- self.preferences = self.DEFAULT_PREFERENCES
61- else:
62- self.preferences = data
63+ self.preferences = self.plugin_api.load_configuration_object(
64+ self.PLUGIN_NAME, "preferences",
65+ default_values = self.DEFAULT_PREFERENCES)
66
67 def _preferences_store(self):
68 """ Store user preferences """
69
70=== modified file 'GTG/plugins/hamster/hamster.py'
71--- GTG/plugins/hamster/hamster.py 2012-11-25 19:19:44 +0000
72+++ GTG/plugins/hamster/hamster.py 2013-01-18 16:20:28 +0000
73@@ -307,13 +307,9 @@
74 return True
75
76 def preferences_load(self):
77- data = self.plugin_api.load_configuration_object(
78- self.PLUGIN_NAMESPACE, "preferences")
79- self.preferences = {}
80- self.preferences.update(self.DEFAULT_PREFERENCES)
81-
82- if type(data) == type(dict()):
83- self.preferences.update(data)
84+ self.preferences = self.plugin_api.load_configuration_object(
85+ self.PLUGIN_NAME, "preferences",
86+ default_values = self.DEFAULT_PREFERENCES)
87
88 def preferences_store(self):
89 self.plugin_api.save_configuration_object(self.PLUGIN_NAMESPACE,
90
91=== modified file 'GTG/plugins/notification_area/notification_area.py'
92--- GTG/plugins/notification_area/notification_area.py 2012-12-16 14:59:32 +0000
93+++ GTG/plugins/notification_area/notification_area.py 2013-01-18 16:20:28 +0000
94@@ -385,13 +385,9 @@
95
96 ### Preferences methods #######################################################
97 def preferences_load(self):
98- data = self.__plugin_api.load_configuration_object(self.PLUGIN_NAME,
99- "preferences")
100- # We first load the preferences then update the dict
101- # This way new default options are recognized with old cfg files
102- self.preferences = self.DEFAULT_PREFERENCES
103- if isinstance(data, dict):
104- self.preferences.update(data)
105+ self.preferences = self.__plugin_api.load_configuration_object(
106+ self.PLUGIN_NAME, "preferences",
107+ default_values = self.DEFAULT_PREFERENCES)
108
109 def preferences_store(self):
110 self.__plugin_api.save_configuration_object(self.PLUGIN_NAME,
111
112=== modified file 'GTG/plugins/task_reaper/reaper.py'
113--- GTG/plugins/task_reaper/reaper.py 2012-07-13 17:24:28 +0000
114+++ GTG/plugins/task_reaper/reaper.py 2013-01-18 16:20:28 +0000
115@@ -165,12 +165,9 @@
116 self.preferences_dialog.hide()
117
118 def preferences_load(self):
119- data = self.plugin_api.load_configuration_object(self.PLUGIN_NAME,
120- "preferences")
121- if data == None or type(data) != type(dict()):
122- self.preferences = self.DEFAULT_PREFERENCES
123- else:
124- self.preferences = data
125+ self.preferences = self.plugin_api.load_configuration_object(
126+ self.PLUGIN_NAME, "preferences",
127+ default_values = self.DEFAULT_PREFERENCES)
128
129 def preferences_store(self):
130 self.plugin_api.save_configuration_object(self.PLUGIN_NAME,
131
132=== modified file 'GTG/plugins/untouched_tasks/untouchedTasks.py'
133--- GTG/plugins/untouched_tasks/untouchedTasks.py 2012-12-14 12:14:01 +0000
134+++ GTG/plugins/untouched_tasks/untouchedTasks.py 2013-01-18 16:20:28 +0000
135@@ -180,12 +180,9 @@
136 self.preferences_dialog.hide()
137
138 def preferences_load(self):
139- data = self.plugin_api.load_configuration_object(self.PLUGIN_NAME,
140- "preferences")
141- if data == None or type(data) != type(dict()):
142- self.preferences = self.DEFAULT_PREFERENCES
143- else:
144- self.preferences = data
145+ self.preferences = self.plugin_api.load_configuration_object(
146+ self.PLUGIN_NAME, "preferences",
147+ default_values = self.DEFAULT_PREFERENCES)
148
149 def preferences_store(self):
150 self.plugin_api.save_configuration_object(self.PLUGIN_NAME,
151
152=== modified file 'GTG/plugins/urgency_color/urgency_color.py'
153--- GTG/plugins/urgency_color/urgency_color.py 2012-12-23 08:47:15 +0000
154+++ GTG/plugins/urgency_color/urgency_color.py 2013-01-18 16:20:28 +0000
155@@ -212,27 +212,19 @@
156 self.prefs_update_widgets()
157
158 def prefs_load(self):
159- data = self._plugin_api.load_configuration_object( \
160- self.PLUGIN_NAME,
161- 'preferences')
162- if not data or not isinstance(data, dict):
163- self._pref_data = dict(self.DEFAULT_PREFS)
164- else:
165- # CORRECT NAMES FROM OLD PREFERENCES
166- # This is a dirty fix and thus should be removed in a
167- # distant future, when nobody has "red", "yellow" or "green"
168- # settings
169- namepairs = {'red':'high','yellow':'normal','green':'low'}
170- for key,val in data.iteritems():
171- for oldname,newname in namepairs.iteritems():
172- if key == "color_"+oldname:
173- data['color_'+newname] = data.pop(key)
174- # Add new preferences where not present
175- for setting in self.DEFAULT_PREFS.iterkeys():
176- if setting not in data:
177- data[setting] = self.DEFAULT_PREFS[setting]
178- self._pref_data = dict(data)
179+ self._pref_data = self._plugin_api.load_configuration_object(
180+ self.PLUGIN_NAME, "preferences",
181+ default_values = self.DEFAULT_PREFS)
182
183+ # CORRECT NAMES FROM OLD PREFERENCES
184+ # This is a dirty fix and thus should be removed in a
185+ # distant future, when nobody has "red", "yellow" or "green"
186+ # settings
187+ namepairs = {'red':'high', 'yellow':'normal', 'green':'low'}
188+ for oldname, newname in namepairs.iteritems():
189+ old_key, new_key = "color_" + oldname, "color_" + newname
190+ if old_key in self._pref_data:
191+ self._pref_data[new_key] = self._pref_data.pop(old_key)
192
193 def prefs_store(self):
194 self._plugin_api.save_configuration_object( \

Subscribers

People subscribed via source and target branches

to status/vote changes: