Salome HOME
b900ade1a5bbe45016abaef4bcce1543901ade36
[tools/eficas.git] / Noyau / N_CONVERT.py
1 # coding=utf-8
2 # Copyright (C) 2007-2017   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         #print ('je convertis : ', o, 'en ', self.typeACreer )
112         #import traceback
113         #traceback.print_stack()
114         if o == None : return None
115         nouvelObj=self.typeACreer(o)
116         return nouvelObj
117
118 _convertI = IntConversion()
119 _convertR = FloatConversion()
120
121
122 def ConversionFactory(name, typ):
123     if name == 'type':
124         if 'I' in typ   : return _convertI
125         elif 'R' in typ : return _convertR
126     if name == 'UserASSD':
127        #print(typ)
128        return (UserASSDConversion(typ))
129     return None