Salome HOME
BOUNDARY... dans Telemac2D
[tools/eficas.git] / generator / Formatage.py
index 1eaf04b453358bf83cc5f87fc7d6fa7d03fc7348..0dc4c0572fd26b46c4631411e943600cf135048c 100644 (file)
@@ -1,31 +1,38 @@
 # -*- coding: utf-8 -*-
-#            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.
+# Copyright (C) 2007-2013   EDF R&D
 #
-# 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.
+# 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.
 #
-# 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.
+# 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.
 #
+# 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
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
-# ======================================================================
 """
     Ce module contient la classe Formatage qui permet le formatage d'une 
     liste de chaines de caractères dans une syntaxe représentative d'un
     jeu de commandes en un texte présentable
 """
-import types,string
+from __future__ import absolute_import
+from __future__ import print_function
+try :
+   from builtins import object
+except : pass
+import types,re
+from Extensions.i18n import tr
+filePattern="'[^\(\)]([^\(\)]*\([^\(\)]*\))*[^\(\)]*'"
+filePattern2='"[^\(\)]([^\(\)]*\([^\(\)]*\))*[^\(\)]*"'
 
-class Formatage :
+class Formatage (object):
   """ 
      Cette classe contient toutes les méthodes nécessaires au formatage
      de la chaine de caracteres issue d'un generator en un fichier
@@ -62,10 +69,13 @@ class Formatage :
        self.l_max = 72
 
   def formate_jdc(self):
+    comment=re.compile("\n#")
+    commentaireavant=0
     for etape in self.l_jdc:
       self.count = self.count+1
       self.texte_etape = ''
-      if type(etape)==types.ListType:
+      #if type(etape)==types.ListType:
+      if type(etape)==list:
         # L'etape est sous la forme d'une liste dont le premier element est une chaine
         self.indent=[]
         self.indent.append(len(etape[0]))
@@ -77,8 +87,26 @@ class Formatage :
         # L'etape est deja sous forme de chaine de caracteres
         self.indent=[]
         self.texte_etape = etape
-      self.jdc_fini = self.jdc_fini + '\n' + self.texte_etape
-    #on enleve la premiere ligne si elle est blanche :
+
+      m=comment.match(self.texte_etape)
+      # si ce n est pas la premiere ligne
+      if self.jdc_fini != ""  : 
+        # si il n y avait pas de commentaire avant on met un saut de ligne
+        if commentaireavant == 0 :
+           self.jdc_fini = self.jdc_fini + '\n' + self.texte_etape
+        else :
+           self.jdc_fini = self.jdc_fini + self.texte_etape
+      # si c est la premiere ligne
+      else :
+        # on ne met pas de saut de ligne avant la premiere ligne 
+        # si c est un commentaire on enleve le saut de ligne precedent
+        if m : self.texte_etape=self.texte_etape[1:]
+        self.jdc_fini = self.texte_etape
+      if m : 
+        commentaireavant=1 
+      else :
+        commentaireavant=0 
+
     return self.jdc_fini
   
   
@@ -104,16 +132,17 @@ class Formatage :
         try:
           increment = len(('\n'+self.indent_courant*' ')*ind + element[0])
         except:
-          print 'ERREUR'
-          print liste
-          print element
-        self.texte_etape = self.texte_etape + ('\n'+self.indent_courant*' ')*ind + element[0]
+          print (tr('ERREUR'))
+          print (liste)
+          print (element)
+        self.texte_etape = self.texte_etape + (u'\n'+self.indent_courant*' ')*ind + element[0]
         length = len(self.indent)
         self.indent.insert(length,self.indent[length-1]+len(element[0]))
         self.indent_courant = self.indent[length]
         # on écrit ses fils
         self.formate_etape(element[1:])
-      elif type(element) == types.StringType:
+      #elif type(element) == types.StringType:
+      elif type(element) == bytes:
 
         # il s'agit d'un mot-clé simple ou de ')' ou ');' ou '),' ou ');\n'
 
@@ -141,7 +170,7 @@ class Formatage :
           self.indent_courant=self.indent[length-2]
       else :
           self.indent_courant=self.indent[0]
-      self.texte_etape = self.texte_etape + string.strip(s_etape)
+      self.texte_etape = self.texte_etape + s_etape.strip()
 
   def traite_mcfact(self,s_mcfact,ind) :
       """
