Commit 12bb4c76 authored by Hugo H. Maia Vieira's avatar Hugo H. Maia Vieira

Add getTableItem


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@41555 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent dc29520f
1.0.10 (unreleased)
===================
- Add getTableItemList for OOGranulate
- Add getTableItem and getTableItemList for OOGranulate
- Add getParagraphItemList and getParagraphItem for OOGranulate
- Add getImageItemList and getImage for OOGranulate
- Add OdfDocument
......
......@@ -27,6 +27,10 @@
##############################################################################
from zope.interface import implements
from zipfile import ZipFile
from StringIO import StringIO
from lxml import etree
from os import path
from cloudooo.document import OdfDocument
from cloudooo.interfaces.granulate import ITableGranulator, \
IImageGranulator, \
......@@ -42,6 +46,19 @@ class OOGranulate(object):
def __init__(self, file, source_format):
self.document = OdfDocument(file, source_format)
def _odfWithoutContentXml(self, format='odt'):
"""Returns an odf document without content.xml
It is a way to escape from this issue: http://bugs.python.org/issue6818"""
new_odf_document = ZipFile(StringIO(), 'a')
template_path = path.join(path.dirname(__file__), 'template.%s' % format)
template_file = ZipFile(template_path)
for item in template_file.filelist:
buffer = template_file.read(item.filename)
if item.filename != 'content.xml':
new_odf_document.writestr(item.filename, buffer)
template_file.close()
return new_odf_document
def getTableItemList(self):
"""Returns the list of table IDs in the form of (id, title)."""
xml_table_list = self.document.parsed_content.xpath('.//table:table',
......@@ -56,6 +73,33 @@ class OOGranulate(object):
table_list.append((id, title))
return table_list
def getTableItem(self, id, format='odt'):
"""Returns the table into a new 'format' file."""
try:
template_path = path.join(path.dirname(__file__), 'template.%s' % format)
template = ZipFile(template_path)
content_xml = etree.fromstring(template.read('content.xml'))
template.close()
table = self.document.parsed_content.xpath(
'//table:table[@table:name="%s"]' % id,
namespaces=self.document.parsed_content.nsmap)[0]
# Next line do this <office:content><office:body><office:text><table:table>
content_xml[-1][0].append(table)
# XXX: Next line replace the <office:automatic-styles> tag. This include a
# lot of unused style tags. Will be better detect the used styles and
# include only those.
content_xml.replace(content_xml[-2],
self.document.parsed_content[-2])
odf_document = self._odfWithoutContentXml(format)
odf_document.writestr('content.xml', etree.tostring(content_xml))
odf_document_as_string = odf_document.fp
odf_document.close()
odf_document_as_string.seek(0)
return odf_document_as_string.read()
except:
return None
def getColumnItemList(self, file, table_id):
"""Return the list of columns in the form of (id, title)."""
raise NotImplementedError
......
......@@ -35,6 +35,9 @@ class ITableGranulator(Interface):
def getTableItemList():
"""Returns the list of table IDs in the form of (id, title)."""
def getTableItem(id, format):
"""Returns the table into a new 'format' file."""
def getColumnItemList(file, table_id):
"""Return the list of columns in the form of (id, title)."""
......
......@@ -56,7 +56,10 @@ class TestInterface(unittest.TestCase):
def testITableGranulator(self):
"""Test if OOGranulate implements ITableGranulator"""
self.assertEquals(ITableGranulator.implementedBy(OOGranulate), True)
method_list = ['getColumnItemList', 'getLineItemList', 'getTableItemList']
method_list = ['getTableItem',
'getLineItemList',
'getColumnItemList',
'getTableItemList']
self.assertEquals(ITableGranulator.names(), method_list)
def testITextGranulator(self):
......@@ -84,7 +87,7 @@ class TestInterface(unittest.TestCase):
method_list = ['getContentXml',
'parsed_content',
'source_format',
'getFile',]
'getFile']
self.assertEquals(IOdfDocument.names(), method_list)
def testIFilter(self):
......
......@@ -30,6 +30,7 @@
import unittest
from zipfile import ZipFile
from StringIO import StringIO
from lxml import etree
from cloudoooTestCase import cloudoooTestCase, make_suite
from cloudooo.granulate.oogranulate import OOGranulate
......@@ -40,6 +41,18 @@ class TestOOGranulate(cloudoooTestCase):
data = open('./data/granulate_test.odt').read()
self.oogranulate = OOGranulate(data, 'odt')
def testOdfWithoutContentXml(self):
"""Test if _odfWithoutContentXml() return a ZipFile instance without the
content.xml file"""
odf_without_content_xml = self.oogranulate._odfWithoutContentXml('odt')
self.assertTrue(isinstance(odf_without_content_xml, ZipFile))
complete_name_list = []
for item in self.oogranulate.document._zipfile.filelist:
complete_name_list.append(item.filename)
for item in odf_without_content_xml.filelist:
self.assertTrue(item.filename in complete_name_list)
self.assertTrue(item.filename != 'content.xml')
def testgetTableItemList(self):
"""Test if getTableItemList() returns the right tables list"""
data = open('./data/granulate_table_test.odt').read()
......@@ -49,6 +62,27 @@ class TestOOGranulate(cloudoooTestCase):
('SoccerTeams', 'Tabela 2: Soccer Teams')]
self.assertEquals(table_list, oogranulate.getTableItemList())
def testGetTableItem(self):
"""Test if getTableItem() returns on odf file with the right table"""
data = open('./data/granulate_table_test.odt').read()
oogranulate = OOGranulate(data, 'odt')
table_data_doc = oogranulate.getTableItem('Developers')
content_xml_str = ZipFile(StringIO(table_data_doc)).read('content.xml')
content_xml = etree.fromstring(content_xml_str)
table_list = content_xml.xpath('//table:table',
namespaces=content_xml.nsmap)
self.assertEquals(1, len(table_list))
table = table_list[0]
name_key = '{urn:oasis:names:tc:opendocument:xmlns:table:1.0}name'
self.assertEquals('Developers', table.attrib[name_key])
def testGetTableItemWithoutSuccess(self):
"""Test if getTableItem() returns None for an non existent table name"""
data = open('./data/granulate_table_test.odt').read()
oogranulate = OOGranulate(data, 'odt')
table_data = oogranulate.getTableItem('NonExistentTable')
self.assertEquals(table_data, None)
def testGetColumnItemList(self):
"""Test if getColumnItemList() returns the right table columns list"""
self.assertRaises(NotImplementedError, self.oogranulate.getColumnItemList,
......
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