Salome HOME
reindent + copyright + merge manuel avec la V9_dev sauf repertoires metier
[tools/eficas.git] / Editeur / Eficas_utils.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021   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     Ce module contient des utilitaires divers
22 """
23 import os,re
24 import glob
25 import traceback
26 import codecs,types
27
28 from Extensions.i18n import tr
29
30 def substractList(liste1,liste2):
31     """
32         Enleve tous les elements de liste2 presents dans liste1 et retourne liste1
33     """
34     for item in liste2:
35         try:
36             liste1.remove(item)
37         except:
38             pass
39     return liste1
40
41 def getRepUser(dir):
42     """
43         Determine sur quelle plate-forme s'execute Eficas et recherche
44         le repertoire de l'utilisateur /$home/Eficas_install
45     """
46
47     #rep_user_eficas= os.path.join(os.environ['HOME'],dir)
48     rep_user_eficas= os.path.join(os.path.expanduser("~"),dir)
49     if os.path.exists(rep_user_eficas):
50         if os.path.isfile(rep_user_eficas) :
51             print (tr("Un fichier de nom %s existe deja : impossible de creer un repertoire de meme nom", rep_user_eficas))
52             rep_user_eficas=None
53     else :
54         try:
55             os.mkdir(rep_user_eficas)
56         except:
57             print (tr("Creation du repertoire %s impossible\n Verifiez vos droits d'acces", rep_user_eficas))
58     return rep_user_eficas
59
60 def read_file(file):
61     """
62         ouvre le fichier file et retourne son contenu
63         si pbe retourne None
64     """
65     try :
66         with open(file) as fd :
67             text=fd.read()
68         return text
69     except:
70         return None
71
72 def save_in_file(file,text,dir=None):
73     """
74         cree le fichier file (ou l'ecrase s'il existe) et ecrit text dedans
75         retourne 1 si OK 0 sinon
76     """
77     try :
78         if dir != None:
79             os.chdir(dir)
80         with open(file) as fd :
81             fd.write(text)
82         return 1
83     except:
84         return 0
85
86 def extension_fichier(pathAndFile):
87     """ Return ext if path/filename.ext is given """
88     return os.path.splitext(pathAndFile)[1][1:]
89
90 def stripPath(pathAndFile):
91     """ Return filename.ext if path/filename.ext is given """
92     return os.path.split(pathAndFile)[1]
93
94 def initRep_CataDev(fic_cata,rep_goal):
95     """
96         Initialise le repertoire des catalogues developpeurs (chemin d'acces donne
97         dans le fichier eficas.ini cad :
98           - le cree s'il n'existe pas encore
99           - copie dedans les 3 fichiers necessaires :
100             * __init__.py (pour que ce repertoire puisse etre interprete comme un package)
101             * entete.py (pour realiser les import necessaires a l'interpretation des catalogues)
102             * declaration_concepts.py (idem)
103           - cree le fichier cata_developpeur.py qui sera par la suite importe
104     """
105     try :
106         if not os.path.isdir(rep_goal) :
107             os.mkdir(rep_goal)
108         #texte_entete = getEnteteCata(fic_cata)
109         texte_entete=""
110         # rep_goal doit contenir les catalogues du developpeur sous la forme *.capy
111         # il faut creer le catalogue developpeur par concatenation de entete,declaration_concepts
112         # et de tous ces fichiers
113         cur_dir = os.getcwd()
114         os.chdir(rep_goal)
115         l_cata_dev = glob.glob('*.capy')
116         if os.path.isfile('cata_developpeur.py') : os.remove('cata_developpeur.py')
117         if len(l_cata_dev) :
118             # des catalogues developpeurs sont effectivement presents : on cree cata_dev.py dans rep_goal
119             str = ''
120             str = str + texte_entete+'\n'
121             for file in l_cata_dev :
122                 str = str + open(file,'r').read() +'\n'
123             open('cata_developpeur.py','w+').write(str)
124         os.chdir(cur_dir)
125     except:
126         traceback.print_exc()
127         print ( tr("Impossible de transferer les fichiers requis dans : %s", str(rep_goal)))
128
129 def getEnteteCata(fic_cata):
130     """ Retrouve l'entete du catalogue """
131     l_lignes = open(fic_cata,'r').readlines()
132     txt = ''
133     flag = 0
134     for ligne in l_lignes :
135         if re.match(u"# debut entete",ligne) : flag = 1
136         if re.match(u"# fin entete",ligne) : break
137         if not flag : continue
138         txt = txt + ligne
139     return txt