2 # Copyright (C) 2007-2021 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
21 Module de conversion des valeurs saisies par l'utilisateur après vérification.
24 from __future__ import absolute_import
26 from builtins import object
29 from .N_types import isInt, isFloat, isSequence
32 def hasIntValue(real):
33 """Est-ce que 'real' a une valeur entière ?
35 return abs(int(real) - real) < 1.e-12
38 class Conversion(object):
40 """Conversion de type.
43 def __init__(self, name, typeACreer):
45 self.typeACreer = typeACreer
47 def convert(self, obj):
50 in_as_seq = isSequence(obj)
56 result.append(self.function(o))
61 # ne marche pas avec MACR_RECAL qui attend une liste et non un
65 def function(self, o):
66 raise NotImplementedError('cette classe doit être dérivée')
69 class TypeConversion(Conversion):
71 """Conversion de typeACreer
74 def __init__(self, typeACreer):
75 Conversion.__init__(self, 'type', typeACreer)
78 class IntConversion(TypeConversion):
80 """Conversion en entier
84 TypeConversion.__init__(self, 'I')
86 def function(self, o):
87 if isFloat(o) and hasIntValue(o):
92 class FloatConversion(TypeConversion):
98 TypeConversion.__init__(self, 'R')
100 def function(self, o):
106 class UserASSDConversion(TypeConversion):
107 def __init__(self, classUser):
108 TypeConversion.__init__(self, classUser)
110 def function(self, o):
111 #print ('je convertis : ', o, 'en ', self.typeACreer )
113 #traceback.print_stack()
114 if o == None : return None
115 #print ('je cree UserASSDConversion', o, ' ', self.typeACreer)
116 nouvelObj=self.typeACreer(o)
119 class UserASSDMultipleConversion(TypeConversion):
120 def __init__(self, classUser):
121 TypeConversion.__init__(self, classUser)
123 def function(self, o):
124 if o == None : return None
125 #print ('je cree dans UserASSDMultipleConversion', o, ' ', self.typeACreer)
126 nouvelObj=self.typeACreer(o)
129 _convertI = IntConversion()
130 _convertR = FloatConversion()
133 def ConversionFactory(name, typ):
135 if 'I' in typ : return _convertI
136 elif 'R' in typ : return _convertR
137 if name == 'UserASSD':
139 return (UserASSDConversion(typ))
140 if name == 'UserASSDMultiple':
141 return (UserASSDMultipleConversion(typ))