Salome HOME
legere difference ds VARIABLES_TO_BE...
[tools/eficas.git] / Traducteur / load.py
1 # -*- coding: utf-8 -*-
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 import os
22 import re
23 from Traducteur import parseur
24 from Traducteur.mocles import parseKeywords
25
26 import sets
27 jdcSet=sets.Set()
28
29
30 class JDCTrad:
31     """Cet objet conserve toutes les informations relatives à un fichier de commandes .comm"""
32
33     def __init__(self,src,atraiter):
34     #----------------------------------------
35         self.atraiter=atraiter
36         self.init(src,atraiter)
37         commands= self.root.childNodes[:]
38         commands.reverse()
39         for c in commands:
40             jdcSet.add(c.name)
41
42     def init(self,src,atraiter):
43     #---------------------------
44     # construction de self.lines
45         self.root=parseur.Parser(src,atraiter)
46         self.lines=src.splitlines(1)
47
48     def parseKeywords(self):
49     #-----------------------
50     # construction de fils (cf mocles.py)
51         parseKeywords(self.root)
52
53     def reset(self,src):
54     #-----------------------
55     # reconstruction 
56         self.init(src,self.atraiter)
57         self.parseKeywords()
58
59     def getSource(self):
60     #-----------------------
61     # retourne la concatenation de
62     # toutes les lignes 
63         return  "".join(self.getLines())
64
65     def getLine(self,linenum):
66     #-----------------------
67     # retourne la linenumieme ligne
68         return self.getLines()[linenum-1]
69
70     def getLines(self):
71     #----------------------------
72     # retourne toutes les lignes 
73         return self.lines
74
75     def addLine(self,ligne,numero) :
76     #----------------------------
77     # insere le texte contenu dans ligne
78     # dans la liste self.lines au rang numero
79         Ldebut=self.lines[0:numero]
80         Lmilieu=[ligne,]
81         Lfin=self.lines[numero:]
82         self.lines=Ldebut+Lmilieu+Lfin
83
84
85     def splitLine(self,numeroLigne,numeroColonne) :
86     #----------------------------------------------
87     # coupe la ligne numeroLigne en 2 a numeroColonne
88     # ajoute des blancs en debut de 2nde Ligne pour
89     # aligner 
90         numeroLigne = numeroLigne -1
91         Ldebut=self.lines[0:numeroLigne]
92         if len(self.lines) > numeroLigne :
93            Lfin=self.lines[numeroLigne+1:]
94         else :
95            Lfin=[]
96         Lsplit=self.lines[numeroLigne]
97         LigneSplitDebut=Lsplit[0:numeroColonne]+"\n"
98         LigneSplitFin=" "*numeroColonne+Lsplit[numeroColonne:]
99         Lmilieu=[LigneSplitDebut,LigneSplitFin]
100
101         self.lines=Ldebut+Lmilieu+Lfin
102
103     def joinLineandNext(self,numeroLigne) :
104     #--------------------------------------
105     # concatene les lignes numeroLigne et numeroLigne +1
106     # enleve les blancs de debut de la ligne (numeroLigne +1)
107         Ldebut=self.lines[0:numeroLigne-1]
108         if len(self.lines) > numeroLigne :
109            Lfin=self.lines[numeroLigne+1:]
110         else :
111            Lfin=[]
112
113         ligneMilieuDeb=self.lines[numeroLigne - 1 ]
114         ligneMilieuDeb=ligneMilieuDeb[0:-1]
115         ligneMilieuFin=self.lines[numeroLigne]
116         for i in range(len(ligneMilieuFin)):
117             if ligneMilieuFin[i] != " " :
118                ligneMilieuFin=ligneMilieuFin[i:]
119                break
120         Lmilieu=[ligneMilieuDeb+ligneMilieuFin,]
121
122         self.lines=Ldebut+Lmilieu+Lfin
123
124     def supLignes(self,debut,fin):
125     #------------------------
126         Ldebut=self.lines[0:debut-1]
127         Lfin=self.lines[fin:]
128         self.lines=Ldebut+Lfin
129
130     def remplaceLine(self,numeroLigne,nouveauTexte) :
131     #------------------------------------------------
132         self.lines[numeroLigne]=nouveauTexte
133
134 def getJDC(filename,atraiter):
135 #----------------------------
136 # lit le JDC
137     f=open(filename)
138     src=f.read()
139     f.close()
140     jdc=JDCTrad(src,atraiter)
141     return jdc
142
143 def getJDCFromTexte(texte,atraiter):
144 #-----------------------------------
145 # lit le JDC
146     jdc=JDCTrad(texte,atraiter)
147     return jdc