~durga/maus/rel709

« back to all changes in this revision

Viewing changes to src/common_py/gui/gui_exception_handler.py

  • Committer: Durga Rajaram
  • Date: 2013-11-23 13:36:13 UTC
  • mfrom: (659.1.78 rc)
  • Revision ID: durga@fnal.gov-20131123133613-7t1s89cksp4x9bev
Tags: MAUS-v0.7.5
MAUS-v0.7.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
 
2
 
3
#  MAUS is free software: you can redistribute it and/or modify
 
4
#  it under the terms of the GNU General Public License as published by
 
5
#  the Free Software Foundation, either version 3 of the License, or
 
6
#  (at your option) any later version.
 
7
 
8
#  MAUS 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 General Public License
 
14
#  along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
"""
 
17
Gui handler class 
 
18
"""
 
19
 
 
20
import sys
 
21
import ROOT
 
22
 
 
23
import gui.window
 
24
from gui.window import GuiError
 
25
 
 
26
#pylint: disable=E1101
 
27
 
 
28
class MessageWindow: #pylint: disable=R0903
 
29
    """
 
30
    Make a window that just holds a label with a given message
 
31
    """
 
32
    def __init__(self, message):
 
33
        """
 
34
        Initialise; parent will always be main_frame
 
35
        """
 
36
        _message = str(message)
 
37
        _json_data = {
 
38
          "type":"transient_frame",
 
39
          "name":"error",
 
40
          "children":[{
 
41
                  "type":"label",
 
42
                  "label_length":len(_message)+5,
 
43
                  "name":_message,
 
44
              }, {
 
45
                  "type":"button",
 
46
                  "name":"&Okay"
 
47
              }
 
48
          ]
 
49
        }
 
50
        self.window = gui.window.Window(ROOT.gClient.GetRoot(),
 
51
                                        ROOT.gClient.GetRoot(),
 
52
                                        json_data=_json_data)
 
53
        self.window.set_button_action("&Okay", self.okay_action)
 
54
 
 
55
    def okay_action(self):
 
56
        """
 
57
        Pressing Okay just closes the window
 
58
        """
 
59
        self.window.close_window()
 
60
 
 
61
ERROR_LEVEL = 1
 
62
SYS_EXCEPTHOOK = sys.excepthook
 
63
 
 
64
def set_error_level(error_level):
 
65
    """
 
66
    Set the error level that will be handled by the exception handler
 
67
      - error_level: string or corresponding integer error level that will cause
 
68
        the exception handler to respond. Options are "errors", "exceptions",
 
69
        "gui_exceptions", "nothing"
 
70
 
 
71
    Also calls set_excepthook()
 
72
    """
 
73
    global ERROR_LEVEL # pylint: disable=W0603
 
74
    my_error_levels = ["errors", "exceptions", "gui_exceptions", "nothing"]
 
75
    if error_level in my_error_levels:
 
76
        ERROR_LEVEL = my_error_levels.index(error_level)
 
77
    elif error_level in range(4):
 
78
        ERROR_LEVEL = error_level
 
79
    else:
 
80
        raise ValueError("Did not recognise error_level "+str(error_level))
 
81
    set_excepthook()
 
82
 
 
83
def set_excepthook():
 
84
    """
 
85
    Set the excepthook to gui_exception_handler excepthook
 
86
    """
 
87
    sys.excepthook = _excepthook
 
88
 
 
89
def reset_excepthook():
 
90
    """
 
91
    Reset the excepthook to sys.excepthook
 
92
    """
 
93
    sys.excepthook = SYS_EXCEPTHOOK
 
94
 
 
95
def _excepthook(_type, _value, _traceback):
 
96
    """
 
97
    GuiExceptionHandler excepthook
 
98
    """
 
99
    if ERROR_LEVEL == 0:
 
100
        MessageWindow(str(_value))
 
101
    elif ERROR_LEVEL == 1 and isinstance(_value, Exception):
 
102
        MessageWindow(str(_value))
 
103
    elif ERROR_LEVEL == 2 and isinstance(_value, GuiError):
 
104
        MessageWindow(str(_value))
 
105
    SYS_EXCEPTHOOK(_type, _value, _traceback)
 
106
 
 
107