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

Add OdfDocument


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@40566 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 9f4f4636
1.0.10 (unreleased)
===================
- Add OdfDocument
- Add granulate interface.
1.0.9
......@@ -13,9 +14,10 @@
=====
- Remove all attributes that works with cloudooo script paths.
- Use all scripts according to your python eggs.
- Fixed problem when a spreadsheet will be converted to html.
- Fixed problem when a spreadsheet will be converted to html.
1.0.7
=====
- Remove entry points, treat those as ordinary files.
- Search all script files using pkg_resources.
......@@ -27,13 +27,14 @@
##############################################################################
import mimetypes
import tempfile
from os.path import join, exists, curdir, abspath
from os import listdir, remove, chdir
from zope.interface import implements
from interfaces.document import IDocument
from zipfile import ZipFile, is_zipfile
from shutil import rmtree
import tempfile
from StringIO import StringIO
from interfaces.document import IDocument, IOdfDocument
class FileSystemDocument(object):
......@@ -47,6 +48,7 @@ class FileSystemDocument(object):
Keyword arguments:
base_folder_url -- Full path to create a temporary folder
data -- Content of the document
source_format -- Document Extension
"""
self.base_folder_url = base_folder_url
self.directory_name = self._createDirectory()
......@@ -136,3 +138,30 @@ class FileSystemDocument(object):
def __del__(self):
self.trash()
class OdfDocument(object):
"""Manipulates odf documents in memory"""
implements(IOdfDocument)
def __init__(self, data, source_format):
"""Open the the file in memory.
Keyword arguments:
data -- Content of the document
source_format -- Document Extension
"""
self._zipfile = ZipFile(StringIO(data))
self.source_format = source_format
def getContentXml(self):
"""Returns the content.xml file as string"""
return self._zipfile.read('content.xml')
def getFile(self, path):
"""If exists, returns file as string, else return None"""
try:
return self._zipfile.read(path)
except KeyError:
return None
......@@ -33,7 +33,8 @@ from zope.interface import Attribute
class IDocument(Interface):
"""Manipulates documents in file system"""
base_folder_url = Attribute("Url of folder that is used to create temporary files")
base_folder_url = Attribute("Url of folder that is used to create temporary \
files")
directory_name = Attribute("String of directory name")
source_format = Attribute("Document Extension")
url = Attribute("Complete path of document in file system")
......@@ -57,3 +58,16 @@ class IDocument(Interface):
def restoreOriginal():
"""Restore the document with the original document"""
class IOdfDocument(Interface):
"""Manipulates odf documents in memory"""
source_format = Attribute("Document Extension")
def getContentXml():
"""Returns the content.xml file as string"""
def getFile(path):
"""Returns, as string, the file located in the given path, inside de odf
file"""
......@@ -27,7 +27,7 @@
##############################################################################
import unittest
from cloudooo.document import FileSystemDocument
from cloudooo.document import FileSystemDocument, OdfDocument
from cloudooo.handler.oohandler import OOHandler
from cloudooo.application.openoffice import OpenOffice
from cloudooo.manager import Manager
......@@ -36,7 +36,7 @@ from cloudooo.filter import Filter
from cloudooo.application.xvfb import Xvfb
from cloudooo.monitor.request import MonitorRequest
from cloudooo.granulate.oogranulate import OOGranulate
from cloudooo.interfaces.document import IDocument
from cloudooo.interfaces.document import IDocument, IOdfDocument
from cloudooo.interfaces.lockable import ILockable
from cloudooo.interfaces.manager import IManager
from cloudooo.interfaces.application import IApplication
......@@ -78,6 +78,14 @@ class TestInterface(unittest.TestCase):
"""Test if FileSystemDocument implements IDocument"""
self.assertEquals(IDocument.implementedBy(FileSystemDocument), True)
def testIOdfDocument(self):
"""Test if OdfDocument implements IOdfDocument"""
self.assertEquals(IOdfDocument.implementedBy(OdfDocument), True)
method_list = ['getFile',
'getContentXml',
'source_format']
self.assertEquals(IOdfDocument.names(), method_list)
def testIFilter(self):
"""Test if Filter implements IDocument"""
self.assertEquals(IFilter.implementedBy(Filter), True)
......
##############################################################################
#
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
# Hugo H. Maia Vieira <hugomaia@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import unittest
from zipfile import ZipFile
from cloudoooTestCase import cloudoooTestCase, make_suite
from cloudooo.document import OdfDocument
class TestOdfDocument(cloudoooTestCase):
def setUp(self):
data = open('./data/granulate_test.odt').read()
self.oodocument = OdfDocument(data, 'odt')
def testReceivedGoodFile(self):
"""Test if received path is from a good document returing an ZipFile"""
self.assertEquals(isinstance(self.oodocument._zipfile, ZipFile), True)
def testGetContentXml(self):
"""Test if the getContentXml method returns the content.xml file"""
content_xml = self.oodocument.getContentXml()
self.assertEquals('The content of this file is just' in content_xml, True)
def testGetExistentFile(self):
"""Test if the getFile method returns the requested file"""
requested_file = self.oodocument.getFile('content.xml')
self.assertEquals(requested_file, self.oodocument.getContentXml())
def testGetNotPresentFile(self):
"""Test if the getFile method returns None for not present file request"""
requested_file = self.oodocument.getFile('not_present.xml')
self.assertEquals(requested_file, None)
def test_suite():
return make_suite(TestOdfDocument)
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(TestOdfDocument)
unittest.TextTestRunner(verbosity=2).run(suite)
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