Merge branch 'master' of 192.168.0.2:/home/pouns/iramuteqNG
[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         'history' : os.path.join(ConfigPath, 'history.db'),
121     }
122     return DictConfigPath
123
124 def ConstructGlobalPath(AppliPath):
125     ConfigPath = os.path.join(AppliPath, 'configuration')
126     DictConfigPath = {
127             'global': os.path.join(ConfigPath, 'global.cfg'),
128             }
129     return DictConfigPath
130
131 def ConstructDicoPath(AppliPath):
132     BasePath = os.path.join(AppliPath, 'dictionnaires')
133     DictPath = {
134         'french': os.path.join(BasePath, 'lexique_fr.txt'),
135         'french_exp': os.path.join(BasePath, 'expression_fr.txt'),
136         'english': os.path.join(BasePath, 'lexique_en.txt'),
137         'english_exp': os.path.join(BasePath, 'expression_en.txt'),
138         'german' :  os.path.join(BasePath, 'lexique_de.txt'),
139         'german_exp' : os.path.join(BasePath, 'expression_de.txt'),
140         'italian' : os.path.join(BasePath, 'lexique_it.txt'),
141         'italian_exp' : os.path.join(BasePath, 'expression_it.txt'),
142     }
143     return DictPath
144
145 def ConstructAfcmPath(FilePath):
146     DictAfcmPath = {
147         'Levels': ffr(os.path.join(FilePath, 'afcm-levels.csv')),
148         'nd': ffr(os.path.join(FilePath, 'afcm-nd.csv')),
149         'FileActTemp': ffr(os.path.join(FilePath, 'fileActTamp.csv')),
150         'FileEtTemp': ffr(os.path.join(FilePath, 'FileEtTemp')),
151         'resultat': os.path.join(FilePath, 'Resultats-afcm.html'),
152         'Rafc3d': ffr(tempfile.mkstemp(prefix='iramuteq')[1])
153     }
154     return DictAfcmPath
155
156 def ConstructAfcUciPath(filepath):
157     DictAfcUciPath = {
158         'TableCont': ffr(os.path.join(filepath, 'TableCont.csv')),
159         'TableSup': ffr(os.path.join(filepath, 'TableSup.csv')),
160         'TableEt': ffr(os.path.join(filepath, 'TableEt.csv')),
161         'AfcColAct': ffr(os.path.join(filepath, 'AfcColAct.png')),
162         'AfcColSup': ffr(os.path.join(filepath, 'AfcColSup.png')),
163         'AfcColEt': ffr(os.path.join(filepath, 'AfcColEt.png')),
164         'afcdiv4': ffr(os.path.join(filepath, 'afcdiv4_')),
165         'AfcRow': ffr(os.path.join(filepath, 'AfcRow.png')),
166         'ListAct': ffr(os.path.join(filepath, 'ListAct.csv')),
167         'ListSup': ffr(os.path.join(filepath, 'ListSup.csv')),
168         'GraphAfcTot': os.path.join(filepath, 'GraphAfcTot.html'),
169         'Rafcuci': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
170         'afc_row': ffr(os.path.join(filepath, 'afc_row.csv')),
171         'afc_col': ffr(os.path.join(filepath, 'afc_col.csv')),
172         'ira' : os.path.join(filepath, 'Analyse.ira'),
173     }
174     return DictAfcUciPath
175
176 ChdTxtPathOut = {'TableUc1': 'TableUc1.csv',
177         'TableUc2': 'TableUc2.csv',
178         'listeuce1':  'listeUCE1.csv',
179         'listeuce2':  'listeUCE2.csv',
180         'DicoMots':  'DicoMots.csv',
181         'DicoLem':  'DicoLem.csv',
182         'profile':  'profiles.csv',
183         'antiprofile':  'antiprofiles.csv',
184         'afc':  'AFC.csv',
185         'rapport':  'RAPPORT.txt',
186         'pre_rapport' : 'info.txt',
187         'uce':  'uce.csv',
188         'Rchdtxt': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
189         'arbre1':  'arbre_1.png',
190         'arbre2':  'arbre_2.png',
191         'dendro1':  'dendro1.png',
192         'dendro2':  'dendro2.png',
193         'Rdendro':  'dendrogramme.RData',
194         'Contout':  'classe_mod.csv',
195         'RData':  'RData.RData',
196         'ContSupOut':  'tablesup.csv',
197         'ContEtOut':  'tableet.csv',
198         'PROFILE_OUT':  'profiles.csv',
199         'ANTIPRO_OUT':  'antiprofiles.csv',
200         'SbyClasseOut':  'SbyClasseOut.csv',
201         'chisqtable' :  'chisqtable.csv',
202         'ptable' :  'ptable.csv',
203         'ira': 'Analyse.ira',
204         'AFC2DL_OUT':  'AFC2DL.png',
205         'AFC2DSL_OUT':  'AFC2DSL.png',
206         'AFC2DEL_OUT':  'AFC2DEL.png',
207         'AFC2DCL_OUT':  'AFC2DCL.png',
208         'AFC2DCoul':  'AFC2DCoul.png',
209         'AFC2DCoulSup':  'AFC2DCoulSup.png',
210         'AFC2DCoulEt':  'AFC2DCoulEt.png',
211         'AFC2DCoulCl':  'AFC2DCoulCl.png',
212         'Rafc3d': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
213         'R3DCoul': ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
214         'RESULT_CHD':  'resultats-chd.html',
215         'RESULT_AFC':  'resultats-afc.html',
216         'Act01':  'Act01.csv',
217         'Et01':  'Et01.csv',
218         'Rchdquest':ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
219         'RTxtProfGraph':ffr(tempfile.mkstemp(prefix='iramuteq')[1]),
220         'typelist': 'typelist.csv',
221         'concord':'concordancier.csv',
222         'bduceforme':'bduceforme.csv',
223         'uceuci': 'uceuci.csv',
224         'uciet': 'uciet.csv',
225         'ContTypeOut': 'tabletype.csv',
226         'liste_graph_afc' : 'liste_graph_afc.txt',
227         'liste_graph_chd' : 'liste_graph_chd.txt',
228         'afc_row':  'afc_row.csv',
229         'afc_col':  'afc_col.csv',
230         'afc_facteur':  'afc_facteur.csv',
231         'corpus_exp' : 'corpus_out.txt',
232         'segments_classes' :  'segments_classes.csv',
233         'prof_seg' :  'prof_segments.csv',
234         'antiprof_seg' :  'antiprof_segments.csv',
235         'prof_type' :  'profil_type.csv',
236         'antiprof_type' :  'antiprof_type.csv',
237         'type_cl' :  'type_cl.csv',
238         'db' : 'analyse.db',
239     }
240
241 def StatTxtPathOut(pathout):
242     d = {'tableafcm':ffr(os.path.join(pathout, 'tableafcm.csv')),
243           'tabletypem': ffr(os.path.join(pathout, 'tabletypem.csv')),
244           'tablespecf': ffr(os.path.join(pathout, 'tablespecf.csv')),
245           'tablespect': ffr(os.path.join(pathout, 'tablespect.csv')),
246           'eff_relatif_forme': ffr(os.path.join(pathout, 'eff_relatif_forme.csv')),
247           'eff_relatif_type': ffr(os.path.join(pathout, 'eff_relatif_type.csv')),
248           'afcf_row' : ffr(os.path.join(pathout, 'afcf_row.png')),
249           'afcf_col' : ffr(os.path.join(pathout, 'afcf_col.png')),
250           'afct_row' : ffr(os.path.join(pathout, 'afct_row.png')),
251           'afct_col' : ffr(os.path.join(pathout, 'afct_col.png')),
252           'RData' : ffr(os.path.join(pathout, 'RData.RData')),
253           'liste_graph_afcf' : os.path.join(pathout, 'liste_graph_afcf.txt'),
254           'liste_graph_afct' : os.path.join(pathout, 'liste_graph_afct.txt'),
255           'afcf_row_csv': ffr(os.path.join(pathout, 'afcf_row.csv')),
256           'afcf_col_csv': ffr(os.path.join(pathout, 'afcf_col.csv')),
257           'afcf_facteur_csv': ffr(os.path.join(pathout, 'afcf_facteur.csv')),
258           'afct_row_csv': ffr(os.path.join(pathout, 'afct_row.csv')),
259           'afct_col_csv': ffr(os.path.join(pathout, 'afct_col.csv')),
260           'afct_facteur_csv': ffr(os.path.join(pathout, 'afct_facteur.csv')),
261           'ira' : ffr(os.path.join(pathout, 'Analyse.ira')),
262           'db' : os.path.join(pathout, 'analyse.db'),
263           'zipf' : ffr(os.path.join(pathout, 'zipf.png')),
264     }
265     return d
266
267 def construct_simipath(pathout):
268     d = {'mat01' : ffr(os.path.join(pathout, 'mat01.csv')),
269           'matsimi' : ffr(os.path.join(pathout, 'matsimi.csv')),
270           'eff' : ffr(os.path.join(pathout, 'eff.csv')),
271           'RData' : ffr(os.path.join(pathout, 'RData.RData')),
272           'liste_graph' : os.path.join(pathout, 'liste_graph.txt'),
273           'ira' : os.path.join(pathout, 'Analyse.ira'),
274           'film' : ffr(pathout),
275           'db' : os.path.join(pathout, 'analyse.db'),
276           'corpus' : os.path.join(pathout, 'corpus.db'),
277         }
278     return d