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