Merge lp:~jtatum/mago/gconf-docupd into lp:~mago-contributors/mago/mago-1.0
- gconf-docupd
- Merge into mago-1.0
Proposed by
James Tatum
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~jtatum/mago/gconf-docupd |
Merge into: | lp:~mago-contributors/mago/mago-1.0 |
Diff against target: |
255 lines 1 file modified
mago/gconfwrapper.py (+94/-42) |
To merge this branch: | bzr merge lp:~jtatum/mago/gconf-docupd |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ara Pulido | Approve | ||
Nagappan Alagappan | Approve | ||
Javier Collado (community) | Approve | ||
Review via email: mp+12413@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
James Tatum (jtatum) wrote : | # |
Revision history for this message
Javier Collado (javier.collado) wrote : | # |
Looks good to me. Documentation improvements are always welcome.
review:
Approve
Revision history for this message
Nagappan Alagappan (nagappan) : | # |
review:
Approve
Revision history for this message
Ara Pulido (ara) wrote : | # |
Approved, merged and documentation updated at http://
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'mago/gconfwrapper.py' | |||
2 | --- mago/gconfwrapper.py 2009-09-03 13:06:36 +0000 | |||
3 | +++ mago/gconfwrapper.py 2009-09-25 13:40:25 +0000 | |||
4 | @@ -8,29 +8,44 @@ | |||
5 | 8 | class GConf: | 8 | class GConf: |
6 | 9 | """ | 9 | """ |
7 | 10 | This class provides a handy interface for manipulating a gconf key. | 10 | This class provides a handy interface for manipulating a gconf key. |
9 | 11 | 11 | ||
10 | 12 | >>> from gconfwrapper import GConf | 12 | >>> from gconfwrapper import GConf |
11 | 13 | >>> interface=GConf("/desktop/gnome/interface/") | 13 | >>> interface=GConf("/desktop/gnome/interface/") |
12 | 14 | >>> interface["accessibility"] | 14 | >>> interface["accessibility"] |
14 | 15 | True | 15 | False |
15 | 16 | >>> interface["gtk_theme"]="Human" | 16 | >>> interface["gtk_theme"]="Human" |
16 | 17 | |||
17 | 18 | This class can also be used without being instantiated. | ||
18 | 19 | |||
19 | 17 | >>> GConf.get_item('/desktop/gnome/interface/accessibility') | 20 | >>> GConf.get_item('/desktop/gnome/interface/accessibility') |
22 | 18 | True | 21 | False |
23 | 19 | 22 | >>> GConf.set_item('/desktop/gnome/interface/accessibility') | |
24 | 23 | |||
25 | 20 | The instance methods support the __getitem__ and __setitem__ special methods | 24 | The instance methods support the __getitem__ and __setitem__ special methods |
26 | 21 | automatically invoke get_item and set_item to handle properties. Generally, | 25 | automatically invoke get_item and set_item to handle properties. Generally, |
27 | 22 | instance methods should not be called directly. | 26 | instance methods should not be called directly. |
29 | 23 | 27 | ||
30 | 24 | The static methods are for convenience, avoiding instance instantiation when | 28 | The static methods are for convenience, avoiding instance instantiation when |
31 | 25 | getting or setting only a single gconf key. | 29 | getting or setting only a single gconf key. |
32 | 30 | |||
33 | 31 | @group Automatically invoked accessors: *value, *string, *bool, *int, | ||
34 | 32 | *float, __getitem__, __setitem__, _get_type | ||
35 | 26 | """ | 33 | """ |
36 | 34 | |||
37 | 35 | |||
38 | 27 | class GConfError(Exception): | 36 | class GConfError(Exception): |
39 | 28 | """ | 37 | """ |
41 | 29 | Exception class for GConf exceptions. | 38 | Exception class for GConf exceptions |
42 | 30 | """ | 39 | """ |
43 | 31 | pass | 40 | pass |
44 | 32 | 41 | ||
45 | 33 | def __init__ (self, domain): | 42 | def __init__ (self, domain): |
46 | 43 | """ | ||
47 | 44 | Constructor for the GConf class | ||
48 | 45 | |||
49 | 46 | @param domain: The GConf domain, for instance: /desktop/gnome/interface/ | ||
50 | 47 | @type domain: string | ||
51 | 48 | """ | ||
52 | 34 | self._domain = domain | 49 | self._domain = domain |
53 | 35 | self._gconf_client = gconf.client_get_default () | 50 | self._gconf_client = gconf.client_get_default () |
54 | 36 | 51 | ||
55 | @@ -52,23 +67,51 @@ | |||
56 | 52 | return 'bool' | 67 | return 'bool' |
57 | 53 | else: | 68 | else: |
58 | 54 | raise self.GConfError, 'unsupported type: %s' % str (KeyType) | 69 | raise self.GConfError, 'unsupported type: %s' % str (KeyType) |
60 | 55 | 70 | ||
61 | 56 | # Public functions | 71 | # Public functions |
62 | 57 | 72 | ||
63 | 58 | def set_domain (self, domain): | 73 | def set_domain (self, domain): |
64 | 74 | """ | ||
65 | 75 | Change the domain of the current GConf instance | ||
66 | 76 | |||
67 | 77 | @param domain: New domain to use | ||
68 | 78 | @type domain: string | ||
69 | 79 | """ | ||
70 | 59 | self._domain = domain | 80 | self._domain = domain |
72 | 60 | 81 | ||
73 | 61 | def get_domain (self): | 82 | def get_domain (self): |
74 | 83 | """ | ||
75 | 84 | Get the domain of the current GConf instance | ||
76 | 85 | |||
77 | 86 | @return: Domain of the current GConf instance | ||
78 | 87 | @rtype: string | ||
79 | 88 | """ | ||
80 | 62 | return self._domain | 89 | return self._domain |
82 | 63 | 90 | ||
83 | 64 | def get_gconf_client (self): | 91 | def get_gconf_client (self): |
84 | 92 | """ | ||
85 | 93 | Access the pygtk GConf client | ||
86 | 94 | |||
87 | 95 | @return: The pygtk GConf client | ||
88 | 96 | @rtype: pygtk gconf object | ||
89 | 97 | """ | ||
90 | 65 | return self._gconf_client | 98 | return self._gconf_client |
92 | 66 | 99 | ||
93 | 67 | def get_value (self, key): | 100 | def get_value (self, key): |
95 | 68 | '''returns the value of key `key' ''' | 101 | ''' |
96 | 102 | Returns the value of key 'key' | ||
97 | 103 | |||
98 | 104 | This is automatically invoked by the instance __getitem__ and | ||
99 | 105 | __setitem__ so should not need to be called directly. | ||
100 | 106 | |||
101 | 107 | @param key: Target key to get | ||
102 | 108 | @type key: string | ||
103 | 109 | @return: Current value of target key | ||
104 | 110 | @rtype: bool, int, string, float | ||
105 | 111 | ''' | ||
106 | 69 | if '/' in key: | 112 | if '/' in key: |
107 | 70 | raise self.GConfError, 'key must not contain /' | 113 | raise self.GConfError, 'key must not contain /' |
109 | 71 | 114 | ||
110 | 72 | value = self._gconf_client.get (self._domain + key) | 115 | value = self._gconf_client.get (self._domain + key) |
111 | 73 | ValueType = value.type | 116 | ValueType = value.type |
112 | 74 | if ValueType == VALUE_BOOL: | 117 | if ValueType == VALUE_BOOL: |
113 | @@ -79,84 +122,94 @@ | |||
114 | 79 | return value.get_string () | 122 | return value.get_string () |
115 | 80 | elif ValueType == VALUE_FLOAT: | 123 | elif ValueType == VALUE_FLOAT: |
116 | 81 | return value.get_float () | 124 | return value.get_float () |
118 | 82 | 125 | ||
119 | 83 | def set_value (self, key, value): | 126 | def set_value (self, key, value): |
121 | 84 | '''sets the value of key `key' to `value' ''' | 127 | ''' |
122 | 128 | Sets the value of key 'key' to 'value' | ||
123 | 129 | |||
124 | 130 | This is automatically invoked by the instance __getitem__ and | ||
125 | 131 | __setitem__ so should not need to be called directly. | ||
126 | 132 | |||
127 | 133 | @param key: Target key to set | ||
128 | 134 | @type key: string | ||
129 | 135 | @param value: Value to set | ||
130 | 136 | @type value: bool, int, string, float | ||
131 | 137 | ''' | ||
132 | 85 | value_type = self._get_type (value) | 138 | value_type = self._get_type (value) |
134 | 86 | 139 | ||
135 | 87 | if '/' in key: | 140 | if '/' in key: |
136 | 88 | raise self.GConfError, 'key must not contain /' | 141 | raise self.GConfError, 'key must not contain /' |
138 | 89 | 142 | ||
139 | 90 | func = getattr (self._gconf_client, 'set_' + value_type) | 143 | func = getattr (self._gconf_client, 'set_' + value_type) |
140 | 91 | apply (func, (self._domain + key, value)) | 144 | apply (func, (self._domain + key, value)) |
142 | 92 | 145 | ||
143 | 93 | def get_string (self, key): | 146 | def get_string (self, key): |
144 | 94 | if '/' in key: | 147 | if '/' in key: |
145 | 95 | raise self.GConfError, 'key must not contain /' | 148 | raise self.GConfError, 'key must not contain /' |
147 | 96 | 149 | ||
148 | 97 | return self._gconf_client.get_string (self._domain + key) | 150 | return self._gconf_client.get_string (self._domain + key) |
150 | 98 | 151 | ||
151 | 99 | def set_string (self, key, value): | 152 | def set_string (self, key, value): |
152 | 100 | if type (value) != StringType: | 153 | if type (value) != StringType: |
153 | 101 | raise self.GConfError, 'value must be a string' | 154 | raise self.GConfError, 'value must be a string' |
154 | 102 | if '/' in key: | 155 | if '/' in key: |
155 | 103 | raise self.GConfError, 'key must not contain /' | 156 | raise self.GConfError, 'key must not contain /' |
157 | 104 | 157 | ||
158 | 105 | self._gconf_client.set_string (self._domain + key, value) | 158 | self._gconf_client.set_string (self._domain + key, value) |
160 | 106 | 159 | ||
161 | 107 | def get_bool (self, key): | 160 | def get_bool (self, key): |
162 | 108 | if '/' in key: | 161 | if '/' in key: |
163 | 109 | raise self.GConfError, 'key must not contain /' | 162 | raise self.GConfError, 'key must not contain /' |
165 | 110 | 163 | ||
166 | 111 | return self._gconf_client.get_bool (self._domain + key) | 164 | return self._gconf_client.get_bool (self._domain + key) |
168 | 112 | 165 | ||
169 | 113 | def set_bool (self, key, value): | 166 | def set_bool (self, key, value): |
170 | 114 | if type (value) != IntType and \ | 167 | if type (value) != IntType and \ |
171 | 115 | (key != 0 or key != 1): | 168 | (key != 0 or key != 1): |
172 | 116 | raise self.GConfError, 'value must be a boolean' | 169 | raise self.GConfError, 'value must be a boolean' |
173 | 117 | if '/' in key: | 170 | if '/' in key: |
174 | 118 | raise self.GConfError, 'key must not contain /' | 171 | raise self.GConfError, 'key must not contain /' |
176 | 119 | 172 | ||
177 | 120 | self._gconf_client.set_bool (self._domain + key, value) | 173 | self._gconf_client.set_bool (self._domain + key, value) |
179 | 121 | 174 | ||
180 | 122 | def get_int (self, key): | 175 | def get_int (self, key): |
181 | 123 | if '/' in key: | 176 | if '/' in key: |
182 | 124 | raise self.GConfError, 'key must not contain /' | 177 | raise self.GConfError, 'key must not contain /' |
184 | 125 | 178 | ||
185 | 126 | return self._gconf_client.get_int (self._domain + key) | 179 | return self._gconf_client.get_int (self._domain + key) |
187 | 127 | 180 | ||
188 | 128 | def set_int (self, key, value): | 181 | def set_int (self, key, value): |
189 | 129 | if type (value) != IntType: | 182 | if type (value) != IntType: |
190 | 130 | raise self.GConfError, 'value must be an int' | 183 | raise self.GConfError, 'value must be an int' |
191 | 131 | if '/' in key: | 184 | if '/' in key: |
192 | 132 | raise self.GConfError, 'key must not contain /' | 185 | raise self.GConfError, 'key must not contain /' |
194 | 133 | 186 | ||
195 | 134 | self._gconf_client.set_int (self._domain + key, value) | 187 | self._gconf_client.set_int (self._domain + key, value) |
197 | 135 | 188 | ||
198 | 136 | def get_float (self, key): | 189 | def get_float (self, key): |
199 | 137 | if '/' in key: | 190 | if '/' in key: |
200 | 138 | raise self.GConfError, 'key must not contain /' | 191 | raise self.GConfError, 'key must not contain /' |
202 | 139 | 192 | ||
203 | 140 | return self._gconf_client.get_float (self._domain + key) | 193 | return self._gconf_client.get_float (self._domain + key) |
205 | 141 | 194 | ||
206 | 142 | def set_float (self, key, value): | 195 | def set_float (self, key, value): |
207 | 143 | if type (value) != FloatType: | 196 | if type (value) != FloatType: |
208 | 144 | raise self.GConfError, 'value must be an float' | 197 | raise self.GConfError, 'value must be an float' |
210 | 145 | 198 | ||
211 | 146 | if '/' in key: | 199 | if '/' in key: |
212 | 147 | raise self.GConfError, 'key must not contain /' | 200 | raise self.GConfError, 'key must not contain /' |
214 | 148 | 201 | ||
215 | 149 | self._gconf_client.set_float (self._domain + key, value) | 202 | self._gconf_client.set_float (self._domain + key, value) |
217 | 150 | 203 | ||
218 | 151 | # Some even simpler methods for the truly lazy | 204 | # Some even simpler methods for the truly lazy |
219 | 152 | @staticmethod | 205 | @staticmethod |
220 | 153 | def get_item(key): | 206 | def get_item(key): |
221 | 154 | """ | 207 | """ |
224 | 155 | Pass this a key and it will return the value. | 208 | Pass this a key and it will return the value |
225 | 156 | 209 | ||
226 | 157 | >>> GConf.get_item("/desktop/gnome/interface/accessibility") | 210 | >>> GConf.get_item("/desktop/gnome/interface/accessibility") |
227 | 158 | True | 211 | True |
229 | 159 | 212 | ||
230 | 160 | @type key: string | 213 | @type key: string |
231 | 161 | @param key: The gconf path to the target key | 214 | @param key: The gconf path to the target key |
232 | 162 | @rtype: string, int, bool, float | 215 | @rtype: string, int, bool, float |
233 | @@ -170,10 +223,10 @@ | |||
234 | 170 | @staticmethod | 223 | @staticmethod |
235 | 171 | def set_item(key, value): | 224 | def set_item(key, value): |
236 | 172 | """ | 225 | """ |
239 | 173 | Set key to value provided: | 226 | Set key to value provided |
240 | 174 | 227 | ||
241 | 175 | >>> GConf.set_item("/desktop/gnome/interface/accessibility", True) | 228 | >>> GConf.set_item("/desktop/gnome/interface/accessibility", True) |
243 | 176 | 229 | ||
244 | 177 | @type key: string | 230 | @type key: string |
245 | 178 | @param key: The gconf path to the target key | 231 | @param key: The gconf path to the target key |
246 | 179 | @type value: string, int, bool, float | 232 | @type value: string, int, bool, float |
247 | @@ -194,7 +247,6 @@ | |||
248 | 194 | print "Accessibility: %s" % GConf.get_item('/desktop/gnome/interface/accessibility') | 247 | print "Accessibility: %s" % GConf.get_item('/desktop/gnome/interface/accessibility') |
249 | 195 | GConf.set_item('/apps/test-gconf/foobar', True) | 248 | GConf.set_item('/apps/test-gconf/foobar', True) |
250 | 196 | GConf.set_item('/apps/test-gconf/barfoo', False) | 249 | GConf.set_item('/apps/test-gconf/barfoo', False) |
252 | 197 | 250 | ||
253 | 198 | if __name__ == '__main__': | 251 | if __name__ == '__main__': |
254 | 199 | test() | 252 | test() |
255 | 200 |
Adding/cleaning epydoc and indentation in gconfwrapper.