quick_app: better PEP-8 compliance

This commit is contained in:
souliane 2014-03-16 21:25:49 +01:00
commit 73b374f6fc

View file

@ -28,6 +28,7 @@ from optparse import OptionParser
from sat_frontends.quick_frontend.constants import Const
class QuickApp(object):
"""This class contain the main methods needed for the frontend"""
@ -38,7 +39,7 @@ class QuickApp(object):
## bridge ##
try:
self.bridge=DBusBridgeFrontend()
self.bridge = DBusBridgeFrontend()
except BridgeExceptionNoService:
print(_(u"Can't connect to SàT backend, are you sure it's launched ?"))
sys.exit(1)
@ -85,7 +86,7 @@ class QuickApp(object):
self.current_action_ids = set()
self.current_action_ids_cb = {}
self.media_dir = self.bridge.getConfig('','media_dir')
self.media_dir = self.bridge.getConfig('', 'media_dir')
def check_profile(self, profile):
"""Tell if the profile is currently followed by the application"""
@ -101,7 +102,7 @@ class QuickApp(object):
def check_options(self):
"""Check command line options"""
usage=_("""
usage = _("""
%prog [options]
%prog --help for options list
@ -127,21 +128,21 @@ class QuickApp(object):
if not profile:
error(_("The profile asked doesn't exist"))
return
if self.profiles.has_key(profile):
if profile in self.profiles:
warning(_("The profile is already plugged"))
return
self.profiles[profile]={}
self.profiles[profile] = {}
if self.single_profile:
self.profile = profile
###now we get the essential params###
self.bridge.asyncGetParamA("JabberID","Connection", profile_key=profile,
self.bridge.asyncGetParamA("JabberID", "Connection", profile_key=profile,
callback=lambda _jid: self.plug_profile_2(_jid, profile), errback=self._getParamError)
def plug_profile_2(self, _jid, profile):
self.profiles[profile]['whoami'] = JID(_jid)
self.bridge.asyncGetParamA("autoconnect","Connection", profile_key=profile,
callback=lambda value: self.plug_profile_3(value=="true", profile), errback=self._getParamError)
self.bridge.asyncGetParamA("autoconnect", "Connection", profile_key=profile,
callback=lambda value: self.plug_profile_3(value == "true", profile), errback=self._getParamError)
def plug_profile_3(self, autoconnect, profile):
self.bridge.asyncGetParamA("Watched", "Misc", profile_key=profile,
@ -155,10 +156,10 @@ class QuickApp(object):
self.plug_profile_5(watched, autoconnect, profile)
def plug_profile_5(self, watched, autoconnect, profile):
self.profiles[profile]['watched'] = watched.split() #TODO: put this in a plugin
self.profiles[profile]['watched'] = watched.split() # TODO: put this in a plugin
## misc ##
self.profiles[profile]['onlineContact'] = set() #FIXME: temporary
self.profiles[profile]['onlineContact'] = set() # FIXME: temporary
#TODO: manage multi-profiles here
if not self.bridge.isConnected(profile):
@ -173,12 +174,12 @@ class QuickApp(object):
presences = self.bridge.getPresenceStatus(profile)
for contact in presences:
for res in presences[contact]:
jabber_id = contact+('/'+res if res else '')
jabber_id = contact + ('/' + res if res else '')
show = presences[contact][res][0]
priority = presences[contact][res][1]
statuses = presences[contact][res][2]
self.presenceUpdate(jabber_id, show, priority, statuses, profile)
data = self.bridge.getEntityData(contact, ['avatar','nick'], profile)
data = self.bridge.getEntityData(contact, ['avatar', 'nick'], profile)
for key in ('avatar', 'nick'):
if key in data:
self.entityDataUpdated(contact, key, data[key], profile)
@ -199,12 +200,10 @@ class QuickApp(object):
for confirm_id, confirm_type, data in self.bridge.getWaitingConf(profile):
self.askConfirmation(confirm_id, confirm_type, data, profile)
def unplug_profile(self, profile):
"""Tell the application to not follow anymore the profile"""
if not profile in self.profiles:
warning (_("This profile is not plugged"))
warning(_("This profile is not plugged"))
return
self.profiles.remove(profile)
@ -240,7 +239,7 @@ class QuickApp(object):
def newContact(self, JabberId, attributes, groups, profile):
if not self.check_profile(profile):
return
entity=JID(JabberId)
entity = JID(JabberId)
_groups = list(groups)
self.contact_list.replace(entity, _groups, attributes)
@ -293,8 +292,8 @@ class QuickApp(object):
def newAlert(self, msg, title, alert_type, profile):
if not self.check_profile(profile):
return
assert alert_type in ['INFO','ERROR']
self.showDialog(unicode(msg),unicode(title),alert_type.lower())
assert alert_type in ['INFO', 'ERROR']
self.showDialog(unicode(msg), unicode(title), alert_type.lower())
def setStatusOnline(self, online=True, show="", statuses={}):
raise NotImplementedError
@ -333,18 +332,18 @@ class QuickApp(object):
"""Called when a MUC room is joined"""
if not self.check_profile(profile):
return
debug (_("Room [%(room_jid)s] joined by %(profile)s, users presents:%(users)s") % {'room_jid':room_jid, 'profile': profile, 'users':room_nicks})
debug(_("Room [%(room_jid)s] joined by %(profile)s, users presents:%(users)s") % {'room_jid': room_jid, 'profile': profile, 'users': room_nicks})
self.chat_wins[room_jid].setUserNick(user_nick)
self.chat_wins[room_jid].setType("group")
self.chat_wins[room_jid].id = room_jid
self.chat_wins[room_jid].setPresents(list(set([user_nick]+room_nicks)))
self.chat_wins[room_jid].setPresents(list(set([user_nick] + room_nicks)))
self.contact_list.setSpecial(JID(room_jid), "MUC", show=True)
def roomLeft(self, room_jid_s, profile):
"""Called when a MUC room is left"""
if not self.check_profile(profile):
return
debug (_("Room [%(room_jid)s] left by %(profile)s") % {'room_jid':room_jid_s, 'profile': profile})
debug(_("Room [%(room_jid)s] left by %(profile)s") % {'room_jid': room_jid_s, 'profile': profile})
del self.chat_wins[room_jid_s]
self.contact_list.remove(JID(room_jid_s))
@ -352,174 +351,174 @@ class QuickApp(object):
"""Called when an user joined a MUC room"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].replaceUser(user_nick)
debug (_("user [%(user_nick)s] joined room [%(room_jid)s]") % {'user_nick':user_nick, 'room_jid':room_jid})
debug(_("user [%(user_nick)s] joined room [%(room_jid)s]") % {'user_nick': user_nick, 'room_jid': room_jid})
def roomUserLeft(self, room_jid, user_nick, user_data, profile):
"""Called when an user joined a MUC room"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].removeUser(user_nick)
debug (_("user [%(user_nick)s] left room [%(room_jid)s]") % {'user_nick':user_nick, 'room_jid':room_jid})
debug(_("user [%(user_nick)s] left room [%(room_jid)s]") % {'user_nick': user_nick, 'room_jid': room_jid})
def roomUserChangedNick(self, room_jid, old_nick, new_nick, profile):
"""Called when an user joined a MUC room"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].changeUserNick(old_nick, new_nick)
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})
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})
def roomNewSubject(self, room_jid, subject, profile):
"""Called when subject of MUC room change"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].setSubject(subject)
debug (_("new subject for room [%(room_jid)s]: %(subject)s") % {'room_jid':room_jid, "subject":subject})
debug(_("new subject for room [%(room_jid)s]: %(subject)s") % {'room_jid': room_jid, "subject": subject})
def tarotGameStarted(self, room_jid, referee, players, profile):
if not self.check_profile(profile):
return
debug (_("Tarot Game Started \o/"))
if self.chat_wins.has_key(room_jid):
debug(_("Tarot Game Started \o/"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].startGame("Tarot", referee, players)
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]})
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]})
def tarotGameNew(self, room_jid, hand, profile):
if not self.check_profile(profile):
return
debug (_("New Tarot Game"))
if self.chat_wins.has_key(room_jid):
debug(_("New Tarot Game"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Tarot").newGame(hand)
def tarotChooseContrat(self, room_jid, xml_data, profile):
"""Called when the player has to select his contrat"""
if not self.check_profile(profile):
return
debug (_("Tarot: need to select a contrat"))
if self.chat_wins.has_key(room_jid):
debug(_("Tarot: need to select a contrat"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Tarot").chooseContrat(xml_data)
def tarotShowCards(self, room_jid, game_stage, cards, data, profile):
if not self.check_profile(profile):
return
debug (_("Show cards"))
if self.chat_wins.has_key(room_jid):
debug(_("Show cards"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Tarot").showCards(game_stage, cards, data)
def tarotMyTurn(self, room_jid, profile):
if not self.check_profile(profile):
return
debug (_("My turn to play"))
if self.chat_wins.has_key(room_jid):
debug(_("My turn to play"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Tarot").myTurn()
def tarotScore(self, room_jid, xml_data, winners, loosers, profile):
"""Called when the game is finished and the score are updated"""
if not self.check_profile(profile):
return
debug (_("Tarot: score received"))
if self.chat_wins.has_key(room_jid):
debug(_("Tarot: score received"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Tarot").showScores(xml_data, winners, loosers)
def tarotCardsPlayed(self, room_jid, player, cards, profile):
if not self.check_profile(profile):
return
debug (_("Card(s) played (%(player)s): %(cards)s") % {"player":player, "cards":cards})
if self.chat_wins.has_key(room_jid):
debug(_("Card(s) played (%(player)s): %(cards)s") % {"player": player, "cards": cards})
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Tarot").cardsPlayed(player, cards)
def tarotInvalidCards(self, room_jid, phase, played_cards, invalid_cards, profile):
if not self.check_profile(profile):
return
debug (_("Cards played are not valid: %s") % invalid_cards)
if self.chat_wins.has_key(room_jid):
debug(_("Cards played are not valid: %s") % invalid_cards)
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Tarot").invalidCards(phase, played_cards, invalid_cards)
def quizGameStarted(self, room_jid, referee, players, profile):
if not self.check_profile(profile):
return
debug (_("Quiz Game Started \o/"))
if self.chat_wins.has_key(room_jid):
debug(_("Quiz Game Started \o/"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].startGame("Quiz", referee, players)
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]})
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]})
def quizGameNew(self, room_jid, data, profile):
if not self.check_profile(profile):
return
debug (_("New Quiz Game"))
if self.chat_wins.has_key(room_jid):
debug(_("New Quiz Game"))
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Quiz").quizGameNew(data)
def quizGameQuestion(self, room_jid, question_id, question, timer, profile):
"""Called when a new question is asked"""
if not self.check_profile(profile):
return
debug (_(u"Quiz: new question: %s") % question)
if self.chat_wins.has_key(room_jid):
debug(_(u"Quiz: new question: %s") % question)
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Quiz").quizGameQuestion(question_id, question, timer)
def quizGamePlayerBuzzed(self, room_jid, player, pause, profile):
"""Called when a player pushed the buzzer"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Quiz").quizGamePlayerBuzzed(player, pause)
def quizGamePlayerSays(self, room_jid, player, text, delay, profile):
"""Called when a player say something"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Quiz").quizGamePlayerSays(player, text, delay)
def quizGameAnswerResult(self, room_jid, player, good_answer, score, profile):
"""Called when a player say something"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Quiz").quizGameAnswerResult(player, good_answer, score)
def quizGameTimerExpired(self, room_jid, profile):
"""Called when nobody answered the question in time"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Quiz").quizGameTimerExpired()
def quizGameTimerRestarted(self, room_jid, time_left, profile):
"""Called when the question is not answered, and we still have time"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
if room_jid in self.chat_wins:
self.chat_wins[room_jid].getGame("Quiz").quizGameTimerRestarted(time_left)
def _subscribe_cb(self, answer, data):
entity, profile = data
if answer:
self.bridge.subscription("subscribed", entity.bare, profile_key = profile)
self.bridge.subscription("subscribed", entity.bare, profile_key=profile)
else:
self.bridge.subscription("unsubscribed", entity.bare, profile_key = profile)
self.bridge.subscription("unsubscribed", entity.bare, profile_key=profile)
def subscribe(self, type, raw_jid, profile):
"""Called when a subsciption management signal is received"""
if not self.check_profile(profile):
return
entity = JID(raw_jid)
if type=="subscribed":
if type == "subscribed":
# this is a subscription confirmation, we just have to inform user
self.showDialog(_("The contact %s has accepted your subscription") % entity.bare, _('Subscription confirmation'))
elif type=="unsubscribed":
elif type == "unsubscribed":
# this is a subscription refusal, we just have to inform user
self.showDialog(_("The contact %s has refused your subscription") % entity.bare, _('Subscription refusal'), 'error')
elif type=="subscribe":
elif type == "subscribe":
# this is a subscriptionn request, we have to ask for user confirmation
answer = self.showDialog(_("The contact %s wants to subscribe to your presence.\nDo you accept ?") % entity.bare, _('Subscription confirmation'), 'yes/no', answer_cb = self._subscribe_cb, answer_data=(entity, profile))
answer = self.showDialog(_("The contact %s wants to subscribe to your presence.\nDo you accept ?") % entity.bare, _('Subscription confirmation'), 'yes/no', answer_cb=self._subscribe_cb, answer_data=(entity, profile))
def showDialog(self, message, title, type="info", answer_cb = None):
def showDialog(self, message, title, type="info", answer_cb=None):
raise NotImplementedError
def showAlert(self, message):
@ -528,12 +527,12 @@ class QuickApp(object):
def paramUpdate(self, name, value, namespace, profile):
if not self.check_profile(profile):
return
debug(_("param update: [%(namespace)s] %(name)s = %(value)s") % {'namespace':namespace, 'name':name, 'value':value})
if (namespace,name) == ("Connection", "JabberID"):
debug (_("Changing JID to %s"), value)
self.profiles[profile]['whoami']=JID(value)
elif (namespace,name) == ("Misc", "Watched"):
self.profiles[profile]['watched']=value.split()
debug(_("param update: [%(namespace)s] %(name)s = %(value)s") % {'namespace': namespace, 'name': name, 'value': value})
if (namespace, name) == ("Connection", "JabberID"):
debug(_("Changing JID to %s"), value)
self.profiles[profile]['whoami'] = JID(value)
elif (namespace, name) == ("Misc", "Watched"):
self.profiles[profile]['watched'] = value.split()
def contactDeleted(self, jid, profile):
if not self.check_profile(profile):
@ -570,9 +569,8 @@ class QuickApp(object):
#TODO: mange multi-profile here
try:
if self.bridge.isConnected(self.profile):
if self.bridge.getParamA("autodisconnect","Connection", profile_key=self.profile) == "true":
if self.bridge.getParamA("autodisconnect", "Connection", profile_key=self.profile) == "true":
#The user wants autodisconnection
self.bridge.disconnect(self.profile)
except:
pass