Salome HOME
travail sur monPlusieurs
[tools/eficas.git] / Noyau / N_CONVERT.py
1 # -*- coding: iso-8859-1 -*-
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_enum, 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    """Conversion de type.
35    """
36    def __init__(self, name, typ):
37       self.name = name
38       self.typ  = typ
39
40    def convert(self, obj):
41       """Filtre liste
42       """
43       in_as_seq = is_sequence(obj)
44       if not in_as_seq:
45          obj = (obj,)
46
47       result = []
48       for o in obj:
49          result.append(self.function(o))
50       
51       if not in_as_seq:
52          return result[0]
53       else:
54          # ne marche pas avec MACR_RECAL qui attend une liste et non un tuple
55          return tuple(result)
56
57    def function(self, o):
58       raise NotImplementedError, 'cette classe doit être dérivée'
59
60
61 class TypeConversion(Conversion):
62    """Conversion de type
63    """
64    def __init__(self, typ):
65       Conversion.__init__(self, 'type', typ)
66
67
68 class IntConversion(TypeConversion):
69    """Conversion en entier
70    """
71    def __init__(self):
72       TypeConversion.__init__(self, 'I')
73
74    def function(self, o):
75       if is_float(o) and has_int_value(o):
76          o = int(o)
77       return o
78
79
80 class FloatConversion(TypeConversion):
81    """Conversion de type
82    """
83    def __init__(self):
84       TypeConversion.__init__(self, 'R')
85
86    def function(self, o):
87       if is_float(o):
88          o = float(o)
89       return o
90
91
92 _convertI = IntConversion()
93 _convertR = FloatConversion()
94
95 def ConversionFactory(name, typ):
96    if name == 'type':
97       if 'I' in typ:
98          return _convertI
99       elif 'R' in typ:
100          return _convertR
101    return None
102
103