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