Salome HOME
gestion des listes et label sur 2
[tools/eficas.git] / Noyau / N_CONVERT.py
1 # coding=utf-8
2 # Copyright (C) 2007-2013   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 N_types import is_int, is_float, is_sequence
25
26
27 def has_int_value(real):
28     """Est-ce que 'real' a une valeur entière ?
29     """
30     return abs(int(real) - real) < 1.e-12
31
32
33 class Conversion:
34
35     """Conversion de type.
36     """
37
38     def __init__(self, name, typ):
39         self.name = name
40         self.typ = typ
41
42     def convert(self, obj):
43         """Filtre liste
44         """
45         in_as_seq = is_sequence(obj)
46         if not in_as_seq:
47             obj = (obj,)
48
49         result = []
50         for o in obj:
51             result.append(self.function(o))
52
53         if not in_as_seq:
54             return result[0]
55         else:
56             # ne marche pas avec MACR_RECAL qui attend une liste et non un
57             # tuple
58             return tuple(result)
59
60     def function(self, o):
61         raise NotImplementedError, 'cette classe doit être dérivée'
62
63
64 class TypeConversion(Conversion):
65
66     """Conversion de type
67     """
68
69     def __init__(self, typ):
70         Conversion.__init__(self, 'type', typ)
71
72
73 class IntConversion(TypeConversion):
74
75     """Conversion en entier
76     """
77
78     def __init__(self):
79         TypeConversion.__init__(self, 'I')
80
81     def function(self, o):
82         if is_float(o) and has_int_value(o):
83             o = int(o)
84         return o
85
86
87 class FloatConversion(TypeConversion):
88
89     """Conversion de type
90     """
91
92     def __init__(self):
93         TypeConversion.__init__(self, 'R')
94
95     def function(self, o):
96         if is_float(o):
97             o = float(o)
98         return o
99
100
101 _convertI = IntConversion()
102 _convertR = FloatConversion()
103
104
105 def ConversionFactory(name, typ):
106     if name == 'type':
107         if 'I' in typ:
108             return _convertI
109         elif 'R' in typ:
110             return _convertR
111     return None