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)
===================
- Add getImageItemList for OOGranulate
- Add OdfDocument
- Add granulate interface.
......@@ -20,4 +21,3 @@
=====
- Remove entry points, treat those as ordinary files.
- Search all script files using pkg_resources.
......@@ -27,6 +27,7 @@
##############################################################################
from zope.interface import implements
from cloudooo.document import OdfDocument
from cloudooo.interfaces.granulate import ITableGranulator, \
IImageGranulator, \
ITextGranulator
......@@ -38,6 +39,36 @@ class OOGranulate(object):
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):
"""Returns the list of table IDs in the form of (id, title)."""
raise NotImplementedError
......@@ -50,9 +81,16 @@ class OOGranulate(object):
"""Returns the lines of a given table as (key, value) pairs."""
raise NotImplementedError
def getImageItemList(self, file):
"""Return the list of images in the form of (id, title)."""
raise NotImplementedError
def getImageItemList(self):
"""Return a list of tuples with the id and title of image files"""
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):
"""Return the given image."""
......
......@@ -45,10 +45,10 @@ class ITableGranulator(Interface):
class IImageGranulator(Interface):
"""Provides methods to granulate a document into images."""
def getImageItemList(file):
def getImageItemList():
"""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."""
......
......@@ -34,7 +34,33 @@ from cloudooo.granulate.oogranulate import OOGranulate
class TestOOGranulate(cloudoooTestCase):
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):
"""Test if getTableItemList() returns the right tables list"""
......@@ -55,8 +81,16 @@ class TestOOGranulate(cloudoooTestCase):
def testGetImageItemList(self):
"""Test if getImageItemList() returns the right images list"""
self.assertRaises(NotImplementedError, self.oogranulate.getImageItemList,
'file')
image_list = self.oogranulate.getImageItemList()
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):
"""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