File: exception_handler.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 (101 lines) | stat: -rw-r--r-- 3,048 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


import logging
import sys
import traceback
import types

from enthought.traits.api import Any, Bool, HasTraits, Instance, Str


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


#starting with python 2.5 exceptions are no longer instances of types.ClassType
EXCEPTION_BASE_TYPE = types.ClassType
try:
    import exceptions
    if not isinstance(exceptions.Exception, types.ClassType):
        EXCEPTION_BASE_TYPE = types.ObjectType
except:
    pass

class ExceptionHandler(HasTraits):
    """ Provides standardized exception handling.

    Instantiate immediately after 'except:' to capture the exception and
    stack frame information.
    """

    # Application message.
    message = Str

    # Initialized form rom sys.exc_info on object creation.
    ex_type = Instance(EXCEPTION_BASE_TYPE)

    # Initialized form rom sys.exc_info on object creation.
    ex_value = Any # Instance(Exception)

    # Initialized form rom sys.exc_info on object creation.
    ex_traceback = Instance(types.TracebackType)

    # Formatted text for the exception.
    exception_text = Str

    # Formatted text for the exception only. I.e. without stack trace.
    exception_only_text = Str

    # Enter message in the log using enthought.logger; default is True.
    use_logger = Bool(True)

    def __init__(self, **traits):
        """ Creates an ExceptionHandler initialized with the most recent
        traceback information.
        Optionally logs the exception using enthought.logger.
        """
        super(ExceptionHandler,self).__init__(**traits)
        self.ex_type, self.ex_value, self.ex_traceback = sys.exc_info()
        if self.use_logger:
            logger.error( str(self) )
        return

    def __str__(self):
        """ Returns string representation of self. """
        text = self.message + '\n' + self.exception_text
        return text

    def _exception_text_default(self):
        """ Returns formatted exception. """
        list_o_lines = traceback.format_exception( self.ex_type,
                                                   self.ex_value,
                                                   self.ex_traceback)

        lines = ''.join(list_o_lines)

        # remove trailing \n
        return lines.strip()


    def _exception_only_text_default(self):
        """ Returns formatted exception only (see traceback).
        I.e. without stack trace.
        """
        list_o_lines = traceback.format_exception_only( self.ex_type,
                                                        self.ex_value)

        lines = ''.join(list_o_lines)

        # remove trailing \n
        return lines.strip()

    def trait_view(self, name=None, view_element=None):
        """ Returns a View """
        if (name or view_element) != None:
            return super(ExceptionHandler, self).trait_view( name=name,
                                                view_element=view_element )

        from exception_handler_view import ExceptionHandlerView
        return ExceptionHandlerView()

### EOF