~ubuntu-branches/ubuntu/saucy/pida/saucy

« back to all changes in this revision

Viewing changes to src/debugwindow.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Async Open Source and Sicem S.L.
2
 
#
3
 
# This program is free software; you can redistribute it and/or
4
 
# modify it under the terms of the GNU Lesser General Public License
5
 
# as published by the Free Software Foundation; either version 2
6
 
# of the License, or (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU Lesser General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
 
 
17
 
import gettext
18
 
import linecache
19
 
import os
20
 
import sys
21
 
import traceback
22
 
 
23
 
import pango
24
 
import gtk
25
 
 
26
 
_ = gettext.gettext
27
 
 
28
 
class DebugWindow(gtk.Dialog):
29
 
 
30
 
    application = None
31
 
    
32
 
    def __init__(self, parent=None):
33
 
        gtk.Dialog.__init__(self, 'An Error Occurred', parent,
34
 
                            gtk.DIALOG_MODAL | gtk.DIALOG_NO_SEPARATOR)
35
 
 
36
 
        self._build_ui()
37
 
        #self.send = self._create_button(gtk.STOCK_NETWORK, _('_Report bug ...'))
38
 
        #self.send.connect('clicked', self._on_send__clicked)
39
 
        #self.action_area.pack_end(self.send)
40
 
 
41
 
        
42
 
        quit = self._create_button(gtk.STOCK_OK, _('_Continue'))
43
 
        self.add_action_widget(quit, gtk.RESPONSE_ACCEPT)
44
 
        
45
 
        self._pwd = os.getcwd()
46
 
 
47
 
    def _create_button(self, stock, text):
48
 
        b = gtk.Button()
49
 
        #a = gtk.Alignment(0.5, 0.5, 1.0, 1.0)
50
 
        h = gtk.HBox()
51
 
        #a.add(h)
52
 
        i = gtk.Image()
53
 
        i.set_from_stock(stock, gtk.ICON_SIZE_BUTTON)
54
 
        l = gtk.Label(text)
55
 
        if '_' in text:
56
 
            l.set_use_underline(True)
57
 
 
58
 
        h.pack_start(i)
59
 
        h.pack_start(l)
60
 
        b.add(h)
61
 
        b.show_all()
62
 
        return b
63
 
        
64
 
    def _build_ui(self):
65
 
        # 416 = 600 / sqrt(2) = golden number
66
 
        self.set_default_size(600, 416) 
67
 
        self.set_border_width(12)
68
 
 
69
 
        hbox = gtk.HBox()
70
 
        self.vbox.pack_start(hbox, False, False)
71
 
        self.vbox.set_spacing(6)
72
 
        lbl = gtk.Label('<b>Exception type:</b>')
73
 
        lbl.set_use_markup(True)
74
 
        hbox.pack_start(lbl, False, False, 6)
75
 
        lbl.show()
76
 
        self._info_label = gtk.Label()
77
 
        hbox.pack_start(self._info_label, False, False)
78
 
        self._info_label.show()
79
 
        hbox.show()
80
 
 
81
 
        self.notebook = gtk.Notebook()
82
 
        self.vbox.pack_start(self.notebook)
83
 
 
84
 
        sw = gtk.ScrolledWindow()
85
 
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
86
 
        sw.set_shadow_type(gtk.SHADOW_IN)
87
 
        sw.show()
88
 
        
89
 
        self._buffer = gtk.TextBuffer()
90
 
        self._buffer.create_tag('filename', style=pango.STYLE_ITALIC, foreground='gray20')
91
 
        self._buffer.create_tag('name', foreground='#000055')
92
 
        self._buffer.create_tag('lineno', weight=pango.WEIGHT_BOLD)
93
 
        self._buffer.create_tag('exc', foreground='#880000', weight=pango.WEIGHT_BOLD)
94
 
        
95
 
        self._textview = gtk.TextView(self._buffer)
96
 
        self._textview.set_editable(False)
97
 
        self._textview.set_cursor_visible(False)
98
 
        sw.add(self._textview)
99
 
        self._textview.show()
100
 
        
101
 
        self.notebook.append_page(sw, tab_label=gtk.Label("Exception"))
102
 
        
103
 
 
104
 
        lsw = gtk.ScrolledWindow()
105
 
        lsw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
106
 
        lsw.set_shadow_type(gtk.SHADOW_IN)
107
 
        self._lbuf = gtk.TextBuffer()
108
 
        self._lview = gtk.TextView(self._lbuf)
109
 
        lsw.add(self._lview)
110
 
        self.notebook.append_page(lsw, tab_label=gtk.Label("Log"))
111
 
 
112
 
        isw = gtk.ScrolledWindow()
113
 
        isw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
114
 
        isw.set_shadow_type(gtk.SHADOW_IN)
115
 
        self._ibuf = gtk.TextBuffer()
