util/ExceptionHandler.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Tue, 12 Sep 2017 18:32:13 +0300
changeset 1801 58ff55053518
parent 1792 4d1de8b0183f
child 1881 091005ec69c4
permissions -rw-r--r--
found installed Inkscape on Windows

tested with Inkscape version 0.9 on Windows 7.
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     3
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     6
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     7
# Copyright (C) 2007:      Edouard TISSERANT and Laurent BESSARD
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     8
# Copyright (C) 2016-2017: Andrey Skvortsov <andrej.skvortzov@gmail.com>
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     9
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    10
# See COPYING file for copyrights details.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    11
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    12
# This program is free software; you can redistribute it and/or
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    13
# modify it under the terms of the GNU General Public License
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    14
# as published by the Free Software Foundation; either version 2
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    15
# of the License, or (at your option) any later version.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    16
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    17
# This program is distributed in the hope that it will be useful,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    20
# GNU General Public License for more details.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    21
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    22
# You should have received a copy of the GNU General Public License
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    23
# along with this program; if not, write to the Free Software
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    25
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    26
import os
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    27
import sys
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    28
import time
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    29
import tempfile
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    30
import platform
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    31
import traceback
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    32
import threading
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    33
import wx
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    34
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    35
Max_Traceback_List_Size = 20
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    36
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    37
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    38
def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    39
    trcbck_lst = []
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    40
    for i, line in enumerate(traceback.extract_tb(e_tb)):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    41
        trcbck = " " + str(i+1) + ". "
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    42
        if line[0].find(os.getcwd()) == -1:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    43
            trcbck += "file : " + str(line[0]) + ",   "
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    44
        else:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    45
            trcbck += "file : " + str(line[0][len(os.getcwd()):]) + ",   "
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    46
        trcbck += "line : " + str(line[1]) + ",   " + "function : " + str(line[2])
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    47
        trcbck_lst.append(trcbck)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    48
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    49
    # Allow clicking....
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    50
    cap = wx.Window_GetCapture()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    51
    if cap:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    52
        cap.ReleaseMouse()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    53
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    54
    dlg = wx.SingleChoiceDialog(
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    55
        None,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    56
        _("""
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    57
An unhandled exception (bug) occured. Bug report saved at :
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    58
(%s)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    59
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    60
Please be kind enough to send this file to:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    61
beremiz-devel@lists.sourceforge.net
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    62
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    63
You should now restart program.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    64
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    65
Traceback:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    66
""") % bug_report_path +
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    67
        repr(e_type) + " : " + repr(e_value),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    68
        _("Error"),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    69
        trcbck_lst)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    70
    try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    71
        res = (dlg.ShowModal() == wx.ID_OK)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    72
    finally:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    73
        dlg.Destroy()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    74
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    75
    return res
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    76
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    77
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    78
def get_last_traceback(tb):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    79
    while tb.tb_next:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    80
        tb = tb.tb_next
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    81
    return tb
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    82
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    83
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    84
def format_namespace(d, indent='    '):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    85
    return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    86
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    87
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    88
ignored_exceptions = []  # a problem with a line in a module is only reported once per session
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    89
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    90
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    91
def AddExceptHook(app_version='[No version]'):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    92
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    93
    def save_bug_report(e_type, e_value, e_traceback, bug_report_path, date):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    94
        info = {
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    95
            'app-title': wx.GetApp().GetAppName(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    96
            'app-version': app_version,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    97
            'wx-version': wx.VERSION_STRING,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    98
            'wx-platform': wx.Platform,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    99
            'python-version': platform.python_version(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   100
            'platform': platform.platform(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   101
            'e-type': e_type,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   102
            'e-value': e_value,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   103
            'date': date,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   104
            'cwd': os.getcwd(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   105
        }
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   106
        if e_traceback:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   107
            info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   108
            last_tb = get_last_traceback(e_traceback)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   109
            exception_locals = last_tb.tb_frame.f_locals  # the locals at the level of the stack trace where the exception actually occurred
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   110
            info['locals'] = format_namespace(exception_locals)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   111
            if 'self' in exception_locals:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   112
                try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   113
                    info['self'] = format_namespace(exception_locals['self'].__dict__)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   114
                except Exception:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   115
                    pass
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   116
        path = os.path.dirname(bug_report_path)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   117
        if not os.path.exists(path):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   118
            os.mkdir(path)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   119
        output = open(bug_report_path, 'w')
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   120
        lst = info.keys()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   121
        lst.sort()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   122
        for a in lst:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   123
            output.write(a + ":\n" + str(info[a]) + "\n\n")
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   124
        output.close()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   125
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   126
    def handle_exception(e_type, e_value, e_traceback):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   127
        traceback.print_exception(e_type, e_value, e_traceback)  # this is very helpful when there's an exception in the rest of this func
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   128
        last_tb = get_last_traceback(e_traceback)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   129
        ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   130
        if ex not in ignored_exceptions:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   131
            ignored_exceptions.append(ex)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   132
            date = time.ctime()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   133
            path = tempfile.gettempdir()+os.sep+wx.GetApp().GetAppName()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   134
            bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt"
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   135
            save_bug_report(e_type, e_value, e_traceback, bug_report_path, date)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   136
            Display_Exception_Dialog(e_type, e_value, e_traceback, bug_report_path)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   137
    # sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   138
    sys.excepthook = handle_exception
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   139
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   140
    init_old = threading.Thread.__init__
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   141
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   142
    def init(self, *args, **kwargs):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   143
        init_old(self, *args, **kwargs)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   144
        run_old = self.run
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   145
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   146
        def run_with_except_hook(*args, **kw):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   147
            try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   148
                run_old(*args, **kw)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   149
            except (KeyboardInterrupt, SystemExit):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   150
                raise
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   151
            except Exception:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   152
                sys.excepthook(*sys.exc_info())
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   153
        self.run = run_with_except_hook
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   154
    threading.Thread.__init__ = init