1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 __date__ ="$15 avr. 2010 19:44:17$"
26 from uiexception import DevelException
28 # Most usable class types
29 TypeString= "".__class__
31 TypeInteger = __ref_integer.__class__
33 TypeDouble = __ref_double.__class__
35 TypeList = __ref_list.__class__
37 TypeDictionnary = __ref_dict.__class__
39 # There is no control to do for these attributes. They are attributes for the
40 # class management and not data of the model.
41 UNCHECKED_ATTRIBUTES = [
50 This class is a placeholder for modeling data. An object based on this class
51 (particular instance or specialized derived class) can defined attributes with
52 the following properties:
54 - a type : the class or the type of the attribute. Setting an attribute to
55 a value whose type is not the specified type raises an exception.
56 - a range : a list of the possible values for the attribute. Setting an
57 attribute to a value not in the range raises an exception
58 - a default: the default value of an attribute when an instance is created
59 - a void flag: the attribute can be authorized to be None or not using this
60 flag. Setting an attribute to a None value while the flag is not set to
61 True raises an exception. By default, a None value is not allowed.
63 These properties are dictionnaries mapping the attribute name to its
64 associated value for the property.
66 A typical usage is to derived this class in a specialized form where the
67 attributes names and there properties are defined in the constructor. See
68 use cases at the end of this file.
71 def __init__(self, typemap=None, rangemap=None, defaultmap=None, voidmap=None):
73 self._rangemap = {} # possible values
74 self._defaultmap = {} # defaults values
75 self._voidmap = {} # None values are allowed
77 if typemap is not None:
78 self._typemap.update(typemap)
79 if rangemap is not None:
80 self._rangemap.update(rangemap)
81 if voidmap is not None:
82 self._voidmap.update(voidmap)
84 # Default initialization (if any)
85 if defaultmap is not None:
86 self._defaultmap.update(defaultmap)
87 for name in self._defaultmap.keys():
88 self.__setattr__(name,self._defaultmap[name])
90 def addAttribute(self, name, type=None, range=None, default=None, void=None):
92 A None argument means that no entry is created in the associated maps.
94 self._typemap[name] = type
97 self._rangemap[name] = range
100 self._voidmap[name] = void
102 if (not void) and (default is None):
105 self.__setattr__(name,default)
107 def __setattr__(self, name, val):
108 if name in UNCHECKED_ATTRIBUTES:
109 self.__dict__[name] = val
113 if name == "_typemap":
114 print "WARNING WARNING WARNING : changing value of _typemap by ",val
116 if name not in self._typemap.keys():
117 raise DevelException("The class "+str(self.__class__)+" has no attribute "+str(name))
120 if not self.__isVoidAllowed(name):
121 raise DevelException("The attribute "+str(name)+" can't be None")
123 # We can stop here and set the value to None
124 self.__dict__[name] = None
127 if self.__isNotValidType(name,val):
128 raise DevelException("The attribute "+str(name)+" must be an instance of "+str(self._typemap[name]))
130 if self.__isNotValidRange(name,val):
131 raise DevelException("The attribute "+str(name)+" must be a value in :"+str(self._rangemap[name]))
133 self.__dict__[name] = val
135 def __getattribute__(self, name):
136 if name in UNCHECKED_ATTRIBUTES:
137 return self.__dict__[name]
139 if name not in self._typemap.keys():
140 raise DevelException("The class "+str(self.__class__)+" has no attribute "+str(name))
141 # The attribute coulb be requested while it has not been created yet (for
142 # example if we did't call the setter before).
143 if not self.__dict__.has_key(name):
146 return self.__dict__[name]
148 def __isNotValidType(self, name, val):
150 ( self._typemap[name] is not None) and
151 ( not isinstance(val,self._typemap[name]) ) )
155 def __isNotValidRange(self, name, val):
157 ( self._rangemap is not None) and
158 ( self._rangemap.has_key(name) ) and
159 ( self._rangemap[name] is not None ) and
160 ( val not in self._rangemap[name] ) )
164 def __isVoidAllowed(self,name):
166 ( self._voidmap is not None) and
167 ( self._voidmap.has_key(name) ) and
168 ( self._voidmap[name] is True ) )
173 print "DATAMODELER ["+str(self.__class__)+"]: self._typemap.keys() = "+str(self._typemap.keys())
179 # ==============================================================================
180 # Basic use cases and unit tests
181 # ==============================================================================
185 typemap["stringdata"] = TypeString
186 typemap["integerdata"] = TypeInteger
187 typemap["anydata"] = None # can be anything
189 data = DataModeler(typemap)
193 data.stringdata = sdata
194 data.integerdata = idata
197 data.anydata = "any value"
200 print data.integerdata
203 def TEST_addAttribute():
205 typemap["stringdata"] = TypeString
206 typemap["integerdata"] = TypeInteger
207 data = DataModeler(typemap)
208 data.stringdata = "a string value"
219 if data.myAttr != ref_value:
228 data.myAttr = "bad type value"
234 def TEST_badAttributeName():
236 map["stringdata"] = TypeString
237 map["integerdata"] = TypeInteger
239 data = DataModeler(map)
241 # this should raise an exception
249 def TEST_badAttributeType():
251 map["stringdata"] = TypeString
252 map["integerdata"] = TypeInteger
254 data = DataModeler(map)
255 # this should raise an exception
263 def TEST_badAttributeRange():
265 map["stringdata"] = TypeString
266 map["integerdata"] = TypeInteger
270 range["integerdata"] = [1,ref_integervalue,7]
272 data = DataModeler(map,range)
273 # this should not raise an exception
275 data.integerdata = ref_integervalue
276 data.stringdata = "anything (no restriction has been defined)"
281 # this should raise an exception
283 data.integerdata = 9999 # a value not in the range
289 def TEST_voidAttributeAllowed():
291 map["stringdata"] = TypeString
292 map["integerdata"] = TypeInteger
295 voidmap["stringdata"] = True
297 data = DataModeler(typemap=map,voidmap=voidmap)
299 # this should not raise an exception
300 data.stringdata = None
301 print data.stringdata
307 # this should raise an exception
308 data.integerdata = None
314 def TEST_defaultValues():
316 typemap["stringdata"] = TypeString
317 typemap["integerdata"] = TypeInteger
319 ref_value = "my initial value"
321 defaultmap["stringdata"] = ref_value
323 data = DataModeler(typemap=typemap,defaultmap=defaultmap)
324 print data.stringdata
325 if data.stringdata != ref_value:
330 if __name__ == "__main__":
331 from unittester import run
332 run("salome/kernel/datamodeler","TEST_usecase")
333 run("salome/kernel/datamodeler","TEST_addAttribute")
334 run("salome/kernel/datamodeler","TEST_badAttributeName")
335 run("salome/kernel/datamodeler","TEST_badAttributeType")
336 run("salome/kernel/datamodeler","TEST_badAttributeRange")
337 run("salome/kernel/datamodeler","TEST_voidAttributeAllowed")
338 run("salome/kernel/datamodeler","TEST_defaultValues")