...+++...
[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
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 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 1')
80             self.doopen(self.parent.history.openedcorpus[self.conf['uuid']])
81         else :
82             corpus = Corpus(self, parametres = self.conf, read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['uuid']]]['ira'])
83             self.parent.history.openedcorpus[self.conf['uuid']] = corpus
84             self.opencorpus_analyses()
85             self.doopen(corpus)
86
87     def opencorpus_analyses(self) :
88         log.info('open analysis')
89         basepath = self.conf['pathout']
90         analyses = []
91         for root, subfolders, files in os.walk(basepath) :
92             for folder in subfolders :
93                 if os.path.exists(os.path.join(folder, 'Analyse.ira')) :
94                     analyse_conf = DoConf(os.path.join(folder, 'Analyse.ira')).getoptions()
95                     analyse_conf = self.redopath(analyse_conf, os.path.join(folder, 'Analyse.ira'))
96                     if analyse_conf['corpus'] == self.conf['uuid'] :
97                         analyses.append(analyse_conf)
98         if len(analyses) :
99             self.parent.history.addmultiple(analyses)
100         for analyse in analyses :
101             self.parent.tree.AddAnalyse(analyse, bold = False)
102
103     def openanalyse(self) :
104         if self.conf['corpus'] in self.parent.history.openedcorpus :
105             log.info('corpus is already opened 2')
106             corpus = copycorpus(self.parent.history.openedcorpus[self.conf['corpus']])
107         else :
108             if os.path.exists(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']) :
109                 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'])
110                 self.parent.history.openedcorpus[self.conf['corpus']] = corpus
111         self.parent.history.add(self.conf)
112         return corpus
113
114     def doopen(self, corpus) :
115         if self.conf['type'] == 'corpus' :
116             self.parent.ShowMenu(_("Text analysis"))
117             OpenCorpus(self.parent, self.conf) 
118         elif self.conf['type'] == 'stat' :
119             self.parent.ShowMenu(_("Text analysis"))
120             StatLayout(self.parent, corpus, self.conf)
121         elif self.conf['type'] == 'spec' :
122             self.parent.ShowMenu(_("Text analysis"))
123             dolexlayout(self.parent, corpus, self.conf)
124         elif self.conf['type'] == 'alceste' :
125             self.parent.ShowMenu(_("Text analysis"))
126             OpenCHDS(self.parent,  corpus, self.conf, Alceste = True)
127         elif self.conf['type'] == 'simitxt' or self.conf['type'] == 'clustersimitxt' :
128             self.parent.ShowMenu(_("Text analysis"))
129             SimiLayout(self.parent, corpus, self.conf)
130         elif self.conf['type'] == 'wordcloud' :
131             self.parent.ShowMenu(_("Text analysis"))
132             WordCloudLayout(self.parent, corpus, self.conf)
133         elif self.conf['type'] == 'gnepamatrix' :
134             #self.parent.ShowMenu(_("Spreadsheet analysis"))
135             OpenCHDS(self.parent,  corpus, self.conf, Alceste = False)
136         elif self.conf['type'] == 'simimatrix' :
137             #self.parent.ShowMenu(_("Spreadsheet analysis"))
138             SimiMatLayout(self.parent, corpus, self.conf)
139