Merge lp:~robru/gwibber/libmessaging into lp:~barry/gwibber/py3
- libmessaging
- Merge into py3
Proposed by
Robert Bruce Park
Status: | Merged |
---|---|
Merged at revision: | 1402 |
Proposed branch: | lp:~robru/gwibber/libmessaging |
Merge into: | lp:~barry/gwibber/py3 |
Diff against target: |
361 lines (+47/-136) 1 file modified
gwibber/microblog/dispatcher.py (+47/-136) |
To merge this branch: | bzr merge lp:~robru/gwibber/libmessaging |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Barry Warsaw | Approve | ||
Review via email: mp+121483@code.launchpad.net |
Commit message
Description of the change
Merged in larsu's libmessaging work and updated for py3 using 2to3.
To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'gwibber/microblog/dispatcher.py' |
2 | --- gwibber/microblog/dispatcher.py 2012-08-21 22:53:27 +0000 |
3 | +++ gwibber/microblog/dispatcher.py 2012-08-27 19:43:20 +0000 |
4 | @@ -14,7 +14,7 @@ |
5 | import logging |
6 | logger = logging.getLogger("Dispatcher") |
7 | |
8 | -from util.const import * |
9 | +from .util.const import * |
10 | import subprocess |
11 | |
12 | try: |
13 | @@ -30,14 +30,14 @@ |
14 | # Try to import * from custom, install custom.py to include packaging |
15 | # customizations like distro API keys, etc |
16 | try: |
17 | - from util.custom import * |
18 | + from .util.custom import * |
19 | except: |
20 | pass |
21 | |
22 | try: |
23 | - from gi.repository import Indicate as indicate |
24 | + from gi.repository import MessagingMenu |
25 | except: |
26 | - indicate = None |
27 | + MessagingMenu = None |
28 | |
29 | GLib.threads_init() |
30 | |
31 | @@ -69,7 +69,7 @@ |
32 | self.callback_failure = callback_failure |
33 | |
34 | def run(self): |
35 | - self.id = ''.join(random.choice(string.letters) for i in xrange(5)) |
36 | + self.id = ''.join(random.choice(string.ascii_letters) for i in range(5)) |
37 | self.t_start = datetime.now() |
38 | (account, opname, args, transient) = self.job |
39 | try: |
40 | @@ -87,7 +87,7 @@ |
41 | if message_data is not None: |
42 | for m in message_data: |
43 | try: |
44 | - if isinstance(m, dict) and m.has_key("mid"): |
45 | + if isinstance(m, dict) and "mid" in m: |
46 | m["id"] = uuid.uuid1().hex |
47 | m["operation"] = opname |
48 | m["stream"] = m.get("stream", stream) |
49 | @@ -95,7 +95,7 @@ |
50 | m["time"] = m.get("time", 0) |
51 | if not m["text"]: m["text"] = "" |
52 | m["rtl"] = util.isRTL(re.sub(text_cleaner, "", m["text"])) |
53 | - if m.has_key("type"): |
54 | + if "type" in m: |
55 | if m["type"] == "link": m["stream"] = "links" |
56 | if m["type"] == "video": m["stream"] = "videos" |
57 | if m["type"] == "photo": m["stream"] = "images" |
58 | @@ -117,7 +117,7 @@ |
59 | m.get("reply", {}).get("nick", None), |
60 | json.dumps(m) |
61 | )) |
62 | - elif isinstance(m, dict) and m.has_key("error"): |
63 | + elif isinstance(m, dict) and "error" in m: |
64 | new_messages.insert(0, ( |
65 | "error", |
66 | json.dumps(m) |
67 | @@ -170,10 +170,10 @@ |
68 | def on_account_changed(self, account_service, account): |
69 | iterator = account.get_settings_iter(None) |
70 | settings = [] |
71 | - (ok, key, value) = iterator.next() |
72 | + (ok, key, value) = next(iterator) |
73 | while ok: |
74 | settings.append((key, value)) |
75 | - (ok, key, value) = iterator.next() |
76 | + (ok, key, value) = next(iterator) |
77 | |
78 | settings.sort() |
79 | for (key, value) in settings: |
80 | @@ -201,7 +201,7 @@ |
81 | def __contains__(self, key): |
82 | return key in self._dict |
83 | def __iter__(self): |
84 | - return self._dict.iteritems() |
85 | + return iter(self._dict.items()) |
86 | |
87 | |
88 | class OperationCollector: |
89 | @@ -269,10 +269,10 @@ |
90 | yield (account, "search", args, search["id"]) |
91 | |
92 | def account_to_operations(self, acct): |
93 | - if isinstance(acct, basestring): |
94 | + if isinstance(acct, str): |
95 | acct = self.get_account(acct) |
96 | |
97 | - if SERVICES.has_key(acct["service"]): |
98 | + if acct["service"] in SERVICES: |
99 | for opname in SERVICES[acct["service"]]["default_streams"]: |
100 | if self.validate_operation(acct, opname): |
101 | args = self.handle_max_id(acct, opname) |
102 | @@ -328,41 +328,23 @@ |
103 | self.connection_monitor.connect_to_signal("ConnectionOnline", self.on_connection_online) |
104 | self.connection_monitor.connect_to_signal("ConnectionOffline", self.on_connection_offline) |
105 | |
106 | - self.indicator_items = {} |
107 | + self.stream_names = { 'messages': _('Messages'), 'replies': _('Replies'), 'private': _('Private') } |
108 | self.notified_items = [] |
109 | self.notified_errors = {} |
110 | - self.messages_indicator = None |
111 | - self.replies_indicator = None |
112 | - self.private_indicator = None |
113 | self.unseen_counts = {} |
114 | for s in "messages", "replies", "private": |
115 | self.unseen_counts[s] = 0 |
116 | |
117 | - self.indicate = None |
118 | + self.mmapp = None |
119 | self.launcher = None |
120 | |
121 | self.job_list = [] |
122 | |
123 | - if indicate and util.resources.get_desktop_file(): |
124 | - self.indicate = indicate.Server.ref_default () |
125 | - self.indicate.set_type("message.gwibber") |
126 | - self.indicate.set_desktop_file(util.resources.get_desktop_file()) |
127 | - if Dbusmenu and self.indicate: |
128 | - post_menu = Dbusmenu.Menuitem.new () |
129 | - post_menu.property_set (Dbusmenu.MENUITEM_PROP_LABEL, _("Update Status")) |
130 | - post_menu.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True) |
131 | - post_menu.connect("item-activated", self.show_poster) |
132 | - |
133 | - menu_server = Dbusmenu.Server.new("/messaging/commands") |
134 | - root = Dbusmenu.Menuitem.new () |
135 | - root.child_append (post_menu) |
136 | - menu_server.set_root(root) |
137 | - self.indicate.set_menu (menu_server) |
138 | - self.indicate.connect("server-display", self.on_indicator_server_activate) |
139 | - self.indicate.connect("interest-added", self.on_indicator_interest_added) |
140 | - self.indicate.connect("interest-removed", self.on_indicator_interest_removed) |
141 | - self.update_indicators(self.unseen_counts) |
142 | - self.indicate.show() |
143 | + if MessagingMenu: |
144 | + self.mmapp = MessagingMenu.App(desktop_id='gwibber.desktop') |
145 | + self.mmapp.register() |
146 | + self.mmapp.connect('activate-source', self.on_messaging_menu_source_activated) |
147 | + self.update_message_sources(self.unseen_counts) |
148 | |
149 | if Unity and Dbusmenu: |
150 | self.launcher = Unity.LauncherEntry.get_for_desktop_id ("gwibber.desktop") |
151 | @@ -565,7 +547,7 @@ |
152 | service = dbus.Interface(obj, "com.Gwibber.Service") |
153 | service.UpdateIndicators("stream") |
154 | """ |
155 | - self.handle_indicator_counts(stream) |
156 | + self.handle_message_source_counts(stream) |
157 | |
158 | @dbus.service.method("com.Gwibber.Service", in_signature="s") |
159 | def SendMessage(self, message): |
160 | @@ -702,28 +684,6 @@ |
161 | logger.info("Gwibber Service is being shutdown") |
162 | self.mainloop.quit() |
163 | |
164 | - @dbus.service.method("com.Gwibber.Service", out_signature="b") |
165 | - def IndicatorInterestCheck(self): |
166 | - """ |
167 | - Check for interest from the messaging menu indicator |
168 | - Returns a boolean |
169 | - example: |
170 | - import dbus |
171 | - obj = dbus.SessionBus().get_object("com.Gwibber.Service", "/com/gwibber/Service") |
172 | - service = dbus.Interface(obj, "com.Gwibber.Service") |
173 | - res = service.IndicatorInterestCheck() |
174 | - """ |
175 | - if indicate and self.indicate: |
176 | - return self.indicate.check_interest(indicate.INTEREST_SERVER_DISPLAY) |
177 | - else: |
178 | - return False |
179 | - |
180 | - @dbus.service.signal("com.Gwibber.Service") |
181 | - def IndicatorInterestAdded(self): pass |
182 | - |
183 | - @dbus.service.signal("com.Gwibber.Service") |
184 | - def IndicatorInterestRemoved(self): pass |
185 | - |
186 | @dbus.service.signal("com.Gwibber.Service", signature="s") |
187 | def Error(self, error): pass |
188 | |
189 | @@ -736,12 +696,12 @@ |
190 | if not isinstance(error, dict): |
191 | logger.error("Failed to parse error message: %s", error) |
192 | return |
193 | - if error.has_key("error"): |
194 | + if "error" in error: |
195 | error = json.loads(error)["error"] |
196 | else: |
197 | error = json.loads(error) |
198 | |
199 | - if self.notified_errors.has_key(error["account"]["service"]): |
200 | + if error["account"]["service"] in self.notified_errors: |
201 | if self.notified_errors[error["account"]["service"]] == error["message"]: |
202 | return |
203 | if util.can_notify: |
204 | @@ -803,7 +763,7 @@ |
205 | ",".join(self.messages.columns), |
206 | ",".join("?" * len(self.messages.columns))), items) |
207 | |
208 | - GLib.idle_add (self.update_indicators, self.unseen_counts) |
209 | + GLib.idle_add (self.update_message_sources, self.unseen_counts) |
210 | query = """ |
211 | SELECT * FROM messages WHERE rowid > {0} AND |
212 | ((operation == "receive" AND to_me = 0) OR |
213 | @@ -831,44 +791,18 @@ |
214 | self.LoadingComplete() |
215 | logger.info("Loading complete: %s - %s", self.refresh_count, output.rowcount if output.rowcount > 0 else 0) |
216 | |
217 | - def update_indicators(self, counts): |
218 | + def update_message_sources(self, counts): |
219 | total_unseen = 0 |
220 | - if indicate: |
221 | - if counts.has_key("messages"): |
222 | - if not self.messages_indicator: |
223 | - self.messages_indicator = indicate.Indicator() if hasattr(indicate, "Indicator") else indicate.IndicatorMessage() |
224 | - self.messages_indicator.connect("user-display", self.on_indicator_activate) |
225 | - self.messages_indicator.set_property("name", _("Messages")) |
226 | - self.messages_indicator.set_property("stream", "messages") |
227 | - self.messages_indicator.show() |
228 | - self.messages_indicator.set_property("count", str(counts["messages"])) |
229 | - total_unseen += counts["messages"] |
230 | - if self.messages_indicator not in self.indicator_items: |
231 | - self.indicator_items["messages"] = self.messages_indicator |
232 | - if counts.has_key("replies"): |
233 | - if not self.replies_indicator: |
234 | - self.replies_indicator = indicate.Indicator() if hasattr(indicate, "Indicator") else indicate.IndicatorMessage() |
235 | - self.replies_indicator.connect("user-display", self.on_indicator_activate) |
236 | - self.replies_indicator.set_property("name", _("Replies")) |
237 | - self.replies_indicator.set_property("stream", "replies") |
238 | - self.replies_indicator.show() |
239 | - self.replies_indicator.set_property("count", str(counts["replies"])) |
240 | - total_unseen += counts["replies"] |
241 | - if self.replies_indicator not in self.indicator_items: |
242 | - self.indicator_items["replies"] = self.replies_indicator |
243 | - if counts.has_key("private"): |
244 | - if not self.private_indicator: |
245 | - self.private_indicator = indicate.Indicator() if hasattr(indicate, "Indicator") else indicate.IndicatorMessage() |
246 | - self.private_indicator.connect("user-display", self.on_indicator_activate) |
247 | - self.private_indicator.set_property("name", _("Private")) |
248 | - self.private_indicator.set_property("stream", "private") |
249 | - self.private_indicator.show() |
250 | - self.private_indicator.set_property("count", str(counts["private"])) |
251 | - total_unseen += counts["private"] |
252 | - if counts["private"] > 0: |
253 | - self.private_indicator.set_property_bool("draw-attention", True) |
254 | - if self.private_indicator not in self.indicator_items: |
255 | - self.indicator_items["private"] = self.private_indicator |
256 | + pos = 0 |
257 | + if self.mmapp: |
258 | + for source, name in self.stream_names.items(): |
259 | + if source in counts and counts[source] > 0: |
260 | + if self.mmapp.has_source(source): |
261 | + self.mmapp.set_source_count(source, counts[source]) |
262 | + else: |
263 | + self.mmapp.insert_source_with_count(pos, source, None, name, counts[source]) |
264 | + total_unseen += counts[source] |
265 | + pos += 1 |
266 | if Unity and self.launcher: |
267 | self.launcher.set_property("count", total_unseen) |
268 | if total_unseen < 1: |
269 | @@ -877,28 +811,10 @@ |
270 | self.launcher.set_property("count_visible", True) |
271 | return False |
272 | |
273 | - def on_indicator_interest_added(self, server, interest): |
274 | - self.IndicatorInterestAdded() |
275 | - |
276 | - def on_indicator_interest_removed(self, server, interest): |
277 | - self.IndicatorInterestRemoved() |
278 | - |
279 | - def on_indicator_server_activate(self, indicator, timestamp=None): |
280 | - dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
281 | - client_bus = dbus.SessionBus() |
282 | - logger.debug("Raising gwibber client") |
283 | - try: |
284 | - self.handle_indicator_counts() |
285 | - except: |
286 | - pass |
287 | - self.show_client() |
288 | - |
289 | - def on_indicator_activate(self, indicator, timestamp=None): |
290 | - if not indicate: return |
291 | - stream = indicator.get_property("stream") |
292 | + def on_messaging_menu_source_activated(self, mmapp, stream): |
293 | logger.debug("Raising gwibber client, focusing %s stream", stream) |
294 | try: |
295 | - self.handle_indicator_counts(stream) |
296 | + self.handle_message_source_counts(stream) |
297 | except: |
298 | pass |
299 | self.show_client(stream=stream) |
300 | @@ -909,29 +825,24 @@ |
301 | def handle_focus_error(self, *args): |
302 | logger.error("Failed to raise client %s", args) |
303 | |
304 | - def handle_indicator_counts(self, stream=None): |
305 | - if indicate: |
306 | + def handle_message_source_counts(self, stream=None): |
307 | + if self.mmapp: |
308 | if not stream or stream == "home": |
309 | - for s in self.indicator_items.keys(): |
310 | - self.indicator_items[s].set_property("count", str(0)) |
311 | - self.unseen_counts[s] = 0 |
312 | - if s == "private": |
313 | - self.private_indicator.set_property_bool("draw-attention", False) |
314 | + for s in self.stream_names.keys(): |
315 | + self.mmapp.remove_source(s) |
316 | if self.launcher: |
317 | self.launcher.set_property("count", 0) |
318 | self.launcher.set_property("count_visible", False) |
319 | return |
320 | - if self.indicator_items.has_key(stream): |
321 | - self.indicator_items[stream].set_property("count", str(0)) |
322 | - if stream == "private": |
323 | - self.private_indicator.set_property_bool("draw-attention", False) |
324 | + else: |
325 | + self.mmapp.remove_source(stream) |
326 | |
327 | self.unseen_counts[stream] = 0 |
328 | if Unity: |
329 | total_unseen = 0 |
330 | for s in self.unseen_counts.keys(): |
331 | total_unseen += self.unseen_counts[s] |
332 | - logger.debug ("handle_indicator_counts total_unseen is %d", total_unseen) |
333 | + logger.debug ("handle_message_source_counts total_unseen is %d", total_unseen) |
334 | if self.launcher: |
335 | self.launcher.set_property("count", total_unseen) |
336 | if total_unseen < 1: |
337 | @@ -966,14 +877,14 @@ |
338 | notify_text = "" |
339 | if len(message["text"]) > 0: |
340 | notify_text = message["text"] |
341 | - elif message.has_key("stream"): |
342 | + elif "stream" in message: |
343 | if message["stream"] == "images": |
344 | notify_text = _("has shared a photo") |
345 | if message["stream"] == "links": |
346 | notify_text = _("has shared a link") |
347 | if message["stream"] == "videos": |
348 | notify_text = _("has shared a video") |
349 | - if message["sender"].has_key("image"): |
350 | + if "image" in message["sender"]: |
351 | image = util.resources.get_avatar_path(message["sender"]["image"]) |
352 | else: |
353 | #FIXME - we need to fix finding service icons in the service |
354 | @@ -986,7 +897,7 @@ |
355 | return False |
356 | |
357 | def cache_avatar (self, message): |
358 | - if message["sender"].has_key("image"): |
359 | + if "image" in message["sender"]: |
360 | util.resources.get_avatar_path(message["sender"]["image"]) |
361 | return False |
362 |