Salome HOME
Edit popup
[modules/adao.git] / src / daSalome / daGUI / daGuiImpl / enumerate.py
1 # -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 #  This library is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU Lesser General Public
9 #  License as published by the Free Software Foundation; either
10 #  version 2.1 of the License.
11 #
12 #  This library is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public
18 #  License along with this library; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 __author__ = "gboulant"
25 __date__ = "$1 avr. 2010 09:08:02$"
26
27 class Enumerate(object):
28     """
29     This class emulates a C-like enum for python.
30     """
31     def __init__(self, keys, offset=0):
32         """
33         Canonical constructor
34         @keys a list of keys string to be used as the enum symbols
35         """
36         self._dict_keynumbers = {}
37         for number, key in enumerate(keys):
38             value = offset+number
39             setattr(self, key, value)
40             self._dict_keynumbers[key] = value
41
42
43     def contains(self, key):
44         """
45         Return true if this enumerate contains the specified key
46         @key a key to test
47         """
48         return (key in self._dict_keynumbers.keys())
49
50     def isValid(self, value):
51         return (value in self._dict_keynumbers.values())
52
53     def listkeys(self):
54         list = self._dict_keynumbers.keys()
55         list.sort()
56         return list
57
58     def listvalues(self):
59         list = self._dict_keynumbers.values()
60         list.sort()
61         return list
62
63 #
64 # ==============================================================================
65 # Basic use cases and unit test functions
66 # ==============================================================================
67 #
68
69 def TEST_simple():
70     TYPES_LIST=Enumerate([
71         'SEP',
72         'OTHER'
73     ])
74     print TYPES_LIST.listvalues()
75     return True
76
77 def TEST_createFromList():
78     codes = Enumerate([
79         'KERNEL', # This should take the value 0
80         'GUI', # This should take the value 1
81         'GEOM', # ...
82         'MED',
83         'SMESH'])
84
85     print codes.KERNEL
86     print codes.GEOM
87     if (codes.KERNEL == 0 and codes.GEOM == 2):
88         return True
89     else:
90         return False
91
92 def TEST_createFromString():
93     aList = "KERNEL GUI GEOM MED"
94
95     codes = Enumerate(aList.split())
96
97     print codes.KERNEL
98     print codes.GEOM
99     if (codes.KERNEL == 0 and codes.GEOM == 2):
100         return True
101     else:
102         return False
103
104 def TEST_contains():
105     codes = Enumerate([
106         'KERNEL', # This should take the value 0
107         'GUI', # This should take the value 1
108         'GEOM', # ...
109         'MED',
110         'SMESH'])
111
112     print "VISU in enumerate?", codes.contains("VISU")
113     if ( not codes.contains("VISU") ):
114         return True
115     else:
116         return False
117
118 def TEST_isValid():
119     codes = Enumerate([
120         'KERNEL', # This should take the value 0
121         'GUI', # This should take the value 1
122         'GEOM', # ...
123         'MED',
124         'SMESH'])
125
126     if ( not codes.isValid(23) ):
127         return True
128     else:
129         return False
130
131 def TEST_offset():
132     codes = Enumerate([
133         'KERNEL', # This should take the value 0
134         'GUI', # This should take the value 1
135         'GEOM', # ...
136         'MED',
137         'SMESH'], offset=20)
138
139     print codes.KERNEL
140     print codes.GEOM
141     if (codes.KERNEL == 20 and codes.GEOM == 22):
142         return True
143     else:
144         return False
145
146 def TEST_listvalues():
147     codes = Enumerate([
148         'KERNEL', # This should take the value 0
149         'GUI', # This should take the value 1
150         'GEOM', # ...
151         'MED',
152         'SMESH'], offset=20)
153
154     print codes.listvalues()
155     if codes.listvalues() != [20,21,22,23,24]:
156         return False
157     return True
158
159
160 if __name__ == "__main__":
161     import unittester
162     unittester.run("enumerate","TEST_simple")
163     unittester.run("enumerate","TEST_createFromList")
164     unittester.run("enumerate","TEST_createFromString")
165     unittester.run("enumerate","TEST_contains")
166     unittester.run("enumerate","TEST_isValid")
167     unittester.run("enumerate","TEST_offset")
168     unittester.run("enumerate","TEST_listvalues")