multisplit
[iramuteq] / tableau.py
index 5760ad7..204df9c 100644 (file)
@@ -22,7 +22,8 @@ import os
 from copy import copy
 import re
 import html.entities
-import shelve
+#import shelve
+import json
 from uuid import uuid4
 import logging
 
@@ -95,7 +96,7 @@ class Tableau() :
             self.parametres['type'] = 'matrix'
             self.parametres['matrix_name'] = os.path.basename(filename)
             self.parametres['uuid'] = str(uuid4())
-            self.parametres['shelves'] = os.path.join(self.parametres['pathout'], 'shelve.db')
+            self.parametres['shelves'] = os.path.join(self.parametres['pathout'], 'shelve')
             self.parametres['ira'] = os.path.join(self.parametres['pathout'], 'Matrix.ira')
         else :
             self.parametres = parametres
@@ -118,10 +119,8 @@ class Tableau() :
         #self.parametres = self.parametre
 
     def read_tableau(self, fileout) :
-        d=shelve.open(fileout)
-        #self.parametres = d['parametres']
-        #if 'syscoding' not in self.parametres :
-        #    self.parametres['syscoding'] = sys.getdefaultencoding()
+        with open(fileout, 'r', encoding='utf8') as f :
+            d = json.load(f)
         self.actives = d['actives']
         self.sups = d['sups']
         self.classes = d['classes']
@@ -136,7 +135,6 @@ class Tableau() :
             self.lchi = d['lchi']
         if 'content' in d :
             self.content = d['content']
-        d.close()
 
     def open(self):
         print('open matrix')
@@ -146,7 +144,7 @@ class Tableau() :
         self.colnb = len(self.linecontent[0])
 
     def save_tableau(self, fileout) :
-        d=shelve.open(fileout)
+        d = {}
         d['parametres'] = self.parametres
         d['actives'] = self.actives
         d['sups'] = self.sups
@@ -161,7 +159,8 @@ class Tableau() :
         if 'lchi' in dir(self) :
             d['lchi'] = self.lchi
         d['content'] = self.content
-        d.close()
+        with open(fileout, 'w', encoding='utf8') as f :
+            json.dump(d, f)
 
     def make_content(self) :
         self.pathout.createdir(self.parametres['pathout'])
@@ -173,7 +172,6 @@ class Tableau() :
             self.read_ods()
         self.parametres['csvfile'] = os.path.join(self.parametres['pathout'], 'csvfile.csv')
         self.make_tmpfile()
-        print(self.parametres)
         DoConf().makeoptions(['matrix'],[self.parametres], self.parametres['ira'])
         self.parent.history.addMatrix(self.parametres)
 
@@ -181,7 +179,7 @@ class Tableau() :
         self.parametres['csvfile'] = os.path.join(self.parametres['pathout'], 'csvfile.csv')
         self.make_tmpfile()
         DoConf().makeoptions(['matrix'],[self.parametres], self.parametres['ira'])
-        self.parent.history.addMatrix(self.parametres)        
+        self.parent.history.addMatrix(self.parametres)
 
     def read_xls(self) :
         #FIXME : encodage
@@ -207,7 +205,7 @@ class Tableau() :
 
     def read_csv(self) :
         with codecs.open(self.parametres['originalpath'], 'r', self.parametres['encodage']) as f :
-            content = f.read() 
+            content = f.read()
         self.linecontent = [line.split(self.parametres['colsep']) for line in content.splitlines()]
         self.linecontent = [[val.replace('"','').replace(';',' ').replace('\t', ' ').strip() for val in line] for line in self.linecontent]
 
@@ -234,7 +232,7 @@ class Tableau() :
         else :
             self.rownames = [repr(i) for i in range(self.rownb)]
             self.idname = 'identifiant'
-        self.csvtable = [[self.idname] + self.colnames] + [[self.rownames[i]] + self.linecontent[i] for i in range(len(self.rownames))] 
+        self.csvtable = [[self.idname] + self.colnames] + [[self.rownames[i]] + self.linecontent[i] for i in range(len(self.rownames))]
         self.write_csvfile()
 
     def read_csvfile(self):
@@ -287,10 +285,9 @@ class Tableau() :
     def getactlistfromselection(self, listact) :
         selcol = self.select_col(listact)
         self.actives = self.make_dico(selcol)
-        return [[val, self.actives[val][0]] for val in self.actives]       
+        return [[val, self.actives[val][0]] for val in self.actives]
 
     def make_listactives(self) :
-        print(self.actives, self.parametres['mineff'])
         self.listactives = [val for val in self.actives if val != 'NA' and self.actives[val][0] >= self.parametres['mineff']]
 
     def write01(self, fileout, dico, linecontent) :
@@ -300,7 +297,7 @@ class Tableau() :
         for i, forme in enumerate(self.listactives) :
             for line in dico[forme][1] :
                 out[line][i] = '1'
-        #out = [[self.rownames[i]] + out[i] for i in range(len(linecontent))] 
+        #out = [[self.rownames[i]] + out[i] for i in range(len(linecontent))]
         #out.insert(0,[self.idname] + self.listactives)
         out.insert(0, self.listactives)
         with open(fileout, 'w', encoding='utf8') as f :
@@ -321,7 +318,7 @@ class Tableau() :
                     if forme[0] == '*':
                         UpdateDico(self.sups, forme, i)
                     else:
-                        UpdateDico(self.actives, forme, i)        
+                        UpdateDico(self.actives, forme, i)
         self.listactives = [val for val in self.actives if self.actives[val][0] >= self.parametres['mineff']]
         table = [['0' for i in range(len(self.listactives))] for j in range(self.rownb)]
         for i, val in enumerate(self.listactives) :
@@ -378,9 +375,9 @@ class Tableau() :
                         if sup in self.linecontent[uce]:
                             line[i + 1] += 1
             tablecontet.append([line[0]] + [repr(don) for don in line if type(don) == type(1)])
-            
+
         self.printtable(self.pathout['ContEtOut'], tablecontet)
-        self.printtable(self.pathout['Contout'], tablecont)        
+        self.printtable(self.pathout['Contout'], tablecont)
 
     def get_colnames(self) :
         return self.colnames[:]