home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / pycentral / deskbar-applet / site-packages / deskbar / gtkexcepthook.py < prev    next >
Encoding:
Python Source  |  2006-08-29  |  2.5 KB  |  87 lines

  1. import sys
  2. import gtk, pango
  3. from gettext import gettext as _
  4. from cStringIO import *
  5. import traceback
  6. import tempfile
  7. import os
  8.         
  9. def fallback_gtk_exception(type, value, tb):
  10.     dialog = gtk.MessageDialog(parent=None,
  11.                    flags=0,
  12.                    type=gtk.MESSAGE_WARNING,
  13.                    buttons=gtk.BUTTONS_CLOSE,
  14.                    message_format=_("A programming error has been detected"))
  15.     dialog.format_secondary_text(_("It probably isn't fatal, but should be reported"
  16.                                    " to the developers nonetheless. The program may behave erratically from now on."))
  17.     dialog.set_title(_("Bug Detected"))
  18.     dialog.set_default_response(gtk.RESPONSE_CLOSE)
  19.     #dialog.set_border_width(12)
  20.     #dialog.vbox.get_children()[0].set_spacing(12)
  21.  
  22.     # Details
  23.     textview = gtk.TextView(); textview.show()
  24.     textview.set_editable(False)
  25.     textview.modify_font(pango.FontDescription("Monospace"))
  26.     sw = gtk.ScrolledWindow(); sw.show()
  27.     sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  28.     sw.add(textview)
  29.     frame = gtk.Frame()
  30.     frame.set_shadow_type(gtk.SHADOW_IN)
  31.     frame.add(sw)
  32.     frame.set_border_width(6)
  33.     textbuffer = textview.get_buffer()
  34.     trace = StringIO()
  35.     traceback.print_exception(type, value, tb, None, trace)
  36.     textbuffer.set_text(trace.getvalue())
  37.     textview.set_size_request(gtk.gdk.screen_width()/2, gtk.gdk.screen_height()/3)
  38.     frame.show()
  39.     expander = gtk.Expander("Details")
  40.     expander.add(frame)
  41.     expander.show()
  42.     dialog.vbox.add(expander)
  43.     
  44.     dialog.set_position(gtk.WIN_POS_CENTER)
  45.     dialog.set_gravity(gtk.gdk.GRAVITY_CENTER)
  46.     
  47.     dialog.run()
  48.     dialog.destroy()
  49.     
  50. def bug_buddy_exception(type, value, tb):
  51.     # Shamelessly stolen from /gnome-python/examples/bug-buddy-integration.py
  52.     # Original credit to Fernando Herrera
  53.     msg = "".join(traceback.format_exception(type, value, tb))
  54.     fd, name = tempfile.mkstemp()
  55.     try:
  56.         os.write(fd,msg)
  57.         os.system("bug-buddy --include=\"%s\" --appname=\"%s\"" % (name, "deskbar-applet"))
  58.     finally:
  59.         os.unlink(name)
  60.  
  61. _exception_in_progress = 0
  62. def _info(type, value, tb):
  63.     global _exception_in_progress
  64.     if _exception_in_progress:
  65.         # Exceptions have piled up, so we use the default exception
  66.         # handler for such exceptions
  67.         _excepthook_save(type, value, tb)
  68.         return
  69.     _exception_in_progress = 1
  70.     
  71.     bug_buddy_exception(type, value, tb)
  72.     fallback_gtk_exception(type, value, tb)
  73.     
  74.     _exception_in_progress = 0
  75.     
  76. if not sys.stderr.isatty():
  77.     print 'Using GTK exception handler'
  78.     _excepthook_save = sys.excepthook
  79.     sys.excepthook = _info
  80.  
  81. if __name__ == '__main__':
  82.     _excepthook_save = sys.excepthook
  83.     sys.excepthook = _info
  84.     raise Exception
  85.  
  86.     
  87.