116
 
        self._iview = gtk.TextView(self._ibuf)
117
 
        
118
 
        isw.add(self._iview)
119
 
        self.notebook.append_page(isw, tab_label=gtk.Label("Stdio"))
120
 
        
121
 
        self.notebook.show_all()
122
 
 
123
 
    def show_exception(self, exctype, value, tb):
124
 
        self._info_label.set_text(str(exctype))
125
 
        self.print_tb(tb)
126
 
        lines = traceback.format_exception_only(exctype, value)
127
 
 
128
 
        msg = traceback.format_exception_only(exctype, value)[0]
129
 
        result = msg.split(' ', 1)
130
 
        if len(result) == 1:
131
 
            msg = result[0]
132
 
            arguments = ''
133
 
        else:
134
 
            msg, arguments = result
135
 
        self._insert(msg, 'exc')
136
 
        self._insert(' ' + arguments)
137
 
 
138
 
        self.show_log()
139
 
 
140
 
    def show_log(self):
141
 
        logfn = self.application.registry.files.log.value()
142
 
        iofn = '%s.io' % logfn
143
 
        lf = open(logfn, 'r')
144
 
        self._lbuf.set_text(lf.read())
145
 
        lf.close()
146
 
        of = open(iofn, 'r')
147
 
        self._ibuf.set_text(of.read())
148
 
        of.close()
149
 
        
150
 
    def _print(self, line):
151
 
        self._buffer.insert_at_cursor(line + '\n')
152
 
 
153
 
    def _insert(self, text, *tags):
154
 
        end_iter = self._buffer.get_end_iter()
155
 
        self._buffer.insert_with_tags_by_name(end_iter, text, *tags)
156
 
        
157
 
    def _print_file(self, filename, lineno, name):
158
 
        if filename.startswith(self._pwd):
159
 
            filename = filename.replace(self._pwd, '')[1:]
160
 
 
161
 
        self._insert('  File ')
162
 
        self._insert(filename, 'filename')
163
 
        self._insert(', line ')
164
 
        self._insert(str(lineno), 'lineno')
165
 
        self._insert(', in ')
166
 
        self._insert(name, 'name')
167
 
        self._insert('\n')
168
 
        
169
 
    def print_tb(self, tb, limit=None):
170
 
        """Print up to 'limit' stack trace entries from the traceback 'tb'.
171
 
 
172
 
        If 'limit' is omitted or None, all entries are printed.  If 'file'
173
 
        is omitted or None, the output goes to sys.stderr; otherwise
174
 
        'file' should be an open file or file-like object with a write()
175
 
        method.
176
 
        """
177
 
 
178
 
        if limit is None:
179
 
            if hasattr(sys, 'tracebacklimit'):
180
 
                limit = sys.tracebacklimit
181
 
        n = 0
182
 
        while tb is not None and (limit is None or n < limit):
183
 
            f = tb.tb_frame
184
 
            lineno = tb.tb_lineno
185
 
            co = f.f_code
186
 
            filename = co.co_filename
187
 
            name = co.co_name
188
 
            self._print_file(filename, lineno, name)
189
 
            line = linecache.getline(filename, lineno)
190
 
            if line:
191
 
                self._print('    ' + line.strip())
192
 
            tb = tb.tb_next
193
 
            n = n+1
194
 
 
195
 
    def _start_debugger(self):
196
 
        import pdb
197
 
        pdb.pm()
198
 
 
199
 
    def _on_send__clicked(self, button):
200
 
        exception_text = self._buffer.get_text(*self._buffer.get_bounds())
201
 
        if DebugWindow.application.send_bugreport(exception_text, self):
202
 
            # the bug was sent and we don't want the user to send it again
203
 
            self.send.set_sensitive(False)
204
 
 
205
 
    def _on_save__clicked(self, button):
206
 
        DebugWindow.application._save_cb(None)
207
 
 
208
 
    def _should_save_be_sensitive(self):
209
 
        #if not DebugWindow.application._project:
210
 
        #    return False
211
 
 
212
 
        #if not DebugWindow.application._project._changed:
213
 
        #    return False
214
 
 
215
 
        return True
216
 
    
217
 
def show(exctype, value, tb):
218
 
    if exctype is not KeyboardInterrupt:
219
 
        dw = DebugWindow()
220
 
        dw.show_exception(exctype, value, tb)
221
 
        dw.run()
222
 
        dw.destroy()
223
 
 
224
 
def show_log():
225
 
    dw = DebugWindow()
226
 
    dw.show_log()
227
 
    dw.notebook.set_current_page(1)
228
 
    dw.run()
229
 
    dw.destroy()
230
 
 
231
 
if __name__ == '__main__':
232
 
    dw = DebugWindow()
233
 
    resp = dw.run()
234
 
    print resp
235
 
    dw.destroy()
236
 
 
237
 
        
238
 
 
239