File: quality_agent_view.py

package info (click to toggle)
python-enthoughtbase 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 960 kB
  • ctags: 1,034
  • sloc: python: 6,104; makefile: 9; sh: 5
file content (359 lines) | stat: -rw-r--r-- 12,264 bytes parent folder | download | duplicates (2)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought logger package component>
#------------------------------------------------------------------------------

# Standard library imports.
import logging

# Enthought library imports.
from enthought.pyface.api import Dialog
from enthought.traits.api import Any, Str, Tuple


# Setup a logger for this module.
logger = logging.getLogger(__name__)


priority_levels = ['Low', 'Medium', 'High', 'Critical']


class QualityAgentView(Dialog):

    size = Tuple((700, 900))
    title = Str('Quality Agent')

    # The associated LoggerService.
    service = Any()

    msg = Str('')
    subject = Str('Untitled Error Report')
    to_address = Str()
    cc_address = Str('')
    from_address = Str()
    smtp_server = Str()
    priority = Str(priority_levels[2])
    comments = Str('None')
    include_userdata = Any

    ###########################################################################
    # Protected 'Dialog' interface.
    ###########################################################################

    # fixme: Ideally, this should be passed in; this topic ID belongs to the
    #        Enlib help project/plug-in.
    help_id = 'enlib|HID_Quality_Agent_Dlg'

    def _create_dialog_area(self, parent):
        """ Creates the main content of the dialog. """
        import wx

        parent.SetSizeHints(minW=300, minH=575)

        # Add the main panel
        sizer = wx.BoxSizer(wx.VERTICAL)
        panel = wx.Panel(parent, -1)
        panel.SetSizer(sizer)
        panel.SetAutoLayout(True)


        # Add a descriptive label at the top ...
        label = wx.StaticText(panel, -1, "Send a comment or bug report ...")
        sizer.Add(label, 0, wx.ALL, border=5)

        # Add the stack trace view ...
        error_panel = self._create_error_panel(panel)
        sizer.Add(error_panel, 1, wx.ALL|wx.EXPAND|wx.CLIP_CHILDREN, border=5)

        # Update the layout:
        sizer.Fit(panel)

        # Add the error report view ...
        report_panel = self._create_report_panel(panel)
        sizer.Add(report_panel, 2, wx.ALL|wx.EXPAND|wx.CLIP_CHILDREN, border=5)

        # Update the layout:
        sizer.Fit(panel)

        return panel


    def _create_buttons(self, parent):
        """ Creates the buttons. """
        import wx

        sizer = wx.BoxSizer(wx.HORIZONTAL)

        # 'Send' button.
        send = wx.Button(parent, wx.ID_OK, "Send")
        wx.EVT_BUTTON(parent, wx.ID_OK, self._on_send)
        sizer.Add(send)
        send.SetDefault()

        # 'Cancel' button.
        cancel = wx.Button(parent, wx.ID_CANCEL, "Cancel")
        wx.EVT_BUTTON(parent, wx.ID_CANCEL, self._wx_on_cancel)
        sizer.Add(cancel, 0, wx.LEFT, 10)

        # 'Help' button.
        if len(self.help_id) > 0:
            help = wx.Button(parent, wx.ID_HELP, "Help")
            wx.EVT_BUTTON(parent, wx.ID_HELP, self._wx_on_help)
            sizer.Add(help, 0, wx.LEFT, 10)

        return sizer

    def _on_help(self, event):
        """Called when the 'Help' button is pressed. """

        hp = self.service.application.get_service('enthought.help.IHelp')
        hp.library.show_topic(self.help_id)

        return


    ### Utility methods #######################################################

    def _create_error_panel(self, parent):
        import wx

        box = wx.StaticBox(parent, -1, "Message:")
        sizer = wx.StaticBoxSizer(box, wx.VERTICAL)

        # Print the stack trace
        label2 = wx.StaticText(parent, -1,"The following information will be included in the report:")
        sizer.Add(label2, 0, wx.LEFT|wx.TOP|wx.BOTTOM|wx.CLIP_CHILDREN, border=5)

        details = wx.TextCtrl(parent, -1, self.msg, size=(-1,75),
                              style=wx.TE_MULTILINE |
                                    wx.TE_READONLY |
                                    wx.HSCROLL |
                                    wx.VSCROLL |
                                    wx.TE_RICH2 |
                                    wx.CLIP_CHILDREN)
        details.SetSizeHints(minW=-1, minH=75)
        # Set the font to not be proportional
        font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL)
        details.SetStyle(0, len(self.msg), wx.TextAttr(font=font))
        sizer.Add(details, 1, wx.EXPAND|wx.ALL|wx.CLIP_CHILDREN, 5)


        return sizer


    def _create_report_panel(self, parent):
        import wx

        box = wx.StaticBox(parent, -1, "Report Information:")
        sizer = wx.StaticBoxSizer(box, wx.VERTICAL)

        # Add email info ...
        sizer.Add(self._create_email_info(parent), 0, wx.ALL|wx.EXPAND, 5)

        # Add priority combo:
        sizer.Add(self._create_priority_combo(parent), 0, wx.ALL|wx.RIGHT, 5)

        # Extra comments from the user:
        label3 = wx.StaticText(parent, -1, "Additional Comments:")
        sizer.Add(label3, 0, wx.LEFT|wx.TOP|wx.BOTTOM|wx.CLIP_CHILDREN, 5)

        comments_field = wx.TextCtrl(parent, -1, self.comments, size=(-1,75),
                                     style=wx.TE_MULTILINE |
                                           wx.TE_RICH2 |
                                           wx.CLIP_CHILDREN)
        comments_field.SetSizeHints(minW=-1, minH=75)
        font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL)
        comments_field.SetStyle(0, len(self.comments), wx.TextAttr(font=font))
        sizer.Add(comments_field, 1, wx.ALL|wx.EXPAND|wx.CLIP_CHILDREN, 5)
        wx.EVT_TEXT(parent, comments_field.GetId(), self._on_comments)

        # Include the project combobox?
        if len(self.service.mail_files) > 0:
            sizer.Add(self._create_project_upload(parent), 0, wx.ALL, border=5)

        return sizer


    def _create_email_info(self, parent):
        import wx

        # Layout setup ..
        sizer = wx.FlexGridSizer(5,2,10,10)
        sizer.AddGrowableCol(1)

        title_label = wx.StaticText(parent, -1, "Subject:")
        sizer.Add(title_label , 0, wx.ALL|wx.ALIGN_RIGHT)
        title_field = wx.TextCtrl(parent, -1, self.subject, wx.Point(-1,-1))
        sizer.Add(title_field, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT|wx.CLIP_CHILDREN)
        wx.EVT_TEXT(parent, title_field.GetId(), self._on_subject)

        to_label = wx.StaticText(parent, -1, "To:")
        sizer.Add(to_label , 0, wx.ALL|wx.ALIGN_RIGHT)
        to_field = wx.TextCtrl(parent, -1, self.to_address)
        sizer.Add(to_field, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT|wx.CLIP_CHILDREN)
        wx.EVT_TEXT(parent, to_field.GetId(), self._on_to)

        cc_label = wx.StaticText(parent, -1, "Cc:")
        sizer.Add(cc_label, 0, wx.ALL|wx.ALIGN_RIGHT)
        cc_field = wx.TextCtrl(parent, -1, "")
        sizer.Add(cc_field, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT|wx.CLIP_CHILDREN)
        wx.EVT_TEXT(parent, cc_field.GetId(), self._on_cc)

        from_label = wx.StaticText(parent, -1, "From:")
        sizer.Add(from_label, 0, wx.ALL|wx.ALIGN_RIGHT)
        from_field = wx.TextCtrl(parent, -1, self.from_address)
        sizer.Add(from_field, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT|wx.CLIP_CHILDREN)
        wx.EVT_TEXT(parent, from_field.GetId(), self._on_from)

        smtp_label = wx.StaticText(parent, -1, "SMTP Server:")
        sizer.Add(smtp_label, 0, wx.ALL|wx.ALIGN_RIGHT)
        smtp_server_field = wx.TextCtrl(parent, -1, self.smtp_server)
        sizer.Add(smtp_server_field, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT|wx.CLIP_CHILDREN)
        wx.EVT_TEXT(parent, smtp_server_field.GetId(), self._on_smtp_server)

        return sizer


    def _create_priority_combo(self, parent):
        import wx

        sizer = wx.BoxSizer(wx.HORIZONTAL)

        label = wx.StaticText(parent, -1, "How critical is this issue?")
        sizer.Add(label, 0, wx.ALL, border=0)

        cb = wx.ComboBox(parent, -1, self.priority,
                         wx.Point(90, 50), wx.Size(95, -1),
                         priority_levels, wx.CB_READONLY)
        sizer.Add(cb, 1, wx.EXPAND|wx.LEFT|wx.CLIP_CHILDREN, border=10)

        wx.EVT_COMBOBOX(parent, cb.GetId(), self._on_priority)

        return sizer


    def _create_project_upload(self, parent):
        import wx

        id = wx.NewId()
        cb = wx.CheckBox(parent, id, "Include Workspace Files (will increase email size)        ",
                                wx.Point(65, 80), wx.Size(-1, 20), wx.NO_BORDER)
        wx.EVT_CHECKBOX(parent, id, self._on_project)

        return cb


    ## UI Listeners ###########################################################

    def _on_subject(self, event):
        self.subject = event.GetEventObject().GetValue()


    def _on_to(self, event):
        self.to_address = event.GetEventObject().GetValue()


    def _on_cc(self, event):
        self.cc_address = event.GetEventObject().GetValue()


    def _on_from(self, event):
        self.from_address = event.GetEventObject().GetValue()


    def _on_smtp_server(self, event):
        self.smtp_server = event.GetEventObject().GetValue()


    def _on_priority(self, event):
        self.priority = event.GetEventObject().GetStringSelection()


    def _on_comments(self, event):
        self.comments = event.GetEventObject().GetValue()


    def _on_project(self, event):
        self.include_userdata = event.Checked()
        cb = event.GetEventObject()

        if event.Checked():
            cb.SetLabel("Include Workspace Files (approx. %.2f MBytes)" % self._compute_project_size())
        else:
            cb.SetLabel("Include Workspace Files (will increase email size)")
        return

    def _on_send(self, event):
        import wx
        # Disable the Send button while we go through the possibly
        # time-consuming email-sending process.
        button = event.GetEventObject()
        button.Enable(0)

        fromaddr, toaddrs, ccaddrs = self._create_email_addresses()
        message = self._create_email(fromaddr, toaddrs, ccaddrs)

        self.service.send_bug_report(self.smtp_server, fromaddr, toaddrs,
            ccaddrs, message)

        # save the user's preferences
        self.service.preferences.smtp_server = self.smtp_server
        self.service.preferences.to_address = self.to_address
        self.service.preferences.from_address = self.from_address

        # finally we close the dialog
        self._wx_on_ok(event)

        return

    ## Private ################################################################

    def _create_email_addresses(self):
        # utility function map addresses from ui into the standard format
        # FIXME: We should use standard To: header parsing instead of this ad
        # hoc whitespace-only approach.
        fromaddr = self.from_address
        if "" == fromaddr.strip():
            fromaddr = "anonymous"
        toaddrs = self.to_address.split()
        ccaddrs = self.cc_address.split()

        return fromaddr, toaddrs, ccaddrs


    def _compute_project_size(self):
        # determine size of email in MBytes
        fromaddr, toaddrs, ccaddrs = self._create_email_addresses()
        message = self._create_email(fromaddr, toaddrs, ccaddrs)
        return len(message.as_string()) / (2.0**20)


    def _create_email(self, fromaddr, toaddrs, ccaddrs):
        return self.service.create_email_message(
            fromaddr, toaddrs, ccaddrs,
            self.subject,
            self.priority,
            self.include_userdata,
            self.msg,
            self.comments,
        )

    def _to_address_default(self):
        return self.service.preferences.to_address

    def _from_address_default(self):
        return self.service.preferences.from_address

    def _smtp_server_default(self):
        return self.service.preferences.smtp_server

####### EOF #############################################################