Commit 394d2a1f authored by Hugo H. Maia Vieira's avatar Hugo H. Maia Vieira

Refactor getImageItemList. Remove all auxiliar methods.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@41117 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 3942ddcf
...@@ -42,33 +42,6 @@ class OOGranulate(object): ...@@ -42,33 +42,6 @@ class OOGranulate(object):
def __init__(self, file, source_format): def __init__(self, file, source_format):
self.document = OdfDocument(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
...@@ -83,12 +56,15 @@ class OOGranulate(object): ...@@ -83,12 +56,15 @@ class OOGranulate(object):
def getImageItemList(self): def getImageItemList(self):
"""Return a list of tuples with the id and title of image files""" """Return a list of tuples with the id and title of image files"""
xml_image_list = self._getElementsByTagName(self.document.parsed_content, xml_image_list = self.document.parsed_content.xpath('.//draw:image',
'draw:image') namespaces=self.document.parsed_content.nsmap)
image_list = [] image_list = []
for image in xml_image_list: for xml_image in xml_image_list:
title = self._getImageTitle(image) title_list = xml_image.xpath('.//../../text() | .//../../*/text()',
id = image.values()[0].split('/')[-1] namespaces=xml_image.nsmap)
title = ''.join(title_list)
id = xml_image.values()[0].split('/')[-1]
image_list.append((id, title)) image_list.append((id, title))
return image_list return image_list
......
...@@ -39,33 +39,6 @@ class TestOOGranulate(cloudoooTestCase): ...@@ -39,33 +39,6 @@ class TestOOGranulate(cloudoooTestCase):
data = open('./data/granulate_test.odt').read() data = open('./data/granulate_test.odt').read()
self.oogranulate = OOGranulate(data, 'odt') self.oogranulate = OOGranulate(data, 'odt')
def testGetElementsByTagName(self):
"""Test if _getElementsByTagName() returns right elements list"""
element_list = self.oogranulate._getElementsByTagName(
self.oogranulate.document.parsed_content,
'draw:image')
self.assertEquals(len(element_list), 5)
for element in element_list:
self.assertTrue(element.tag.endswith('image'))
def testHasAncertor(self):
"""_hasAncestor() should vefify if the elements has the ancestor or not"""
image_list = self.oogranulate._getElementsByTagName(
self.oogranulate.document.parsed_content,
'draw:image')
self.assertFalse(self.oogranulate._hasAncestor(image_list[0], 'text-box'))
self.assertTrue(self.oogranulate._hasAncestor(image_list[0], 'frame'))
self.assertTrue(self.oogranulate._hasAncestor(image_list[2], 'text-box'))
def testGetImageTitle(self):
"""_hasAncestor() should vefify if the elements has the ancestor or not"""
image_list = self.oogranulate._getElementsByTagName(
self.oogranulate.document.parsed_content,
'draw:image')
self.assertEquals(self.oogranulate._getImageTitle(image_list[0]), '')
self.assertEquals(self.oogranulate._getImageTitle(image_list[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"""
self.assertRaises(NotImplementedError, self.oogranulate.getTableItemList, self.assertRaises(NotImplementedError, self.oogranulate.getTableItemList,
......
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