Commit a61fd142 authored by Hugo H. Maia Vieira's avatar Hugo H. Maia Vieira

Implement getImageItemList

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@41090 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent d968d7fb
1.0.10 (unreleased) 1.0.10 (unreleased)
=================== ===================
- Add getImageItemList for OOGranulate
- Add OdfDocument - Add OdfDocument
- Add granulate interface. - Add granulate interface.
...@@ -20,4 +21,3 @@ ...@@ -20,4 +21,3 @@
===== =====
- Remove entry points, treat those as ordinary files. - Remove entry points, treat those as ordinary files.
- Search all script files using pkg_resources. - Search all script files using pkg_resources.
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
############################################################################## ##############################################################################
from zope.interface import implements from zope.interface import implements
from cloudooo.document import OdfDocument
from cloudooo.interfaces.granulate import ITableGranulator, \ from cloudooo.interfaces.granulate import ITableGranulator, \
IImageGranulator, \ IImageGranulator, \
ITextGranulator ITextGranulator
...@@ -38,6 +39,36 @@ class OOGranulate(object): ...@@ -38,6 +39,36 @@ class OOGranulate(object):
implements(ITableGranulator, IImageGranulator, ITextGranulator) implements(ITableGranulator, IImageGranulator, ITextGranulator)
def __init__(self, file, source_format):
self.document = OdfDocument(file, source_format)
def _getElementsByTagName(self, xml_element, tag):
"""Returns a list with the xml elements of the given tag
tag -- tag name with the namespace (e.g. namespace:tag_name)"""
return xml_element.xpath('.//%s' % tag, namespaces=xml_element.nsmap)
def _hasAncestor(self, xml_element, required_ancestor):
"""Verifies if xml_element have an ancestor tag at a maximum level.
required_ancestor -- tag name without the namespace"""
for ancestor in xml_element.iterancestors():
actual_ancestor = ancestor.tag.split('}')[-1]
if actual_ancestor == required_ancestor:
return True
return False
def _getImageTitle(self, xml_element):
"""Returns, if exists, the title of the given xml image element"""
if self._hasAncestor(xml_element, 'text-box'):
draw_frame = xml_element.getparent()
text_p = draw_frame.getparent()
title = ''
for word in text_p.itertext():
title += word
return title
return ''
def getTableItemList(self, file): def getTableItemList(self, file):
"""Returns the list of table IDs in the form of (id, title).""" """Returns the list of table IDs in the form of (id, title)."""
raise NotImplementedError raise NotImplementedError
...@@ -50,9 +81,16 @@ class OOGranulate(object): ...@@ -50,9 +81,16 @@ class OOGranulate(object):
"""Returns the lines of a given table as (key, value) pairs.""" """Returns the lines of a given table as (key, value) pairs."""
raise NotImplementedError raise NotImplementedError
def getImageItemList(self, file): def getImageItemList(self):
"""Return the list of images in the form of (id, title).""" """Return a list of tuples with the id and title of image files"""
raise NotImplementedError xml_images = self._getElementsByTagName(self.document.parsed_content,
'draw:image')
image_list = []
for image in xml_images:
title = self._getImageTitle(image)
id = image.values()[0].split('/')[-1]
image_list.append((id, title))
return image_list
def getImage(self, file, image_id, format=None, resolution=None, **kw): def getImage(self, file, image_id, format=None, resolution=None, **kw):
"""Return the given image.""" """Return the given image."""
......
...@@ -45,10 +45,10 @@ class ITableGranulator(Interface): ...@@ -45,10 +45,10 @@ class ITableGranulator(Interface):
class IImageGranulator(Interface): class IImageGranulator(Interface):
"""Provides methods to granulate a document into images.""" """Provides methods to granulate a document into images."""
def getImageItemList(file): def getImageItemList():
"""Return the list of images in the form of (id, title).""" """Return the list of images in the form of (id, title)."""
def getImage(file, image_id, format=None, resolution=None, **kw): def getImage(image_id, format=None, resolution=None, **kw):
"""Return the given image.""" """Return the given image."""
......
...@@ -34,7 +34,33 @@ from cloudooo.granulate.oogranulate import OOGranulate ...@@ -34,7 +34,33 @@ from cloudooo.granulate.oogranulate import OOGranulate
class TestOOGranulate(cloudoooTestCase): class TestOOGranulate(cloudoooTestCase):
def setUp(self): def setUp(self):
self.oogranulate = OOGranulate() data = open('./data/granulate_test.odt').read()
self.oogranulate = OOGranulate(data, 'odt')
def testGetElementsByTagName(self):
"""Test if _getElementsByTagName() returns right elements list"""
elements = self.oogranulate._getElementsByTagName(
self.oogranulate.document.parsed_content,
'draw:image')
self.assertEquals(len(elements), 5)
for element in elements:
self.assertTrue(element.tag.endswith('image'))
def testHasAncertor(self):
images = self.oogranulate._getElementsByTagName(
self.oogranulate.document.parsed_content,
'draw:image')
self.assertFalse(self.oogranulate._hasAncestor(images[0], 'text-box'))
self.assertTrue(self.oogranulate._hasAncestor(images[0], 'frame'))
self.assertTrue(self.oogranulate._hasAncestor(images[2], 'text-box'))
def testGetImageTitle(self):
images = self.oogranulate._getElementsByTagName(
self.oogranulate.document.parsed_content,
'draw:image')
self.assertEquals(self.oogranulate._getImageTitle(images[0]), '')
self.assertEquals(self.oogranulate._getImageTitle(images[2]),
'Illustration 1: TioLive Logo')
def testgetTableItemList(self): def testgetTableItemList(self):
"""Test if getTableItemList() returns the right tables list""" """Test if getTableItemList() returns the right tables list"""
...@@ -55,8 +81,16 @@ class TestOOGranulate(cloudoooTestCase): ...@@ -55,8 +81,16 @@ class TestOOGranulate(cloudoooTestCase):
def testGetImageItemList(self): def testGetImageItemList(self):
"""Test if getImageItemList() returns the right images list""" """Test if getImageItemList() returns the right images list"""
self.assertRaises(NotImplementedError, self.oogranulate.getImageItemList, image_list = self.oogranulate.getImageItemList()
'file') self.assertEquals([
('10000000000000C80000009C38276C51.jpg', ''),
('10000201000000C80000004E7B947D46.png', ''),
('10000201000000C80000004E7B947D46.png', 'Illustration 1: TioLive Logo'),
# XXX The svg image are stored into odf as svm
('2000004F00004233000013707E7DE37A.svm', 'Figure 1: Python Logo'),
('10000201000000C80000004E7B947D46.png',
'Illustration 2: Again TioLive Logo'),
], image_list)
def testGetImage(self): def testGetImage(self):
"""Test if getImage() returns the right image file""" """Test if getImage() returns the right image file"""
......
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