comparison frontends/src/primitivus/primitivus @ 1010:73a0b7f94674

primitivus: use of new logging system: - default output is \\memory - logs can be seen with :messages command - now useless writeLog method has been removed
author Goffi <goffi@goffi.org>
date Mon, 05 May 2014 20:12:19 +0200
parents 6f1e03068b5f
children 6a16ec17a458
comparison
equal deleted inserted replaced
1009:d1084f7e56a5 1010:73a0b7f94674
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 20
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from sat_frontends.primitivus.constants import Const as C
23 from sat.core import log as logging
24 logging.satConfigure(C.LOG_BACKEND_STANDARD, C)
25 log = logging.getLogger(__name__)
22 import urwid 26 import urwid
23 from urwid_satext import sat_widgets 27 from urwid_satext import sat_widgets
24 from urwid_satext.files_management import FileDialog 28 from urwid_satext.files_management import FileDialog
25 from sat_frontends.quick_frontend.quick_app import QuickApp 29 from sat_frontends.quick_frontend.quick_app import QuickApp
26 from sat_frontends.quick_frontend.quick_chat_list import QuickChatList 30 from sat_frontends.quick_frontend.quick_chat_list import QuickChatList
30 from sat_frontends.primitivus.chat import Chat 34 from sat_frontends.primitivus.chat import Chat
31 from sat_frontends.primitivus.xmlui import XMLUI 35 from sat_frontends.primitivus.xmlui import XMLUI
32 from sat_frontends.primitivus.progress import Progress 36 from sat_frontends.primitivus.progress import Progress
33 from sat_frontends.primitivus.notify import Notify 37 from sat_frontends.primitivus.notify import Notify
34 from sat_frontends.tools.misc import InputHistory 38 from sat_frontends.tools.misc import InputHistory
35 from sat_frontends.primitivus.constants import Const 39 from sat_frontends.constants import Const as commonConst # FIXME
36 from sat_frontends.constants import Const as commonConst
37 import logging
38 from logging import debug, info, error
39 from sat.tools.jid import JID 40 from sat.tools.jid import JID
40 from os.path import join 41 from os.path import join
41 42
42
43 ### logging configuration FIXME: put this elsewhere ###
44 logging.basicConfig(level=logging.CRITICAL, #TODO: configure it to put messages in a log file
45 format='%(message)s')
46 ###
47 43
48 class ChatList(QuickChatList): 44 class ChatList(QuickChatList):
49 """This class manage the list of chat windows""" 45 """This class manage the list of chat windows"""
50 46
51 def createChat(self, target): 47 def createChat(self, target):
117 # and completion method 113 # and completion method
118 command = self.get_edit_text() 114 command = self.get_edit_text()
119 if command == 'quit': 115 if command == 'quit':
120 self.app.onExit() 116 self.app.onExit()
121 raise urwid.ExitMainLoop() 117 raise urwid.ExitMainLoop()
118 elif command == 'messages':
119 wid = sat_widgets.GenericList(logging.memoryGet())
120 self.app.addWindow(wid)
122 elif command == 'presence': 121 elif command == 'presence':
123 self.app.status_bar.onPresenceClick() 122 self.app.status_bar.onPresenceClick()
124 elif command in ['presence %s' % show for show in commonConst.PRESENCE.keys()]: 123 elif command in ['presence %s' % show for show in commonConst.PRESENCE.keys()]:
125 self.app.status_bar.onChange(user_data=sat_widgets.ClickableText(commonConst.PRESENCE[command[9:]])) 124 self.app.status_bar.onChange(user_data=sat_widgets.ClickableText(commonConst.PRESENCE[command[9:]]))
126 elif command == 'status': 125 elif command == 'status':
164 def __init__(self): 163 def __init__(self):
165 QuickApp.__init__(self) 164 QuickApp.__init__(self)
166 165
167 ## main loop setup ## 166 ## main loop setup ##
168 self.main_widget = ProfileManager(self) 167 self.main_widget = ProfileManager(self)
169 self.loop = urwid.MainLoop(self.main_widget, Const.PALETTE, event_loop=urwid.GLibEventLoop(), input_filter=self.inputFilter, unhandled_input=self.keyHandler) 168 self.loop = urwid.MainLoop(self.main_widget, C.PALETTE, event_loop=urwid.GLibEventLoop(), input_filter=self.inputFilter, unhandled_input=self.keyHandler)
170 169
171 ##misc setup## 170 ##misc setup##
172 self.chat_wins = ChatList(self) 171 self.chat_wins = ChatList(self)
173 self.notBar = sat_widgets.NotificationBar() 172 self.notBar = sat_widgets.NotificationBar()
174 urwid.connect_signal(self.notBar, 'change', self.onNotification) 173 urwid.connect_signal(self.notBar, 'change', self.onNotification)
185 @mode.setter 184 @mode.setter
186 def mode(self, value): 185 def mode(self, value):
187 self.editBar.mode = value 186 self.editBar.mode = value
188 187
189 def modeHint(self, value): 188 def modeHint(self, value):
190 """Change mode if make sens (i.e.: if there is nothing in the editBar""" 189 """Change mode if make sens (i.e.: if there is nothing in the editBar)"""
191 if not self.editBar.get_edit_text(): 190 if not self.editBar.get_edit_text():
192 self.mode = value 191 self.mode = value
193 192
194 def debug(self): 193 def debug(self):
195 """convenient method to reset screen and launch p(u)db""" 194 """convenient method to reset screen and launch (i)p(u)db"""
195 log.info('Entered debug mode')
196 try: 196 try:
197 import pudb 197 import pudb
198 pudb.set_trace() 198 pudb.set_trace()
199 except: 199 except ImportError:
200 import os,pdb 200 import os
201 os.system('reset') 201 os.system('reset')
202 print 'Entered debug mode' 202 try:
203 pdb.set_trace() 203 import ipdb
204 204 ipdb.set_trace()
205 def writeLog(self, log, file_name='/tmp/primitivus_log'): 205 except ImportError:
206 """method to write log in a temporary file, useful for debugging""" 206 import pdb
207 with open(file_name, 'a') as f: 207 pdb.set_trace()
208 f.write(log+"\n")
209 208
210 def redraw(self): 209 def redraw(self):
211 """redraw the screen""" 210 """redraw the screen"""
212 try: 211 try:
213 self.loop.draw_screen() 212 self.loop.draw_screen()
294 menu.addMenu(contact) 293 menu.addMenu(contact)
295 communication = _("Communication") 294 communication = _("Communication")
296 menu.addMenu(communication, _("Join room"), self.onJoinRoomRequest, 'meta j') 295 menu.addMenu(communication, _("Join room"), self.onJoinRoomRequest, 'meta j')
297 #additionals menus 296 #additionals menus
298 #FIXME: do this in a more generic way (in quickapp) 297 #FIXME: do this in a more generic way (in quickapp)
299 add_menus = self.bridge.getMenus('', Const.NO_SECURITY_LIMIT) 298 add_menus = self.bridge.getMenus('', C.NO_SECURITY_LIMIT)
300 def add_menu_cb(callback_id): 299 def add_menu_cb(callback_id):
301 self.launchAction(callback_id, None, profile_key = self.profile) 300 self.launchAction(callback_id, None, profile_key = self.profile)
302 for id_, type_, path, path_i18n in add_menus: 301 for id_, type_, path, path_i18n in add_menus:
303 assert(type_=="NORMAL") #TODO: manage other types 302 assert(type_=="NORMAL") #TODO: manage other types
304 if len(path) != 2: 303 if len(path) != 2:
415 popup = sat_widgets.ConfirmDialog(unicode(message), 414 popup = sat_widgets.ConfirmDialog(unicode(message),
416 yes_cb=self._dialogOkCb, yes_value = (answer_cb, answer_data), 415 yes_cb=self._dialogOkCb, yes_value = (answer_cb, answer_data),
417 no_cb=self._dialogCancelCb, no_value = (answer_cb, answer_data)) 416 no_cb=self._dialogCancelCb, no_value = (answer_cb, answer_data))
418 else: 417 else:
419 popup = sat_widgets.Alert(unicode(title), unicode(message), ok_cb=answer_cb or self.removePopUp) #FIXME: remove unicode here when DBus Bridge will no return dbus.String anymore 418 popup = sat_widgets.Alert(unicode(title), unicode(message), ok_cb=answer_cb or self.removePopUp) #FIXME: remove unicode here when DBus Bridge will no return dbus.String anymore
420 error(_('unmanaged dialog type: %s'), type_) 419 log.error(_('unmanaged dialog type: %s'), type_)
421 self.showPopUp(popup) 420 self.showPopUp(popup)
422 421
423 def onNotification(self, notBar): 422 def onNotification(self, notBar):
424 """Called when a new notification has been received""" 423 """Called when a new notification has been received"""
425 if not isinstance(self.main_widget, sat_widgets.FocusFrame): 424 if not isinstance(self.main_widget, sat_widgets.FocusFrame):
430 #the notification bar is not visible and has usefull informations, we show it 429 #the notification bar is not visible and has usefull informations, we show it
431 pile = urwid.Pile([self.notBar, self.editBar]) 430 pile = urwid.Pile([self.notBar, self.editBar])
432 self.main_widget.footer = pile 431 self.main_widget.footer = pile
433 else: 432 else:
434 if not isinstance(self.main_widget.footer, urwid.Pile): 433 if not isinstance(self.main_widget.footer, urwid.Pile):
435 error(_("INTERNAL ERROR: Unexpected class for main widget's footer")) 434 log.error(_("INTERNAL ERROR: Unexpected class for main widget's footer"))
436 assert(False) 435 assert(False)
437 if self.notBar.canHide(): 436 if self.notBar.canHide():
438 #No notification left, we can hide the bar 437 #No notification left, we can hide the bar
439 self.main_widget.footer = self.editBar 438 self.main_widget.footer = self.editBar
440 439
497 # FIXME: to be removed 496 # FIXME: to be removed
498 if not self.check_profile(profile): 497 if not self.check_profile(profile):
499 return 498 return
500 499
501 if not id in self.current_action_ids: 500 if not id in self.current_action_ids:
502 debug (_('unknown id, ignoring')) 501 log.debug (_('unknown id, ignoring'))
503 return 502 return
504 if type_ == "SUPPRESS": 503 if type_ == "SUPPRESS":
505 self.current_action_ids.remove(id) 504 self.current_action_ids.remove(id)
506 elif type_ == "XMLUI": 505 elif type_ == "XMLUI":
507 self.current_action_ids.remove(id) 506 self.current_action_ids.remove(id)
508 debug (_("XML user interface received")) 507 log.debug (_("XML user interface received"))
509 misc = {} 508 misc = {}
510 #FIXME FIXME FIXME: must clean all this crap ! 509 #FIXME FIXME FIXME: must clean all this crap !
511 title = _('Form') 510 title = _('Form')
512 if data['type'] == 'registration': 511 if data['type'] == 'registration':
513 title = _('Registration') 512 title = _('Registration')
532 if self.current_action_ids_cb.has_key(id): 531 if self.current_action_ids_cb.has_key(id):
533 callback = self.current_action_ids_cb[id] 532 callback = self.current_action_ids_cb[id]
534 del self.current_action_ids_cb[id] 533 del self.current_action_ids_cb[id]
535 callback(data) 534 callback(data)
536 else: 535 else:
537 error (_("FIXME FIXME FIXME: type [%s] not implemented") % type_) 536 log.error (_("FIXME FIXME FIXME: type [%s] not implemented") % type_)
538 raise NotImplementedError 537 raise NotImplementedError
539 538
540 ##DIALOGS CALLBACKS## 539 ##DIALOGS CALLBACKS##
541 def onJoinRoom(self, button, edit): 540 def onJoinRoom(self, button, edit):
542 self.removePopUp() 541 self.removePopUp()
543 room_jid = JID(edit.get_edit_text()) 542 room_jid = JID(edit.get_edit_text())
544 if room_jid.is_valid(): 543 if room_jid.is_valid():
545 self.bridge.joinMUC(room_jid, self.profiles[self.profile]['whoami'].node, {}, self.profile) 544 self.bridge.joinMUC(room_jid, self.profiles[self.profile]['whoami'].node, {}, self.profile)
546 else: 545 else:
547 message = _("'%s' is an invalid JID !") % room_jid 546 message = _("'%s' is an invalid JID !") % room_jid
548 error (message) 547 log.error (message)
549 self.showPopUp(sat_widgets.Alert(_("Error"), message, ok_cb=self.removePopUp)) 548 self.showPopUp(sat_widgets.Alert(_("Error"), message, ok_cb=self.removePopUp))
550 549
551 #MENU EVENTS# 550 #MENU EVENTS#
552 def onConnectRequest(self, menu): 551 def onConnectRequest(self, menu):
553 self.bridge.connect(self.profile) 552 self.bridge.connect(self.profile)
559 def success(params): 558 def success(params):
560 self.addWindow(XMLUI(self, xml_data=params)) 559 self.addWindow(XMLUI(self, xml_data=params))
561 560
562 def failure(error): 561 def failure(error):
563 self.showPopUp(sat_widgets.Alert(_("Error"), _("Can't get parameters"), ok_cb=self.removePopUp)) 562 self.showPopUp(sat_widgets.Alert(_("Error"), _("Can't get parameters"), ok_cb=self.removePopUp))
564 self.bridge.getParamsUI(app=Const.APP_NAME, profile_key=self.profile, callback=success, errback=failure) 563 self.bridge.getParamsUI(app=C.APP_NAME, profile_key=self.profile, callback=success, errback=failure)
565 564
566 def onExitRequest(self, menu): 565 def onExitRequest(self, menu):
567 QuickApp.onExit(self) 566 QuickApp.onExit(self)
568 raise urwid.ExitMainLoop() 567 raise urwid.ExitMainLoop()
569 568
571 """User wants to join a MUC room""" 570 """User wants to join a MUC room"""
572 pop_up_widget = sat_widgets.InputDialog(_("Entering a MUC room"), _("Please enter MUC's JID"), default_txt = 'room@muc_service.server.tld', cancel_cb=self.removePopUp, ok_cb=self.onJoinRoom) 571 pop_up_widget = sat_widgets.InputDialog(_("Entering a MUC room"), _("Please enter MUC's JID"), default_txt = 'room@muc_service.server.tld', cancel_cb=self.removePopUp, ok_cb=self.onJoinRoom)
573 self.showPopUp(pop_up_widget) 572 self.showPopUp(pop_up_widget)
574 573
575 def onAboutRequest(self, menu): 574 def onAboutRequest(self, menu):
576 self.showPopUp(sat_widgets.Alert(_("About"), Const.APP_NAME + " v" + self.bridge.getVersion(), ok_cb=self.removePopUp)) 575 self.showPopUp(sat_widgets.Alert(_("About"), C.APP_NAME + " v" + self.bridge.getVersion(), ok_cb=self.removePopUp))
577 576
578 #MISC CALLBACKS# 577 #MISC CALLBACKS#
579 578
580 def setStatusOnline(self, online=True, show="", statuses={}): 579 def setStatusOnline(self, online=True, show="", statuses={}):
581 if not online or not statuses: 580 if not online or not statuses: