dist labbe
[iramuteq] / chemins.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008 Pierre Ratinaud
5 #License: GNU/GPL
6
7 import os
8 import sys
9 import tempfile
10 import logging
11
12 log = logging.getLogger('iramuteq.chemins')
13
14
15 def normpath_win32(path) :
16     if not sys.platform == 'win32' :
17         return path
18     while '\\\\' in path :
19         path = path.replace('\\\\', '\\')
20     if sys.platform == 'win32' and path.startswith('\\') and not path.startswith('\\\\') :
21         path = '\\' + path
22     return path
23     
24 class PathOut :
25     def __init__(self, filename = None, analyse_type = '', dirout = None) :
26         if filename is not None :
27             self.filepath = os.path.abspath(filename)
28             self.filebasename = os.path.basename(filename)
29             self.directory = os.path.abspath(os.path.dirname(filename))
30             self.filename, self.fileext = os.path.splitext(self.filebasename)
31         self.analyse = analyse_type
32         #self.dirout = self.mkdirout(dirout)
33         if dirout is not None: 
34             self.dirout = os.path.abspath(dirout)
35         elif filename is not None and dirout is None:
36             self.dirout = os.path.abspath(self.directory)
37         self.d = {} 
38
39     def mkdirout(self) :
40         dirout = os.path.join(self.dirout, self.filename + '_' + self.analyse + '_')
41         nb = 1
42         tdirout = dirout + `nb`
43         while os.path.exists(tdirout) :
44             nb += 1
45             tdirout = dirout + `nb`
46         self.name = os.path.splitext(tdirout)[1]
47         return tdirout
48     
49     def makenew(self, filename, ext):
50         nb = 1
51         newfile = '_'.join([os.path.join(self.dirout, filename), '%i' % nb]) + '.' + ext
52         while os.path.exists(newfile) :
53             nb += 1
54             newfile = '_'.join([os.path.join(self.dirout, filename), '%i' % nb]) + '.' + ext
55         return newfile
56
57     def createdir(self, tdirout) :
58         if not os.path.exists(tdirout) :
59             os.mkdir(tdirout)
60
61     def basefiles(self, ndict) :
62         self.d = ndict
63
64     def __getitem__(self, key) :
65         if key == 'temp' :
66             self.temp = tempfile.mkstemp(prefix='iramuteq')[1].replace('\\', '\\\\')
67             return self.temp
68         elif key not in self.d :
69             f = os.path.join(self.dirout, key).replace('\\', '\\\\')
70             return normpath_win32(f)
71             #return os.path.join(self.dirout, key).replace('\\', '\\\\')
72         else :
73             f = os.path.join(self.dirout, self.d[key]).replace('\\', '\\\\')
74             return normpath_win32(f)
75             #return os.path.join(self.dirout, self.d[key]).replace('\\', '\\\\')
76     
77     def getF(self, key) :
78         return self.__getitem__(key).replace('\\', '/')
79
80
81 def ffr(filename):
82     return filename.replace('\\', '\\\\')
83     
84 def FFF(filename):
85     return filename.replace('\\', '/')
86
87 RscriptsPath = {
88         'Rfunct': 'Rfunct.R',
89         'chdfunct': 'chdfunct.R',
90         'Rgraph': 'Rgraph.R',
91         'plotafcm': 'plotafcm.R',
92         'afc_graph' : 'afc_graph.R',
93         #'CHD': 'CHDPOND.R',
94         'CHD': 'CHD.R',
95         #'CHD' : 'NCHD.R',
96         'chdtxt': 'chdtxt.R',
97         'chdquest': 'chdquest.R',
98         'pamtxt' : 'pamtxt.R',
99         'anacor' : 'anacor.R',
100         #'anacor' : 'Nanacor.R',
101         'simi' : 'simi.R',
102     }
103
104 def ConstructRscriptsPath(AppliPath):
105     RScriptsPath = os.path.join(AppliPath, 'Rscripts')
106     #print('@@@@@@@@@@@PONDERATION CHDPOND.R@@@@@@@@@@@@@@@@')
107     #print('@@@@@@@@@@@ NEW SVD CHEMIN @@@@@@@@@@@@@@@@')
108     #print '@@@@@@@@@@@ NEW NCHD CHEMIN @@@@@@@@@@@@@@@@'
109     DictRscripts = {
110         'Rfunct': ffr(os.path.join(RScriptsPath, 'Rfunct.R')),
111         'chdfunct': ffr(os.path.join(RScriptsPath, 'chdfunct.R')),
112         'Rgraph': ffr(os.path.join(RScriptsPath, 'Rgraph.R')),
113         'plotafcm': ffr(os.path.join(RScriptsPath, 'plotafcm.R')),
114         'afc_graph' : ffr(os.path.join(RScriptsPath, 'afc_graph.R')),
115         #'CHD': ffr(os.path.join(RScriptsPath, 'CHDPOND.R')),
116         'CHD': ffr(os.path.join(RScriptsPath, 'CHD.R')),
117         #'CHD' : ffr(os.path.join(RScriptsPath, 'NCHD.R')),
118         'chdtxt': ffr(os.path.join(RScriptsPath, 'chdtxt.R')),
119         'chdquest': ffr(os.path.join(RScriptsPath, 'chdquest.R')),
120         'pamtxt' : ffr(os.path.join(RScriptsPath, 'pamtxt.R')),
121         'anacor' : ffr(os.path.join(RScriptsPath, 'anacor.R')),
122         #'anacor' : ffr(os.path.join(RScriptsPath, 'Nanacor.R')),
123         'simi' : ffr(os.path.join(RScriptsPath, 'simi.R')),
124     }
125     return DictRscripts
126
127 def ConstructPathOut(Filename, analyse_type):
128     FileBaseName = os.path.basename(Filename)
129     FileBasePath = os.path.dirname(Filename)
130     PathFile = os.path.splitext(FileBaseName)
131     PathFile = os.path.join(FileBasePath, PathFile[0] + '_' + analyse_type + '_1')
132     splitpath = PathFile.split('_')
133     number = int(splitpath[len(splitpath) - 1])
134     while os.path.isdir(PathFile) :
135         if number < 10:
136             PathFile = PathFile[0:len(PathFile) - 1] + str(number + 1)
137             pass
138         elif (number >= 10) and (number < 100):
139             PathFile = PathFile[0:len(PathFile) - 2] + str(number + 1)
140             pass
141         elif number >= 100 :
142             PathFile = PathFile[0:len(PathFile) - 3] + str(number + 1)
143             pass
144         number += 1
145     os.mkdir(os.path.join(FileBasePath, PathFile))
146     return os.path.join(FileBasePath, PathFile)
147
148 def ConstructConfigPath(AppliPath, user=True):
149     if not user:
150         ConfigPath = os.path.join(AppliPath, 'configuration')
151     else :
152         ConfigPath = AppliPath
153     DictConfigPath = {
154         'reinert': os.path.join(ConfigPath, 'reinert.cfg'),
155         'key': os.path.join(ConfigPath, 'key.cfg'),
156         'path': os.path.join(ConfigPath, 'path.cfg'),
157         'preferences' : os.path.join(ConfigPath, 'iramuteq.cfg'),
158         'pam' : os.path.join(ConfigPath, 'pam.cfg'),
159         'corpus' : os.path.join(ConfigPath, 'corpus.cfg'),
160         'stat' : os.path.join(ConfigPath, 'stat.cfg'),
161         'simitxt' : os.path.join(ConfigPath, 'simitxt.cfg'),
162         'matrix' : os.path.join(ConfigPath, 'matrix.cfg'),
163     }
164     return DictConfigPath
165
166 def ConstructGlobalPath(AppliPath):
167     ConfigPath = os.path.join(AppliPath, 'configuration')
168     DictConfigPath = {
169             'global': os.path.join(ConfigPath, 'global.cfg'),
170             'preferences': os.path.join(ConfigPath, 'iramuteq.cfg'),
171             }
172     return DictConfigPath
173
174 def ConstructDicoPath(AppliPath):
175     BasePath = os.path.join(AppliPath, 'dictionnaires')
176     DictPath = {
177         'french': os.path.join(BasePath, 'lexique_fr.txt'),
178         'french_exp': os.path.join(BasePath, 'expression_fr.txt'),
179         'english': os.path.join(BasePath, 'lexique_en.txt'),
180         'english_exp': os.path.join(BasePath, 'expression_en.txt'),
181         'german' :  os.path.join(BasePath, 'lexique_de.txt'),
182         'german_exp' : os.path.join(BasePath, 'expression_de.txt'),
183         'italian' : os.path.join(BasePath, 'lexique_it.txt'),
184         'italian_exp' : os.path.join(BasePath, 'expression_it.txt'),
185         'swedish' :  os.path.join(BasePath, 'lexique_sw.txt'),
186         'swedish_exp' :  os.path.join(BasePath, 'expression_sw.txt'),
187         'portuguese' : os.path.join(BasePath, 'lexique_pt.txt'),
188         'portuguese_exp': os.path.join(BasePath, 'expression_pt.txt'),
189         'greek' : os.path.join(BasePath, 'lexique_gr.txt'),
190         'greek_exp' : os.path.join(BasePath, 'expression_gr.txt'),
191         'spanish' :  os.path.join(BasePath, 'lexique_sp.txt'),
192         'spanish_exp' :  os.path.join(BasePath, 'expression_sp.txt'),
193         'galician' : os.path.join(BasePath, 'lexique_gl.txt'),
194         'galician_exp' : os.path.join(BasePath, 'expression_gl.txt'),
195     }
196     return DictPath
197
198 def ConstructAfcmPath(FilePath):
199     DictAfcmPath = {
200         'Levels': ffr(os.path.join(FilePath, 'afcm-levels.csv')),
201         'nd': ffr(os.path.join(FilePath, 'afcm-nd.csv')),
202         'FileActTemp': ffr(os.path.join(FilePath, 'fileActTamp.csv')),
203         'FileEtTemp': ffr(os.path.join(FilePath, 'FileEtTemp')),
204         'resultat': os.path.join(FilePath, 'Resultats-afcm.html'),
205         'Rafc3d': ffr(tempfile.mkstemp(prefix='iramuteq')[1])
206     }
207     return DictAfcmPath
208
209 def ConstructAfcUciPath(filepath):
210     DictAfcUciPath = {
211         'TableCont': ffr(os.path.join(filepath, 'TableCont.csv')),
212         'TableSup': ffr(os.path.join(filepath, 'TableSup.csv')),
213         'TableEt': ffr(os.path.join(filepath, 'TableEt.csv')),
214         'AfcColAct': ffr(os.path.join(filepath, 'AfcColAct.png')),
215         'AfcColSup': ffr(os.path.join(filepath, 'AfcColSup.png')),
216         'AfcColEt': ffr(os.path.join(filepath, 'AfcColEt.png')),
217         'afcdiv4': ffr(os.path.join(filepath, 'afcdiv4_')),
218         'AfcRow': ffr(os.path.join(filepath, 'AfcRow.png')),
219         'ListAct': ffr(os.path.join(filepath, 'ListAct.csv')),
220         'ListSup': ffr(os.path.join(filepath, 'ListSup.csv')),
221         'GraphAfcTot': os.path.join(filepath, 'GraphAfcTot.html'),
222         'Rafcuci': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
223         'afc_row': ffr(os.path.join(filepath, 'afc_row.csv')),
224         'afc_col': ffr(os.path.join(filepath, 'afc_col.csv')),
225         'ira' : os.path.join(filepath, 'Analyse.ira'),
226     }
227     return DictAfcUciPath
228
229 ChdTxtPathOut = {'TableUc1': 'TableUc1.csv',
230         'TableUc2': 'TableUc2.csv',
231         'listeuce1':  'listeUCE1.csv',
232         'listeuce2':  'listeUCE2.csv',
233         'DicoMots':  'DicoMots.csv',
234         'DicoLem':  'DicoLem.csv',
235         'profile':  'profiles.csv',
236         'antiprofile':  'antiprofiles.csv',
237         'afc':  'AFC.csv',
238         'rapport':  'RAPPORT.txt',
239         'pre_rapport' : 'info.txt',
240         'uce':  'uce.csv',
241         'Rchdtxt': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
242         'arbre1':  'arbre_1.png',
243         'arbre2':  'arbre_2.png',
244         'dendro1':  'dendro1.png',
245         'dendro2':  'dendro2.png',
246         'Rdendro':  'dendrogramme.RData',
247         'Contout':  'classe_mod.csv',
248         'RData':  'RData.RData',
249         'ContSupOut':  'tablesup.csv',
250         'ContEtOut':  'tableet.csv',
251         'PROFILE_OUT':  'profiles.csv',
252         'ANTIPRO_OUT':  'antiprofiles.csv',
253         'SbyClasseOut':  'SbyClasseOut.csv',
254         'chisqtable' :  'chisqtable.csv',
255         'ptable' :  'ptable.csv',
256         'ira': 'Analyse.ira',
257         'AFC2DL_OUT':  'AFC2DL.png',
258         'AFC2DSL_OUT':  'AFC2DSL.png',
259         'AFC2DEL_OUT':  'AFC2DEL.png',
260         'AFC2DCL_OUT':  'AFC2DCL.png',
261         'AFC2DCoul':  'AFC2DCoul.png',
262         'AFC2DCoulSup':  'AFC2DCoulSup.png',
263         'AFC2DCoulEt':  'AFC2DCoulEt.png',
264         'AFC2DCoulCl':  'AFC2DCoulCl.png',
265         'Rafc3d': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
266         'R3DCoul': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
267         'RESULT_CHD':  'resultats-chd.html',
268         'RESULT_AFC':  'resultats-afc.html',
269         'Et01':  'Et01.csv',
270         'Rchdquest':ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
271         'RTxtProfGraph':ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
272         'typelist': 'typelist.csv',
273         'concord':'concordancier.csv',
274         'bduceforme':'bduceforme.csv',
275         'uceuci': 'uceuci.csv',
276         'uciet': 'uciet.csv',
277         'ContTypeOut': 'tabletype.csv',
278         'liste_graph_afc' : 'liste_graph_afc.txt',
279         'liste_graph_chd' : 'liste_graph_chd.txt',
280         'afc_row':  'afc_row.csv',
281         'afc_col':  'afc_col.csv',
282         'afc_facteur':  'afc_facteur.csv',
283         'corpus_exp' : 'corpus_out.txt',
284         'segments_classes' :  'segments_classes.csv',
285         'prof_seg' :  'prof_segments.csv',
286         'antiprof_seg' :  'antiprof_segments.csv',
287         'prof_type' :  'profil_type.csv',
288         'antiprof_type' :  'antiprof_type.csv',
289         'type_cl' :  'type_cl.csv',
290         'db' : 'analyse.db',
291     }
292
293 def StatTxtPathOut(pathout):
294     d = {'tableafcm':ffr(os.path.join(pathout, 'tableafcm.csv')),
295           'tabletypem': ffr(os.path.join(pathout, 'tabletypem.csv')),
296           'tablespecf': ffr(os.path.join(pathout, 'tablespecf.csv')),
297           'tablespect': ffr(os.path.join(pathout, 'tablespect.csv')),
298           'eff_relatif_forme': ffr(os.path.join(pathout, 'eff_relatif_forme.csv')),
299           'eff_relatif_type': ffr(os.path.join(pathout, 'eff_relatif_type.csv')),
300           'afcf_row' : ffr(os.path.join(pathout, 'afcf_row.png')),
301           'afcf_col' : ffr(os.path.join(pathout, 'afcf_col.png')),
302           'afct_row' : ffr(os.path.join(pathout, 'afct_row.png')),
303           'afct_col' : ffr(os.path.join(pathout, 'afct_col.png')),
304           'RData' : ffr(os.path.join(pathout, 'RData.RData')),
305           'liste_graph_afcf' : os.path.join(pathout, 'liste_graph_afcf.txt'),
306           'liste_graph_afct' : os.path.join(pathout, 'liste_graph_afct.txt'),
307           'afcf_row_csv': ffr(os.path.join(pathout, 'afcf_row.csv')),
308           'afcf_col_csv': ffr(os.path.join(pathout, 'afcf_col.csv')),
309           'afcf_facteur_csv': ffr(os.path.join(pathout, 'afcf_facteur.csv')),
310           'afct_row_csv': ffr(os.path.join(pathout, 'afct_row.csv')),
311           'afct_col_csv': ffr(os.path.join(pathout, 'afct_col.csv')),
312           'afct_facteur_csv': ffr(os.path.join(pathout, 'afct_facteur.csv')),
313           'ira' : ffr(os.path.join(pathout, 'Analyse.ira')),
314           'db' : os.path.join(pathout, 'analyse.db'),
315           'zipf' : ffr(os.path.join(pathout, 'zipf.png')),
316     }
317     return d
318
319 #def construct_simipath(pathout):
320 #    d = {'mat01' : 'mat01.csv',
321 #          'matsimi' : 'matsimi.csv',
322 #          'eff' : 'eff.csv',
323 #          'RData' : 'RData.RData',
324 #          'liste_graph' : 'liste_graph.txt',
325 #          'ira' : 'Analyse.ira',
326 #          'film' : '',
327 #          'db' : 'analyse.db',
328 #        }
329
330 simipath = {'mat01' :  'mat01.csv',
331           'matsimi' : 'matsimi.csv',
332           'eff' : 'eff.csv',
333           'RData' : 'RData.RData',
334           'liste_graph' :'liste_graph.txt',
335           'ira' : 'Analyse.ira',
336           'film' : '',
337           'db' : 'analyse.db',
338           'corpus' : 'corpus.db',
339         }
340