Salome HOME
travail sur monPlusieurs
[tools/eficas.git] / Noyau / nommage.py
index 057badd4a33a4321b0cad379cce62e79a88ff833..e8bb5e844fa27ce760912f25dd2cbf106f287f38 100644 (file)
@@ -1,28 +1,30 @@
-#@ MODIF nommage Noyau  DATE 27/03/2002   AUTEUR DURAND C.DURAND 
-#            CONFIGURATION MANAGEMENT OF EDF VERSION
-# ======================================================================
-# COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
-# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
-# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
-# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR   
-# (AT YOUR OPTION) ANY LATER VERSION.                                 
+# -*- coding: iso-8859-1 -*-
+
+# Copyright (C) 2007-2013   EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
 #
-# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT 
-# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF          
-# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU    
-# GENERAL PUBLIC LICENSE FOR MORE DETAILS.                            
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 #
-# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE   
-# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,       
-#    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.      
-#                                                                       
-#                                                                       
-# ======================================================================
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+
+
 """
    Ce module sert à nommer les concepts produits par les commandes.
    Le nom du concept est obtenu en appelant la fonction GetNomConceptResultat
    du module avec le nom de la commande en argument.
-   
+
    Cette fonction parcourt le source dans lequel la commande se trouve, parse le
    fichier et retrouve le nom du concept qui se trouve à gauche du signe = précédant
    le nom de la commande.
 # Modules Python
 import re,string
 import linecache
+from functools import partial
 
 # Modules EFICAS
 import N_utils
 
-regex1='=\s*%s\s*\('
+regex1='=?\s*%s\s*\('
+#commentaire standard precede d'un nombre quelconque de blancs (pas multiligne)
+pattern_comment   = re.compile(r"^\s*#.*")
 
-def GetNomConceptResultat(ope):
+def _GetNomConceptResultat(ope, level=2):
   """
      Cette fonction recherche dans la pile des appels, l'appel à la commande
      qui doit etre situé à 2 niveaux au-dessus (cur_frame(2)).
@@ -52,40 +57,37 @@ def GetNomConceptResultat(ope):
      et on vérifie que cette ligne correspond véritablement à l'appel.
 
      En effet, lorsque les commandes tiennent sur plusieurs lignes, on retrouve
-     la dernière ligne. Il faut donc remonter dans le source jusqu'à la première 
+     la dernière ligne. Il faut donc remonter dans le source jusqu'à la première
      ligne.
 
      Enfin la fonction evalnom forme un nom acceptable lorsque le concept est un
      élément d'une liste, par exemple.
 
   """
-  f=N_utils.cur_frame(2)
+  f=N_utils.cur_frame(level)
   lineno = f.f_lineno     # XXX Too bad if -O is used
   #lineno = f_lineno(f)  # Ne marche pas toujours
   co = f.f_code
   filename = co.co_filename
   name = co.co_name
-  #print "NOMOP,FICHIER, LIGNE ",ope,filename,lineno
-  line = linecache.getline(filename, lineno)
-  if not line: line = None
+  #pattern pour identifier le debut de la commande
+  pattern_oper=re.compile(regex1 % ope)
 
   list=[]
-  list.append(line)
   while lineno > 0:
-    #print "LIGNE ",line
-    if re.search(regex1 % ope,line):
-      l=re.split(regex1 % ope,line)
+    line = linecache.getline(filename, lineno)
+    lineno=lineno-1
+    if pattern_comment.match(line):continue
+    list.append(line)
+    if pattern_oper.search(line):
+      l=pattern_oper.split(line)
       list.reverse()
-      #print "COMMANDE ",string.join(list)
-      #print "SPLIT ",l
       # On suppose que le concept resultat a bien ete
       # isole en tete de la ligne de source
       m=evalnom(string.strip(l[0]),f.f_locals)
       #print "NOMS ",m
-      return m[-1]
-    lineno=lineno-1
-    line = linecache.getline(filename, lineno)
-    list.append(line)
+      if m!=[] :  return m[-1]
+      else : return ''
   #print "appel inconnu"
   return ""
 
@@ -93,18 +95,16 @@ def evalnom(text,d):
   """
    Retourne un nom pour le concept resultat identifie par text
    Pour obtenir ce nom il y a plusieurs possibilites :
-    1-text est un identificateur python c est le nom du concept
-    2-text est un element d une liste on construit le nom en
-      evaluant la partie indice dans le contexte de l appelant d
+    1. text est un identificateur python c'est le nom du concept
+    2. text est un element d'une liste on construit le nom en
+      evaluant la partie indice dans le contexte de l'appelant d
   """
   l=re.split('([\[\]]+)',text)
-  #print l
   if l[-1] == '' :l=l[:-1]
   lll=[]
   i=0
   while i<len(l):
     s=l[i]
-    ll=string.split(s,',')
     ll=re.split('[ ,]+',s)
     if ll[0] == '' :ll=ll[1:]
     if len(ll) == 1:
@@ -141,3 +141,26 @@ def f_lineno(f):
        line = line + ord(tab[i+1])
    return line
 
+
+class NamingSystem(N_utils.Singleton):
+    """Cette classe définit un système de nommage dynamique des concepts."""
+    _singleton_id = 'nommage.NamingSystem'
+    
+    def __init__(self):
+        """Initialisation"""
+        self.native = _GetNomConceptResultat
+        self.use_global_naming()
+
+    def use_naming_function(self, function):
+        """Utilise une fonction particulière de nommage."""
+        self.naming_func = function
+
+    def use_global_naming(self):
+        """Utilise la fonction native de nommage."""
+        self.naming_func = partial(self.native, level=3)
+
+    def __call__(self, *args):
+        """Appel à la fonction de nommage."""
+        return self.naming_func(*args)
+
+GetNomConceptResultat = NamingSystem()