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