Salome HOME
debug intempestif
[tools/eficas.git] / Noyau / N_CONVERT.py
1 # coding=utf-8
2 # Copyright (C) 2007-2021   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 """
21    Module de conversion des valeurs saisies par l'utilisateur après vérification.
22 """
23
24 from __future__ import absolute_import
25 try:
26     from builtins import object
27 except : pass
28
29 from .N_types import isInt, isFloat, isSequence
30
31
32 def hasIntValue(real):
33     """Est-ce que 'real' a une valeur entière ?
34     """
35     return abs(int(real) - real) < 1.e-12
36
37
38 class Conversion(object):
39
40     """Conversion de type.
41     """
42
43     def __init__(self, name, typeACreer):
44         self.name = name
45         self.typeACreer = typeACreer
46
47     def convert(self, obj):
48         """Filtre liste
49         """
50         in_as_seq = isSequence(obj)
51         if not in_as_seq:
52             obj = (obj,)
53
54         result = []
55         for o in obj:
56             result.append(self.function(o))
57
58         if not in_as_seq:
59             return result[0]
60         else:
61             # ne marche pas avec MACR_RECAL qui attend une liste et non un
62             # tuple
63             return tuple(result)
64
65     def function(self, o):
66         raise NotImplementedError('cette classe doit être dérivée')
67
68
69 class TypeConversion(Conversion):
70
71     """Conversion de typeACreer
72     """
73
74     def __init__(self, typeACreer):
75         Conversion.__init__(self, 'type', typeACreer)
76
77
78 class IntConversion(TypeConversion):
79
80     """Conversion en entier
81     """
82
83     def __init__(self):
84         TypeConversion.__init__(self, 'I')
85
86     def function(self, o):
87         if isFloat(o) and hasIntValue(o):
88             o = int(o)
89         return o
90
91
92 class FloatConversion(TypeConversion):
93
94     """Conversion de type
95     """
96
97     def __init__(self):
98         TypeConversion.__init__(self, 'R')
99
100     def function(self, o):
101         if isFloat(o):
102             o = float(o)
103         return o
104
105
106 class UserASSDConversion(TypeConversion):
107     def __init__(self, classUser):
108         TypeConversion.__init__(self, classUser)
109
110     def function(self, o):
111         if o == None : return None
112         #print ('je cree UserASSDConversion', o, ' ', self.typeACreer)
113         nouvelObj=self.typeACreer(o)
114         return nouvelObj
115
116 class UserASSDMultipleConversion(TypeConversion):
117     def __init__(self, classUser):
118         TypeConversion.__init__(self, classUser)
119
120     def function(self, o):
121         if o == None : return None
122         #print ('je cree dans UserASSDMultipleConversion', o, ' ', self.typeACreer)
123         nouvelObj=self.typeACreer(o)
124         return nouvelObj
125
126 _convertI = IntConversion()
127 _convertR = FloatConversion()
128
129
130 def ConversionFactory(name, typ):
131     if name == 'type':
132         if 'I' in typ   : return _convertI
133         elif 'R' in typ : return _convertR
134     if name == 'UserASSD':
135         return (UserASSDConversion(typ))
136     if name == 'UserASSDMultiple':
137         return (UserASSDMultipleConversion(typ))
138     return None