...
[iramuteq] / chemins.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008 Pierre Ratinaud
5 #Lisense: 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 def ConstructRscriptsPath(AppliPath):
66     RScriptsPath = os.path.join(AppliPath, 'Rscripts')
67     #print('@@@@@@@@@@@PONDERATION CHDPOND.R@@@@@@@@@@@@@@@@')
68     #print('@@@@@@@@@@@ NEW SVD CHEMIN @@@@@@@@@@@@@@@@')
69     #print '@@@@@@@@@@@ NEW NCHD CHEMIN @@@@@@@@@@@@@@@@'
70     DictRscripts = {
71         'Rfunct': ffr(os.path.join(RScriptsPath, 'Rfunct.R')),
72         'chdfunct': ffr(os.path.join(RScriptsPath, 'chdfunct.R')),
73         'Rgraph': ffr(os.path.join(RScriptsPath, 'Rgraph.R')),
74         'plotafcm': ffr(os.path.join(RScriptsPath, 'plotafcm.R')),
75         'afc_graph' : ffr(os.path.join(RScriptsPath, 'afc_graph.R')),
76         #'CHD': ffr(os.path.join(RScriptsPath, 'CHDPOND.R')),
77         'CHD': ffr(os.path.join(RScriptsPath, 'CHD.R')),
78         #'CHD' : ffr(os.path.join(RScriptsPath, 'NCHD.R')),
79         'chdtxt': ffr(os.path.join(RScriptsPath, 'chdtxt.R')),
80         'chdquest': ffr(os.path.join(RScriptsPath, 'chdquest.R')),
81         'pamtxt' : ffr(os.path.join(RScriptsPath, 'pamtxt.R')),
82         'anacor' : ffr(os.path.join(RScriptsPath, 'anacor.R')),
83         #'anacor' : ffr(os.path.join(RScriptsPath, 'Nanacor.R')),
84         'simi' : ffr(os.path.join(RScriptsPath, 'simi.R')),
85     }
86     return DictRscripts
87
88 def ConstructPathOut(Filename, analyse_type):
89     FileBaseName = os.path.basename(Filename)
90     FileBasePath = os.path.dirname(Filename)
91     PathFile = os.path.splitext(FileBaseName)
92     PathFile = os.path.join(FileBasePath, PathFile[0] + '_' + analyse_type + '_1')
93     splitpath = PathFile.split('_')
94     number = int(splitpath[len(splitpath) - 1])
95     while os.path.isdir(PathFile) :
96         if number < 10:
97             PathFile = PathFile[0:len(PathFile) - 1] + str(number + 1)
98             pass
99         elif (number >= 10) and (number < 100):
100             PathFile = PathFile[0:len(PathFile) - 2] + str(number + 1)
101             pass
102         elif number >= 100 :
103             PathFile = PathFile[0:len(PathFile) - 3] + str(number + 1)
104             pass
105         number += 1
106     os.mkdir(os.path.join(FileBasePath, PathFile))
107     return os.path.join(FileBasePath, PathFile)
108
109 def ConstructConfigPath(AppliPath, user=True):
110     if not user:
111         ConfigPath = os.path.join(AppliPath, 'configuration')
112     else :
113         ConfigPath = AppliPath
114     DictConfigPath = {
115         'alceste': os.path.join(ConfigPath, 'alceste.cfg'),
116         'key': os.path.join(ConfigPath, 'key.cfg'),
117         'path': os.path.join(ConfigPath, 'path.cfg'),
118         'preferences' : os.path.join(ConfigPath, 'iramuteq.cfg'),
119         'pam' : os.path.join(ConfigPath, 'pam.cfg'),
120         'corpus' : os.path.join(ConfigPath, 'corpus.cfg'),
121         'stat' : os.path.join(ConfigPath, 'stat.cfg'),
122         'simitxt' : os.path.join(ConfigPath, 'simitxt.cfg'),
123     }
124     return DictConfigPath
125
126 def ConstructGlobalPath(AppliPath):
127     ConfigPath = os.path.join(AppliPath, 'configuration')
128     DictConfigPath = {
129             'global': os.path.join(ConfigPath, 'global.cfg'),
130             }
131     return DictConfigPath
132
133 def ConstructDicoPath(AppliPath):
134     BasePath = os.path.join(AppliPath, 'dictionnaires')
135     DictPath = {
136         'french': os.path.join(BasePath, 'lexique_fr.txt'),
137         'french_exp': os.path.join(BasePath, 'expression_fr.txt'),
138         'english': os.path.join(BasePath, 'lexique_en.txt'),
139         'english_exp': os.path.join(BasePath, 'expression_en.txt'),
140         'german' :  os.path.join(BasePath, 'lexique_de.txt'),
141         'german_exp' : os.path.join(BasePath, 'expression_de.txt'),
142         'italian' : os.path.join(BasePath, 'lexique_it.txt'),
143         'italian_exp' : os.path.join(BasePath, 'expression_it.txt'),
144         'swedish' :  os.path.join(BasePath, 'lexique_sw.txt'),
145         'swedish_exp' :  os.path.join(BasePath, 'expression_sw.txt'),
146         'portuguese' : os.path.join(BasePath, 'lexique_pt.txt'),
147         'portuguese_exp': os.path.join(BasePath, 'expression_pt.txt'),
148         'greek' : os.path.join(BasePath, 'lexique_gr.txt'),
149         'greek_exp' : os.path.join(BasePath, 'expression_gr.txt'),
150     }
151     return DictPath
152
153 def ConstructAfcmPath(FilePath):
154     DictAfcmPath = {
155         'Levels': ffr(os.path.join(FilePath, 'afcm-levels.csv')),
156         'nd': ffr(os.path.join(FilePath, 'afcm-nd.csv')),
157         'FileActTemp': ffr(os.path.join(FilePath, 'fileActTamp.csv')),
158         'FileEtTemp': ffr(os.path.join(FilePath, 'FileEtTemp')),
159         'resultat': os.path.join(FilePath, 'Resultats-afcm.html'),
160         'Rafc3d': ffr(tempfile.mkstemp(prefix='iramuteq')[1])
161     }
162     return DictAfcmPath
163
164 def ConstructAfcUciPath(filepath):
165     DictAfcUciPath = {
166         'TableCont': ffr(os.path.join(filepath, 'TableCont.csv')),
167         'TableSup': ffr(os.path.join(filepath, 'TableSup.csv')),
168         'TableEt': ffr(os.path.join(filepath, 'TableEt.csv')),
169         'AfcColAct': ffr(os.path.join(filepath, 'AfcColAct.png')),
170         'AfcColSup': ffr(os.path.join(filepath, 'AfcColSup.png')),
171         'AfcColEt': ffr(os.path.join(filepath, 'AfcColEt.png')),
172         'afcdiv4': ffr(os.path.join(filepath, 'afcdiv4_')),
173         'AfcRow': ffr(os.path.join(filepath, 'AfcRow.png')),
174         'ListAct': ffr(os.path.join(filepath, 'ListAct.csv')),
175         'ListSup': ffr(os.path.join(filepath, 'ListSup.csv')),
176         'GraphAfcTot': os.path.join(filepath, 'GraphAfcTot.html'),
177         'Rafcuci': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
178         'afc_row': ffr(os.path.join(filepath, 'afc_row.csv')),
179         'afc_col': ffr(os.path.join(filepath, 'afc_col.csv')),
180         'ira' : os.path.join(filepath, 'Analyse.ira'),
181     }
182     return DictAfcUciPath
183
184 ChdTxtPathOut = {'TableUc1': 'TableUc1.csv',
185         'TableUc2': 'TableUc2.csv',
186         'listeuce1':  'listeUCE1.csv',
187         'listeuce2':  'listeUCE2.csv',
188         'DicoMots':  'DicoMots.csv',
189         'DicoLem':  'DicoLem.csv',
190         'profile':  'profiles.csv',
191         'antiprofile':  'antiprofiles.csv',
192         'afc':  'AFC.csv',
193         'rapport':  'RAPPORT.txt',
194         'pre_rapport' : 'info.txt',
195         'uce':  'uce.csv',
196         'Rchdtxt': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
197         'arbre1':  'arbre_1.png',
198         'arbre2':  'arbre_2.png',
199         'dendro1':  'dendro1.png',
200         'dendro2':  'dendro2.png',
201         'Rdendro':  'dendrogramme.RData',
202         'Contout':  'classe_mod.csv',
203         'RData':  'RData.RData',
204         'ContSupOut':  'tablesup.csv',
205         'ContEtOut':  'tableet.csv',
206         'PROFILE_OUT':  'profiles.csv',
207         'ANTIPRO_OUT':  'antiprofiles.csv',
208         'SbyClasseOut':  'SbyClasseOut.csv',
209         'chisqtable' :  'chisqtable.csv',
210         'ptable' :  'ptable.csv',
211         'ira': 'Analyse.ira',
212         'AFC2DL_OUT':  'AFC2DL.png',
213         'AFC2DSL_OUT':  'AFC2DSL.png',
214         'AFC2DEL_OUT':  'AFC2DEL.png',
215         'AFC2DCL_OUT':  'AFC2DCL.png',
216         'AFC2DCoul':  'AFC2DCoul.png',
217         'AFC2DCoulSup':  'AFC2DCoulSup.png',
218         'AFC2DCoulEt':  'AFC2DCoulEt.png',
219         'AFC2DCoulCl':  'AFC2DCoulCl.png',
220         'Rafc3d': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
221         'R3DCoul': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
222         'RESULT_CHD':  'resultats-chd.html',
223         'RESULT_AFC':  'resultats-afc.html',
224         'Et01':  'Et01.csv',
225         'Rchdquest':ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
226         'RTxtProfGraph':ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
227         'typelist': 'typelist.csv',
228         'concord':'concordancier.csv',
229         'bduceforme':'bduceforme.csv',
230         'uceuci': 'uceuci.csv',
231         'uciet': 'uciet.csv',
232         'ContTypeOut': 'tabletype.csv',
233         'liste_graph_afc' : 'liste_graph_afc.txt',
234         'liste_graph_chd' : 'liste_graph_chd.txt',
235         'afc_row':  'afc_row.csv',
236         'afc_col':  'afc_col.csv',
237         'afc_facteur':  'afc_facteur.csv',
238         'corpus_exp' : 'corpus_out.txt',
239         'segments_classes' :  'segments_classes.csv',
240         'prof_seg' :  'prof_segments.csv',
241         'antiprof_seg' :  'antiprof_segments.csv',
242         'prof_type' :  'profil_type.csv',
243         'antiprof_type' :  'antiprof_type.csv',
244         'type_cl' :  'type_cl.csv',
245         'db' : 'analyse.db',
246     }
247
248 def StatTxtPathOut(pathout):
249     d = {'tableafcm':ffr(os.path.join(pathout, 'tableafcm.csv')),
250           'tabletypem': ffr(os.path.join(pathout, 'tabletypem.csv')),
251           'tablespecf': ffr(os.path.join(pathout, 'tablespecf.csv')),
252           'tablespect': ffr(os.path.join(pathout, 'tablespect.csv')),
253           'eff_relatif_forme': ffr(os.path.join(pathout, 'eff_relatif_forme.csv')),
254           'eff_relatif_type': ffr(os.path.join(pathout, 'eff_relatif_type.csv')),
255           'afcf_row' : ffr(os.path.join(pathout, 'afcf_row.png')),
256           'afcf_col' : ffr(os.path.join(pathout, 'afcf_col.png')),
257           'afct_row' : ffr(os.path.join(pathout, 'afct_row.png')),
258           'afct_col' : ffr(os.path.join(pathout, 'afct_col.png')),
259           'RData' : ffr(os.path.join(pathout, 'RData.RData')),
260           'liste_graph_afcf' : os.path.join(pathout, 'liste_graph_afcf.txt'),
261           'liste_graph_afct' : os.path.join(pathout, 'liste_graph_afct.txt'),
262           'afcf_row_csv': ffr(os.path.join(pathout, 'afcf_row.csv')),
263           'afcf_col_csv': ffr(os.path.join(pathout, 'afcf_col.csv')),
264           'afcf_facteur_csv': ffr(os.path.join(pathout, 'afcf_facteur.csv')),
265           'afct_row_csv': ffr(os.path.join(pathout, 'afct_row.csv')),
266           'afct_col_csv': ffr(os.path.join(pathout, 'afct_col.csv')),
267           'afct_facteur_csv': ffr(os.path.join(pathout, 'afct_facteur.csv')),
268           'ira' : ffr(os.path.join(pathout, 'Analyse.ira')),
269           'db' : os.path.join(pathout, 'analyse.db'),
270           'zipf' : ffr(os.path.join(pathout, 'zipf.png')),
271     }
272     return d
273
274 #def construct_simipath(pathout):
275 #    d = {'mat01' : 'mat01.csv',
276 #          'matsimi' : 'matsimi.csv',
277 #          'eff' : 'eff.csv',
278 #          'RData' : 'RData.RData',
279 #          'liste_graph' : 'liste_graph.txt',
280 #          'ira' : 'Analyse.ira',
281 #          'film' : '',
282 #          'db' : 'analyse.db',
283 #        }
284
285 simipath = {'mat01' :  'mat01.csv',
286           'matsimi' : 'matsimi.csv',
287           'eff' : 'eff.csv',
288           'RData' : 'RData.RData',
289           'liste_graph' :'liste_graph.txt',
290           'ira' : 'Analyse.ira',
291           'film' : '',
292           'db' : 'analyse.db',
293           'corpus' : 'corpus.db',
294         }
295