Commit 820a6e27 authored by Kevin Deldycke's avatar Kevin Deldycke

Change some methods name for consistency;

Table reduction to its minimal bounds is now repaired;
Table normalization is now optionnal


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2982 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0ec8555a
...@@ -132,14 +132,14 @@ class OOoParser: ...@@ -132,14 +132,14 @@ class OOoParser:
security.declarePublic('getSpreadsheetsMapping') security.declarePublic('getSpreadsheetsMapping')
def getSpreadsheetsMapping(self, include_embedded=False, no_empty_lines=False): def getSpreadsheetsMapping(self, include_embedded=False, no_empty_lines=False, normalize=True):
""" """
Return a list of table-like spreadsheets (optionnaly included embedded ones) Return a list of table-like spreadsheets (optionnaly included embedded ones)
""" """
tables = {} tables = {}
tables = self.getPlainSpreadsheetsMapping(no_empty_lines) tables = self.getPlainSpreadsheetsMapping(no_empty_lines, normalize)
if include_embedded == True: if include_embedded == True:
embedded_tables = self.getEmbeddedSpreadsheetsMapping(no_empty_lines) embedded_tables = self.getEmbeddedSpreadsheetsMapping(no_empty_lines, normalize)
tables = self._getTableListUnion(tables, embedded_tables) tables = self._getTableListUnion(tables, embedded_tables)
return tables return tables
...@@ -157,13 +157,13 @@ class OOoParser: ...@@ -157,13 +157,13 @@ class OOoParser:
security.declarePublic('getPlainSpreadsheetsMapping') security.declarePublic('getPlainSpreadsheetsMapping')
def getPlainSpreadsheetsMapping(self, no_empty_lines=False): def getPlainSpreadsheetsMapping(self, no_empty_lines=False, normalize=True):
""" """
Return a list of plain spreadsheets from the document and transform them as table Return a list of plain spreadsheets from the document and transform them as table
""" """
tables = {} tables = {}
for spreadsheet in self.getPlainSpreadsheetsDom(): for spreadsheet in self.getPlainSpreadsheetsDom():
new_table = self.getSpreadsheetMapping(spreadsheet, no_empty_lines) new_table = self.getSpreadsheetMapping(spreadsheet, no_empty_lines, normalize)
if new_table != None: if new_table != None:
tables = self._getTableListUnion(tables, new_table) tables = self._getTableListUnion(tables, new_table)
return tables return tables
...@@ -190,20 +190,20 @@ class OOoParser: ...@@ -190,20 +190,20 @@ class OOoParser:
security.declarePublic('getEmbeddedSpreadsheetsMapping') security.declarePublic('getEmbeddedSpreadsheetsMapping')
def getEmbeddedSpreadsheetsMapping(self, no_empty_lines=False): def getEmbeddedSpreadsheetsMapping(self, no_empty_lines=False, normalize=True):
""" """
Return a list of embedded spreadsheets in the document as table Return a list of embedded spreadsheets in the document as table
""" """
tables = {} tables = {}
for spreadsheet in self.getEmbeddedSpreadsheetsDom(): for spreadsheet in self.getEmbeddedSpreadsheetsDom():
new_table = self.getSpreadsheetMapping(spreadsheet, no_empty_lines) new_table = self.getSpreadsheetMapping(spreadsheet, no_empty_lines, normalize)
if new_table != None: if new_table != None:
tables = self._getTableListUnion(tables, new_table) tables = self._getTableListUnion(tables, new_table)
return tables return tables
security.declarePublic('getSpreadsheetMapping') security.declarePublic('getSpreadsheetMapping')
def getSpreadsheetMapping(self, spreadsheet=None, no_empty_lines=False): def getSpreadsheetMapping(self, spreadsheet=None, no_empty_lines=False, normalize=True):
""" """
This method convert an OpenOffice spreadsheet to a simple table. This method convert an OpenOffice spreadsheet to a simple table.
This code is based on the oo2pt tool (http://cvs.sourceforge.net/viewcvs.py/collective/CMFReportTool/oo2pt). This code is based on the oo2pt tool (http://cvs.sourceforge.net/viewcvs.py/collective/CMFReportTool/oo2pt).
...@@ -271,7 +271,6 @@ class OOoParser: ...@@ -271,7 +271,6 @@ class OOoParser:
# Add the cell to the line # Add the cell to the line
table_line.append(cell_text) table_line.append(cell_text)
# Delete empty lines if needed # Delete empty lines if needed
if no_empty_lines: if no_empty_lines:
empty_cell = 0 empty_cell = 0
...@@ -285,25 +284,29 @@ class OOoParser: ...@@ -285,25 +284,29 @@ class OOoParser:
if table_line != None: if table_line != None:
table.append(table_line) table.append(table_line)
# Reduce the table to the minimum # Reduce the table to the minimum
text_min_bounds = self._getTableMinimalBounds(table) new_table = self._getReducedTable(table)
table = self._setTableBounds( table
, width = text_min_bounds['width'] # Get a homogenized table
, height = text_min_bounds['height'] if normalize:
table_size = self._getTableSizeDict(new_table)
new_table = self._getNormalizedBoundsTable( table = new_table
, width = table_size['width']
, height = table_size['height']
) )
return {table_name: table} return {table_name: new_table}
security.declarePrivate('_getTableMinimalBounds') security.declarePrivate('_getReducedTable')
def _getTableMinimalBounds(self, table): def _getReducedTable(self, table):
""" """
Calcul the minimum size of a table Reduce the table to its minimum size
""" """
empty_lines = 0 empty_lines = 0
no_more_empty_lines = 0 no_more_empty_lines = 0
# Eliminate all empty cells at the ends of lines and columns # Eliminate all empty cells at the ends of lines and columns
# Browse the table starting from the bottom for easy empty lines count
for line in range(len(table)-1, -1, -1): for line in range(len(table)-1, -1, -1):
empty_cells = 0 empty_cells = 0
line_content = table[line] line_content = table[line]
...@@ -312,6 +315,7 @@ class OOoParser: ...@@ -312,6 +315,7 @@ class OOoParser:
empty_cells += 1 empty_cells += 1
else: else:
break break
if (not no_more_empty_lines) and (empty_cells == len(line_content)): if (not no_more_empty_lines) and (empty_cells == len(line_content)):
empty_lines += 1 empty_lines += 1
else: else:
...@@ -319,25 +323,31 @@ class OOoParser: ...@@ -319,25 +323,31 @@ class OOoParser:
table[line] = line_content[:line_size] table[line] = line_content[:line_size]
no_more_empty_lines = 1 no_more_empty_lines = 1
texts_size = len(table) - empty_lines table_height = len(table) - empty_lines
table = table[:texts_size]
return table[:table_height]
# Determine minimum bounds security.declarePrivate('_getTableSizeDict')
def _getTableSizeDict(self, table):
"""
Get table dimension as dictionnary contain both height and width
"""
max_cols = 0 max_cols = 0
for line in range(len(table)): for line_index in range(len(table)):
line_content = table[line] line = table[line_index]
if len(line_content) > max_cols: if len(line) > max_cols:
max_cols = len(line_content) max_cols = len(line)
return { 'width' : max_cols return { 'width' : max_cols
, 'height': len(table) , 'height': len(table)
} }
security.declarePrivate('_setTableBounds') security.declarePrivate('_getNormalizedBoundsTable')
def _setTableBounds(self, table, width=0, height=0): def _getNormalizedBoundsTable(self, table, width=0, height=0):
""" """
Enlarge a text table to given bounds Add necessary cells and lines to obtain given bounds
""" """
while height > len(table): while height > len(table):
table.append([]) table.append([])
...@@ -364,5 +374,6 @@ class OOoParser: ...@@ -364,5 +374,6 @@ class OOoParser:
return list1 return list1
InitializeClass(OOoParser) InitializeClass(OOoParser)
allow_class(OOoParser) allow_class(OOoParser)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment