File: ErrorDialogDemo.py

package info (click to toggle)
editra 0.6.58-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 13,320 kB
  • sloc: python: 71,924; sql: 258; ansic: 242; sh: 187; php: 45; tcl: 38; lisp: 38; perl: 23; java: 22; pascal: 21; cpp: 20; haskell: 20; xml: 18; cs: 18; erlang: 17; ruby: 16; asm: 15; ada: 9; csh: 9; makefile: 9; ml: 9
file content (149 lines) | stat: -rw-r--r-- 4,635 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
###############################################################################
# Name: ErrorDialogDemo.py                                                    #
# Purpose: Test and demo file for eclib.ErrorDialog                           #
# Author: Cody Precord <cprecord@editra.org>                                  #
# Copyright: (c) 2009 Cody Precord <staff@editra.org>                         #
# Licence: wxWindows Licence                                                  #
###############################################################################

"""
Test file for testing the File InfoDialog.

"""

__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: ErrorDialogDemo.py 61806 2009-09-02 01:31:01Z CJP $"
__revision__ = "$Revision: 61806 $"

#-----------------------------------------------------------------------------#
# Imports
import os
import sys
import wx

# Put local package on the path
#sys.path.insert(0, os.path.abspath('../../src'))
import eclib

#-----------------------------------------------------------------------------#

class TestErrorDialog(eclib.ErrorDialog):
    def __init__(self, msg):
        eclib.ErrorDialog.__init__(self, None, title="Error Report", message=msg)

        # Setup
        self.SetDescriptionLabel("Error: An Error has occured read below")

    def Abort(self):
        """Abort the application"""
        wx.MessageBox("Abort Clicked", "Abort Callback")
        TestErrorDialog.ABORT = False # HACK for testing to keep app from being aborted for real

    def GetProgramName(self):
        """Get the program name to display in error report"""
        return "ErrorDialog Demo"

    def Send(self):
        """Send the error report"""
        wx.MessageBox("Send Clicked", "Send Callback")

#-----------------------------------------------------------------------------#

def ExceptionHook(exctype, value, trace):
    """Handler for all unhandled exceptions
    @param exctype: Exception Type
    @param value: Error Value
    @param trace: Trace back info

    """
    # Format the traceback
    ftrace = TestErrorDialog.FormatTrace(exctype, value, trace)

    # Ensure that error gets raised to console as well
    print ftrace

    # If abort has been set and we get here again do a more forcefull shutdown
    if TestErrorDialog.ABORT:
        os._exit(1)

    # Prevent multiple reporter dialogs from opening at once
    if not TestErrorDialog.REPORTER_ACTIVE and not TestErrorDialog.ABORT:
        dlg = TestErrorDialog(ftrace)
        dlg.ShowModal()
        dlg.Destroy()

#-----------------------------------------------------------------------------#

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, wx.ID_ANY, size=(500,500))

        # Attributes
        self.exc = wx.Button(self, label="Raise Exception")
        self.log = log

        # Setup
        self._oldhook = sys.excepthook
        sys.excepthook = ExceptionHook

        # Layout
        self.__DoLayout()

        # Event Handers
        self.Bind(wx.EVT_BUTTON, self.OnButton, self.exc)

    def __del__(self):
        sys.excepthook = self._oldhook

    def __DoLayout(self):
        """Layout the panel"""
        vsizer = wx.BoxSizer(wx.VERTICAL)

        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        hsizer.Add(self.exc, 0, wx.ALIGN_CENTER)

        vsizer.AddStretchSpacer()
        vsizer.Add(hsizer, 0, wx.ALIGN_CENTER)
        vsizer.AddStretchSpacer()

        self.SetSizer(vsizer)
        self.SetAutoLayout(True)

    def OnButton(self, evt):
        """Raise an exception to trigger the Error Dialog"""
        raise Exception("An error has occurred")

#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win

class TestLog:
    def __init__(self):
        pass

    def write(self, msg):
        print msg

#----------------------------------------------------------------------

overview = eclib.errdlg.__doc__
title = "ErrorDialog"

#-----------------------------------------------------------------------------#
if __name__ == '__main__':
    try:
        import run
    except ImportError:
        app = wx.PySimpleApp(False)
        frame = wx.Frame(None, title="File Info Dialog Demo")
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(TestPanel(frame, TestLog()), 1, wx.EXPAND)
        frame.CreateStatusBar()
        frame.SetSizer(sizer)
        frame.SetInitialSize((300, 300))
        frame.Show()
        app.MainLoop()
    else:
        run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])