gajim-plural/gajim/gtkexcepthook.py

146 lines
4.3 KiB
Python
Raw Normal View History

2018-09-05 02:34:18 +02:00
# Copyright (C) 2016-2018 Philipp Hörist <philipp AT hoerist.com>
# Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
# Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
# Copyright (C) 2008 Stephan Erb <steve-e AT h3c.de>
#
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import traceback
2005-09-13 13:11:39 +02:00
import threading
2017-01-02 23:45:11 +01:00
import webbrowser
import platform
from io import StringIO
from urllib.parse import urlencode
import nbxmpp
from gi.repository import Gtk, GObject
2018-04-22 22:34:27 +02:00
try:
import gajim
gajim_version = gajim.__version__
except ImportError:
# For standalone testing
gajim_version = 'Package not installed'
if __name__ == '__main__':
glade_file = os.path.join('data', 'gui', 'exception_dialog.ui')
else:
from gajim.common import configpaths
gui_path = configpaths.get('GUI')
glade_file = os.path.join(gui_path, 'exception_dialog.ui')
2017-01-02 23:45:11 +01:00
2005-09-13 13:11:39 +02:00
_exception_in_progress = threading.Lock()
ISSUE_TEXT = '''## Versions
- OS: {}
- GTK+ Version: {}
- PyGObject Version: {}
- python-nbxmpp Version: {}
2018-02-05 21:00:28 +01:00
- Gajim Version: {}
## Traceback
```
{}
```
## Steps to reproduce the problem
...'''
2017-01-02 23:45:11 +01:00
2018-04-22 22:34:27 +02:00
2017-01-02 23:45:11 +01:00
def _hook(type_, value, tb):
if not _exception_in_progress.acquire(False):
# Exceptions have piled up, so we use the default exception
# handler for such exceptions
2017-01-02 23:45:11 +01:00
sys.__excepthook__(type_, value, tb)
return
2017-01-02 23:45:11 +01:00
ExceptionDialog(type_, value, tb)
_exception_in_progress.release()
2008-12-03 22:56:12 +01:00
2017-01-02 23:45:11 +01:00
class ExceptionDialog():
def __init__(self, type_, value, tb):
builder = Gtk.Builder()
builder.add_from_file(glade_file)
self.dialog = builder.get_object("ExceptionDialog")
builder.connect_signals(self)
builder.get_object("report_btn").grab_focus()
self.exception_view = builder.get_object("exception_view")
buffer_ = self.exception_view.get_buffer()
trace = StringIO()
traceback.print_exception(type_, value, tb, None, trace)
self.text = self.get_issue_text(trace.getvalue())
buffer_.set_text(self.text)
print(self.text, file=sys.stderr)
2017-01-02 23:45:11 +01:00
self.exception_view.set_editable(False)
self.dialog.show()
2018-04-22 22:34:27 +02:00
if __name__ == '__main__':
self.dialog.connect('delete-event', self._on_delete_event)
2017-01-02 23:45:11 +01:00
def on_report_clicked(self, *args):
issue_url = 'https://dev.gajim.org/gajim/gajim/issues/new'
params = {'issue[description]': self.text}
url = '{}?{}'.format(issue_url, urlencode(params))
2017-01-02 23:45:11 +01:00
webbrowser.open(url, new=2)
2017-01-02 23:45:11 +01:00
def on_close_clicked(self, *args):
self.dialog.destroy()
2018-04-22 22:34:27 +02:00
if __name__ == '__main__':
Gtk.main_quit()
def _on_delete_event(self, *args):
Gtk.main_quit()
def get_issue_text(self, traceback_text):
gtk_ver = '%i.%i.%i' % (
Gtk.get_major_version(),
Gtk.get_minor_version(),
Gtk.get_micro_version())
gobject_ver = '.'.join(map(str, GObject.pygobject_version))
return ISSUE_TEXT.format(get_os_info(),
gtk_ver,
gobject_ver,
nbxmpp.__version__,
2018-04-22 22:34:27 +02:00
gajim_version,
traceback_text)
2008-12-03 22:56:12 +01:00
2017-01-02 23:45:11 +01:00
def init():
if os.name == 'nt' or not sys.stderr.isatty():
sys.excepthook = _hook
def get_os_info():
if os.name == 'nt' or sys.platform == 'darwin':
return platform.system() + " " + platform.release()
2018-09-18 10:14:04 +02:00
if os.name == 'posix':
try:
import distro
return distro.name(pretty=True)
except ImportError:
return platform.system()
return ''
2018-04-22 22:34:27 +02:00
# this is just to assist testing (python3 gtkexcepthook.py)
if __name__ == '__main__':
2017-01-02 23:45:11 +01:00
init()
print(sys.version)
ExceptionDialog(None, None, None)
Gtk.main()