@@ -151,7 +180,7 @@ class Formatage :
           L'attribut self.indent est modifié par le traitement
           L'attribut self.indent_courant est modifié par le traitement
       """
-      self.texte_etape = self.texte_etape + string.strip(s_mcfact)
+      self.texte_etape = self.texte_etape + s_mcfact.strip()
       length = len(self.indent)
       if length > 1:
            last = self.indent[length-1]
@@ -185,17 +214,17 @@ class Formatage :
       else : 
           bool_fonction=0
       longueur = self.longueur(self.texte_etape)
-      increment = len(('\n'+self.indent_courant*' ')*ind + string.strip(s_mcsimp))
-      #self.jdc_fini = self.jdc_fini + ('\n'+self.indent_courant*' ')*ind + string.strip(s_mcsimp)
+      increment = len((u'\n'+self.indent_courant*' ')*ind + s_mcsimp.strip())
       if (bool_fonction == 1 ) :
-          self.texte_etape = self.texte_etape +s_mcsimp
+          self.texte_etape = self.texte_etape+'\n'+self.indent_courant*' ' +s_mcsimp
       elif ( ((1-ind)*longueur+increment) <= self.l_max ) :
-          self.texte_etape = self.texte_etape + ('\n'+self.indent_courant*' ')*ind + string.strip(s_mcsimp)
+          self.texte_etape = self.texte_etape + ('\n'+self.indent_courant*' ')*ind +s_mcsimp.strip() 
       else :
           # il faut couper ...
-          nom,valeur = string.split(s_mcsimp,self.sep,1)
+          #nom,valeur = string.split(s_mcsimp,self.sep,1)
+          nom,valeur = str.split(s_mcsimp,self.sep,1)
           chaine = self.creer_chaine(nom,valeur,'\n'+self.indent_courant*' ',ind)
-          #self.jdc_fini = self.jdc_fini + ('\n'+self.indent_courant*' ')*ind + string.strip(s_mcsimp)
+          #self.jdc_fini = self.jdc_fini + ('\n'+self.indent_courant*' ')*ind + s_mcsimp.strip()
           self.texte_etape = self.texte_etape + chaine
       return
 
@@ -205,8 +234,11 @@ class Formatage :
        texte est une string qui peut contenir des retours chariots
        Cette méthode retourne la longueur de la dernière ligne de texte 
     """
-    liste = string.split(texte,'\n')
-    return len(liste[-1])
+    #liste = texte.split('\n')
+    #return len(liste[-1])
+    if texte [-1] == '\n' : return 0 
+    return len(texte[texte.rfind('\n'):-1])
+    
 
   def creer_chaine(self,nom,valeur,increment,ind):
     """
@@ -221,19 +253,26 @@ class Formatage :
       s=texte + label
       longueur = len(increment + label)
 
-      if ('(' not in valeur) or (valeur[0:3]=='"""'):
-#      if ('(' not in valeur):
+      if ('(' not in valeur) or (valeur[0:3]=='"""') :
         # il s'agit d'une vraie chaîne de caractères
         val = len(valeur)
         texte = (self.l_max-2-val)*' '+valeur
         s=s+'\n'+texte
-
+      elif re.match(filePattern,valeur) or re.match(filePattern2,valeur):
+        val = len(valeur)
+        texte = (self.l_max-2-val)*' '+valeur
+        s=s+'\n'+texte
       elif ',' in valeur:
+        # il s'agit d'une liste de tuple
+        # c est trop complique on ne splitte pas
+        if valeur[0:2]=='((' or valeur[0:2]=='[(':
+           s=s+valeur
+           return s
         # il s'agit d'une liste
-        liste = string.split(valeur,',')
+        liste = valeur.split(',')
         i=0
         for arg in liste :
-          ajout = string.strip(arg)
+          ajout = arg.strip()
           if len(ajout) == 0 : continue
           longueur = self.longueur(texte = (texte + label)) + len(ajout +',') + (1-i)*len(increment)
           if longueur  <= self.l_max:
@@ -258,3 +297,28 @@ class Formatage :
         s=s+'\n'+texte
 
     return s
+
+class FormatageLigne(Formatage) :
+  def __init__(self,l_jdc,code=None,mode=None,sep='=',l_max="**"):
+      Formatage.__init__(self,l_jdc,code=None,mode=None,sep='=',l_max="**")
+      
+  def formate_jdc(self):
+      texte1=Formatage.formate_jdc(self)
+      newText=""
+      lignes=texte1.split("\n")
+      texte=""
+      pattern_debut_blanc  = re.compile(r"^ \s*.*")
+      pattern_commentaire   = re.compile(r"^\s*#.*")
+      pattern_vide=re.compile(r"\s*^$")
+      for l in lignes :
+          if pattern_commentaire.match(l) or pattern_vide.match(l): 
+             newText+=l+"\n"
+             continue
+          if not pattern_debut_blanc.match(l) : texte=l 
+          else : texte+=re.sub(r'^ \s*',' ',l)
+          if texte[-1]==";" :
+             newText+=texte+"\n"
+             texte=""
+      return newText
+
+