Salome HOME
modif pour MT
[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, typ):
44         self.name = name
45         self.typ = typ
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 type
72     """
73
74     def __init__(self, typ):
75         Conversion.__init__(self, 'type', typ)
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 _convertI = IntConversion()
107 _convertR = FloatConversion()
108
109
110 def ConversionFactory(name, typ):
111     if name == 'type':
112         if 'I' in typ:
113             return _convertI
114         elif 'R' in typ:
115             return _convertR
116     return None