from mac
[iramuteq] / openanalyse.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2012, Pierre Ratinaud
5 #Lisense: GNU/GPL
6
7 from chemins import ChdTxtPathOut, StatTxtPathOut, PathOut
8 from layout import OpenCHDS, dolexlayout, StatLayout, WordCloudLayout, OpenCorpus, SimiLayout, SimiMatLayout
9 from corpus import Corpus, copycorpus
10 from tableau import Tableau
11 import os
12 import shelve
13 #from tabsimi import DoSimi
14 from functions import BugReport, DoConf, progressbar
15 from tableau import Tableau
16 import logging
17
18 log = logging.getLogger('iramuteq.openanalyse')
19
20 class OpenAnalyse():
21     def __init__(self, parent, parametres, Alceste=True, simifromprof = False):
22         log.info('OpenAnalyse')
23         self.parent = parent
24         if isinstance(parametres, dict) :
25             self.conf = DoConf(parametres['ira']).getoptions()
26             self.path = parametres['ira']
27         else :
28             self.conf = DoConf(parametres).getoptions()
29             self.path = parametres
30             self.conf = self.redopath(self.conf, parametres)
31         
32         if self.conf['type'] == 'corpus' :
33             corpus = self.opencorpus()
34         elif self.conf.get('corpus', False) in self.parent.history.corpus :
35             if self.conf['uuid'] in self.parent.history.analyses :
36                 intree  = True
37             else :
38                 intree = False
39             corpus = self.openanalyse()
40             if self.conf.get('lem',1) :
41                corpus.make_lems(True)
42             else :
43                corpus.make_lems(False)
44             if not intree :
45                 self.parent.tree.AddAnalyse(self.conf, bold = True)
46             else :
47                 self.parent.tree.GiveFocus(uuid = self.conf['uuid'], bold = True)
48             self.doopen(corpus)
49         else :
50             corpus = None
51             if isinstance(parametres, dict) :
52                 tableau = Tableau(parent, parametres['ira'])
53             else :
54                 tableau = Tableau(parent, parametres)
55             tableau.parametres = self.conf 
56             tableau.dictpathout = PathOut(filename = tableau.parametres['filename'], dirout = self.conf['pathout'], analyse_type = self.conf['type'])
57             tableau.dictpathout.basefiles(ChdTxtPathOut)
58             tableau.read_tableau(tableau.dictpathout['db'])
59             if self.parent.tree.IsInTree(uuid = self.conf['uuid']) :
60                 self.parent.tree.GiveFocus(uuid = self.conf['uuid'], bold = True)
61             else :
62                 self.parent.tree.AddAnalyse(self.conf, bold = True)
63             self.doopen(tableau)
64         self.parent.history.addtab(self.conf)
65     
66     def redopath(self, conf, path) :
67         conf['ira'] = os.path.realpath(path)
68         conf['pathout'] = os.path.dirname(os.path.realpath(path))
69         DoConf(conf['ira']).makeoptions([conf['type']], [conf])
70         return conf
71     
72     def opencorpus(self) :
73         log.info('open corpus')
74         if self.conf['uuid'] not in self.parent.history.corpus :
75             self.parent.history.add(self.conf) 
76             log.info('add corpus to history')
77             self.parent.tree.OnItemAppend(self.conf)
78         if self.conf['uuid'] in self.parent.history.openedcorpus :
79             log.info('corpus is already opened')
80             self.doopen(self.parent.history.openedcorpus[self.conf['uuid']])
81         else :
82             #dial = progressbar(2)
83             #dial.Update(1, 'Ouverture du corpus')
84             corpus = Corpus(self, parametres = self.conf, read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['uuid']]]['ira'])
85             #dial.Update(2, 'Fini')
86             #dial.Destroy()
87             self.parent.history.openedcorpus[self.conf['uuid']] = corpus
88             self.opencorpus_analyses()
89             self.doopen(corpus)
90
91     def opencorpus_analyses(self) :
92         log.info('open analysis')
93         basepath = self.conf['pathout']
94         analyses = []
95         for root, subfolders, files in os.walk(basepath) :
96             for folder in subfolders :
97                 if os.path.exists(os.path.join(folder, 'Analyse.ira')) :
98                     analyse_conf = DoConf(os.path.join(folder, 'Analyse.ira')).getoptions()
99                     analyse_conf = self.redopath(analyse_conf, os.path.join(folder, 'Analyse.ira'))
100                     if analyse_conf['corpus'] == self.conf['uuid'] :
101                         analyses.append(analyse_conf)
102         if len(analyses) :
103             self.parent.history.addmultiple(analyses)
104         for analyse in analyses :
105             self.parent.tree.AddAnalyse(analyse, bold = False)
106
107     def openanalyse(self) :
108         if self.conf['corpus'] in self.parent.history.openedcorpus :
109             log.info('corpus is already opened')
110             corpus = copycorpus(self.parent.history.openedcorpus[self.conf['corpus']])
111         else :
112             if os.path.exists(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']) :
113                 corpus = Corpus(self, parametres = DoConf(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']).getoptions('corpus'), read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira'])
114                 self.parent.history.openedcorpus[self.conf['corpus']] = corpus
115         self.parent.history.add(self.conf)
116         return corpus
117
118     def doopen(self, corpus) :
119         if self.conf['type'] == 'corpus' :
120             self.parent.ShowMenu(_("Text analysis"))
121             OpenCorpus(self.parent, self.conf) 
122         elif self.conf['type'] == 'stat' :
123             self.parent.ShowMenu(_("Text analysis"))
124             StatLayout(self.parent, corpus, self.conf)
125         elif self.conf['type'] == 'spec' :
126             self.parent.ShowMenu(_("Text analysis"))
127             dolexlayout(self.parent, corpus, self.conf)
128         elif self.conf['type'] == 'alceste' :
129             self.parent.ShowMenu(_("Text analysis"))
130             OpenCHDS(self.parent,  corpus, self.conf, Alceste = True)
131         elif self.conf['type'] == 'simitxt' or self.conf['type'] == 'clustersimitxt' :
132             self.parent.ShowMenu(_("Text analysis"))
133             SimiLayout(self.parent, corpus, self.conf)
134         elif self.conf['type'] == 'wordcloud' :
135             self.parent.ShowMenu(_("Text analysis"))
136             WordCloudLayout(self.parent, corpus, self.conf)
137         elif self.conf['type'] == 'gnepamatrix' :
138             #self.parent.ShowMenu(_("Spreadsheet analysis"))
139             OpenCHDS(self.parent,  corpus, self.conf, Alceste = False)
140         elif self.conf['type'] == 'simimatrix' :
141             #self.parent.ShowMenu(_("Spreadsheet analysis"))
142             SimiMatLayout(self.parent, corpus, self.conf)
143