Salome HOME
Modif V6_4_°
[tools/eficas.git] / Aster / Cata / cataSTA10 / Macro / lire_table_ops.py
1 #@ MODIF lire_table_ops Macro  DATE 15/03/2010   AUTEUR COURTOIS M.COURTOIS 
2 # -*- coding: iso-8859-1 -*-
3 #            CONFIGURATION MANAGEMENT OF EDF VERSION
4 # ======================================================================
5 # COPYRIGHT (C) 1991 - 2004  EDF R&D                  WWW.CODE-ASTER.ORG
6 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY  
7 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY  
8 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR     
9 # (AT YOUR OPTION) ANY LATER VERSION.                                                  
10 #                                                                       
11 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT   
12 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF            
13 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU      
14 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.                              
15 #                                                                       
16 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE     
17 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,         
18 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.        
19 # ======================================================================
20
21 import os
22 import re
23
24 # ------------------------------------------------------------------------------
25 def msplit(chaine, separ):
26    """Equivalent de chaine.split(separ) en acceptant une ou plusieurs
27    occurrences du séparateur.
28    """
29    return re.split('%s+' % re.escape(separ), chaine.strip(separ))
30
31 # ------------------------------------------------------------------------------
32 def lecture_table(texte, nume, separ):
33    """Méthode de construction de l'objet Table à partir d'un texte d'une table
34    au format ASTER.
35    """
36    from Utilitai.transpose import transpose
37    from Utilitai.Table     import Table
38    from Utilitai.Utmess    import  UTMESS
39    
40    tab_lue = {}
41    nume_lign = []
42    idt_deb = '#DEBUT_TABLE\n'
43    idt_fin = '#FIN_TABLE\n'
44    idt_tit = '#TITRE'
45    id_vide = '-'
46    
47    # expression régulière pour découper les N tables du fichier
48    exp = re.compile(re.escape(idt_deb) + '(.*?)' + re.escape(idt_fin),
49                     re.MULTILINE | re.DOTALL)
50    l_txt = exp.findall(texte)
51    nbbloc = len(l_txt)
52    if nume > nbbloc:
53       UTMESS('F', 'TABLE0_10', vali=(nume, nbbloc))
54    txttab = l_txt[nume - 1]
55   
56    # expression régulière pour extraire le titre
57    exp = re.compile(re.escape(idt_tit) + '(.*)$', re.MULTILINE)
58    titre_tab = os.linesep.join([s.strip(separ) for s in exp.findall(txttab)])
59   
60    # restent dans la table les lignes non vides qui ne sont pas des titres
61    txttab = [line for line in txttab.splitlines() \
62                      if line.strip(separ) != '' and not line.startswith(idt_tit)]
63   
64    # ligne des paramètres et des types
65    list_para = msplit(txttab.pop(0), separ)
66    list_type = msplit(txttab.pop(0), separ)
67    nb_para = len(list_type)
68    
69    # format de lecture
70    fmt = {
71       'I' : '([0-9\-\+]+)',
72       'R' : '([0-9\.,\-\+eEdD]+)',
73       'K' : '(.{%(len)s})'
74    }
75    lfmt = ('%s+' % re.escape(separ)).join(
76       [fmt[typ[0]] % { 'len' : typ[1:] } for typ in list_type]
77    )
78    
79    # construction des lignes de la Table
80    l_rows = []
81    for i, line in enumerate(txttab):
82       mat = re.search(lfmt, line)
83       if mat is None or nb_para != len(mat.groups()):
84          UTMESS('F+', 'TABLE0_11', vali=i + 1)
85          if mat is not None:
86             UTMESS('F+', 'TABLE0_12', vali=len(mat.groups()))
87          UTMESS('F', 'TABLE0_13', vali=nb_para)
88       dico = {}
89       for para, typ, ch in zip(list_para, list_type, mat.groups()):
90          ch = ch.strip()
91          if ch != id_vide:
92             if typ == 'I':
93                val = int(ch)
94             elif typ == 'R':
95                val = float(ch)
96             else:
97                val = ch
98             dico[para] = val
99       l_rows.append(dico)
100    
101    tab = Table(l_rows, list_para, list_type, titre_tab)
102    return tab
103
104
105 # ------------------------------------------------------------------------------
106 def lire_table_ops(self, **args):
107    """Méthode corps de la macro LIRE_TABLE
108    """
109    from Utilitai.Utmess     import  UTMESS
110    from Utilitai.UniteAster import UniteAster
111    
112    ier = 0
113    nompro = 'LIRE_TABLE'
114    ### On importe les definitions des commandes a utiliser dans la macro
115    CREA_TABLE = self.get_cmd('CREA_TABLE')
116    UNITE      = self['UNITE']
117    FORMAT     = self['FORMAT']
118    NUME_TABLE = self['NUME_TABLE']
119    SEPARATEUR = self['SEPARATEUR']
120    PARA       = self['PARA']
121    TITRE      = self['TITRE']
122    
123    ### La macro compte pour 1 dans la numerotation des commandes
124    self.set_icmd(1)
125    
126    ### Lecture de la table dans un fichier d unité logique UNITE
127    UL = UniteAster()
128    nomfich=UL.Nom(UNITE)
129    if not os.path.isfile(nomfich):
130       UTMESS('F', 'FONCT0_41', valk=nomfich)
131    
132    texte = open(nomfich,'r').read()
133    # remet UNITE dans son état initial
134    UL.EtatInit()
135    
136    ### mise en forme de la liste de valeurs suivant le format choisi :
137    # pour le moment uniquement ASTER
138    if FORMAT=='ASTER':
139       tab_lue = lecture_table(texte, NUME_TABLE, SEPARATEUR)
140    else:
141       pass
142    
143    ### création de la table ASTER :
144    self.DeclareOut('ut_tab', self.sd)
145    motscles = tab_lue.dict_CREA_TABLE()
146    ut_tab=CREA_TABLE(**motscles)
147    
148    return ier