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