import xlrd
import ooolib
import os
-import tempfile
+from copy import copy
import re
import htmlentitydefs
import shelve
def copymatrix(tableau):
log.info('copy matrix')
copymat = Tableau(tableau.parent, parametres = tableau.parametres)
- copymat.linecontent = tableau.linecontent
- copymat.csvtable = tableau.csvtable
- copymat.pathout = tableau.pathout
- copymat.colnames = tableau.colnames
- copymat.rownb = tableau.rownb
- copymat.colnb = tableau.colnb
+ copymat.linecontent = copy(tableau.linecontent)
+ copymat.csvtable = copy(tableau.csvtable)
+ copymat.pathout = copy(tableau.pathout)
+ copymat.colnames = copy(tableau.colnames)
+ copymat.rownb = copy(tableau.rownb)
+ copymat.colnb = copy(tableau.colnb)
if copymat.csvtable is None :
copymat.open()
return copymat
def __init__(self, parent, filename = '', filetype = 'csv', encodage = 'utf-8', parametres = None) :
self.parent = parent
if parametres is None :
- self.parametres = DoConf(os.path.join(self.parent.UserConfigPath,'matrix.cfg')).getoptions('matrix')
+ self.parametres = DoConf(self.parent.ConfigPath['matrix']).getoptions('matrix')
self.parametres['pathout'] = PathOut(filename, 'matrix').mkdirout()
self.parametres['originalpath'] = filename
self.parametres['filetype'] = filetype
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)
+
+ def make_content_simple(self):
+ 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)
def read_xls(self) :
#FIXME : encodage
#datafile = xlrd.open_workbook(self.parametre['filename'], encoding_override="azerazerazer")
datafile = xlrd.open_workbook(self.parametres['originalpath'])
datatable = datafile.sheet_by_index(self.parametres['sheetnb']-1)
- self.linecontent = [[str(datatable.cell_value(rowx = i, colx = j)).replace(u'"','').replace(u';','').replace(u'\n',' ').strip() for j in range(datatable.ncols)] for i in range(datatable.nrows)]
+ self.linecontent = [[str(datatable.cell_value(rowx = i, colx = j)).replace(u'"','').replace(u';',' ').replace(u'\n',' ').replace('\r', ' ').replace('\t', ' ').strip() for j in range(datatable.ncols)] for i in range(datatable.nrows)]
def read_ods(self) :
doc = ooolib.Calc(opendoc=self.parametres['originalpath'])
for col in range(1, cols + 1):
data = doc.get_cell_value(col, row)
if data is not None :
- ligne.append(unescape(data[1].replace(u'"','').replace(u';','').replace(u'\n', ' ').strip()))
+ ligne.append(unescape(data[1].replace(u'"','').replace(u';',' ').replace(u'\n', ' ').replace('\t', ' ').strip()))
else :
ligne.append('')
self.linecontent.append(ligne)
with codecs.open(self.parametres['originalpath'], 'r', self.parametres['encodage']) as f :
content = f.read()
self.linecontent = [line.split(self.parametres['colsep']) for line in content.splitlines()]
- self.linecontent = [[val.replace(u'"','').strip() for val in line] for line in self.linecontent]
+ self.linecontent = [[val.replace(u'"','').replace(u';',' ').replace('\t', ' ').strip() for val in line] for line in self.linecontent]
def write_csvfile(self) :
with open(self.parametres['csvfile'], 'w') as f :
self.csvtable = [line.split('\t') for line in f.read().splitlines()]
self.linecontent = [line[1:] for line in self.csvtable]
self.linecontent.pop(0)
+
+ def extractfrommod(self, col, val):
+ return ([''] + self.colnames) + [line for line in self.csvtable[1:] if line[col + 1] == val]
+
+ def splitfromvar(self, col):
+ newtabs = {}
+ for line in self.csvtable[1:] :
+ mod = line[col+1]
+ if mod in newtabs :
+ newtabs[mod].append(line)
+ else :
+ newtabs[mod] = [line]
+ for mod in newtabs :
+ newtabs[mod].insert(0, [''] + self.colnames)
+ return newtabs
def check_rownames(self) :
if len(self.rownames) == len(list(set(self.rownames))) :
dc = dict(zip(listcol, listcol))
selcol = [[val for i, val in enumerate(row) if i in dc] for row in self.linecontent]
return selcol
+
+ def countmultiple(self, liscol):
+ return self.make_dico(self.select_col(liscol))
def getactlistfromselection(self, listact) :
selcol = self.select_col(listact)