29a0d6375fc11607b8eccc400a51245bdf26be2f
[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, construct_simipath
8 from layout import OpenCHDS, dolexlayout, StatLayout, WordCloudLayout, OpenCorpus, SimiLayout
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 import logging
16
17 log = logging.getLogger('iramuteq.openanalyse')
18
19 class OpenAnalyse():
20     def __init__(self, parent, parametres, Alceste=True, simifromprof = False):
21         log.info('OpenAnalyse')
22         self.parent = parent
23         if isinstance(parametres, dict) :
24             self.conf = DoConf(parametres['ira']).getoptions()
25             self.path = parametres['ira']
26         else :
27             self.conf = DoConf(parametres).getoptions()
28             self.path = parametres
29             self.conf = self.redopath(self.conf, parametres)
30         
31         if self.conf['type'] == 'corpus' :
32             corpus = self.opencorpus()
33         elif self.conf['corpus'] in self.parent.history.corpus :
34             if self.conf['uuid'] in self.parent.history.analyses :
35                 intree  = True
36             else :
37                 intree = False
38             corpus = self.openanalyse()
39             if self.conf.get('lem',1) :
40                corpus.make_lems(True)
41             else :
42                corpus.make_lems(False)
43             if not intree :
44                 self.parent.tree.AddAnalyse(self.conf, bold = True)
45             else :
46                 self.parent.tree.GiveFocus(uuid = self.conf['uuid'], bold = True)
47             self.doopen(corpus)
48         else :
49             corpus = None
50         self.parent.history.addtab(self.conf)
51     
52     def redopath(self, conf, path) :
53         conf['ira'] = os.path.realpath(path)
54         conf['pathout'] = os.path.dirname(os.path.realpath(path))
55         DoConf(conf['ira']).makeoptions([conf['type']], [conf])
56         return conf
57     
58     def opencorpus(self) :
59         log.info('open corpus')
60         if self.conf['uuid'] not in self.parent.history.corpus :
61             self.parent.history.add(self.conf) 
62             log.info('add to history')
63             self.parent.tree.OnItemAppend(self.conf)
64         if self.conf['uuid'] in self.parent.history.openedcorpus :
65             log.info('corpus is already opened 1')
66             self.doopen(self.parent.history.openedcorpus[self.conf['uuid']])
67         else :
68             corpus = Corpus(self, parametres = self.conf, read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['uuid']]]['ira'])
69             self.parent.history.openedcorpus[self.conf['uuid']] = corpus
70             self.opencorpus_analyses()
71             self.doopen(corpus)
72
73     def opencorpus_analyses(self) :
74         log.info('open analysis')
75         basepath = self.conf['pathout']
76         analyses = []
77         for root, subfolders, files in os.walk(basepath) :
78             for folder in subfolders :
79                 if os.path.exists(os.path.join(folder, 'Analyse.ira')) :
80                     analyse_conf = DoConf(os.path.join(folder, 'Analyse.ira')).getoptions()
81                     analyse_conf = self.redopath(analyse_conf, os.path.join(folder, 'Analyse.ira'))
82                     if analyse_conf['corpus'] == self.conf['uuid'] :
83                         analyses.append(analyse_conf)
84         if len(analyses) :
85             self.parent.history.addmultiple(analyses)
86         for analyse in analyses :
87             self.parent.tree.AddAnalyse(analyse, bold = False)
88
89     def openanalyse(self) :
90         if self.conf['corpus'] in self.parent.history.openedcorpus :
91             log.info('corpus is already opened 2')
92             corpus = self.parent.history.openedcorpus[self.conf['corpus']]
93         else :
94             if os.path.exists(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']) :
95                 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'])
96                 self.parent.history.openedcorpus[self.conf['corpus']] = corpus
97         self.parent.history.add(self.conf)
98         return corpus
99
100     def doopen(self, corpus) :
101         if self.conf['type'] == 'corpus' :
102             self.parent.ShowMenu(_("Text analysis"))
103             OpenCorpus(self.parent, self.conf) 
104         elif self.conf['type'] == 'stat' :
105             self.parent.ShowMenu(_("Text analysis"))
106             StatLayout(self.parent, corpus, self.conf)
107         elif self.conf['type'] == 'spec' :
108             self.parent.ShowMenu(_("Text analysis"))
109             dolexlayout(self.parent, corpus, self.conf)
110         elif self.conf['type'] == 'alceste' :
111             self.parent.ShowMenu(_("Text analysis"))
112             OpenCHDS(self.parent,  corpus, self.conf, Alceste = True)
113         elif self.conf['type'] == 'simitxt' or self.conf['type'] == 'clustersimitxt' :
114             self.parent.ShowMenu(_("Text analysis"))
115             SimiLayout(self.parent, corpus, self.conf)
116         elif self.conf['type'] == 'wordcloud' :
117             self.parent.ShowMenu(_("Text analysis"))
118             WordCloudLayout(self.parent, corpus, self.conf)
119