comparison frontends/src/quick_frontend/quick_app.py @ 1009:d1084f7e56a5

quick_frontend: use of new logging system
author Goffi <goffi@goffi.org>
date Mon, 05 May 2014 18:58:34 +0200
parents cad958239b5c
children 6a16ec17a458
comparison
equal deleted inserted replaced
1008:d70d4fe5c5f8 1009:d1084f7e56a5
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 import sys 21 import sys
22 from logging import debug, info, warning, error 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 from sat.tools.jid import JID 24 from sat.tools.jid import JID
24 from sat_frontends.bridge.DBus import DBusBridgeFrontend 25 from sat_frontends.bridge.DBus import DBusBridgeFrontend
25 from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError 26 from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError
26 from sat_frontends.quick_frontend.quick_utils import escapePrivate, unescapePrivate 27 from sat_frontends.quick_frontend.quick_utils import escapePrivate, unescapePrivate
27 from optparse import OptionParser 28 from optparse import OptionParser
94 95
95 def postInit(self): 96 def postInit(self):
96 """Must be called after initialization is done, do all automatic task (auto plug profile)""" 97 """Must be called after initialization is done, do all automatic task (auto plug profile)"""
97 if self.options.profile: 98 if self.options.profile:
98 if not self.bridge.getProfileName(self.options.profile): 99 if not self.bridge.getProfileName(self.options.profile):
99 error(_("Trying to plug an unknown profile (%s)" % self.options.profile)) 100 log.error(_("Trying to plug an unknown profile (%s)" % self.options.profile))
100 else: 101 else:
101 self.plug_profile(self.options.profile) 102 self.plug_profile(self.options.profile)
102 103
103 def check_options(self): 104 def check_options(self):
104 """Check command line options""" 105 """Check command line options"""
115 if self.options.profile: 116 if self.options.profile:
116 self.options.profile = self.options.profile.decode('utf-8') 117 self.options.profile = self.options.profile.decode('utf-8')
117 return args 118 return args
118 119
119 def _getParamError(self, ignore): 120 def _getParamError(self, ignore):
120 error(_("Can't get profile parameter")) 121 log.error(_("Can't get profile parameter"))
121 122
122 def plug_profile(self, profile_key='@DEFAULT@'): 123 def plug_profile(self, profile_key='@DEFAULT@'):
123 """Tell application which profile must be used""" 124 """Tell application which profile must be used"""
124 if self.single_profile and self.profiles: 125 if self.single_profile and self.profiles:
125 error(_('There is already one profile plugged (we are in single profile mode) !')) 126 log.error(_('There is already one profile plugged (we are in single profile mode) !'))
126 return 127 return
127 profile = self.bridge.getProfileName(profile_key) 128 profile = self.bridge.getProfileName(profile_key)
128 if not profile: 129 if not profile:
129 error(_("The profile asked doesn't exist")) 130 log.error(_("The profile asked doesn't exist"))
130 return 131 return
131 if profile in self.profiles: 132 if profile in self.profiles:
132 warning(_("The profile is already plugged")) 133 log.warning(_("The profile is already plugged"))
133 return 134 return
134 self.profiles[profile] = {} 135 self.profiles[profile] = {}
135 if self.single_profile: 136 if self.single_profile:
136 self.profile = profile 137 self.profile = profile
137 138
149 callback=lambda watched: self.plug_profile_4(watched, autoconnect, profile), errback=self._getParamError) 150 callback=lambda watched: self.plug_profile_4(watched, autoconnect, profile), errback=self._getParamError)
150 151
151 def plug_profile_4(self, watched, autoconnect, profile): 152 def plug_profile_4(self, watched, autoconnect, profile):
152 if autoconnect and not self.bridge.isConnected(profile): 153 if autoconnect and not self.bridge.isConnected(profile):
153 #Does the user want autoconnection ? 154 #Does the user want autoconnection ?
154 self.bridge.asyncConnect(profile, callback=lambda: self.plug_profile_5(watched, autoconnect, profile), errback=lambda ignore: error(_('Error during autoconnection'))) 155 self.bridge.asyncConnect(profile, callback=lambda: self.plug_profile_5(watched, autoconnect, profile), errback=lambda ignore: log.error(_('Error during autoconnection')))
155 else: 156 else:
156 self.plug_profile_5(watched, autoconnect, profile) 157 self.plug_profile_5(watched, autoconnect, profile)
157 158
158 def plug_profile_5(self, watched, autoconnect, profile): 159 def plug_profile_5(self, watched, autoconnect, profile):
159 self.profiles[profile]['watched'] = watched.split() # TODO: put this in a plugin 160 self.profiles[profile]['watched'] = watched.split() # TODO: put this in a plugin
201 self.askConfirmation(confirm_id, confirm_type, data, profile) 202 self.askConfirmation(confirm_id, confirm_type, data, profile)
202 203
203 def unplug_profile(self, profile): 204 def unplug_profile(self, profile):
204 """Tell the application to not follow anymore the profile""" 205 """Tell the application to not follow anymore the profile"""
205 if not profile in self.profiles: 206 if not profile in self.profiles:
206 warning(_("This profile is not plugged")) 207 log.warning(_("This profile is not plugged"))
207 return 208 return
208 self.profiles.remove(profile) 209 self.profiles.remove(profile)
209 210
210 def clear_profile(self): 211 def clear_profile(self):
211 self.profiles.clear() 212 self.profiles.clear()
212 213
213 def connected(self, profile): 214 def connected(self, profile):
214 """called when the connection is made""" 215 """called when the connection is made"""
215 if not self.check_profile(profile): 216 if not self.check_profile(profile):
216 return 217 return
217 debug(_("Connected")) 218 log.debug(_("Connected"))
218 self.setStatusOnline(True) 219 self.setStatusOnline(True)
219 220
220 def disconnected(self, profile): 221 def disconnected(self, profile):
221 """called when the connection is closed""" 222 """called when the connection is closed"""
222 if not self.check_profile(profile): 223 if not self.check_profile(profile):
223 return 224 return
224 debug(_("Disconnected")) 225 log.debug(_("Disconnected"))
225 self.contact_list.clearContacts() 226 self.contact_list.clearContacts()
226 self.setStatusOnline(False) 227 self.setStatusOnline(False)
227 228
228 def connectionError(self, error_type, profile): 229 def connectionError(self, error_type, profile):
229 """called when something goes wrong with the connection""" 230 """called when something goes wrong with the connection"""
230 if not self.check_profile(profile): 231 if not self.check_profile(profile):
231 return 232 return
232 debug(_("Connection Error")) 233 log.debug(_("Connection Error"))
233 self.disconnected(profile) 234 self.disconnected(profile)
234 if error_type == "AUTH_ERROR": 235 if error_type == "AUTH_ERROR":
235 self.showDialog(_("Can't connect to account, please check your password"), _("Connection error"), "error") 236 self.showDialog(_("Can't connect to account, please check your password"), _("Connection error"), "error")
236 else: 237 else:
237 error(_('FIXME: error_type %s not implemented') % error_type) 238 log.error(_('FIXME: error_type %s not implemented') % error_type)
238 239
239 def newContact(self, JabberId, attributes, groups, profile): 240 def newContact(self, JabberId, attributes, groups, profile):
240 if not self.check_profile(profile): 241 if not self.check_profile(profile):
241 return 242 return
242 entity = JID(JabberId) 243 entity = JID(JabberId)
300 301
301 def presenceUpdate(self, jabber_id, show, priority, statuses, profile): 302 def presenceUpdate(self, jabber_id, show, priority, statuses, profile):
302 if not self.check_profile(profile): 303 if not self.check_profile(profile):
303 return 304 return
304 305
305 debug(_("presence update for %(jid)s (show=%(show)s, priority=%(priority)s, statuses=%(statuses)s) [profile:%(profile)s]") 306 log.debug(_("presence update for %(jid)s (show=%(show)s, priority=%(priority)s, statuses=%(statuses)s) [profile:%(profile)s]")
306 % {'jid': jabber_id, 'show': show, 'priority': priority, 'statuses': statuses, 'profile': profile}) 307 % {'jid': jabber_id, 'show': show, 'priority': priority, 'statuses': statuses, 'profile': profile})
307 from_jid = JID(jabber_id) 308 from_jid = JID(jabber_id)
308 309
309 if from_jid == self.profiles[profile]['whoami']: 310 if from_jid == self.profiles[profile]['whoami']:
310 if show == "unavailable": 311 if show == "unavailable":
349 350
350 def roomJoined(self, room_jid, room_nicks, user_nick, profile): 351 def roomJoined(self, room_jid, room_nicks, user_nick, profile):
351 """Called when a MUC room is joined""" 352 """Called when a MUC room is joined"""
352 if not self.check_profile(profile): 353 if not self.check_profile(profile):
353 return 354 return
354 debug(_("Room [%(room_jid)s] joined by %(profile)s, users presents:%(users)s") % {'room_jid': room_jid, 'profile': profile, 'users': room_nicks}) 355 log.debug(_("Room [%(room_jid)s] joined by %(profile)s, users presents:%(users)s") % {'room_jid': room_jid, 'profile': profile, 'users': room_nicks})
355 self.chat_wins[room_jid].setUserNick(user_nick) 356 self.chat_wins[room_jid].setUserNick(user_nick)
356 self.chat_wins[room_jid].setType("group") 357 self.chat_wins[room_jid].setType("group")
357 self.chat_wins[room_jid].id = room_jid 358 self.chat_wins[room_jid].id = room_jid
358 self.chat_wins[room_jid].setPresents(list(set([user_nick] + room_nicks))) 359 self.chat_wins[room_jid].setPresents(list(set([user_nick] + room_nicks)))
359 self.contact_list.setSpecial(JID(room_jid), "MUC", show=True) 360 self.contact_list.setSpecial(JID(room_jid), "MUC", show=True)
360 361
361 def roomLeft(self, room_jid_s, profile): 362 def roomLeft(self, room_jid_s, profile):
362 """Called when a MUC room is left""" 363 """Called when a MUC room is left"""
363 if not self.check_profile(profile): 364 if not self.check_profile(profile):
364 return 365 return
365 debug(_("Room [%(room_jid)s] left by %(profile)s") % {'room_jid': room_jid_s, 'profile': profile}) 366 log.debug(_("Room [%(room_jid)s] left by %(profile)s") % {'room_jid': room_jid_s, 'profile': profile})
366 del self.chat_wins[room_jid_s] 367 del self.chat_wins[room_jid_s]
367 self.contact_list.remove(JID(room_jid_s)) 368 self.contact_list.remove(JID(room_jid_s))
368 369
369 def roomUserJoined(self, room_jid, user_nick, user_data, profile): 370 def roomUserJoined(self, room_jid, user_nick, user_data, profile):
370 """Called when an user joined a MUC room""" 371 """Called when an user joined a MUC room"""
371 if not self.check_profile(profile): 372 if not self.check_profile(profile):
372 return 373 return
373 if room_jid in self.chat_wins: 374 if room_jid in self.chat_wins:
374 self.chat_wins[room_jid].replaceUser(user_nick) 375 self.chat_wins[room_jid].replaceUser(user_nick)
375 debug(_("user [%(user_nick)s] joined room [%(room_jid)s]") % {'user_nick': user_nick, 'room_jid': room_jid}) 376 log.debug(_("user [%(user_nick)s] joined room [%(room_jid)s]") % {'user_nick': user_nick, 'room_jid': room_jid})
376 377
377 def roomUserLeft(self, room_jid, user_nick, user_data, profile): 378 def roomUserLeft(self, room_jid, user_nick, user_data, profile):
378 """Called when an user joined a MUC room""" 379 """Called when an user joined a MUC room"""
379 if not self.check_profile(profile): 380 if not self.check_profile(profile):
380 return 381 return
381 if room_jid in self.chat_wins: 382 if room_jid in self.chat_wins:
382 self.chat_wins[room_jid].removeUser(user_nick) 383 self.chat_wins[room_jid].removeUser(user_nick)
383 debug(_("user [%(user_nick)s] left room [%(room_jid)s]") % {'user_nick': user_nick, 'room_jid': room_jid}) 384 log.debug(_("user [%(user_nick)s] left room [%(room_jid)s]") % {'user_nick': user_nick, 'room_jid': room_jid})
384 385
385 def roomUserChangedNick(self, room_jid, old_nick, new_nick, profile): 386 def roomUserChangedNick(self, room_jid, old_nick, new_nick, profile):
386 """Called when an user joined a MUC room""" 387 """Called when an user joined a MUC room"""
387 if not self.check_profile(profile): 388 if not self.check_profile(profile):
388 return 389 return
389 if room_jid in self.chat_wins: 390 if room_jid in self.chat_wins:
390 self.chat_wins[room_jid].changeUserNick(old_nick, new_nick) 391 self.chat_wins[room_jid].changeUserNick(old_nick, new_nick)
391 debug(_("user [%(old_nick)s] is now known as [%(new_nick)s] in room [%(room_jid)s]") % {'old_nick': old_nick, 'new_nick': new_nick, 'room_jid': room_jid}) 392 log.debug(_("user [%(old_nick)s] is now known as [%(new_nick)s] in room [%(room_jid)s]") % {'old_nick': old_nick, 'new_nick': new_nick, 'room_jid': room_jid})
392 393
393 def roomNewSubject(self, room_jid, subject, profile): 394 def roomNewSubject(self, room_jid, subject, profile):
394 """Called when subject of MUC room change""" 395 """Called when subject of MUC room change"""
395 if not self.check_profile(profile): 396 if not self.check_profile(profile):
396 return 397 return
397 if room_jid in self.chat_wins: 398 if room_jid in self.chat_wins:
398 self.chat_wins[room_jid].setSubject(subject) 399 self.chat_wins[room_jid].setSubject(subject)
399 debug(_("new subject for room [%(room_jid)s]: %(subject)s") % {'room_jid': room_jid, "subject": subject}) 400 log.debug(_("new subject for room [%(room_jid)s]: %(subject)s") % {'room_jid': room_jid, "subject": subject})
400 401
401 def tarotGameStarted(self, room_jid, referee, players, profile): 402 def tarotGameStarted(self, room_jid, referee, players, profile):
402 if not self.check_profile(profile): 403 if not self.check_profile(profile):
403 return 404 return
404 debug(_("Tarot Game Started \o/")) 405 log.debug(_("Tarot Game Started \o/"))
405 if room_jid in self.chat_wins: 406 if room_jid in self.chat_wins:
406 self.chat_wins[room_jid].startGame("Tarot", referee, players) 407 self.chat_wins[room_jid].startGame("Tarot", referee, players)
407 debug(_("new Tarot game started by [%(referee)s] in room [%(room_jid)s] with %(players)s") % {'referee': referee, 'room_jid': room_jid, 'players': [str(player) for player in players]}) 408 log.debug(_("new Tarot game started by [%(referee)s] in room [%(room_jid)s] with %(players)s") % {'referee': referee, 'room_jid': room_jid, 'players': [str(player) for player in players]})
408 409
409 def tarotGameNew(self, room_jid, hand, profile): 410 def tarotGameNew(self, room_jid, hand, profile):
410 if not self.check_profile(profile): 411 if not self.check_profile(profile):
411 return 412 return
412 debug(_("New Tarot Game")) 413 log.debug(_("New Tarot Game"))
413 if room_jid in self.chat_wins: 414 if room_jid in self.chat_wins:
414 self.chat_wins[room_jid].getGame("Tarot").newGame(hand) 415 self.chat_wins[room_jid].getGame("Tarot").newGame(hand)
415 416
416 def tarotChooseContrat(self, room_jid, xml_data, profile): 417 def tarotChooseContrat(self, room_jid, xml_data, profile):
417 """Called when the player has to select his contrat""" 418 """Called when the player has to select his contrat"""
418 if not self.check_profile(profile): 419 if not self.check_profile(profile):
419 return 420 return
420 debug(_("Tarot: need to select a contrat")) 421 log.debug(_("Tarot: need to select a contrat"))
421 if room_jid in self.chat_wins: 422 if room_jid in self.chat_wins:
422 self.chat_wins[room_jid].getGame("Tarot").chooseContrat(xml_data) 423 self.chat_wins[room_jid].getGame("Tarot").chooseContrat(xml_data)
423 424
424 def tarotShowCards(self, room_jid, game_stage, cards, data, profile): 425 def tarotShowCards(self, room_jid, game_stage, cards, data, profile):
425 if not self.check_profile(profile): 426 if not self.check_profile(profile):
426 return 427 return
427 debug(_("Show cards")) 428 log.debug(_("Show cards"))
428 if room_jid in self.chat_wins: 429 if room_jid in self.chat_wins:
429 self.chat_wins[room_jid].getGame("Tarot").showCards(game_stage, cards, data) 430 self.chat_wins[room_jid].getGame("Tarot").showCards(game_stage, cards, data)
430 431
431 def tarotMyTurn(self, room_jid, profile): 432 def tarotMyTurn(self, room_jid, profile):
432 if not self.check_profile(profile): 433 if not self.check_profile(profile):
433 return 434 return
434 debug(_("My turn to play")) 435 log.debug(_("My turn to play"))
435 if room_jid in self.chat_wins: 436 if room_jid in self.chat_wins:
436 self.chat_wins[room_jid].getGame("Tarot").myTurn() 437 self.chat_wins[room_jid].getGame("Tarot").myTurn()
437 438
438 def tarotScore(self, room_jid, xml_data, winners, loosers, profile): 439 def tarotScore(self, room_jid, xml_data, winners, loosers, profile):
439 """Called when the game is finished and the score are updated""" 440 """Called when the game is finished and the score are updated"""
440 if not self.check_profile(profile): 441 if not self.check_profile(profile):
441 return 442 return
442 debug(_("Tarot: score received")) 443 log.debug(_("Tarot: score received"))
443 if room_jid in self.chat_wins: 444 if room_jid in self.chat_wins:
444 self.chat_wins[room_jid].getGame("Tarot").showScores(xml_data, winners, loosers) 445 self.chat_wins[room_jid].getGame("Tarot").showScores(xml_data, winners, loosers)
445 446
446 def tarotCardsPlayed(self, room_jid, player, cards, profile): 447 def tarotCardsPlayed(self, room_jid, player, cards, profile):
447 if not self.check_profile(profile): 448 if not self.check_profile(profile):
448 return 449 return
449 debug(_("Card(s) played (%(player)s): %(cards)s") % {"player": player, "cards": cards}) 450 log.debug(_("Card(s) played (%(player)s): %(cards)s") % {"player": player, "cards": cards})
450 if room_jid in self.chat_wins: 451 if room_jid in self.chat_wins:
451 self.chat_wins[room_jid].getGame("Tarot").cardsPlayed(player, cards) 452 self.chat_wins[room_jid].getGame("Tarot").cardsPlayed(player, cards)
452 453
453 def tarotInvalidCards(self, room_jid, phase, played_cards, invalid_cards, profile): 454 def tarotInvalidCards(self, room_jid, phase, played_cards, invalid_cards, profile):
454 if not self.check_profile(profile): 455 if not self.check_profile(profile):
455 return 456 return
456 debug(_("Cards played are not valid: %s") % invalid_cards) 457 log.debug(_("Cards played are not valid: %s") % invalid_cards)
457 if room_jid in self.chat_wins: 458 if room_jid in self.chat_wins:
458 self.chat_wins[room_jid].getGame("Tarot").invalidCards(phase, played_cards, invalid_cards) 459 self.chat_wins[room_jid].getGame("Tarot").invalidCards(phase, played_cards, invalid_cards)
459 460
460 def quizGameStarted(self, room_jid, referee, players, profile): 461 def quizGameStarted(self, room_jid, referee, players, profile):
461 if not self.check_profile(profile): 462 if not self.check_profile(profile):
462 return 463 return
463 debug(_("Quiz Game Started \o/")) 464 log.debug(_("Quiz Game Started \o/"))
464 if room_jid in self.chat_wins: 465 if room_jid in self.chat_wins:
465 self.chat_wins[room_jid].startGame("Quiz", referee, players) 466 self.chat_wins[room_jid].startGame("Quiz", referee, players)
466 debug(_("new Quiz game started by [%(referee)s] in room [%(room_jid)s] with %(players)s") % {'referee': referee, 'room_jid': room_jid, 'players': [str(player) for player in players]}) 467 log.debug(_("new Quiz game started by [%(referee)s] in room [%(room_jid)s] with %(players)s") % {'referee': referee, 'room_jid': room_jid, 'players': [str(player) for player in players]})
467 468
468 def quizGameNew(self, room_jid, data, profile): 469 def quizGameNew(self, room_jid, data, profile):
469 if not self.check_profile(profile): 470 if not self.check_profile(profile):
470 return 471 return
471 debug(_("New Quiz Game")) 472 log.debug(_("New Quiz Game"))
472 if room_jid in self.chat_wins: 473 if room_jid in self.chat_wins:
473 self.chat_wins[room_jid].getGame("Quiz").quizGameNew(data) 474 self.chat_wins[room_jid].getGame("Quiz").quizGameNew(data)
474 475
475 def quizGameQuestion(self, room_jid, question_id, question, timer, profile): 476 def quizGameQuestion(self, room_jid, question_id, question, timer, profile):
476 """Called when a new question is asked""" 477 """Called when a new question is asked"""
477 if not self.check_profile(profile): 478 if not self.check_profile(profile):
478 return 479 return
479 debug(_(u"Quiz: new question: %s") % question) 480 log.debug(_(u"Quiz: new question: %s") % question)
480 if room_jid in self.chat_wins: 481 if room_jid in self.chat_wins:
481 self.chat_wins[room_jid].getGame("Quiz").quizGameQuestion(question_id, question, timer) 482 self.chat_wins[room_jid].getGame("Quiz").quizGameQuestion(question_id, question, timer)
482 483
483 def quizGamePlayerBuzzed(self, room_jid, player, pause, profile): 484 def quizGamePlayerBuzzed(self, room_jid, player, pause, profile):
484 """Called when a player pushed the buzzer""" 485 """Called when a player pushed the buzzer"""
569 pass #FIXME 570 pass #FIXME
570 571
571 def paramUpdate(self, name, value, namespace, profile): 572 def paramUpdate(self, name, value, namespace, profile):
572 if not self.check_profile(profile): 573 if not self.check_profile(profile):
573 return 574 return
574 debug(_("param update: [%(namespace)s] %(name)s = %(value)s") % {'namespace': namespace, 'name': name, 'value': value}) 575 log.debug(_("param update: [%(namespace)s] %(name)s = %(value)s") % {'namespace': namespace, 'name': name, 'value': value})
575 if (namespace, name) == ("Connection", "JabberID"): 576 if (namespace, name) == ("Connection", "JabberID"):
576 debug(_("Changing JID to %s"), value) 577 log.debug(_("Changing JID to %s") % value)
577 self.profiles[profile]['whoami'] = JID(value) 578 self.profiles[profile]['whoami'] = JID(value)
578 elif (namespace, name) == ("Misc", "Watched"): 579 elif (namespace, name) == ("Misc", "Watched"):
579 self.profiles[profile]['watched'] = value.split() 580 self.profiles[profile]['watched'] = value.split()
580 581
581 def contactDeleted(self, jid, profile): 582 def contactDeleted(self, jid, profile):