Salome HOME
e9ac6f484af4f4738d1c169bd54b3900fc2cb362
[tools/eficas.git] / Validation / V_AU_PLUS_UN.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 class AU_PLUS_UN:
22    """
23       La règle vérifie que l'on trouve 1 (au plus) des mots-clés
24       de la règle parmi les arguments d'un OBJECT.
25
26       Ces arguments sont transmis à la règle pour validation sous la forme
27       d'une liste de noms de mots-clés ou d'un dictionnaire dont
28       les clés sont des noms de mots-clés.
29    """
30    def verif(self, args):
31       """
32           La méthode verif vérifie que l'on trouve 1 (au plus) des mos-clés
33           de la liste self.mcs parmi les éléments de args
34
35           args peut etre un dictionnaire ou une liste. Les éléments de args
36           sont soit les éléments de la liste soit les clés du dictionnaire.
37       """
38       #  on compte le nombre de mots cles presents
39       text = ''
40       count = 0
41       args = self.liste_to_dico(args)
42       for mc in self.mcs:
43          count = count + args.get(mc, 0)
44       if count > 1:
45          text = u"- Il ne faut qu'un mot-clé (au plus) parmi : "+`self.mcs`+'\n'
46          return text, 0
47       return text, 1
48
49    def liste_to_dico(self, args) :
50       if type(args) is dict:
51          return args
52       elif type(args) in (list, tuple):
53          dico={}
54          for arg in args:
55             dico[arg] = dico.get(arg, 0) + 1
56          return dico
57       else :
58          raise Exception("Erreur ce n'est ni un dictionnaire ni une liste %s" % args)