Salome HOME
Add functions to know that SALOME module version is development one
[modules/kernel.git] / src / KERNEL_PY / kernel / uiexception.py
1 # -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2010  EDF R&D
3 #
4 #  This library is free software; you can redistribute it and/or
5 #  modify it under the terms of the GNU Lesser General Public
6 #  License as published by the Free Software Foundation; either
7 #  version 2.1 of the License.
8 #
9 #  This library is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #  Lesser General Public License for more details.
13 #
14 #  You should have received a copy of the GNU Lesser General Public
15 #  License along with this library; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # -* Makefile *- 
21 #
22 __author__="gboulant"
23 __date__ ="$31 mars 2010 11:59:33$"
24
25 from enumerate import Enumerate
26
27 class UiException(Exception):
28
29     TYPES = Enumerate([
30         'USER',  # This type should be displayed to end user using a dialog box
31         'ADMIN', # This type should be displayed to admin user in console log
32         'DEVEL'  # This type should be displayed to developer only
33     ])
34
35     _UImessage = "An error occurs"
36     _type = TYPES.USER
37
38     """
39     This exception should be used for functionnal error management, at least in the GUI
40     part of the application, for example to set user oriented messages at point
41     of exception raise.
42     WARN: The exception should NOT be used to hide defaults in the algorithm, but
43     only predicted error in the specified use case.
44     """
45     def __init__(self, message, type=TYPES.USER):
46         """Canonical constructor"""
47         Exception.__init__(self,message)
48         self.setUIMessage(message)
49         self.setType(type)
50
51     def setUIMessage(self, UImessage):
52         self._UImessage = UImessage
53
54     def getUIMessage(self):
55         return self._UImessage
56
57     def setType(self, type):
58         """Specify the type of this exception. To be choosen in the TYPES list."""
59         if not self.TYPES.isValid(type):
60             raise UiException("The exception type "+str(type)+" can't be used",self.TYPES.DEVEL)
61         self._type = type
62
63     def getType(self):
64         return self._type
65
66     def __str__(self):
67         return self.getUIMessage()
68
69
70 def UserException(msg):
71     return UiException(msg,UiException.TYPES.USER)
72
73 def AdminException(msg):
74     return UiException(msg,UiException.TYPES.ADMIN)
75
76 def DevelException(msg):
77     return UiException(msg,UiException.TYPES.DEVEL)
78
79 #
80 # ==============================================================================
81 # Basic use cases and unit test functions
82 # ==============================================================================
83 #
84 def somethingGoesWrong():
85     raise UiException("Something goes wrong")
86
87 def TEST_uimessage():
88     try:
89         somethingGoesWrong()
90         return False
91     except UiException, err:
92         print 'ERROR: %s' % str(err)
93         if ( str(err) == "Something goes wrong" ):
94             return True
95         else:
96             return False
97
98 def TEST_specificException():
99     print DevelException("err")
100     print AdminException("err")
101     print UserException("err")
102     return True
103
104 if __name__ == "__main__":
105     import unittester
106     unittester.run("uiexception","TEST_uimessage")
107     unittester.run("uiexception","TEST_specificException")
108