Commit f62e3fc4 authored by Jérome Perrin's avatar Jérome Perrin

Libreoffice 7.5.2.2 / Python 3

See merge request !32
parents 27d76c56 98055878
Pipeline #28699 failed with stage
in 0 seconds
.. warning:: this documentation is outdated. See https://lab.nexedi.com/nexedi/cloudooo , https://lab.nexedi.com/nexedi/slapos/tree/master/software/cloudooo or https://cloudooo.nexedi.com/ for more up to date information.
Install Cloudooo Install Cloudooo
================ ================
:: ::
$ python2.6 setup.py install $ python setup.py install
Warnings:
- you must have installed setuptools>=0.6c11 in this python.
Install LibreOffice / OpenOffice.org Install LibreOffice / OpenOffice.org
==================================== ====================================
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
import unittest import unittest
import sys import sys
from base64 import encodestring from base64 import encodebytes
from xmlrpclib import ServerProxy from xmlrpc.client import ServerProxy
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
DOCUMENT_STRING = """MemoryMonitor - TimeoutMonitor - DOCUMENT_STRING = b"""MemoryMonitor - TimeoutMonitor -
RequestMonitor\n\nOOHandler\n\nMimemapper\n\nERP5\n""" RequestMonitor\n\nOOHandler\n\nMimemapper\n\nERP5\n"""
HOSTNAME = PORT = None HOSTNAME = PORT = None
...@@ -15,17 +15,17 @@ class CloudoooTestCase(unittest.TestCase): ...@@ -15,17 +15,17 @@ class CloudoooTestCase(unittest.TestCase):
""" """ """ """
def setUp(self): def setUp(self):
self.proxy_address = "http://%s:%s" % (HOSTNAME, PORT) self.proxy_address = f"http://{HOSTNAME}:{PORT}"
def test_run_generate(self): def test_run_generate(self):
data = encodestring(DOCUMENT_STRING) data = encodebytes(DOCUMENT_STRING)
proxy = ServerProxy(self.proxy_address, allow_none=True) proxy = ServerProxy(self.proxy_address, allow_none=True)
res = proxy.run_generate("t.text", data, None, 'pdf', 'text/plain') res = proxy.run_generate("t.text", data, None, 'pdf', 'text/plain')
self.assertEqual(res[1]['mime'], "application/pdf") self.assertEqual(res[1]['mime'], "application/pdf")
self.assertEqual(res[0], 200) self.assertEqual(res[0], 200)
def test_set_metadata(self): def test_set_metadata(self):
data = encodestring(DOCUMENT_STRING) data = encodebytes(DOCUMENT_STRING)
proxy = ServerProxy(self.proxy_address, allow_none=True) proxy = ServerProxy(self.proxy_address, allow_none=True)
odt_data = proxy.convertFile(data, 'txt', 'odt') odt_data = proxy.convertFile(data, 'txt', 'odt')
metadata_dict = proxy.getFileMetadataItemList(odt_data, 'odt') metadata_dict = proxy.getFileMetadataItemList(odt_data, 'odt')
...@@ -33,7 +33,7 @@ class CloudoooTestCase(unittest.TestCase): ...@@ -33,7 +33,7 @@ class CloudoooTestCase(unittest.TestCase):
'application/vnd.oasis.opendocument.text') 'application/vnd.oasis.opendocument.text')
res = proxy.run_setmetadata("t.odt", odt_data, {"Title": "test"}) res = proxy.run_setmetadata("t.odt", odt_data, {"Title": "test"})
self.assertEqual(res[0], 200) self.assertEqual(res[0], 200)
response_code, response_dict, response_message = \ response_code, response_dict, _ = \
proxy.run_convert("t.odt", res[1]['data']) proxy.run_convert("t.odt", res[1]['data'])
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
self.assertEqual(response_dict['meta']['Title'], "test") self.assertEqual(response_dict['meta']['Title'], "test")
...@@ -45,7 +45,7 @@ def main(): ...@@ -45,7 +45,7 @@ def main():
opt_list, _ = getopt(sys.argv[1:], "", opt_list, _ = getopt(sys.argv[1:], "",
["port=", "hostname="]) ["port=", "hostname="])
except GetoptError as e: except GetoptError as e:
print >> sys.stderr, "%s \nUse --port and --hostname" % e print("%s \nUse --port and --hostname" % e, file=sys.stderr)
sys.exit(2) sys.exit(2)
for opt, arg in opt_list: for opt, arg in opt_list:
...@@ -55,7 +55,7 @@ def main(): ...@@ -55,7 +55,7 @@ def main():
HOSTNAME = arg HOSTNAME = arg
if not HOSTNAME and not PORT: if not HOSTNAME and not PORT:
print >> sys.stderr, "Use --port and --hostname" print("Use --port and --hostname", file=sys.stderr)
sys.exit(2) sys.exit(2)
suite = unittest.TestLoader().loadTestsFromTestCase(CloudoooTestCase) suite = unittest.TestLoader().loadTestsFromTestCase(CloudoooTestCase)
unittest.TextTestRunner(verbosity=2).run(suite) unittest.TextTestRunner(verbosity=2).run(suite)
...@@ -6,4 +6,4 @@ def main(): ...@@ -6,4 +6,4 @@ def main():
cloudooo_conf_path = pkg_resources.resource_filename("cloudooo", cloudooo_conf_path = pkg_resources.resource_filename("cloudooo",
path.join("sample", "cloudooo.conf.in")) path.join("sample", "cloudooo.conf.in"))
print open(cloudooo_conf_path).read() print(open(cloudooo_conf_path).read())
...@@ -39,12 +39,12 @@ from cloudooo.interfaces.file import IFile ...@@ -39,12 +39,12 @@ from cloudooo.interfaces.file import IFile
@implementer(IFile) @implementer(IFile)
class File(object): class File:
"""File is used to manipulate one temporary file """File is used to manipulate one temporary file
stored into the filesystem. stored into the filesystem.
""" """
def __init__(self, base_folder_url, data, source_format): def __init__(self, base_folder_url:str, data:bytes, source_format:str):
"""Create an file into file system and store the URL. """Create an file into file system and store the URL.
Keyword arguments: Keyword arguments:
base_folder_url -- Full path to create a temporary folder base_folder_url -- Full path to create a temporary folder
...@@ -60,7 +60,7 @@ class File(object): ...@@ -60,7 +60,7 @@ class File(object):
def _createDirectory(self): def _createDirectory(self):
return tempfile.mkdtemp(dir=self.base_folder_url) return tempfile.mkdtemp(dir=self.base_folder_url)
def load(self): def load(self) -> str:
"""Creates one Temporary Document and write data into it. """Creates one Temporary Document and write data into it.
Return the url for the document. Return the url for the document.
""" """
...@@ -68,7 +68,8 @@ class File(object): ...@@ -68,7 +68,8 @@ class File(object):
file_path = tempfile.mktemp(suffix=".%s" % self.source_format, file_path = tempfile.mktemp(suffix=".%s" % self.source_format,
dir=self.directory_name) dir=self.directory_name)
# stores the data in temporary file # stores the data in temporary file
open(file_path, 'wb').write(self.original_data) with open(file_path, 'wb') as f:
f.write(self.original_data)
# If is a zipfile is need extract all files from whitin the compressed file # If is a zipfile is need extract all files from whitin the compressed file
if is_zipfile(file_path): if is_zipfile(file_path):
zipfile = ZipFile(file_path) zipfile = ZipFile(file_path)
...@@ -91,7 +92,7 @@ class File(object): ...@@ -91,7 +92,7 @@ class File(object):
remove(zipfile_path) remove(zipfile_path)
return file_path return file_path
def getContent(self, zip=False): def getContent(self, zip=False) -> bytes:
"""Open the file and returns the content. """Open the file and returns the content.
Keyword Arguments: Keyword Arguments:
zip -- Boolean attribute. If True""" zip -- Boolean attribute. If True"""
...@@ -106,14 +107,16 @@ class File(object): ...@@ -106,14 +107,16 @@ class File(object):
zipfile.write(file) zipfile.write(file)
finally: finally:
zipfile.close() zipfile.close()
opened_zip = open(zip_path, 'r').read() with open(zip_path, 'rb') as f:
opened_zip = f.read()
remove(zip_path) remove(zip_path)
chdir(current_dir_url) chdir(current_dir_url)
return opened_zip return opened_zip
else: else:
return open(self.url, 'r').read() with open(self.url, 'rb') as f:
return f.read()
def getUrl(self): def getUrl(self) -> str:
"""Returns full path.""" """Returns full path."""
return self.url return self.url
......
...@@ -36,7 +36,7 @@ from subprocess import Popen, PIPE ...@@ -36,7 +36,7 @@ from subprocess import Popen, PIPE
from tempfile import mktemp from tempfile import mktemp
@implementer(IHandler) @implementer(IHandler)
class Handler(object): class Handler:
"""FFMPEG Handler is used to handler inputed audio and video files""" """FFMPEG Handler is used to handler inputed audio and video files"""
def __init__(self, base_folder_url, data, source_format, **kw): def __init__(self, base_folder_url, data, source_format, **kw):
...@@ -56,7 +56,7 @@ class Handler(object): ...@@ -56,7 +56,7 @@ class Handler(object):
# XXX This implementation could use ffmpeg -i pipe:0, but # XXX This implementation could use ffmpeg -i pipe:0, but
# XXX seems super unreliable currently and it generates currupted files in # XXX seems super unreliable currently and it generates currupted files in
# the end # the end
logger.debug("FfmpegConvert: %s > %s" % (self.input.source_format, destination_format)) logger.debug("FfmpegConvert: %s > %s", self.input.source_format, destination_format)
output_url = mktemp(suffix=".%s" % destination_format, output_url = mktemp(suffix=".%s" % destination_format,
dir=self.input.directory_name) dir=self.input.directory_name)
command = ["ffmpeg", command = ["ffmpeg",
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
from os.path import join from os.path import join
from cloudooo.tests.cloudoooTestCase import TestCase from cloudooo.tests.cloudoooTestCase import TestCase
from cloudooo.tests.backportUnittest import skip from unittest import skip
class TestAllSupportedFormat(TestCase): class TestAllSupportedFormat(TestCase):
......
...@@ -44,14 +44,17 @@ class TestServer(TestCase): ...@@ -44,14 +44,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
]
# Try convert one video for a invalid format # Try convert one video for a invalid format
(open(join('data', 'test.ogv')).read(), 'ogv', 'xyz'), with open(join('data', 'test.ogv'), 'rb') as f:
scenario_list.append((f.read(), 'ogv', 'xyz'))
# Try convert one video to format not possible # Try convert one video to format not possible
(open(join('data', 'test.ogv')).read(), 'ogv', 'moov'), with open(join('data', 'test.ogv'), 'rb') as f:
] scenario_list.append((f.read(), 'ogv', 'moov'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid video files""" """Test fail convertion of Invalid video files"""
......
...@@ -38,7 +38,7 @@ from tempfile import mktemp ...@@ -38,7 +38,7 @@ from tempfile import mktemp
@implementer(IHandler) @implementer(IHandler)
class Handler(object): class Handler:
"""ImageMagic Handler is used to handler images.""" """ImageMagic Handler is used to handler images."""
def __init__(self, base_folder_url, data, source_format, **kw): def __init__(self, base_folder_url, data, source_format, **kw):
...@@ -49,7 +49,7 @@ class Handler(object): ...@@ -49,7 +49,7 @@ class Handler(object):
def convert(self, destination_format=None, **kw): def convert(self, destination_format=None, **kw):
"""Convert a image""" """Convert a image"""
logger.debug("ImageMagickConvert: %s > %s" % (self.file.source_format, destination_format)) logger.debug("ImageMagickConvert: %s > %s", self.file.source_format, destination_format)
output_url = mktemp(suffix='.%s' % destination_format, output_url = mktemp(suffix='.%s' % destination_format,
dir=self.base_folder_url) dir=self.base_folder_url)
command = ["convert", self.file.getUrl(), output_url] command = ["convert", self.file.getUrl(), output_url]
...@@ -73,6 +73,7 @@ class Handler(object): ...@@ -73,6 +73,7 @@ class Handler(object):
stdout=PIPE, stdout=PIPE,
stderr=PIPE, stderr=PIPE,
close_fds=True, close_fds=True,
text=True,
env=self.environment).communicate() env=self.environment).communicate()
self.file.trash() self.file.trash()
metadata_dict = {} metadata_dict = {}
...@@ -80,7 +81,7 @@ class Handler(object): ...@@ -80,7 +81,7 @@ class Handler(object):
std = std.strip() std = std.strip()
if re.search("^[a-zA-Z]", std): if re.search("^[a-zA-Z]", std):
if std.count(":") > 1: if std.count(":") > 1:
key, value = re.compile(".*\:\ ").split(std) key, value = re.compile(r".*\:\ ").split(std)
else: else:
key, value = std.split(":") key, value = std.split(":")
metadata_dict[key] = value.strip() metadata_dict[key] = value.strip()
......
...@@ -41,7 +41,8 @@ class TestHandler(HandlerTestCase): ...@@ -41,7 +41,8 @@ class TestHandler(HandlerTestCase):
def testConvertPNGtoJPG(self): def testConvertPNGtoJPG(self):
"""Test conversion of png to jpg""" """Test conversion of png to jpg"""
png_file = open("data/test.png").read() with open("data/test.png", "rb") as f:
png_file = f.read()
handler = Handler(self.tmp_url, png_file, "png", **self.kw) handler = Handler(self.tmp_url, png_file, "png", **self.kw)
jpg_file = handler.convert("jpg") jpg_file = handler.convert("jpg")
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
...@@ -50,7 +51,8 @@ class TestHandler(HandlerTestCase): ...@@ -50,7 +51,8 @@ class TestHandler(HandlerTestCase):
def testgetMetadataFromImage(self): def testgetMetadataFromImage(self):
"""Test if metadata is extracted from image correctly""" """Test if metadata is extracted from image correctly"""
png_file = open("data/test.png").read() with open("data/test.png", "rb") as f:
png_file = f.read()
handler = Handler(self.tmp_url, png_file, "png", **self.kw) handler = Handler(self.tmp_url, png_file, "png", **self.kw)
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(metadata.get("Compression"), "Zip") self.assertEqual(metadata.get("Compression"), "Zip")
...@@ -59,7 +61,7 @@ class TestHandler(HandlerTestCase): ...@@ -59,7 +61,7 @@ class TestHandler(HandlerTestCase):
def testsetMetadata(self): def testsetMetadata(self):
""" Test if metadata are inserted correclty """ """ Test if metadata are inserted correclty """
handler = Handler(self.tmp_url, "", "png", **self.kw) handler = Handler(self.tmp_url, b"", "png", **self.kw)
self.assertRaises(NotImplementedError, handler.setMetadata) self.assertRaises(NotImplementedError, handler.setMetadata)
...@@ -44,14 +44,17 @@ class TestServer(TestCase): ...@@ -44,14 +44,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
# Try convert one video for a invalid format
(open(join('data', 'test.png')).read(), 'png', 'xyz'),
# Try convert one video to format not possible
(open(join('data', 'test.png')).read(), 'png', '8bim'),
] ]
# Try convert one png for a invalid format
with open(join('data', 'test.png'), 'rb') as f:
scenario_list.append((f.read(), 'png', 'xyz'))
# Try convert one png to format not possible
with open(join('data', 'test.png'), 'rb') as f:
scenario_list.append((f.read(), 'png', '8bim'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid image files""" """Test fail convertion of Invalid image files"""
......
...@@ -36,7 +36,7 @@ from psutil import pid_exists, Process, AccessDenied ...@@ -36,7 +36,7 @@ from psutil import pid_exists, Process, AccessDenied
@implementer(IApplication) @implementer(IApplication)
class Application(object): class Application:
"""Base object to create an object that is possible manipulation a """Base object to create an object that is possible manipulation a
process""" process"""
...@@ -44,15 +44,17 @@ class Application(object): ...@@ -44,15 +44,17 @@ class Application(object):
def start(self, init=True): def start(self, init=True):
"""Start Application""" """Start Application"""
logger.debug("Process Started %s, Port %s. Pid %s" % (self.name, logger.debug(
"Process Started %s, Port %s. Pid %s",
self.name,
self.getAddress()[-1], self.getAddress()[-1],
self.pid())) self.pid())
def stop(self): def stop(self):
"""Stop the process""" """Stop the process"""
if hasattr(self, 'process') and self.status(): if hasattr(self, 'process') and self.status():
process_pid = self.process.pid process_pid = self.process.pid
logger.debug("Stop Pid - %s" % process_pid) logger.debug("Stop Pid - %s", process_pid)
try: try:
self.process.terminate() self.process.terminate()
waitStopDaemon(self, self.timeout) waitStopDaemon(self, self.timeout)
...@@ -104,3 +106,9 @@ class Application(object): ...@@ -104,3 +106,9 @@ class Application(object):
if not hasattr(self, 'process'): if not hasattr(self, 'process'):
return None return None
return self.process.pid return self.process.pid
def hasExited(self):
"""Check if process has exited running"""
if not hasattr(self, 'process'):
return True
return self.process.poll() is not None
...@@ -35,7 +35,7 @@ from psutil import AccessDenied, NoSuchProcess ...@@ -35,7 +35,7 @@ from psutil import AccessDenied, NoSuchProcess
from os.path import exists, join from os.path import exists, join
from threading import Lock from threading import Lock
from zope.interface import implementer from zope.interface import implementer
from application import Application from .application import Application
from cloudooo.interfaces.lockable import ILockable from cloudooo.interfaces.lockable import ILockable
from cloudooo.util import logger from cloudooo.util import logger
from cloudooo.handler.ooo.util import waitStartDaemon, \ from cloudooo.handler.ooo.util import waitStartDaemon, \
...@@ -60,8 +60,10 @@ class OpenOffice(Application): ...@@ -60,8 +60,10 @@ class OpenOffice(Application):
def _testOpenOffice(self, host, port): def _testOpenOffice(self, host, port):
"""Test if OpenOffice was started correctly""" """Test if OpenOffice was started correctly"""
logger.debug("Test OpenOffice %s - Pid %s" % (self.getAddress()[-1], logger.debug(
self.pid())) "Test OpenOffice %s - Pid %s",
self.getAddress()[-1],
self.pid())
python = join(self.office_binary_path, "python") python = join(self.office_binary_path, "python")
args = [exists(python) and python or "python3", args = [exists(python) and python or "python3",
pkg_resources.resource_filename("cloudooo", pkg_resources.resource_filename("cloudooo",
...@@ -70,14 +72,14 @@ class OpenOffice(Application): ...@@ -70,14 +72,14 @@ class OpenOffice(Application):
"--hostname=%s" % host, "--hostname=%s" % host,
"--port=%s" % port, "--port=%s" % port,
"--uno_path=%s" % self.uno_path] "--uno_path=%s" % self.uno_path]
logger.debug("Testing Openoffice Instance %s" % port) logger.debug("Testing Openoffice Instance %s", port)
try: try:
subprocess.check_output(args, stderr=subprocess.STDOUT, close_fds=True) subprocess.check_output(args, stderr=subprocess.STDOUT, close_fds=True)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
logger.warning(e.output) logger.warning(e.output)
return False return False
else: else:
logger.debug("Instance %s works" % port) logger.debug("Instance %s works", port)
return True return True
def _cleanRequest(self): def _cleanRequest(self):
...@@ -153,12 +155,12 @@ class OpenOffice(Application): ...@@ -153,12 +155,12 @@ class OpenOffice(Application):
env["TMPDIR"] = self.path_user_installation env["TMPDIR"] = self.path_user_installation
self._startProcess(self.command, env) self._startProcess(self.command, env)
self._cleanRequest() self._cleanRequest()
Application.start(self) super().start()
def stop(self): def stop(self):
"""Stop the instance by pid. By the default """Stop the instance by pid. By the default
the signal is 15.""" the signal is 15."""
Application.stop(self) super().stop()
if socketStatus(self.hostname, self.port): if socketStatus(self.hostname, self.port):
self._releaseOpenOfficePort() self._releaseOpenOfficePort()
self._cleanRequest() self._cleanRequest()
...@@ -174,7 +176,7 @@ class OpenOffice(Application): ...@@ -174,7 +176,7 @@ class OpenOffice(Application):
def release(self): def release(self):
"""Unlock Instance.""" """Unlock Instance."""
logger.debug("OpenOffice %s, %s unlocked" % self.getAddress()) logger.debug("OpenOffice %s, %s unlocked", *self.getAddress())
self._lock.release() self._lock.release()
openoffice = OpenOffice() openoffice = OpenOffice()
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
from zope.interface import implementer from zope.interface import implementer
from zipfile import ZipFile from zipfile import ZipFile
from StringIO import StringIO from io import BytesIO
from lxml import etree from lxml import etree
from cloudooo.interfaces.file import IOdfDocument from cloudooo.interfaces.file import IOdfDocument
from cloudooo.file import File from cloudooo.file import File
...@@ -41,9 +41,10 @@ class FileSystemDocument(File): ...@@ -41,9 +41,10 @@ class FileSystemDocument(File):
@implementer(IOdfDocument) @implementer(IOdfDocument)
class OdfDocument(object): class OdfDocument:
"""Manipulates odf documents in memory""" """Manipulates odf documents in memory"""
def __init__(self, data, source_format): def __init__(self, data, source_format):
"""Open the the file in memory. """Open the the file in memory.
...@@ -51,22 +52,22 @@ class OdfDocument(object): ...@@ -51,22 +52,22 @@ class OdfDocument(object):
data -- Content of the document data -- Content of the document
source_format -- Document Extension source_format -- Document Extension
""" """
self._zipfile = ZipFile(StringIO(data)) self._zipfile = ZipFile(BytesIO(data))
self.source_format = source_format self.source_format = source_format
# XXX - Maybe parsed_content should not be here, but on OOGranulate # XXX - Maybe parsed_content should not be here, but on OOGranulate
self.parsed_content = etree.fromstring(self.getContentXml()) self.parsed_content = etree.fromstring(self.getContentXml())
def getContentXml(self): def getContentXml(self) -> bytes:
"""Returns the content.xml file as string""" """Returns the content.xml file as bytes"""
return self._zipfile.read('content.xml') return self._zipfile.read('content.xml')
def getFile(self, path): def getFile(self, path:str) -> bytes:
"""If exists, returns file as string, else return an empty string""" """If exists, returns file as bytes, otherwise b''"""
try: try:
return self._zipfile.read(path) return self._zipfile.read(path)
except KeyError: except KeyError:
return '' return b''
def trash(self): def trash(self):
"""Remove the file in memory.""" """Remove the file in memory."""
......
...@@ -33,9 +33,10 @@ from cloudooo.interfaces.filter import IFilter ...@@ -33,9 +33,10 @@ from cloudooo.interfaces.filter import IFilter
@implementer(IFilter) @implementer(IFilter)
class Filter(object): class Filter:
"""Filter of OOo.""" """Filter of OOo."""
def __init__(self, extension, filter, mimetype, document_service, **kwargs): def __init__(self, extension, filter, mimetype, document_service, **kwargs):
"""Receives extension, filter and mimetype of filter and saves in object. """Receives extension, filter and mimetype of filter and saves in object.
""" """
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
############################################################################## ##############################################################################
from zipfile import ZipFile from zipfile import ZipFile
from StringIO import StringIO from io import BytesIO
from lxml import etree from lxml import etree
from os import path from os import path
from cloudooo.util import logger from cloudooo.util import logger
...@@ -61,17 +61,17 @@ def getTemplatePath(format): ...@@ -61,17 +61,17 @@ def getTemplatePath(format):
return path.join(path.dirname(__file__), 'template.%s' % format) return path.join(path.dirname(__file__), 'template.%s' % format)
class OOGranulator(object): class OOGranulator:
"""Granulate an OpenOffice document into tables, images, chapters and """Granulate an OpenOffice document into tables, images, chapters and
paragraphs.""" paragraphs."""
def __init__(self, file, source_format): def __init__(self, file:bytes, source_format:str):
self.document = OdfDocument(file, source_format) self.document = OdfDocument(file, source_format)
def _odfWithoutContentXml(self, format='odt'): def _odfWithoutContentXml(self, format='odt'):
"""Returns an odf document without content.xml """Returns an odf document without content.xml
It is a way to escape from this issue: http://bugs.python.org/issue6818""" It is a way to escape from this issue: http://bugs.python.org/issue6818"""
new_odf_document = ZipFile(StringIO(), 'a') new_odf_document = ZipFile(BytesIO(), 'a')
template_path = getTemplatePath(format) template_path = getTemplatePath(format)
template_file = ZipFile(template_path) template_file = ZipFile(template_path)
for item in template_file.filelist: for item in template_file.filelist:
...@@ -202,7 +202,7 @@ class OOGranulator(object): ...@@ -202,7 +202,7 @@ class OOGranulator(object):
image_list = [] image_list = []
for xml_image in xml_image_list: for xml_image in xml_image_list:
id = xml_image.values()[0].split('/')[-1] id = list(xml_image.values())[0].split('/')[-1]
title = ''.join(xml_image.xpath(IMAGE_TITLE_XPATH_QUERY%(stylename_list[0], name_list[0], id), title = ''.join(xml_image.xpath(IMAGE_TITLE_XPATH_QUERY%(stylename_list[0], name_list[0], id),
namespaces=xml_image.nsmap)) namespaces=xml_image.nsmap))
if title != '': if title != '':
...@@ -250,9 +250,9 @@ class OOGranulator(object): ...@@ -250,9 +250,9 @@ class OOGranulator(object):
text = ''.join(paragraph.itertext()) text = ''.join(paragraph.itertext())
if TEXT_ATTRIB_STYLENAME not in paragraph.attrib.keys(): if TEXT_ATTRIB_STYLENAME not in paragraph.attrib:
logger.error("Unable to find %s attribute at paragraph %s " % \ logger.error("Unable to find %s attribute at paragraph %s ",
(TEXT_ATTRIB_STYLENAME, paragraph_id)) TEXT_ATTRIB_STYLENAME, paragraph_id)
return None return None
p_class = paragraph.attrib[TEXT_ATTRIB_STYLENAME] p_class = paragraph.attrib[TEXT_ATTRIB_STYLENAME]
...@@ -262,22 +262,17 @@ class OOGranulator(object): ...@@ -262,22 +262,17 @@ class OOGranulator(object):
chapter_list = self.document.parsed_content.xpath( chapter_list = self.document.parsed_content.xpath(
CHAPTER_XPATH_QUERY, CHAPTER_XPATH_QUERY,
namespaces=self.document.parsed_content.nsmap) namespaces=self.document.parsed_content.nsmap)
return chapter_list return [str(x) for x in chapter_list]
def getChapterItemList(self): def getChapterItemList(self):
"""Returns the list of chapters in the form of (id, level).""" """Returns the list of chapters in the form of (id, level)."""
id = 0 return list(enumerate(self._getChapterList()))
chapter_list = []
for chapter in self._getChapterList():
chapter_list.append((id, chapter.encode('utf-8')))
id += 1
return chapter_list
def getChapterItem(self, chapter_id): def getChapterItem(self, chapter_id):
"""Return the chapter in the form of (title, level).""" """Return the chapter in the form of (title, level)."""
chapter_list = self._getChapterList() chapter_list = self._getChapterList()
try: try:
chapter = chapter_list[chapter_id].encode('utf-8') chapter = chapter_list[chapter_id]
return [chapter, chapter_id] return [chapter, chapter_id]
except IndexError: except IndexError:
msg = "Unable to find chapter %s at chapter list." % chapter_id msg = "Unable to find chapter %s at chapter list." % chapter_id
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
import json import json
import pkg_resources import pkg_resources
import mimetypes import mimetypes
from base64 import decodestring, encodestring from base64 import decodebytes, encodebytes
from os import environ, path from os import environ, path
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
...@@ -46,7 +46,7 @@ from psutil import pid_exists ...@@ -46,7 +46,7 @@ from psutil import pid_exists
@implementer(IHandler) @implementer(IHandler)
class Handler(object): class Handler:
"""OOO Handler is used to access the one Document and OpenOffice. """OOO Handler is used to access the one Document and OpenOffice.
For each Document inputed is created on instance of this class to manipulate For each Document inputed is created on instance of this class to manipulate
the document. This Document must be able to create and remove a temporary the document. This Document must be able to create and remove a temporary
...@@ -76,9 +76,9 @@ class Handler(object): ...@@ -76,9 +76,9 @@ class Handler(object):
# backward compatibility. # backward compatibility.
# The heuristic is "if it's not utf-8", let's assume it's iso-8859-15. # The heuristic is "if it's not utf-8", let's assume it's iso-8859-15.
try: try:
unicode(data, 'utf-8') data.decode('utf-8')
except UnicodeDecodeError: except UnicodeDecodeError:
data = unicode(data, 'iso-8859-15').encode('utf-8') data = data.decode('iso-8859-15').encode('utf-8')
logger.warn("csv data is not utf-8, assuming iso-8859-15") logger.warn("csv data is not utf-8, assuming iso-8859-15")
self.document = FileSystemDocument( self.document = FileSystemDocument(
base_folder_url, base_folder_url,
...@@ -99,7 +99,7 @@ class Handler(object): ...@@ -99,7 +99,7 @@ class Handler(object):
'--document_url=%s' % self.document.getUrl()] '--document_url=%s' % self.document.getUrl()]
for arg in args: for arg in args:
command_list.insert(3, "--%s" % arg) command_list.insert(3, "--%s" % arg)
for k, v in kw.iteritems(): for k, v in kw.items():
command_list.append("--%s=%s" % (k, v)) command_list.append("--%s=%s" % (k, v))
return command_list return command_list
...@@ -138,16 +138,16 @@ class Handler(object): ...@@ -138,16 +138,16 @@ class Handler(object):
stdout, stderr = self._subprocess(command_list) stdout, stderr = self._subprocess(command_list)
if not stdout and stderr: if not stdout and stderr:
first_error = stderr first_error = stderr
logger.error(stderr) logger.error(stderr.decode())
self.document.restoreOriginal() self.document.restoreOriginal()
openoffice.restart() openoffice.restart()
kw['document_url'] = self.document.getUrl() kw['document_url'] = self.document.getUrl()
command = self._getCommand(*feature_list, **kw) command = self._getCommand(*feature_list, **kw)
stdout, stderr = self._subprocess(command) stdout, stderr = self._subprocess(command)
if not stdout and stderr: if not stdout and stderr:
second_error = "\nerror of the second run: " + stderr second_error = b"\nerror of the second run: " + stderr
logger.error(second_error) logger.error(second_error.decode())
raise Exception(first_error + second_error) raise Exception((first_error + second_error).decode(errors='replace'))
return stdout, stderr return stdout, stderr
...@@ -165,7 +165,7 @@ class Handler(object): ...@@ -165,7 +165,7 @@ class Handler(object):
filter_list.append((destination_extension, filter_list.append((destination_extension,
service_type, service_type,
mimemapper.getFilterName(destination_extension, service_type))) mimemapper.getFilterName(destination_extension, service_type)))
logger.debug("Filter List: %r" % filter_list) logger.debug("Filter List: %r", filter_list)
return json.dumps(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension, return json.dumps(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension,
filter_list=filter_list, filter_list=filter_list,
mimetype_by_filter_type=mimemapper._mimetype_by_filter_type)) mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))
...@@ -177,7 +177,7 @@ class Handler(object): ...@@ -177,7 +177,7 @@ class Handler(object):
""" """
if not self.enable_scripting and kw.get('script'): if not self.enable_scripting and kw.get('script'):
raise Exception("ooo: scripting is disabled") raise Exception("ooo: scripting is disabled")
logger.debug("OooConvert: %s > %s" % (self.source_format, destination_format)) logger.debug("OooConvert: %s > %s", self.source_format, destination_format)
kw['source_format'] = self.source_format kw['source_format'] = self.source_format
if destination_format: if destination_format:
kw['destination_format'] = destination_format kw['destination_format'] = destination_format
...@@ -186,10 +186,10 @@ class Handler(object): ...@@ -186,10 +186,10 @@ class Handler(object):
kw['refresh'] = json.dumps(self.refresh) kw['refresh'] = json.dumps(self.refresh)
openoffice.acquire() openoffice.acquire()
try: try:
stdout, stderr = self._callUnoConverter(*['convert'], **kw) stdout, _ = self._callUnoConverter(*['convert'], **kw)
finally: finally:
openoffice.release() openoffice.release()
url = stdout.replace('\n', '') url = stdout.replace(b'\n', b'')
self.document.reload(url) self.document.reload(url)
content = self.document.getContent(self.zip) content = self.document.getContent(self.zip)
self.document.trash() self.document.trash()
...@@ -198,8 +198,8 @@ class Handler(object): ...@@ -198,8 +198,8 @@ class Handler(object):
def getMetadata(self, base_document=False): def getMetadata(self, base_document=False):
"""Returns a dictionary with all metadata of document. """Returns a dictionary with all metadata of document.
Keywords Arguments: Keywords Arguments:
base_document -- Boolean variable. if true, the document is also returned base_document -- Boolean variable. if true, the document content (as bytes)
along with the metadata.""" is also returned along with the metadata."""
logger.debug("getMetadata") logger.debug("getMetadata")
kw = dict(mimemapper=self._serializeMimemapper()) kw = dict(mimemapper=self._serializeMimemapper())
if base_document: if base_document:
...@@ -208,10 +208,10 @@ class Handler(object): ...@@ -208,10 +208,10 @@ class Handler(object):
feature_list = ['getmetadata'] feature_list = ['getmetadata']
openoffice.acquire() openoffice.acquire()
try: try:
stdout, stderr = self._callUnoConverter(*feature_list, **kw) stdout, _ = self._callUnoConverter(*feature_list, **kw)
finally: finally:
openoffice.release() openoffice.release()
metadata = json.loads(decodestring(stdout)) metadata = json.loads(decodebytes(stdout))
if 'document_url' in metadata: if 'document_url' in metadata:
self.document.reload(metadata['document_url']) self.document.reload(metadata['document_url'])
metadata['Data'] = self.document.getContent() metadata['Data'] = self.document.getContent()
...@@ -224,12 +224,11 @@ class Handler(object): ...@@ -224,12 +224,11 @@ class Handler(object):
Keyword arguments: Keyword arguments:
metadata -- expected an dictionary with metadata. metadata -- expected an dictionary with metadata.
""" """
metadata_pickled = json.dumps(metadata) metadata_pickled = json.dumps(metadata).encode()
logger.debug("setMetadata") kw = dict(metadata=encodebytes(metadata_pickled).decode())
kw = dict(metadata=encodestring(metadata_pickled))
openoffice.acquire() openoffice.acquire()
try: try:
stdout, stderr = self._callUnoConverter(*['setmetadata'], **kw) self._callUnoConverter(*['setmetadata'], **kw)
finally: finally:
openoffice.release() openoffice.release()
doc_loaded = self.document.getContent() doc_loaded = self.document.getContent()
...@@ -249,7 +248,7 @@ class Handler(object): ...@@ -249,7 +248,7 @@ class Handler(object):
# XXX please never guess extension from mimetype # XXX please never guess extension from mimetype
output_set = set() output_set = set()
if "/" in source_mimetype: if "/" in source_mimetype:
parsed_mimetype_type = parseContentType(source_mimetype).gettype() parsed_mimetype_type = parseContentType(source_mimetype).get_content_type()
# here `guess_all_extensions` never handles mimetype parameters # here `guess_all_extensions` never handles mimetype parameters
# (even for `text/plain;charset=UTF-8` which is standard) # (even for `text/plain;charset=UTF-8` which is standard)
extension_list = mimetypes.guess_all_extensions(parsed_mimetype_type) # XXX never guess extension_list = mimetypes.guess_all_extensions(parsed_mimetype_type) # XXX never guess
......
...@@ -40,11 +40,6 @@ from base64 import b64encode, b64decode ...@@ -40,11 +40,6 @@ from base64 import b64encode, b64decode
from functools import partial from functools import partial
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
try:
basestring
except NameError:
basestring = str
__doc__ = """ __doc__ = """
usage: unodocument [options] usage: unodocument [options]
...@@ -81,7 +76,17 @@ Options: ...@@ -81,7 +76,17 @@ Options:
""" """
class UnoDocument(object): MARKER = []
def next(gen, default=MARKER):
try:
return gen.__next__()
except StopIteration:
if default is MARKER:
raise
return default
class UnoDocument:
"""A module to easily work with OpenOffice.org.""" """A module to easily work with OpenOffice.org."""
def __init__(self, service_manager, document_url, def __init__(self, service_manager, document_url,
...@@ -242,9 +247,9 @@ class UnoDocument(object): ...@@ -242,9 +247,9 @@ class UnoDocument(object):
continue continue
property_value = getattr(container, property_name, '') property_value = getattr(container, property_name, '')
if property_value: if property_value:
if isinstance(property_value, basestring): if isinstance(property_value, str):
metadata[property_name] = property_value metadata[property_name] = property_value
elif isinstance(property_value, tuple) and isinstance(property_value[0], basestring): elif isinstance(property_value, tuple) and isinstance(property_value[0], str):
metadata[property_name] = property_value metadata[property_name] = property_value
else: else:
try: try:
...@@ -284,7 +289,7 @@ class UnoDocument(object): ...@@ -284,7 +289,7 @@ class UnoDocument(object):
if isinstance(current_value, tuple): if isinstance(current_value, tuple):
if isinstance(value, list): if isinstance(value, list):
value = tuple(value) value = tuple(value)
elif isinstance(value, basestring): elif isinstance(value, str):
# BBB: old ERP5 code sends Keywords as a string # BBB: old ERP5 code sends Keywords as a string
# separated by a whitespace. # separated by a whitespace.
value = tuple(value.split(' ')) value = tuple(value.split(' '))
...@@ -294,7 +299,7 @@ class UnoDocument(object): ...@@ -294,7 +299,7 @@ class UnoDocument(object):
else: else:
new_properties.append([prop, value]) new_properties.append([prop, value])
for prop, value in new_properties: for prop, value in new_properties:
if isinstance(value, basestring): if isinstance(value, str):
user_defined_properties.addProperty(prop, 0, '') user_defined_properties.addProperty(prop, 0, '')
user_defined_properties.setPropertyValue(prop, value) user_defined_properties.setPropertyValue(prop, value)
self.document_loaded.store() self.document_loaded.store()
...@@ -311,7 +316,7 @@ def main(): ...@@ -311,7 +316,7 @@ def main():
help_msg = "\nUse --help or -h\n" help_msg = "\nUse --help or -h\n"
try: try:
opt_list, arg_list = getopt(sys.argv[1:], "h", ["help", "test", opt_list, _ = getopt(sys.argv[1:], "h", ["help", "test",
"convert", "getmetadata", "setmetadata", "convert", "getmetadata", "setmetadata",
"uno_path=", "office_binary_path=", "uno_path=", "office_binary_path=",
"hostname=", "port=", "source_format=", "hostname=", "port=", "source_format=",
...@@ -325,10 +330,8 @@ def main(): ...@@ -325,10 +330,8 @@ def main():
param_list = [tuple[0] for tuple in iter(opt_list)] param_list = [tuple[0] for tuple in iter(opt_list)]
try:
import json import json
except ImportError:
import simplejson as json
metadata = mimemapper = script = None metadata = mimemapper = script = None
hostname = port = office_binary_path = uno_path = None hostname = port = office_binary_path = uno_path = None
document_url = destination_format = source_format = infilter = refresh = None document_url = destination_format = source_format = infilter = refresh = None
......
...@@ -30,23 +30,10 @@ ...@@ -30,23 +30,10 @@
############################################################################## ##############################################################################
import sys import sys
try: import json
import json
except ImportError:
import simplejson as json
import helper_util import helper_util
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
# python3 support
try:
basestring
except NameError:
basestring = str
try:
long
except NameError:
long = int
__doc__ = """ __doc__ = """
...@@ -65,7 +52,7 @@ Options: ...@@ -65,7 +52,7 @@ Options:
""" """
class UnoMimemapper(object): class UnoMimemapper:
""" """ """ """
def __init__(self, hostname, port, uno_path=None, office_binary_path=None): def __init__(self, hostname, port, uno_path=None, office_binary_path=None):
...@@ -74,7 +61,7 @@ class UnoMimemapper(object): ...@@ -74,7 +61,7 @@ class UnoMimemapper(object):
uno_path, uno_path,
office_binary_path) office_binary_path)
def _getElementNameByService(self, uno_service, ignore_name_list=[]): def _getElementNameByService(self, uno_service, ignore_name_list):
"""Returns an dict with elements.""" """Returns an dict with elements."""
name_list = uno_service.getElementNames() name_list = uno_service.getElementNames()
service_dict = {} service_dict = {}
...@@ -84,7 +71,7 @@ class UnoMimemapper(object): ...@@ -84,7 +71,7 @@ class UnoMimemapper(object):
for obj in iter(element_list): for obj in iter(element_list):
if obj.Name in ignore_name_list: if obj.Name in ignore_name_list:
continue continue
if not isinstance(obj.Value, (bool, int, long, basestring, tuple)): if not isinstance(obj.Value, (bool, int, str, tuple)):
continue continue
element_dict[obj.Name] = obj.Value element_dict[obj.Name] = obj.Value
service_dict[name] = element_dict service_dict[name] = element_dict
......
...@@ -33,15 +33,14 @@ from re import findall ...@@ -33,15 +33,14 @@ from re import findall
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from subprocess import STDOUT from subprocess import STDOUT
from zope.interface import implementer from zope.interface import implementer
from filter import Filter from .filter import Filter
from os import environ, path from os import environ, path
from cloudooo.interfaces.mimemapper import IMimemapper from cloudooo.interfaces.mimemapper import IMimemapper
from types import InstanceType
import json import json
@implementer(IMimemapper) @implementer(IMimemapper)
class MimeMapper(object): class MimeMapper:
"""Load all filters from OOo. You can get the filter you want or all """Load all filters from OOo. You can get the filter you want or all
filters of the specific extension. filters of the specific extension.
""" """
...@@ -64,27 +63,10 @@ class MimeMapper(object): ...@@ -64,27 +63,10 @@ class MimeMapper(object):
def _typeToDocumentService(self, document_type): def _typeToDocumentService(self, document_type):
"""Returns the document service according to document type.""" """Returns the document service according to document type."""
for k, v in self._document_type_dict.iteritems(): for k, v in self._document_type_dict.items():
if k.startswith(document_type): if k.startswith(document_type):
return v return v
def _getElementNameByService(self, uno_service, ignore_name_list=[]):
"""Returns an dict with elements."""
name_list = uno_service.getElementNames()
service_dict = {}
for name in iter(name_list):
element_dict = {}
element_list = uno_service.getByName(name)
for obj in iter(element_list):
if obj.Name in ignore_name_list:
continue
elif type(obj.Value) == InstanceType:
continue
element_dict[obj.Name] = obj.Value
service_dict[name] = element_dict
return service_dict
def isLoaded(self): def isLoaded(self):
"""Verify if filters were loaded""" """Verify if filters were loaded"""
return self._loaded return self._loaded
...@@ -103,6 +85,7 @@ class MimeMapper(object): ...@@ -103,6 +85,7 @@ class MimeMapper(object):
alternative_extension_dict = { alternative_extension_dict = {
'Microsoft Excel 2007 XML':'ms.xlsx', 'Microsoft Excel 2007 XML':'ms.xlsx',
'Microsoft Excel 2007-2013 XML':'ms.xlsx', 'Microsoft Excel 2007-2013 XML':'ms.xlsx',
'Excel 2007–365':'ms.xlsx',
'Microsoft Excel 5.0':'5.xls', 'Microsoft Excel 5.0':'5.xls',
'Microsoft Excel 95':'95.xls', 'Microsoft Excel 95':'95.xls',
'Microsoft PowerPoint 2007 XML AutoPlay':'ms.ppsx', 'Microsoft PowerPoint 2007 XML AutoPlay':'ms.ppsx',
...@@ -111,8 +94,10 @@ class MimeMapper(object): ...@@ -111,8 +94,10 @@ class MimeMapper(object):
'Microsoft PowerPoint 2007-2013 XML':'ms.pptx', 'Microsoft PowerPoint 2007-2013 XML':'ms.pptx',
'Microsoft Word 2007 XML':'ms.docx', 'Microsoft Word 2007 XML':'ms.docx',
'Microsoft Word 2007-2013 XML':'ms.docx', 'Microsoft Word 2007-2013 XML':'ms.docx',
'Word 2007–365':'ms.docx',
'Microsoft Word 6.0':'6.doc', 'Microsoft Word 6.0':'6.doc',
'Microsoft Word 95':'95.doc', 'Microsoft Word 95':'95.doc',
'TIFF - Tagged Image File Format': 'tiff',
} }
uno_path = kw.get("uno_path", environ.get('uno_path')) uno_path = kw.get("uno_path", environ.get('uno_path'))
office_binary_path = kw.get("office_binary_path", office_binary_path = kw.get("office_binary_path",
...@@ -133,21 +118,24 @@ class MimeMapper(object): ...@@ -133,21 +118,24 @@ class MimeMapper(object):
filter_dict, type_dict = json.loads(stdout) filter_dict, type_dict = json.loads(stdout)
ooo_disable_filter_list = kw.get("ooo_disable_filter_list") or [] + [ ooo_disable_filter_list = kw.get("ooo_disable_filter_list") or [] + [
# 'writer_jpg_Export', # Seems not working from cloudooo in Libre Office 4.3.3.2 # https://bugs.documentfoundation.org/show_bug.cgi?id=117252
# 'writer_png_Export', # Seems not working from cloudooo in Libre Office 4.3.3.2 'writer_web_jpg_Export',
# 'draw_eps_Export', # Seems not working from cloudooo in Libre Office 5.0.0.5 'writer_web_png_Export',
# 'impress_eps_Export', # Seems not working from cloudooo in Libre Office 5.0.0.5 'writer_web_webp_Export',
] ]
ooo_disable_filter_name_list = kw.get("ooo_disable_filter_name_list") or [] + [ ooo_disable_filter_name_list = kw.get("ooo_disable_filter_name_list") or [] + [
'Text', # Use 'Text - Choose Encoding' instead 'Text', # Use 'Text - Choose Encoding' instead
'Text (StarWriter/Web)', # Use 'Text - Choose Encoding (Writer/Web)' instead 'Text (StarWriter/Web)', # Use 'Text - Choose Encoding (Writer/Web)' instead
'ODF Drawing (Impress)', # broken for presentation
] ]
for filter_name, value in filter_dict.iteritems(): for filter_name, value in filter_dict.items():
if filter_name in ooo_disable_filter_list: if filter_name in ooo_disable_filter_list:
continue continue
ui_name = value.get('UIName') ui_name = value.get('UIName')
filter_type = value.get('Type') filter_type = value.get('Type')
filter_type_dict = type_dict.get(filter_type) filter_type_dict = type_dict.get(filter_type)
if not filter_type_dict:
continue
if not ui_name: if not ui_name:
ui_name = filter_type_dict.get("UIName") ui_name = filter_type_dict.get("UIName")
if ui_name in ooo_disable_filter_name_list or 'Template' in ui_name: if ui_name in ooo_disable_filter_name_list or 'Template' in ui_name:
...@@ -229,7 +217,7 @@ class MimeMapper(object): ...@@ -229,7 +217,7 @@ class MimeMapper(object):
'pdf': ['com.sun.star.drawing.DrawingDocument'], 'pdf': ['com.sun.star.drawing.DrawingDocument'],
'xls': ['com.sun.star.sheet.SpreadsheetDocument'], 'xls': ['com.sun.star.sheet.SpreadsheetDocument'],
}) })
self.document_service_list = self._extension_list_by_type.keys() self.document_service_list = list(self._extension_list_by_type.keys())
self._loaded = True self._loaded = True
def getFilterName(self, extension, document_service): def getFilterName(self, extension, document_service):
...@@ -244,11 +232,11 @@ class MimeMapper(object): ...@@ -244,11 +232,11 @@ class MimeMapper(object):
filter_list = [filter for filter in self.getFilterList(extension) \ filter_list = [filter for filter in self.getFilterList(extension) \
if filter.getDocumentService() == document_service] if filter.getDocumentService() == document_service]
if len(filter_list) > 1: if len(filter_list) > 1:
for filter in iter(filter_list): for filter in filter_list:
if filter.isPreferred(): if filter.isPreferred():
return filter.getName() return filter.getName()
else: else:
for filter in iter(filter_list): for filter in filter_list:
if filter.getName().endswith("Export"): if filter.getName().endswith("Export"):
return filter.getName() return filter.getName()
filter_list.sort(key=lambda x: x.getSortIndex()) filter_list.sort(key=lambda x: x.getSortIndex())
...@@ -290,10 +278,10 @@ class MimeMapper(object): ...@@ -290,10 +278,10 @@ class MimeMapper(object):
allowed_extension_list.extend(self._extension_list_by_type.get(document_type, [])) allowed_extension_list.extend(self._extension_list_by_type.get(document_type, []))
# gets list of extensions of each document type if document_type_list isn't # gets list of extensions of each document type if document_type_list isn't
# empty. # empty.
for type in iter(document_type_list): for type in document_type_list:
# gets list of extensions with key document type # gets list of extensions with key document type
extension_list = self._extension_list_by_type.get(type) extension_list = self._extension_list_by_type.get(type)
for ext in iter(extension_list): for ext in extension_list:
if not ext in allowed_extension_list: if not ext in allowed_extension_list:
allowed_extension_list.append(ext) allowed_extension_list.append(ext)
return tuple(allowed_extension_list) return tuple(allowed_extension_list)
......
from request import MonitorRequest from .request import MonitorRequest
from memory import MonitorMemory from .memory import MonitorMemory
from sleeping_time import MonitorSpleepingTime from .sleeping_time import MonitorSpleepingTime
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.util import convertStringToBool from cloudooo.util import convertStringToBool
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
# #
############################################################################## ##############################################################################
from monitor import Monitor from .monitor import Monitor
from multiprocessing import Process from multiprocessing import Process
import psutil import psutil
from cloudooo.util import logger from cloudooo.util import logger
...@@ -54,7 +54,7 @@ class MonitorMemory(Monitor, Process): ...@@ -54,7 +54,7 @@ class MonitorMemory(Monitor, Process):
if not hasattr(self, 'process') or \ if not hasattr(self, 'process') or \
self.process.pid != int(self.openoffice.pid()): self.process.pid != int(self.openoffice.pid()):
self.create_process() self.create_process()
return self.process.memory_info().rss / (1024 * 1024) return self.process.memory_info().rss // (1024 * 1024)
except TypeError: except TypeError:
logger.debug("OpenOffice is stopped") logger.debug("OpenOffice is stopped")
return 0 return 0
......
...@@ -33,7 +33,7 @@ from cloudooo.interfaces.monitor import IMonitor ...@@ -33,7 +33,7 @@ from cloudooo.interfaces.monitor import IMonitor
@implementer(IMonitor) @implementer(IMonitor)
class Monitor(object): class Monitor:
""" """ """ """
def __init__(self, openoffice, interval): def __init__(self, openoffice, interval):
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
# #
############################################################################## ##############################################################################
from monitor import Monitor from .monitor import Monitor
from threading import Thread from threading import Thread
from cloudooo.util import logger from cloudooo.util import logger
from time import sleep from time import sleep
...@@ -55,8 +55,8 @@ class MonitorRequest(Monitor, Thread): ...@@ -55,8 +55,8 @@ class MonitorRequest(Monitor, Thread):
while self.status_flag: while self.status_flag:
if self.openoffice.request > self.request_limit: if self.openoffice.request > self.request_limit:
self.openoffice.acquire() self.openoffice.acquire()
logger.debug("Openoffice: %s, %s will be restarted" % \ logger.debug("Openoffice: %s, %s will be restarted",
self.openoffice.getAddress()) *self.openoffice.getAddress())
self.openoffice.restart() self.openoffice.restart()
self.openoffice.release() self.openoffice.release()
sleep(self.interval) sleep(self.interval)
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
# #
############################################################################## ##############################################################################
from monitor import Monitor from .monitor import Monitor
from threading import Thread from threading import Thread
from cloudooo.util import logger from cloudooo.util import logger
from time import sleep, time from time import sleep, time
...@@ -66,7 +66,7 @@ class MonitorSpleepingTime(Monitor, Thread): ...@@ -66,7 +66,7 @@ class MonitorSpleepingTime(Monitor, Thread):
current_time = time() current_time = time()
if self.openoffice.status() and\ if self.openoffice.status() and\
(self._touched_at + self.sleeping_time) <= current_time: (self._touched_at + self.sleeping_time) <= current_time:
logger.debug("Stopping OpenOffice after sleeping time of %is" %\ logger.debug("Stopping OpenOffice after sleeping time of %is",
self.sleeping_time) self.sleeping_time)
self.openoffice.acquire() self.openoffice.acquire()
self.openoffice.stop() self.openoffice.stop()
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
# #
############################################################################## ##############################################################################
from monitor import Monitor from .monitor import Monitor
from multiprocessing import Process from multiprocessing import Process
from time import sleep from time import sleep
from cloudooo.util import logger from cloudooo.util import logger
...@@ -47,11 +47,11 @@ class MonitorTimeout(Monitor, Process): ...@@ -47,11 +47,11 @@ class MonitorTimeout(Monitor, Process):
"""Start the process""" """Start the process"""
port = self.openoffice.getAddress()[-1] port = self.openoffice.getAddress()[-1]
pid = self.openoffice.pid() pid = self.openoffice.pid()
logger.debug("Monitoring OpenOffice: Port %s, Pid: %s" % (port, pid)) logger.debug("Monitoring OpenOffice: port %s pid %s timeout: %s", port, pid, self.interval)
self.status_flag = True self.status_flag = True
sleep(self.interval) sleep(self.interval)
if self.openoffice.isLocked(): if self.openoffice.isLocked():
logger.debug("Stop OpenOffice - Port %s - Pid %s" % (port, pid)) logger.debug("Stoping OpenOffice: port %s pid %s", port, pid)
self.openoffice.stop() self.openoffice.stop()
def terminate(self): def terminate(self):
......
...@@ -31,11 +31,11 @@ ...@@ -31,11 +31,11 @@
import sys import sys
from multiprocessing import Process from multiprocessing import Process
from os import listdir from os import listdir
from xmlrpclib import ServerProxy from xmlrpc.client import ServerProxy
from os.path import join from os.path import join
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from time import ctime, time from time import ctime, time
from base64 import encodestring from base64 import encodebytes
__doc__ = """ __doc__ = """
usage: python HighTestLoad.py [options] usage: python HighTestLoad.py [options]
...@@ -51,7 +51,7 @@ Options: ...@@ -51,7 +51,7 @@ Options:
""" """
class Log(object): class Log:
"""Object to manipulate the log file""" """Object to manipulate the log file"""
def __init__(self, log_path, mode='a'): def __init__(self, log_path, mode='a'):
...@@ -109,7 +109,7 @@ class Client(Process): ...@@ -109,7 +109,7 @@ class Client(Process):
folder_list = listdir(self.folder)[:self.number_of_request] folder_list = listdir(self.folder)[:self.number_of_request]
for filename in folder_list: for filename in folder_list:
file_path = join(self.folder, filename) file_path = join(self.folder, filename)
data = encodestring(open(file_path).read()) data = encodebytes(open(file_path).read())
self.log.msg("Input: %s\n" % file_path) self.log.msg("Input: %s\n" % file_path)
try: try:
now = time() now = time()
...@@ -118,7 +118,7 @@ class Client(Process): ...@@ -118,7 +118,7 @@ class Client(Process):
self.log.msg("Duration: %s\n" % response_duration) self.log.msg("Duration: %s\n" % response_duration)
time_list.append(response_duration) time_list.append(response_duration)
self.log.flush() self.log.flush()
except Exception, err: except Exception as err:
self.log.msg("%s\n" % str(err)) self.log.msg("%s\n" % str(err))
self.number_of_request -= 1 self.number_of_request -= 1
...@@ -132,15 +132,15 @@ def main(): ...@@ -132,15 +132,15 @@ def main():
help_msg = "\nUse --help or -h" help_msg = "\nUse --help or -h"
try: try:
opt_list, arg_list = getopt(sys.argv[1:], "hc:n:f:s:l:", ["help"]) opt_list, arg_list = getopt(sys.argv[1:], "hc:n:f:s:l:", ["help"])
except GetoptError, msg: except GetoptError as msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
print >> sys.stderr, msg print(msg, file=sys.stderr)
sys.exit(2) sys.exit(2)
kw = {} kw = {}
for opt, arg in opt_list: for opt, arg in opt_list:
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
print >> sys.stdout, __doc__ print(__doc__, file=sys.stdout)
sys.exit(2) sys.exit(2)
elif opt == '-c': elif opt == '-c':
number_client = int(arg) number_client = int(arg)
......
...@@ -31,10 +31,10 @@ ...@@ -31,10 +31,10 @@
import sys import sys
from multiprocessing import Process from multiprocessing import Process
from os import listdir from os import listdir
from xmlrpclib import ServerProxy from xmlrpc.client import ServerProxy
from os.path import join from os.path import join
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from base64 import encodestring from base64 import encodebytes
""" """
XXX - This file is duplicated and should be refactored to be used for all XXX - This file is duplicated and should be refactored to be used for all
...@@ -58,7 +58,7 @@ Options: ...@@ -58,7 +58,7 @@ Options:
""" """
class Log(object): class Log:
"""Object to manipulate the log file""" """Object to manipulate the log file"""
def __init__(self, log_path, mode='a'): def __init__(self, log_path, mode='a'):
...@@ -114,13 +114,13 @@ class Client(Process): ...@@ -114,13 +114,13 @@ class Client(Process):
folder_list = listdir(self.folder)[:self.number_of_request] folder_list = listdir(self.folder)[:self.number_of_request]
for filename in folder_list: for filename in folder_list:
file_path = join(self.folder, filename) file_path = join(self.folder, filename)
data = encodestring(open(file_path).read()) data = encodebytes(open(file_path).read())
try: try:
proxy.getTableItemList(data, 'odt') proxy.getTableItemList(data, 'odt')
proxy.getChapterItemList(data, 'odt') proxy.getChapterItemList(data, 'odt')
proxy.getImageItemList(data, 'odt') proxy.getImageItemList(data, 'odt')
self.log.msg("Success\n") self.log.msg("Success\n")
except Exception, err: except Exception as err:
self.log.msg("Error - %s\n"%str(err)) self.log.msg("Error - %s\n"%str(err))
self.number_of_request -= 1 self.number_of_request -= 1
self.log.close() self.log.close()
...@@ -131,15 +131,15 @@ def main(): ...@@ -131,15 +131,15 @@ def main():
help_msg = "\nUse --help or -h" help_msg = "\nUse --help or -h"
try: try:
opt_list, arg_list = getopt(sys.argv[1:], "hc:n:f:s:l:", ["help"]) opt_list, arg_list = getopt(sys.argv[1:], "hc:n:f:s:l:", ["help"])
except GetoptError, msg: except GetoptError as msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
print >> sys.stderr, msg print(msg, file=sys.stderr)
sys.exit(2) sys.exit(2)
kw = {} kw = {}
for opt, arg in opt_list: for opt, arg in opt_list:
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
print >> sys.stdout, __doc__ print(__doc__, file=sys.stdout)
sys.exit(2) sys.exit(2)
elif opt == '-c': elif opt == '-c':
number_client = int(arg) number_client = int(arg)
......
...@@ -62,5 +62,5 @@ class TestAllFormats(TestCase): ...@@ -62,5 +62,5 @@ class TestAllFormats(TestCase):
request = {'document_type': document_type} request = {'document_type': document_type}
extension_list = self.proxy.getAllowedExtensionList(request) extension_list = self.proxy.getAllowedExtensionList(request)
for extension in extension_list: for extension in extension_list:
with self.subTest(extension):
self._testConvertFile(input_url, source_format, extension[0], None) self._testConvertFile(input_url, source_format, extension[0], None)
...@@ -28,9 +28,10 @@ ...@@ -28,9 +28,10 @@
# #
############################################################################## ##############################################################################
from xmlrpclib import Fault from xmlrpc.client import Fault
from cloudooo.tests.cloudoooTestCase import TestCase from cloudooo.tests.cloudoooTestCase import TestCase
from base64 import encodestring from base64 import encodebytes
class TestAllFormatsERP5Compatibility(TestCase): class TestAllFormatsERP5Compatibility(TestCase):
""" """
...@@ -54,27 +55,26 @@ class TestAllFormatsERP5Compatibility(TestCase): ...@@ -54,27 +55,26 @@ class TestAllFormatsERP5Compatibility(TestCase):
"""Test all spreadsheet formats""" """Test all spreadsheet formats"""
self.runTestForType('data/test.ods', 'ods', 'application/vnd.oasis.opendocument.spreadsheet') self.runTestForType('data/test.ods', 'ods', 'application/vnd.oasis.opendocument.spreadsheet')
def runTestForType(self, filename, source_format, source_mimetype): def runTestForType(self, filename:str, source_format:str, source_mimetype:str) -> None:
"""Generic test""" """Generic test"""
extension_list = self.proxy.getAllowedTargetItemList(source_mimetype)[1]['response_data'] extension_list = self.proxy.getAllowedTargetItemList(source_mimetype)[1]['response_data']
fault_list = [] with open(filename, 'rb') as f:
for extension, format in extension_list: encoded_data = encodebytes(f.read()).decode()
try:
data_output = self.proxy.run_generate(filename, for extension, _ in extension_list:
encodestring( with self.subTest(extension):
open(filename).read()), _, data_output, fault = self.proxy.run_generate(filename,
encoded_data,
None, None,
extension, extension,
source_mimetype)[1] source_mimetype)
self.assertFalse(fault)
data_output = data_output['data'] data_output = data_output['data']
file_type = self._getFileType(data_output) file_type = self._getFileType(data_output)
if file_type.endswith(": empty"): self.assertNotIn(": empty", file_type)
fault_list.append((source_format, extension, file_type)) if source_format != extension:
except Fault as err: # when no filter exist for destination format, the document is not converted
fault_list.append((source_format, extension, err.faultString)) # but silently returned in source format, the assertion below should detect
if fault_list: # this.
template_message = 'input_format: %r\noutput_format: %r\n traceback:\n%s' self.assertNotEqual(source_mimetype, file_type)
message = '\n'.join([template_message % fault for fault in fault_list])
self.fail('Failed Conversions:\n' + message)
...@@ -30,8 +30,8 @@ ...@@ -30,8 +30,8 @@
import unittest import unittest
import magic import magic
from StringIO import StringIO from io import BytesIO
from base64 import decodestring from base64 import decodebytes
from os import path from os import path
from zipfile import ZipFile from zipfile import ZipFile
from cloudooo.handler.ooo.document import FileSystemDocument from cloudooo.handler.ooo.document import FileSystemDocument
...@@ -43,7 +43,7 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -43,7 +43,7 @@ class TestFileSystemDocument(unittest.TestCase):
def setUp(self): def setUp(self):
"""Create data to tests and instantiated a FileSystemDocument""" """Create data to tests and instantiated a FileSystemDocument"""
self.tmp_url = '/tmp' self.tmp_url = '/tmp'
self.data = decodestring("cloudooo Test") self.data = decodebytes(b"cloudooo Test")
self.fsdocument = FileSystemDocument(self.tmp_url, self.data, 'txt') self.fsdocument = FileSystemDocument(self.tmp_url, self.data, 'txt')
def tearDown(self): def tearDown(self):
...@@ -58,7 +58,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -58,7 +58,8 @@ class TestFileSystemDocument(unittest.TestCase):
document_filename = "document" document_filename = "document"
document_test_url = path.join(self.fsdocument.directory_name, document_test_url = path.join(self.fsdocument.directory_name,
document_filename) document_filename)
open(document_test_url, 'wb').write(decodestring("Test Document")) with open(document_test_url, 'wb') as f:
f.write(decodebytes(b"Test Document"))
self.fsdocument.reload(document_test_url) self.fsdocument.reload(document_test_url)
self.assertEqual(path.exists(old_document_url), False) self.assertEqual(path.exists(old_document_url), False)
self.assertNotEqual(self.fsdocument.original_data, self.assertNotEqual(self.fsdocument.original_data,
...@@ -82,7 +83,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -82,7 +83,8 @@ class TestFileSystemDocument(unittest.TestCase):
def testLoadDocumentFile(self): def testLoadDocumentFile(self):
"""Test if the document is created correctly""" """Test if the document is created correctly"""
url = self.fsdocument.getUrl() url = self.fsdocument.getUrl()
tmp_document = open(url, 'r').read() with open(url, 'rb') as f:
tmp_document = f.read()
self.assertEqual(self.data, tmp_document) self.assertEqual(self.data, tmp_document)
self.fsdocument.trash() self.fsdocument.trash()
self.assertEqual(path.exists(url), False) self.assertEqual(path.exists(url), False)
...@@ -93,7 +95,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -93,7 +95,8 @@ class TestFileSystemDocument(unittest.TestCase):
document_filename = "document" document_filename = "document"
document_test_url = path.join(self.fsdocument.directory_name, document_test_url = path.join(self.fsdocument.directory_name,
document_filename) document_filename)
open(document_test_url, 'wb').write(self.data) with open(document_test_url, 'wb') as f:
f.write(self.data)
self.fsdocument.reload(document_test_url) self.fsdocument.reload(document_test_url)
url = self.fsdocument.getUrl() url = self.fsdocument.getUrl()
self.assertEqual(path.exists(old_document_url), False) self.assertEqual(path.exists(old_document_url), False)
...@@ -103,12 +106,13 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -103,12 +106,13 @@ class TestFileSystemDocument(unittest.TestCase):
def testZipDocumentList(self): def testZipDocumentList(self):
"""Tests if the zip file is returned correctly""" """Tests if the zip file is returned correctly"""
open(path.join(self.fsdocument.directory_name, 'document2'), 'w').write('test') with open(path.join(self.fsdocument.directory_name, 'document2'), 'w') as f:
f.write('test')
zip_file = self.fsdocument.getContent(True) zip_file = self.fsdocument.getContent(True)
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
mimetype = mime.from_buffer(zip_file) mimetype = mime.from_buffer(zip_file)
self.assertEqual(mimetype, 'application/zip') self.assertEqual(mimetype, 'application/zip')
ziptest = ZipFile(StringIO(zip_file), 'r') ziptest = ZipFile(BytesIO(zip_file), 'r')
self.assertEqual(len(ziptest.filelist), 2) self.assertEqual(len(ziptest.filelist), 2)
for file in ziptest.filelist: for file in ziptest.filelist:
if file.filename.endswith("document2"): if file.filename.endswith("document2"):
...@@ -118,15 +122,15 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -118,15 +122,15 @@ class TestFileSystemDocument(unittest.TestCase):
def testSendZipFile(self): def testSendZipFile(self):
"""Tests if the htm is extrated from zipfile""" """Tests if the htm is extrated from zipfile"""
zip_input_url = 'data/test.zip' with open('./data/test.zip', 'rb') as f:
data = open(zip_input_url).read() data = f.read()
zipdocument = FileSystemDocument(self.tmp_url, data, 'zip') zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
mimetype = mime.from_buffer(zipdocument.getContent(True)) mimetype = mime.from_buffer(zipdocument.getContent(True))
self.assertEqual(mimetype, "application/zip") self.assertEqual(mimetype, "application/zip")
mimetype = mime.from_buffer(zipdocument.getContent()) mimetype = mime.from_buffer(zipdocument.getContent())
self.assertEqual(mimetype, "text/html") self.assertEqual(mimetype, "text/html")
zipfile = ZipFile(StringIO(zipdocument.getContent(True))) zipfile = ZipFile(BytesIO(zipdocument.getContent(True)))
self.assertEqual(sorted(zipfile.namelist()), self.assertEqual(sorted(zipfile.namelist()),
sorted(['logo.gif', 'test.htm'])) sorted(['logo.gif', 'test.htm']))
......
# -*- coding: utf-8 -*-
############################################################################## ##############################################################################
# #
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved. # Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
...@@ -30,7 +29,7 @@ ...@@ -30,7 +29,7 @@
############################################################################## ##############################################################################
from zipfile import ZipFile from zipfile import ZipFile
from StringIO import StringIO from io import BytesIO
from lxml import etree from lxml import etree
from cloudooo.tests.handlerTestCase import HandlerTestCase from cloudooo.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.granulator import OOGranulator from cloudooo.handler.ooo.granulator import OOGranulator
...@@ -39,7 +38,8 @@ from cloudooo.handler.ooo.granulator import OOGranulator ...@@ -39,7 +38,8 @@ from cloudooo.handler.ooo.granulator import OOGranulator
class TestOOGranulator(HandlerTestCase): class TestOOGranulator(HandlerTestCase):
def setUp(self): def setUp(self):
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
self.oogranulator = OOGranulator(data, 'odt') self.oogranulator = OOGranulator(data, 'odt')
def testOdfWithoutContentXml(self): def testOdfWithoutContentXml(self):
...@@ -56,7 +56,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -56,7 +56,8 @@ class TestOOGranulator(HandlerTestCase):
def testgetTableItemList(self): def testgetTableItemList(self):
"""Test if getTableItemList() returns the right tables list""" """Test if getTableItemList() returns the right tables list"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
table_list = [('Developers', ''), table_list = [('Developers', ''),
('Prices', 'Table 1: Prices table from Mon Restaurant'), ('Prices', 'Table 1: Prices table from Mon Restaurant'),
...@@ -65,10 +66,11 @@ class TestOOGranulator(HandlerTestCase): ...@@ -65,10 +66,11 @@ class TestOOGranulator(HandlerTestCase):
def testGetTable(self): def testGetTable(self):
"""Test if getTable() returns on odf file with the right table""" """Test if getTable() returns on odf file with the right table"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
table_data_doc = oogranulator.getTable('Developers') table_data_doc = oogranulator.getTable('Developers')
content_xml_str = ZipFile(StringIO(table_data_doc)).read('content.xml') content_xml_str = ZipFile(BytesIO(table_data_doc)).read('content.xml')
content_xml = etree.fromstring(content_xml_str) content_xml = etree.fromstring(content_xml_str)
table_list = content_xml.xpath('//table:table', table_list = content_xml.xpath('//table:table',
namespaces=content_xml.nsmap) namespaces=content_xml.nsmap)
...@@ -79,21 +81,24 @@ class TestOOGranulator(HandlerTestCase): ...@@ -79,21 +81,24 @@ class TestOOGranulator(HandlerTestCase):
def testGetTableItemWithoutSuccess(self): def testGetTableItemWithoutSuccess(self):
"""Test if getTable() returns None for an non existent table name""" """Test if getTable() returns None for an non existent table name"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
table_data = oogranulator.getTable('NonExistentTable') table_data = oogranulator.getTable('NonExistentTable')
self.assertEqual(table_data, None) self.assertEqual(table_data, None)
def testGetColumnItemList(self): def testGetColumnItemList(self):
"""Test if getColumnItemList() returns the right table columns list""" """Test if getColumnItemList() returns the right table columns list"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
self.assertEqual([[0, 'Name'], [1, 'Country']], self.assertEqual([[0, 'Name'], [1, 'Country']],
oogranulator.getColumnItemList('SoccerTeams')) oogranulator.getColumnItemList('SoccerTeams'))
def testGetLineItemList(self): def testGetLineItemList(self):
"""Test if getLineItemList() returns the right table lines list""" """Test if getLineItemList() returns the right table lines list"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
matrix = [['Name', 'Phone', 'Email'], matrix = [['Name', 'Phone', 'Email'],
['Hugo', '+55 (22) 8888-8888', 'hugomaia@tiolive.com'], ['Hugo', '+55 (22) 8888-8888', 'hugomaia@tiolive.com'],
...@@ -121,8 +126,9 @@ class TestOOGranulator(HandlerTestCase): ...@@ -121,8 +126,9 @@ class TestOOGranulator(HandlerTestCase):
def testGetImageSuccessfully(self): def testGetImageSuccessfully(self):
"""Test if getImage() returns the right image file successfully""" """Test if getImage() returns the right image file successfully"""
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
zip = ZipFile(StringIO(data)) data = f.read()
zip = ZipFile(BytesIO(data))
image_id = '10000000000000C80000009C38276C51.jpg' image_id = '10000000000000C80000009C38276C51.jpg'
original_image = zip.read('Pictures/%s' % image_id) original_image = zip.read('Pictures/%s' % image_id)
geted_image = self.oogranulator.getImage(image_id) geted_image = self.oogranulator.getImage(image_id)
...@@ -131,13 +137,14 @@ class TestOOGranulator(HandlerTestCase): ...@@ -131,13 +137,14 @@ class TestOOGranulator(HandlerTestCase):
def testGetImageWithoutSuccess(self): def testGetImageWithoutSuccess(self):
"""Test if getImage() returns an empty string for not existent id""" """Test if getImage() returns an empty string for not existent id"""
obtained_image = self.oogranulator.getImage('anything.png') obtained_image = self.oogranulator.getImage('anything.png')
self.assertEqual('', obtained_image) self.assertEqual(b'', obtained_image)
def testGetParagraphItemList(self): def testGetParagraphItemList(self):
"""Test if getParagraphItemList() returns the right paragraphs list, with """Test if getParagraphItemList() returns the right paragraphs list, with
the ids always in the same order""" the ids always in the same order"""
for i in range(5): for _ in range(5):
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
paragraph_list = oogranulator.getParagraphItemList() paragraph_list = oogranulator.getParagraphItemList()
self.assertEqual((0, 'P3'), paragraph_list[0]) self.assertEqual((0, 'P3'), paragraph_list[0])
...@@ -153,8 +160,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -153,8 +160,8 @@ class TestOOGranulator(HandlerTestCase):
big_paragraph = self.oogranulator.getParagraph(5) big_paragraph = self.oogranulator.getParagraph(5)
self.assertEqual('P8', big_paragraph[1]) self.assertEqual('P8', big_paragraph[1])
self.assertTrue(big_paragraph[0].startswith(u'A prática cotidiana prova')) self.assertTrue(big_paragraph[0].startswith('A prática cotidiana prova'))
self.assertTrue(big_paragraph[0].endswith(u'corresponde às necessidades.')) self.assertTrue(big_paragraph[0].endswith('corresponde às necessidades.'))
def testGetParagraphItemWithoutSuccess(self): def testGetParagraphItemWithoutSuccess(self):
"""Test if getParagraphItem() returns None for not existent id""" """Test if getParagraphItem() returns None for not existent id"""
...@@ -162,7 +169,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -162,7 +169,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetChapterItemList(self): def testGetChapterItemList(self):
"""Test if getChapterItemList() returns the right chapters list""" """Test if getChapterItemList() returns the right chapters list"""
data = open('./data/granulate_chapters_test.odt').read() with open('./data/granulate_chapters_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
self.assertEqual([(0, 'Title 0'), (1, 'Title 1'), (2, 'Title 2'), self.assertEqual([(0, 'Title 0'), (1, 'Title 1'), (2, 'Title 2'),
(3, 'Title 3'), (4, 'Title 4'), (5, 'Title 5'), (3, 'Title 3'), (4, 'Title 4'), (5, 'Title 5'),
...@@ -172,7 +180,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -172,7 +180,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetChapterItem(self): def testGetChapterItem(self):
"""Test if getChapterItem() returns the right chapter""" """Test if getChapterItem() returns the right chapter"""
data = open("./data/granulate_chapters_test.odt").read() with open("./data/granulate_chapters_test.odt", 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
self.assertEqual(['Title 1', 1], oogranulator.getChapterItem(1)) self.assertEqual(['Title 1', 1], oogranulator.getChapterItem(1))
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
import magic import magic
from os import path from os import path
from base64 import encodestring, decodestring from base64 import encodebytes, decodebytes
from cloudooo.tests.handlerTestCase import HandlerTestCase from cloudooo.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.handler import Handler from cloudooo.handler.ooo.handler import Handler
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
...@@ -48,9 +48,8 @@ class TestHandler(HandlerTestCase): ...@@ -48,9 +48,8 @@ class TestHandler(HandlerTestCase):
def _save_document(self, document_output_url, data): def _save_document(self, document_output_url, data):
"""Create document in file system""" """Create document in file system"""
new_file = open(document_output_url, "w") with open(document_output_url, "wb") as f:
new_file.write(data) f.write(data)
new_file.close()
self._file_path_list.append(document_output_url) self._file_path_list.append(document_output_url)
def _assert_document_output(self, document, expected_mimetype): def _assert_document_output(self, document, expected_mimetype):
...@@ -70,18 +69,20 @@ class TestHandler(HandlerTestCase): ...@@ -70,18 +69,20 @@ class TestHandler(HandlerTestCase):
def testConvertOdtToDoc(self): def testConvertOdtToDoc(self):
"""Test convert ODT to DOC""" """Test convert ODT to DOC"""
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
doc_exported = handler.convert("doc") doc_exported = handler.convert("doc")
self._assert_document_output(doc_exported, "application/msword") self._assert_document_output(doc_exported, "application/msword")
def testConvertDocToOdt(self): def testConvertDocToOdt(self):
"""Test convert DOC to ODT""" """Test convert DOC to ODT"""
data = encodestring(open("data/test.doc").read()) with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'doc') 'doc')
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
self._assert_document_output(doc_exported, self._assert_document_output(doc_exported,
...@@ -89,9 +90,10 @@ class TestHandler(HandlerTestCase): ...@@ -89,9 +90,10 @@ class TestHandler(HandlerTestCase):
def testGetMetadata(self): def testGetMetadata(self):
"""Test getMetadata""" """Test getMetadata"""
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(metadata.get('MIMEType'), self.assertEqual(metadata.get('MIMEType'),
...@@ -102,9 +104,10 @@ class TestHandler(HandlerTestCase): ...@@ -102,9 +104,10 @@ class TestHandler(HandlerTestCase):
def testSetMetadata(self): def testSetMetadata(self):
"""Test setMetadata""" """Test setMetadata"""
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
new_data = handler.setMetadata({"Title": "cloudooo Test -"}) new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = Handler(self.tmp_url, new_handler = Handler(self.tmp_url,
...@@ -113,7 +116,7 @@ class TestHandler(HandlerTestCase): ...@@ -113,7 +116,7 @@ class TestHandler(HandlerTestCase):
metadata = new_handler.getMetadata() metadata = new_handler.getMetadata()
self.assertEqual(metadata.get('Title'), "cloudooo Test -") self.assertEqual(metadata.get('Title'), "cloudooo Test -")
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
new_data = handler.setMetadata({"Title": "Namie's working record"}) new_data = handler.setMetadata({"Title": "Namie's working record"})
new_handler = Handler(self.tmp_url, new_handler = Handler(self.tmp_url,
...@@ -125,9 +128,10 @@ class TestHandler(HandlerTestCase): ...@@ -125,9 +128,10 @@ class TestHandler(HandlerTestCase):
def testConvertWithOpenOfficeStopped(self): def testConvertWithOpenOfficeStopped(self):
"""Test convert with openoffice stopped""" """Test convert with openoffice stopped"""
openoffice.stop() openoffice.stop()
data = encodestring(open("data/test.doc").read()) with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'doc') 'doc')
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
self._assert_document_output(doc_exported, self._assert_document_output(doc_exported,
...@@ -136,9 +140,10 @@ class TestHandler(HandlerTestCase): ...@@ -136,9 +140,10 @@ class TestHandler(HandlerTestCase):
def testGetMetadataWithOpenOfficeStopped(self): def testGetMetadataWithOpenOfficeStopped(self):
"""Test getMetadata with openoffice stopped""" """Test getMetadata with openoffice stopped"""
openoffice.stop() openoffice.stop()
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(metadata.get('Title'), 'title') self.assertEqual(metadata.get('Title'), 'title')
...@@ -148,9 +153,10 @@ class TestHandler(HandlerTestCase): ...@@ -148,9 +153,10 @@ class TestHandler(HandlerTestCase):
def testSetMetadataWithOpenOfficeStopped(self): def testSetMetadataWithOpenOfficeStopped(self):
"""Test setMetadata with openoffice stopped""" """Test setMetadata with openoffice stopped"""
openoffice.stop() openoffice.stop()
data = encodestring(open("data/test.doc").read()) with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'doc') 'doc')
new_data = handler.setMetadata({"Title": "cloudooo Test -"}) new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = Handler(self.tmp_url, new_handler = Handler(self.tmp_url,
...@@ -162,9 +168,10 @@ class TestHandler(HandlerTestCase): ...@@ -162,9 +168,10 @@ class TestHandler(HandlerTestCase):
def testRefreshOdt(self): def testRefreshOdt(self):
"""Test refresh argument""" """Test refresh argument"""
# Check when refreshing is disabled # Check when refreshing is disabled
data = encodestring(open("data/test_fields.odt").read()) with open("data/test_fields.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt', 'odt',
refresh=False) refresh=False)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
...@@ -176,9 +183,8 @@ class TestHandler(HandlerTestCase): ...@@ -176,9 +183,8 @@ class TestHandler(HandlerTestCase):
namespaces=content_tree.nsmap)) namespaces=content_tree.nsmap))
# Check when refreshing is enabled # Check when refreshing is enabled
data = encodestring(open("data/test_fields.odt").read())
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt', 'odt',
refresh=True) refresh=True)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
...@@ -194,15 +200,17 @@ class TestHandler(HandlerTestCase): ...@@ -194,15 +200,17 @@ class TestHandler(HandlerTestCase):
def get(*args, **kw): def get(*args, **kw):
return sorted(Handler.getAllowedConversionFormatList(*args, **kw)) return sorted(Handler.getAllowedConversionFormatList(*args, **kw))
text_plain_output_list = [ text_plain_output_list = [
('application/msword', 'Microsoft Word 97-2003'), ('application/epub+zip', 'EPUB Document'),
('application/msword', 'Word 97–2003'),
('application/pdf', 'PDF - Portable Document Format'), ('application/pdf', 'PDF - Portable Document Format'),
('application/rtf', 'Rich Text'), ('application/rtf', 'Rich Text'),
('application/vnd.ms-word.document.macroEnabled.12', 'Word 2007–365 VBA'),
('application/vnd.oasis.opendocument.text', 'ODF Text Document'), ('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'), ('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Microsoft Word 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text (Transitional)'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Word 2007–365'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('text/html', 'HTML Document (Writer)'), ('text/html', 'HTML Document (Writer)'),
('text/plain', 'Text - Choose Encoding')] ('text/plain', 'Text - Choose Encoding')]
self.assertEqual(get("text/plain;ignored=param"), text_plain_output_list) self.assertEqual(get("text/plain;ignored=param"), text_plain_output_list)
...@@ -213,15 +221,17 @@ class TestHandler(HandlerTestCase): ...@@ -213,15 +221,17 @@ class TestHandler(HandlerTestCase):
"""Test allowed conversion format for application/msword""" """Test allowed conversion format for application/msword"""
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("application/msword;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/msword;ignored=param")),
[ ('application/msword', 'Microsoft Word 97-2003'), [ ('application/epub+zip', 'EPUB Document'),
('application/msword', 'Word 97–2003'),
('application/pdf', 'PDF - Portable Document Format'), ('application/pdf', 'PDF - Portable Document Format'),
('application/rtf', 'Rich Text'), ('application/rtf', 'Rich Text'),
('application/vnd.ms-word.document.macroEnabled.12', 'Word 2007–365 VBA'),
('application/vnd.oasis.opendocument.text', 'ODF Text Document'), ('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'), ('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Microsoft Word 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text (Transitional)'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Word 2007–365'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('text/html', 'HTML Document (Writer)'), ('text/html', 'HTML Document (Writer)'),
('text/plain', 'Text - Choose Encoding') ]) ('text/plain', 'Text - Choose Encoding') ])
...@@ -232,12 +242,16 @@ class TestHandler(HandlerTestCase): ...@@ -232,12 +242,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -255,15 +269,17 @@ class TestHandler(HandlerTestCase): ...@@ -255,15 +269,17 @@ class TestHandler(HandlerTestCase):
): ):
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList(content_type)), [ sorted(Handler.getAllowedConversionFormatList(content_type)), [
('application/msword', 'Microsoft Word 97-2003'), ('application/epub+zip', 'EPUB Document'),
('application/msword', 'Word 97–2003'),
('application/pdf', 'PDF - Portable Document Format'), ('application/pdf', 'PDF - Portable Document Format'),
('application/rtf', 'Rich Text'), ('application/rtf', 'Rich Text'),
('application/vnd.ms-word.document.macroEnabled.12', 'Word 2007–365 VBA'),
('application/vnd.oasis.opendocument.text', 'ODF Text Document'), ('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'), ('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Microsoft Word 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text (Transitional)'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Word 2007–365'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('text/html', 'HTML Document (Writer)'), ('text/html', 'HTML Document (Writer)'),
('text/plain', 'Text - Choose Encoding'), ('text/plain', 'Text - Choose Encoding'),
]) ])
...@@ -272,15 +288,17 @@ class TestHandler(HandlerTestCase): ...@@ -272,15 +288,17 @@ class TestHandler(HandlerTestCase):
"""Test allowed conversion format for application/vnd.openxmlformats-officedocument.wordprocessingml.document""" """Test allowed conversion format for application/vnd.openxmlformats-officedocument.wordprocessingml.document"""
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.wordprocessingml.document;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.wordprocessingml.document;ignored=param")),
[ ('application/msword', 'Microsoft Word 97-2003'), [ ('application/epub+zip', 'EPUB Document'),
('application/msword', 'Word 97–2003'),
('application/pdf', 'PDF - Portable Document Format'), ('application/pdf', 'PDF - Portable Document Format'),
('application/rtf', 'Rich Text'), ('application/rtf', 'Rich Text'),
('application/vnd.ms-word.document.macroEnabled.12', 'Word 2007–365 VBA'),
('application/vnd.oasis.opendocument.text', 'ODF Text Document'), ('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'), ('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Microsoft Word 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text (Transitional)'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Word 2007–365'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('text/html', 'HTML Document (Writer)'), ('text/html', 'HTML Document (Writer)'),
('text/plain', 'Text - Choose Encoding') ]) ('text/plain', 'Text - Choose Encoding') ])
...@@ -291,12 +309,16 @@ class TestHandler(HandlerTestCase): ...@@ -291,12 +309,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -307,12 +329,16 @@ class TestHandler(HandlerTestCase): ...@@ -307,12 +329,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -320,23 +346,25 @@ class TestHandler(HandlerTestCase): ...@@ -320,23 +346,25 @@ class TestHandler(HandlerTestCase):
"""Test allowed conversion format for text/html""" """Test allowed conversion format for text/html"""
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("text/html;ignored=param")), sorted(Handler.getAllowedConversionFormatList("text/html;ignored=param")),
[ ('application/msword', 'Microsoft Word 97-2003'), [ ('application/epub+zip', 'EPUB Document'),
('application/msword', 'Word 97–2003'),
('application/pdf', 'PDF - Portable Document Format'), ('application/pdf', 'PDF - Portable Document Format'),
('application/rtf', 'Rich Text'), ('application/rtf', 'Rich Text'),
('application/vnd.ms-excel', 'Microsoft Excel 97-2003'), ('application/vnd.ms-excel', 'Excel 97–2003'),
('application/vnd.ms-excel.sheet.macroEnabled.12', 'Microsoft Excel 2007-2016 XML (macro enabled)'), ('application/vnd.ms-excel.sheet.macroEnabled.12', 'Excel 2007–365 (macro-enabled)'),
('application/vnd.ms-word.document.macroEnabled.12', 'Word 2007–365 VBA'),
('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'),
('application/vnd.oasis.opendocument.spreadsheet-flat-xml', u'Flat XML ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet-flat-xml', 'Flat XML ODF Spreadsheet'),
('application/vnd.oasis.opendocument.text', 'ODF Text Document'), ('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
('application/vnd.oasis.opendocument.text', 'Text (Writer/Web)'), ('application/vnd.oasis.opendocument.text', 'Text (Writer/Web)'),
('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'), ('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Microsoft Excel 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Excel 2007–365'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Microsoft Word 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text (Transitional)'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Word 2007–365'),
('application/vnd.sun.xml.writer', 'OpenOffice.org 1.0 Text Document (Writer/Web)'), ('application/vnd.sun.xml.writer', 'OpenOffice.org 1.0 Text Document (Writer/Web)'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('text/csv', 'Text CSV'), ('text/csv', 'Text CSV'),
('text/html', 'HTML Document'), ('text/html', 'HTML Document'),
('text/html', 'HTML Document (Calc)'), ('text/html', 'HTML Document (Calc)'),
...@@ -352,12 +380,16 @@ class TestHandler(HandlerTestCase): ...@@ -352,12 +380,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -368,12 +400,16 @@ class TestHandler(HandlerTestCase): ...@@ -368,12 +400,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -384,12 +420,16 @@ class TestHandler(HandlerTestCase): ...@@ -384,12 +420,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -397,17 +437,31 @@ class TestHandler(HandlerTestCase): ...@@ -397,17 +437,31 @@ class TestHandler(HandlerTestCase):
"""Test allowed conversion format for image/svg+xml""" """Test allowed conversion format for image/svg+xml"""
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("image/svg+xml;ignored=param")), sorted(Handler.getAllowedConversionFormatList("image/svg+xml;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.ms-powerpoint', 'PowerPoint 97–2003'),
('application/vnd.ms-powerpoint', 'PowerPoint 97–2003 AutoPlay'),
('application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'PowerPoint 2007–365 VBA'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'),
('application/vnd.oasis.opendocument.presentation-flat-xml', 'Flat XML ODF Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'PowerPoint 2007–365'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'PowerPoint 2007–365 AutoPlay'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)'),
('text/html', 'HTML Document (Impress)') ])
def testGetAllowedConversionFormatList_ImageTiff(self): def testGetAllowedConversionFormatList_ImageTiff(self):
"""Test allowed conversion format for image/tiff""" """Test allowed conversion format for image/tiff"""
...@@ -416,14 +470,18 @@ class TestHandler(HandlerTestCase): ...@@ -416,14 +470,18 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)')])
def testGetAllowedConversionFormatList_ImageXCmuRaster(self): def testGetAllowedConversionFormatList_ImageXCmuRaster(self):
"""Test allowed conversion format for image/x-cmu-raster""" """Test allowed conversion format for image/x-cmu-raster"""
...@@ -432,12 +490,16 @@ class TestHandler(HandlerTestCase): ...@@ -432,12 +490,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -448,12 +510,16 @@ class TestHandler(HandlerTestCase): ...@@ -448,12 +510,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -464,12 +530,16 @@ class TestHandler(HandlerTestCase): ...@@ -464,12 +530,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -480,12 +550,16 @@ class TestHandler(HandlerTestCase): ...@@ -480,12 +550,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -496,12 +570,16 @@ class TestHandler(HandlerTestCase): ...@@ -496,12 +570,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -512,12 +590,16 @@ class TestHandler(HandlerTestCase): ...@@ -512,12 +590,16 @@ class TestHandler(HandlerTestCase):
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'), ('application/vnd.oasis.opendocument.graphics', 'ODF Drawing'),
('application/vnd.oasis.opendocument.graphics-flat-xml', u'Flat XML ODF Drawing'), ('application/vnd.oasis.opendocument.graphics-flat-xml', 'Flat XML ODF Drawing'),
('application/x-ms-wmz', 'WMZ - Compressed Windows Metafile'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/svg+xml', 'SVGZ - Compressed Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Draw)') ]) ('text/html', 'HTML Document (Draw)') ])
...@@ -526,13 +608,14 @@ class TestHandler(HandlerTestCase): ...@@ -526,13 +608,14 @@ class TestHandler(HandlerTestCase):
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("application/vnd.ms-excel;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.ms-excel;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/vnd.ms-excel', 'Microsoft Excel 97-2003'), ('application/vnd.ms-excel', 'Excel 97–2003'),
('application/vnd.ms-excel.sheet.macroEnabled.12', 'Microsoft Excel 2007-2016 XML (macro enabled)'), ('application/vnd.ms-excel.sheet.macroEnabled.12', 'Excel 2007–365 (macro-enabled)'),
('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'),
('application/vnd.oasis.opendocument.spreadsheet-flat-xml', u'Flat XML ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet-flat-xml', 'Flat XML ODF Spreadsheet'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Microsoft Excel 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Excel 2007–365'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'),
('image/png', 'PNG - Portable Network Graphic'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphics'),
('text/csv', 'Text CSV'), ('text/csv', 'Text CSV'),
('text/html', 'HTML Document (Calc)') ]) ('text/html', 'HTML Document (Calc)') ])
...@@ -547,13 +630,14 @@ class TestHandler(HandlerTestCase): ...@@ -547,13 +630,14 @@ class TestHandler(HandlerTestCase):
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("application/vnd.oasis.opendocument.spreadsheet;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.oasis.opendocument.spreadsheet;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/vnd.ms-excel', 'Microsoft Excel 97-2003'), ('application/vnd.ms-excel', 'Excel 97–2003'),
('application/vnd.ms-excel.sheet.macroEnabled.12', 'Microsoft Excel 2007-2016 XML (macro enabled)'), ('application/vnd.ms-excel.sheet.macroEnabled.12', 'Excel 2007–365 (macro-enabled)'),
('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'),
('application/vnd.oasis.opendocument.spreadsheet-flat-xml', u'Flat XML ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet-flat-xml', 'Flat XML ODF Spreadsheet'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Microsoft Excel 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Excel 2007–365'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'),
('image/png', 'PNG - Portable Network Graphic'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphics'),
('text/csv', 'Text CSV'), ('text/csv', 'Text CSV'),
('text/html', 'HTML Document (Calc)') ]) ('text/html', 'HTML Document (Calc)') ])
...@@ -562,13 +646,14 @@ class TestHandler(HandlerTestCase): ...@@ -562,13 +646,14 @@ class TestHandler(HandlerTestCase):
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/vnd.ms-excel', 'Microsoft Excel 97-2003'), ('application/vnd.ms-excel', 'Excel 97–2003'),
('application/vnd.ms-excel.sheet.macroEnabled.12', 'Microsoft Excel 2007-2016 XML (macro enabled)'), ('application/vnd.ms-excel.sheet.macroEnabled.12', 'Excel 2007–365 (macro-enabled)'),
('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'),
('application/vnd.oasis.opendocument.spreadsheet-flat-xml', u'Flat XML ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet-flat-xml', 'Flat XML ODF Spreadsheet'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Microsoft Excel 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Excel 2007–365'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'),
('image/png', 'PNG - Portable Network Graphic'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphics'),
('text/csv', 'Text CSV'), ('text/csv', 'Text CSV'),
('text/html', 'HTML Document (Calc)') ]) ('text/html', 'HTML Document (Calc)') ])
...@@ -576,15 +661,17 @@ class TestHandler(HandlerTestCase): ...@@ -576,15 +661,17 @@ class TestHandler(HandlerTestCase):
"""Test allowed conversion format for application/vnd.sun.xml.writer""" """Test allowed conversion format for application/vnd.sun.xml.writer"""
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("application/vnd.sun.xml.writer;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.sun.xml.writer;ignored=param")),
[ ('application/msword', 'Microsoft Word 97-2003'), [ ('application/epub+zip', 'EPUB Document'),
('application/msword', 'Word 97–2003'),
('application/pdf', 'PDF - Portable Document Format'), ('application/pdf', 'PDF - Portable Document Format'),
('application/rtf', 'Rich Text'), ('application/rtf', 'Rich Text'),
('application/vnd.ms-word.document.macroEnabled.12', 'Word 2007–365 VBA'),
('application/vnd.oasis.opendocument.text', 'ODF Text Document'), ('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'), ('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Microsoft Word 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text (Transitional)'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Word 2007–365'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('text/html', 'HTML Document (Writer)'), ('text/html', 'HTML Document (Writer)'),
('text/plain', 'Text - Choose Encoding') ]) ('text/plain', 'Text - Choose Encoding') ])
...@@ -592,23 +679,25 @@ class TestHandler(HandlerTestCase): ...@@ -592,23 +679,25 @@ class TestHandler(HandlerTestCase):
"""Test allowed conversion format for text/csv""" """Test allowed conversion format for text/csv"""
self.assertEqual( self.assertEqual(
sorted(Handler.getAllowedConversionFormatList("text/csv;ignored=param")), sorted(Handler.getAllowedConversionFormatList("text/csv;ignored=param")),
[ ('application/msword', 'Microsoft Word 97-2003'), [ ('application/epub+zip', 'EPUB Document'),
('application/msword', 'Word 97–2003'),
('application/pdf', 'PDF - Portable Document Format'), ('application/pdf', 'PDF - Portable Document Format'),
('application/rtf', 'Rich Text'), ('application/rtf', 'Rich Text'),
('application/vnd.ms-excel', 'Microsoft Excel 97-2003'), ('application/vnd.ms-excel', 'Excel 97–2003'),
('application/vnd.ms-excel.sheet.macroEnabled.12', 'Microsoft Excel 2007-2016 XML (macro enabled)'), ('application/vnd.ms-excel.sheet.macroEnabled.12', 'Excel 2007–365 (macro-enabled)'),
('application/vnd.ms-word.document.macroEnabled.12', 'Word 2007–365 VBA'),
('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet', 'ODF Spreadsheet'),
('application/vnd.oasis.opendocument.spreadsheet-flat-xml', u'Flat XML ODF Spreadsheet'), ('application/vnd.oasis.opendocument.spreadsheet-flat-xml', 'Flat XML ODF Spreadsheet'),
('application/vnd.oasis.opendocument.text', 'ODF Text Document'), ('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
('application/vnd.oasis.opendocument.text', 'Text (Writer/Web)'), ('application/vnd.oasis.opendocument.text', 'Text (Writer/Web)'),
('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'), ('application/vnd.oasis.opendocument.text-flat-xml', 'Flat XML ODF Text Document'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Microsoft Excel 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Excel 2007–365'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Office Open XML Spreadsheet'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Microsoft Word 2007-2013 XML'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text (Transitional)'),
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Office Open XML Text'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Word 2007–365'),
('application/vnd.sun.xml.writer', 'OpenOffice.org 1.0 Text Document (Writer/Web)'), ('application/vnd.sun.xml.writer', 'OpenOffice.org 1.0 Text Document (Writer/Web)'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('text/csv', 'Text CSV'), ('text/csv', 'Text CSV'),
('text/html', 'HTML Document'), ('text/html', 'HTML Document'),
('text/html', 'HTML Document (Calc)'), ('text/html', 'HTML Document (Calc)'),
...@@ -623,20 +712,22 @@ class TestHandler(HandlerTestCase): ...@@ -623,20 +712,22 @@ class TestHandler(HandlerTestCase):
sorted(Handler.getAllowedConversionFormatList("application/vnd.oasis.opendocument.presentation;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.oasis.opendocument.presentation;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003 AutoPlay'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003 AutoPlay'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing (Impress)'), ('application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'PowerPoint 2007–365 VBA'),
('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'), ('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'),
('application/vnd.oasis.opendocument.presentation-flat-xml', u'Flat XML ODF Presentation'), ('application/vnd.oasis.opendocument.presentation-flat-xml', 'Flat XML ODF Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Microsoft PowerPoint 2007-2013 XML'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'), ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Microsoft PowerPoint 2007-2013 XML AutoPlay'), # TEST it ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'PowerPoint 2007–365'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'), ('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'PowerPoint 2007–365 AutoPlay'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Impress)') ]) ('text/html', 'HTML Document (Impress)') ])
...@@ -646,20 +737,22 @@ class TestHandler(HandlerTestCase): ...@@ -646,20 +737,22 @@ class TestHandler(HandlerTestCase):
sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.presentationml.presentation;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.presentationml.presentation;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003 AutoPlay'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003 AutoPlay'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing (Impress)'), ('application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'PowerPoint 2007–365 VBA'),
('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'), ('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'),
('application/vnd.oasis.opendocument.presentation-flat-xml', u'Flat XML ODF Presentation'), ('application/vnd.oasis.opendocument.presentation-flat-xml', 'Flat XML ODF Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Microsoft PowerPoint 2007-2013 XML'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'), ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Microsoft PowerPoint 2007-2013 XML AutoPlay'), ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'PowerPoint 2007–365'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'), ('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'PowerPoint 2007–365 AutoPlay'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Impress)') ]) ('text/html', 'HTML Document (Impress)') ])
...@@ -669,20 +762,22 @@ class TestHandler(HandlerTestCase): ...@@ -669,20 +762,22 @@ class TestHandler(HandlerTestCase):
sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.presentationml.slideshow;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.openxmlformats-officedocument.presentationml.slideshow;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003 AutoPlay'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003 AutoPlay'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing (Impress)'), ('application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'PowerPoint 2007–365 VBA'),
('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'), ('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'),
('application/vnd.oasis.opendocument.presentation-flat-xml', u'Flat XML ODF Presentation'), ('application/vnd.oasis.opendocument.presentation-flat-xml', 'Flat XML ODF Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Microsoft PowerPoint 2007-2013 XML'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'), ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Microsoft PowerPoint 2007-2013 XML AutoPlay'), ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'PowerPoint 2007–365'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'), ('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'PowerPoint 2007–365 AutoPlay'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Impress)') ]) ('text/html', 'HTML Document (Impress)') ])
...@@ -692,20 +787,21 @@ class TestHandler(HandlerTestCase): ...@@ -692,20 +787,21 @@ class TestHandler(HandlerTestCase):
sorted(Handler.getAllowedConversionFormatList("application/vnd.ms-powerpoint;ignored=param")), sorted(Handler.getAllowedConversionFormatList("application/vnd.ms-powerpoint;ignored=param")),
[ ('application/pdf', 'PDF - Portable Document Format'), [ ('application/pdf', 'PDF - Portable Document Format'),
('application/postscript', 'EPS - Encapsulated PostScript'), ('application/postscript', 'EPS - Encapsulated PostScript'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003'),
('application/vnd.ms-powerpoint', 'Microsoft PowerPoint 97-2003 AutoPlay'), ('application/vnd.ms-powerpoint', 'PowerPoint 97–2003 AutoPlay'),
('application/vnd.oasis.opendocument.graphics', 'ODF Drawing (Impress)'), ('application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'PowerPoint 2007–365 VBA'),
('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'), ('application/vnd.oasis.opendocument.presentation', 'ODF Presentation'),
('application/vnd.oasis.opendocument.presentation-flat-xml', u'Flat XML ODF Presentation'), ('application/vnd.oasis.opendocument.presentation-flat-xml', 'Flat XML ODF Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Microsoft PowerPoint 2007-2013 XML'),
('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'), ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Office Open XML Presentation'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Microsoft PowerPoint 2007-2013 XML AutoPlay'), ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'PowerPoint 2007–365'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'), ('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'Office Open XML Presentation AutoPlay'),
('application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'PowerPoint 2007–365 AutoPlay'),
('image/emf', 'EMF - Enhanced Metafile'),
('image/gif', 'GIF - Graphics Interchange Format'), ('image/gif', 'GIF - Graphics Interchange Format'),
('image/jpeg', 'JPEG - Joint Photographic Experts Group'), ('image/jpeg', 'JPEG - Joint Photographic Experts Group'),
('image/png', 'PNG - Portable Network Graphic'), ('image/png', 'PNG - Portable Network Graphics'),
('image/svg+xml', 'SVG - Scalable Vector Graphics'), ('image/svg+xml', 'SVG - Scalable Vector Graphics'),
('image/tiff', 'TIFF - Tagged Image File Format'), ('image/tiff', 'TIFF - Tagged Image File Format'),
('image/wmf', 'WMF - Windows Metafile'),
('image/x-ms-bmp', 'BMP - Windows Bitmap'), ('image/x-ms-bmp', 'BMP - Windows Bitmap'),
('text/html', 'HTML Document (Impress)') ]) ('text/html', 'HTML Document (Impress)') ])
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
# #
############################################################################## ##############################################################################
from base64 import encodestring, decodestring from base64 import encodebytes, decodebytes
from multiprocessing import Process, Array from multiprocessing import Process, Array
from cloudooo.tests.cloudoooTestCase import TestCase from cloudooo.tests.cloudoooTestCase import TestCase
import magic import magic
...@@ -39,8 +39,8 @@ mime_decoder = magic.Magic(mime=True) ...@@ -39,8 +39,8 @@ mime_decoder = magic.Magic(mime=True)
def basicTestToGenerate(id, proxy, data, source_format, destination_format, def basicTestToGenerate(id, proxy, data, source_format, destination_format,
result_list): result_list):
"""Test to use method generate of server""" """Test to use method generate of server"""
document = proxy.convertFile(encodestring(data), source_format, destination_format) document = proxy.convertFile(encodebytes(data), source_format, destination_format)
mimetype = mime_decoder.from_buffer(decodestring(document)) mimetype = mime_decoder.from_buffer(decodebytes(document))
assert mimetype == 'application/pdf' assert mimetype == 'application/pdf'
result_list[id] = True result_list[id] = True
......
...@@ -46,7 +46,7 @@ from cloudooo.interfaces.monitor import IMonitor ...@@ -46,7 +46,7 @@ from cloudooo.interfaces.monitor import IMonitor
from cloudooo.interfaces.granulate import ITableGranulator, \ from cloudooo.interfaces.granulate import ITableGranulator, \
IImageGranulator, \ IImageGranulator, \
ITextGranulator ITextGranulator
from cloudooo.tests.backportUnittest import TestCase, expectedFailure from unittest import TestCase, expectedFailure
import zope.interface.verify import zope.interface.verify
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
# #
############################################################################## ##############################################################################
from base64 import encodestring from base64 import encodebytes
from cloudooo.tests.cloudoooTestCase import TestCase from cloudooo.tests.cloudoooTestCase import TestCase
from pkg_resources import resource_filename from pkg_resources import resource_filename
...@@ -39,10 +39,11 @@ class TestLegacyInterface(TestCase): ...@@ -39,10 +39,11 @@ class TestLegacyInterface(TestCase):
"""Check implicit base conversion of HTML documents.""" """Check implicit base conversion of HTML documents."""
filename = resource_filename('cloudooo.handler.ooo.tests.data', filename = resource_filename('cloudooo.handler.ooo.tests.data',
'test_failure_conversion.html') 'test_failure_conversion.html')
data = open(filename, 'r').read() with open(filename, 'rb') as f:
data = f.read()
status, response_dict, message = self.proxy.run_convert( status, response_dict, message = self.proxy.run_convert(
filename, filename,
encodestring(data), encodebytes(data).decode(),
None, None,
None, None,
'text/html') 'text/html')
...@@ -55,9 +56,10 @@ class TestLegacyInterface(TestCase): ...@@ -55,9 +56,10 @@ class TestLegacyInterface(TestCase):
"""Check conversion of HTML to odt""" """Check conversion of HTML to odt"""
filename = resource_filename('cloudooo.handler.ooo.tests.data', filename = resource_filename('cloudooo.handler.ooo.tests.data',
'test_failure_conversion.html') 'test_failure_conversion.html')
data = open(filename, 'r').read() with open(filename, 'rb') as f:
data = f.read()
status, response_dict, message = self.proxy.run_generate(filename, status, response_dict, message = self.proxy.run_generate(filename,
encodestring(data), encodebytes(data).decode(),
None, None,
'odt', 'odt',
'text/html') 'text/html')
......
...@@ -32,19 +32,26 @@ from cloudooo.tests.handlerTestCase import HandlerTestCase ...@@ -32,19 +32,26 @@ from cloudooo.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.mimemapper import MimeMapper from cloudooo.handler.ooo.mimemapper import MimeMapper
# XXX depending on wether allowed targets are evaluated by mime or by
# extension/document_type, the returned mime types are different for text
text_expected_tuple = ( text_expected_tuple = (
('doc', 'Microsoft Word 97-2003'), ('doc', 'Word 97–2003'),
('ms.docx', 'Microsoft Word 2007-2013 XML'), ('docx', 'Office Open XML Text (Transitional)'),
('docx', 'Office Open XML Text'), ('epub', 'EPUB Document'),
('fodt', 'Flat XML ODF Text Document'), ('fodt', 'Flat XML ODF Text Document'),
('html', 'HTML Document (Writer)'), ('html', 'HTML Document (Writer)'),
('jpg', 'JPEG - Joint Photographic Experts Group'), ('jpg', 'JPEG - Joint Photographic Experts Group'),
('ms.docx', 'Word 2007–365'),
('odt', 'ODF Text Document'), ('odt', 'ODF Text Document'),
('pdf', 'PDF - Portable Document Format'), ('pdf', 'PDF - Portable Document Format'),
('png', 'PNG - Portable Network Graphic'), ('png', 'PNG - Portable Network Graphics'),
('rtf', 'Rich Text'), ('rtf', 'Rich Text'),
('txt', 'Text - Choose Encoding'), ('txt', 'Text - Choose Encoding'),
) )
extra_text_expected_tuple = (
('docm', 'Word 2007–365 VBA'),
('webp', 'WEBP - WebP Image'),
)
global_expected_tuple = ( global_expected_tuple = (
) )
...@@ -52,33 +59,27 @@ global_expected_tuple = ( ...@@ -52,33 +59,27 @@ global_expected_tuple = (
drawing_expected_tuple = ( drawing_expected_tuple = (
('bmp', 'BMP - Windows Bitmap'), ('bmp', 'BMP - Windows Bitmap'),
('emf', 'EMF - Enhanced Metafile'), ('emf', 'EMF - Enhanced Metafile'),
('emz', 'EMZ - Compressed Enhanced Metafile'),
('eps', 'EPS - Encapsulated PostScript'), ('eps', 'EPS - Encapsulated PostScript'),
('fodg', 'Flat XML ODF Drawing'), ('fodg', 'Flat XML ODF Drawing'),
('gif', 'GIF - Graphics Interchange Format'), ('gif', 'GIF - Graphics Interchange Format'),
('html', 'HTML Document (Draw)'), ('html', 'HTML Document (Draw)'),
('jpg', 'JPEG - Joint Photographic Experts Group'), ('jpg', 'JPEG - Joint Photographic Experts Group'),
('met', 'MET - OS/2 Metafile'),
('odg', 'ODF Drawing'), ('odg', 'ODF Drawing'),
('pbm', 'PBM - Portable Bitmap'),
('pct', 'PCT - Mac Pict'),
('pdf', 'PDF - Portable Document Format'), ('pdf', 'PDF - Portable Document Format'),
('pgm', 'PGM - Portable Graymap'), ('png', 'PNG - Portable Network Graphics'),
('png', 'PNG - Portable Network Graphic'),
('ppm', 'PPM - Portable Pixelmap'),
('ras', 'RAS - Sun Raster Image'),
('svg', 'SVG - Scalable Vector Graphics'), ('svg', 'SVG - Scalable Vector Graphics'),
('svm', 'SVM - StarView Metafile'), ('svgz', 'SVGZ - Compressed Scalable Vector Graphics'),
('tif', 'TIFF - Tagged Image File Format'), ('tiff', 'TIFF - Tagged Image File Format'),
('webp', 'WEBP - WebP Image'),
('wmf', 'WMF - Windows Metafile'), ('wmf', 'WMF - Windows Metafile'),
('xpm', 'XPM - X PixMap'), ('wmz', 'WMZ - Compressed Windows Metafile'),
) )
web_expected_tuple = ( web_expected_tuple = (
('html', 'HTML Document'), ('html', 'HTML Document'),
('jpg', 'JPEG - Joint Photographic Experts Group'),
('odt', 'Text (Writer/Web)'), ('odt', 'Text (Writer/Web)'),
('pdf', 'PDF - Portable Document Format'), ('pdf', 'PDF - Portable Document Format'),
('png', 'PNG - Portable Network Graphic'),
('sxw', 'OpenOffice.org 1.0 Text Document (Writer/Web)'), ('sxw', 'OpenOffice.org 1.0 Text Document (Writer/Web)'),
('txt', 'Text (Writer/Web)'), ('txt', 'Text (Writer/Web)'),
('txt', 'Text - Choose Encoding (Writer/Web)'), ('txt', 'Text - Choose Encoding (Writer/Web)'),
...@@ -92,40 +93,35 @@ presentation_expected_tuple = ( ...@@ -92,40 +93,35 @@ presentation_expected_tuple = (
('gif', 'GIF - Graphics Interchange Format'), ('gif', 'GIF - Graphics Interchange Format'),
('html', 'HTML Document (Impress)'), ('html', 'HTML Document (Impress)'),
('jpg', 'JPEG - Joint Photographic Experts Group'), ('jpg', 'JPEG - Joint Photographic Experts Group'),
('met', 'MET - OS/2 Metafile'),
('odg', 'ODF Drawing (Impress)'),
('odp', 'ODF Presentation'), ('odp', 'ODF Presentation'),
('pbm', 'PBM - Portable Bitmap'),
('pct', 'PCT - Mac Pict'),
('pdf', 'PDF - Portable Document Format'), ('pdf', 'PDF - Portable Document Format'),
('pgm', 'PGM - Portable Graymap'), ('png', 'PNG - Portable Network Graphics'),
('png', 'PNG - Portable Network Graphic'), ('pps', 'PowerPoint 97–2003 AutoPlay'),
('ppm', 'PPM - Portable Pixelmap'),
('pps', 'Microsoft PowerPoint 97-2003 AutoPlay'),
('ms.ppsx', 'Microsoft PowerPoint 2007-2013 XML AutoPlay'),
('ppsx', 'Office Open XML Presentation AutoPlay'), ('ppsx', 'Office Open XML Presentation AutoPlay'),
('ppt', 'Microsoft PowerPoint 97-2003'), ('ppsx', 'PowerPoint 2007–365 AutoPlay'),
('ms.pptx', 'Microsoft PowerPoint 2007-2013 XML'), ('ppt', 'PowerPoint 97–2003'),
('pptm', 'PowerPoint 2007–365 VBA'),
('pptx', 'Office Open XML Presentation'), ('pptx', 'Office Open XML Presentation'),
('ras', 'RAS - Sun Raster Image'), ('pptx', 'PowerPoint 2007–365'),
('svg', 'SVG - Scalable Vector Graphics'), ('svg', 'SVG - Scalable Vector Graphics'),
('svm', 'SVM - StarView Metafile'), ('tiff', 'TIFF - Tagged Image File Format'),
('tif', 'TIFF - Tagged Image File Format'), ('webp', 'WEBP - WebP Image'),
('wmf', 'WMF - Windows Metafile'), ('wmf', 'WMF - Windows Metafile'),
('xpm', 'XPM - X PixMap'),
) )
spreadsheet_expected_tuple = ( spreadsheet_expected_tuple = (
('csv', 'Text CSV'), ('csv', 'Text CSV'),
('fods', 'Flat XML ODF Spreadsheet'), ('fods', 'Flat XML ODF Spreadsheet'),
('html', 'HTML Document (Calc)'), ('html', 'HTML Document (Calc)'),
('jpg', 'JPEG - Joint Photographic Experts Group'),
('ods', 'ODF Spreadsheet'), ('ods', 'ODF Spreadsheet'),
('pdf', 'PDF - Portable Document Format'), ('pdf', 'PDF - Portable Document Format'),
('png', 'PNG - Portable Network Graphic'), ('png', 'PNG - Portable Network Graphics'),
('slk', 'SYLK'), ('slk', 'SYLK'),
('xls', 'Microsoft Excel 97-2003'), ('webp', 'WEBP - WebP Image'),
('xlsm', 'Microsoft Excel 2007-2016 XML (macro enabled)'), ('xls', 'Excel 97–2003'),
('ms.xlsx', 'Microsoft Excel 2007-2013 XML'), ('ms.xlsx', 'Excel 2007–365'),
('xlsm', 'Excel 2007–365 (macro-enabled)'),
('xlsx', 'Office Open XML Spreadsheet'), ('xlsx', 'Office Open XML Spreadsheet'),
) )
...@@ -138,9 +134,6 @@ math_expected_tuple = ( ...@@ -138,9 +134,6 @@ math_expected_tuple = (
('smf', 'StarMath 5.0'), ('smf', 'StarMath 5.0'),
) )
chart_expected_tuple = (
('odc', 'ODF Chart'),
)
OPENOFFICE = True OPENOFFICE = True
...@@ -166,17 +159,17 @@ class TestMimeMapper(HandlerTestCase): ...@@ -166,17 +159,17 @@ class TestMimeMapper(HandlerTestCase):
def testIfThereIsDuplicateData(self): def testIfThereIsDuplicateData(self):
"""Test if there is duplicate data.""" """Test if there is duplicate data."""
# XXX It can not exists multiple keys inside a dictionary # XXX It can not exists multiple keys inside a dictionary
extension_list = self.mimemapper._doc_type_list_by_extension.keys() extension_list = list(self.mimemapper._doc_type_list_by_extension.keys())
self.assertEqual(len(extension_list), len(set(extension_list))) self.assertEqual(len(extension_list), len(set(extension_list)))
for type_list in self.mimemapper._doc_type_list_by_extension.values(): for type_list in list(self.mimemapper._doc_type_list_by_extension.values()):
self.assertEqual(len(type_list), len(set(type_list))) self.assertEqual(len(type_list), len(set(type_list)))
document_type_list = self.mimemapper._document_type_dict.keys() document_type_list = list(self.mimemapper._document_type_dict.keys())
self.assertEqual(len(document_type_list), len(set(document_type_list))) self.assertEqual(len(document_type_list), len(set(document_type_list)))
document_service_list = self.mimemapper._document_type_dict.values() document_service_list = list(self.mimemapper._document_type_dict.values())
self.assertEqual(len(document_service_list), len(set(document_service_list))) self.assertEqual(len(document_service_list), len(set(document_service_list)))
document_service_list = self.mimemapper._extension_list_by_type.keys() document_service_list = list(self.mimemapper._extension_list_by_type.keys())
self.assertEqual(len(document_service_list), len(set(document_service_list))) self.assertEqual(len(document_service_list), len(set(document_service_list)))
extension_list = self.mimemapper._extension_list_by_type.values() extension_list = list(self.mimemapper._extension_list_by_type.values())
for extension in extension_list: for extension in extension_list:
self.assertEqual(len(extension), len(set(extension)), self.assertEqual(len(extension), len(set(extension)),
"extension_list_by_type has duplicate data") "extension_list_by_type has duplicate data")
...@@ -195,8 +188,6 @@ class TestMimeMapper(HandlerTestCase): ...@@ -195,8 +188,6 @@ class TestMimeMapper(HandlerTestCase):
document_type_dict = self.mimemapper._document_type_dict document_type_dict = self.mimemapper._document_type_dict
type = document_type_dict.get("text") type = document_type_dict.get("text")
self.assertEqual(type, 'com.sun.star.text.TextDocument') self.assertEqual(type, 'com.sun.star.text.TextDocument')
type = document_type_dict.get("chart")
self.assertEqual(type, 'com.sun.star.chart2.ChartDocument')
type = document_type_dict.get("drawing") type = document_type_dict.get("drawing")
self.assertEqual(type, 'com.sun.star.drawing.DrawingDocument') self.assertEqual(type, 'com.sun.star.drawing.DrawingDocument')
type = document_type_dict.get("presentation") type = document_type_dict.get("presentation")
...@@ -210,87 +201,68 @@ class TestMimeMapper(HandlerTestCase): ...@@ -210,87 +201,68 @@ class TestMimeMapper(HandlerTestCase):
"""Test if function getAllowedExtensionList returns correctly a list with """Test if function getAllowedExtensionList returns correctly a list with
extensions that can generate with extension passed.""" extensions that can generate with extension passed."""
doc_got_list = list(self.mimemapper.getAllowedExtensionList('doc')) doc_got_list = list(self.mimemapper.getAllowedExtensionList('doc'))
_text_expected_tuple = text_expected_tuple + extra_text_expected_tuple
for arg in doc_got_list: for arg in doc_got_list:
self.assertTrue(arg in text_expected_tuple, self.assertIn(arg, _text_expected_tuple)
"%s not in %s" % (arg, text_expected_tuple))
jpeg_got_list = list(self.mimemapper.getAllowedExtensionList('jpeg')) jpeg_got_list = list(self.mimemapper.getAllowedExtensionList('jpeg'))
jpeg_expected_list = list(set(presentation_expected_tuple + jpeg_expected_list = list(set(presentation_expected_tuple +
drawing_expected_tuple)) drawing_expected_tuple))
for arg in jpeg_got_list: for arg in jpeg_got_list:
self.assertTrue(arg in jpeg_expected_list, self.assertIn(arg, jpeg_expected_list)
"%s not in %s" % (arg, jpeg_expected_list))
pdf_got_list = list(self.mimemapper.getAllowedExtensionList('pdf')) pdf_got_list = list(self.mimemapper.getAllowedExtensionList('pdf'))
pdf_expected_list = list(set(presentation_expected_tuple + pdf_expected_list = list(set(presentation_expected_tuple +
drawing_expected_tuple + web_expected_tuple + global_expected_tuple + drawing_expected_tuple + web_expected_tuple + global_expected_tuple +
math_expected_tuple + text_expected_tuple + spreadsheet_expected_tuple)) math_expected_tuple + _text_expected_tuple + spreadsheet_expected_tuple))
for arg in pdf_got_list: for arg in pdf_got_list:
self.assertTrue(arg in pdf_expected_list, self.assertIn(arg, pdf_expected_list)
"%s not in %s" % (arg, pdf_expected_list))
def testGetAllowedExtensionListForText(self): def testGetAllowedExtensionListForText(self):
"""Passing document_type equal to 'text', the return must be equal """Passing document_type equal to 'text', the return must be equal
to text_expected_tuple.""" to text_expected_tuple."""
got_list = list(self.mimemapper.getAllowedExtensionList(document_type='text')) self.assertEqual(
text_expected_list = list(text_expected_tuple) sorted(self.mimemapper.getAllowedExtensionList(document_type='text')),
for arg in got_list: sorted(text_expected_tuple + extra_text_expected_tuple)
self.assertTrue(arg in text_expected_list, )
"%s not in %s" % (arg, text_expected_list))
def testGetAllowedExtensionListForGlobal(self): def testGetAllowedExtensionListForGlobal(self):
"""Passing document_type equal to 'global', the return must be equal """Passing document_type equal to 'global', the return must be equal
to global_expected_tuple.""" to global_expected_tuple."""
got_list = list(self.mimemapper.getAllowedExtensionList(document_type='global')) self.assertEqual(
got_list.sort() sorted(self.mimemapper.getAllowedExtensionList(document_type='global')),
global_expected_list = list(global_expected_tuple) sorted(global_expected_tuple)
global_expected_list.sort() )
self.assertEqual(got_list, global_expected_list)
def testGetAllAllowedExtensionListForDrawing(self): def testGetAllAllowedExtensionListForDrawing(self):
"""Passing document_type equal to 'drawing', the return must be equal """Passing document_type equal to 'drawing', the return must be equal
to drawing_expected_tuple.""" to drawing_expected_tuple."""
got_list = list(self.mimemapper.getAllowedExtensionList(document_type='drawing')) self.assertEqual(
drawing_expected_list = list(drawing_expected_tuple) sorted(self.mimemapper.getAllowedExtensionList(document_type='drawing')),
drawing_expected_list.sort() sorted(drawing_expected_tuple)
for arg in got_list: )
self.assertTrue(arg in drawing_expected_list,
"%s not in %s" % (arg, drawing_expected_list))
def testGetAllAllowedExtensionListForWeb(self): def testGetAllAllowedExtensionListForWeb(self):
"""Passing document_type equal to 'web', the return must be equal """Passing document_type equal to 'web', the return must be equal
to web_expected_tuple.""" to web_expected_tuple."""
got_tuple = list(self.mimemapper.getAllowedExtensionList(document_type='web')) self.assertEqual(
got_tuple.sort() sorted(self.mimemapper.getAllowedExtensionList(document_type='web')),
web_expected_list = list(web_expected_tuple) sorted(web_expected_tuple)
web_expected_list.sort() )
self.assertEqual(got_tuple, web_expected_list)
def testGetAllAllowedExtensionListForPresentation(self): def testGetAllAllowedExtensionListForPresentation(self):
"""Passing document_type equal to 'presentation', the return must be equal """Passing document_type equal to 'presentation', the return must be equal
to presentation_expected_tuple.""" to presentation_expected_tuple."""
got_list = \ self.assertEqual(
list(self.mimemapper.getAllowedExtensionList(document_type='presentation')) sorted(self.mimemapper.getAllowedExtensionList(document_type='presentation')),
presentation_expected_list = list(presentation_expected_tuple) sorted(presentation_expected_tuple)
presentation_expected_list.sort() )
for arg in got_list:
self.assertTrue(arg in presentation_expected_list,
"%s not in %s" % (arg, presentation_expected_list))
def testGetAllAllowedExtensionListForSpreadsheet(self): def testGetAllAllowedExtensionListForSpreadsheet(self):
"""Passing document_type equal to 'spreadsheet', the return must be equal """Passing document_type equal to 'spreadsheet', the return must be equal
to spreadsheet_expected_tuple.""" to spreadsheet_expected_tuple."""
got_list = self.mimemapper.getAllowedExtensionList(document_type='spreadsheet') self.assertEqual(
for arg in got_list: sorted(self.mimemapper.getAllowedExtensionList(document_type='spreadsheet')),
self.assertTrue(arg in spreadsheet_expected_tuple, sorted(spreadsheet_expected_tuple)
"%s not in %s" % (arg, spreadsheet_expected_tuple)) )
def testGetAllAllowedExtensionListForChart(self):
"""Passing document_type equal to 'chart', the return must be equal
to chart_expected_tuple."""
got_list = list(self.mimemapper.getAllowedExtensionList(document_type='chart'))
got_list.sort()
chart_expected_list = list(chart_expected_tuple)
chart_expected_list.sort()
self.assertEqual(got_list, chart_expected_list)
def testGetFilterName(self): def testGetFilterName(self):
"""Test if passing extension and document_type, the filter is correct.""" """Test if passing extension and document_type, the filter is correct."""
......
...@@ -33,7 +33,6 @@ from time import sleep ...@@ -33,7 +33,6 @@ from time import sleep
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.monitor.memory import MonitorMemory from cloudooo.handler.ooo.monitor.memory import MonitorMemory
from psutil import Process from psutil import Process
from types import IntType
OPENOFFICE = True OPENOFFICE = True
...@@ -69,11 +68,14 @@ class TestMonitorMemory(unittest.TestCase): ...@@ -69,11 +68,14 @@ class TestMonitorMemory(unittest.TestCase):
def testMonitorWithLittleMemoryLimit(self): def testMonitorWithLittleMemoryLimit(self):
"""Test the monitor with limit of 10Mb. In This case the openoffice will be """Test the monitor with limit of 10Mb. In This case the openoffice will be
stopped""" stopped after some time"""
try: try:
self.monitor = MonitorMemory(openoffice, 2, 10) self.monitor = MonitorMemory(openoffice, 1, 10)
self.monitor.start() self.monitor.start()
sleep(self.interval) for _ in range(30):
sleep(1)
if not openoffice.status():
break
self.assertEqual(openoffice.status(), False) self.assertEqual(openoffice.status(), False)
finally: finally:
self.monitor.terminate() self.monitor.terminate()
...@@ -94,17 +96,15 @@ class TestMonitorMemory(unittest.TestCase): ...@@ -94,17 +96,15 @@ class TestMonitorMemory(unittest.TestCase):
self.monitor = MonitorMemory(openoffice, 2, 400) self.monitor = MonitorMemory(openoffice, 2, 400)
self.monitor.create_process() self.monitor.create_process()
self.assertTrue(hasattr(self.monitor, 'process')) self.assertTrue(hasattr(self.monitor, 'process'))
self.assertEqual(type(self.monitor.process), Process) self.assertIsInstance(self.monitor.process, Process)
def testGetMemoryUsage(self): def testGetMemoryUsage(self):
"""Test memory usage""" """Test memory usage"""
self.monitor = MonitorMemory(openoffice, 2, 400) self.monitor = MonitorMemory(openoffice, 2, 400)
openoffice.stop() openoffice.stop()
memory_usage_int = self.monitor.get_memory_usage() memory_usage = self.monitor.get_memory_usage()
self.assertEqual(memory_usage_int, 0) self.assertEqual(memory_usage, 0)
if not openoffice.status(): if not openoffice.status():
openoffice.start() openoffice.start()
memory_usage_int = self.monitor.get_memory_usage() memory_usage = self.monitor.get_memory_usage()
self.assertEqual(type(memory_usage_int), IntType) self.assertIsInstance(memory_usage, int)
...@@ -61,12 +61,14 @@ class TestMonitorTimeout(unittest.TestCase): ...@@ -61,12 +61,14 @@ class TestMonitorTimeout(unittest.TestCase):
try: try:
monitor_timeout = self._createMonitor(1) monitor_timeout = self._createMonitor(1)
monitor_timeout.start() monitor_timeout.start()
sleep(2) sleep(5)
self.assertEqual(openoffice.status(), False) self.assertEqual(openoffice.status(), False)
openoffice.restart() openoffice.restart()
self.assertTrue(openoffice.status()) self.assertTrue(openoffice.status())
finally: finally:
try:
monitor_timeout.terminate() monitor_timeout.terminate()
finally:
openoffice.release() openoffice.release()
def testStopOpenOfficeTwice(self): def testStopOpenOfficeTwice(self):
...@@ -75,20 +77,22 @@ class TestMonitorTimeout(unittest.TestCase): ...@@ -75,20 +77,22 @@ class TestMonitorTimeout(unittest.TestCase):
try: try:
monitor_timeout = self._createMonitor(1) monitor_timeout = self._createMonitor(1)
monitor_timeout.start() monitor_timeout.start()
sleep(2) sleep(5)
self.assertEqual(openoffice.status(), False) self.assertEqual(openoffice.status(), False)
monitor_timeout.terminate() monitor_timeout.terminate()
openoffice.restart() openoffice.restart()
self.assertTrue(openoffice.status()) self.assertTrue(openoffice.status())
monitor_timeout = self._createMonitor(1) monitor_timeout = self._createMonitor(1)
monitor_timeout.start() monitor_timeout.start()
sleep(2) sleep(5)
self.assertEqual(openoffice.status(), False) self.assertEqual(openoffice.status(), False)
monitor_timeout.terminate() monitor_timeout.terminate()
sleep(1) sleep(1)
self.assertEqual(monitor_timeout.is_alive(), False) self.assertEqual(monitor_timeout.is_alive(), False)
finally: finally:
try:
monitor_timeout.terminate() monitor_timeout.terminate()
finally:
openoffice.release() openoffice.release()
...@@ -37,17 +37,18 @@ from cloudooo.handler.ooo.document import OdfDocument ...@@ -37,17 +37,18 @@ from cloudooo.handler.ooo.document import OdfDocument
class TestOdfDocument(HandlerTestCase): class TestOdfDocument(HandlerTestCase):
def setUp(self): def setUp(self):
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
self.oodocument = OdfDocument(data, 'odt') self.oodocument = OdfDocument(data, 'odt')
def testReceivedGoodFile(self): def testReceivedGoodFile(self):
"""Test if received path is from a good document returing an ZipFile""" """Test if received path is from a good document returing an ZipFile"""
self.assertTrue(isinstance(self.oodocument._zipfile, ZipFile)) self.assertIsInstance(self.oodocument._zipfile, ZipFile)
def testGetContentXml(self): def testGetContentXml(self):
"""Test if the getContentXml method returns the content.xml file""" """Test if the getContentXml method returns the content.xml file"""
content_xml = self.oodocument.getContentXml() content_xml = self.oodocument.getContentXml()
self.assertTrue('The content of this file is just' in content_xml) self.assertIn(b'The content of this file is just', content_xml)
def testGetExistentFile(self): def testGetExistentFile(self):
"""Test if the getFile method returns the requested file""" """Test if the getFile method returns the requested file"""
...@@ -57,12 +58,12 @@ class TestOdfDocument(HandlerTestCase): ...@@ -57,12 +58,12 @@ class TestOdfDocument(HandlerTestCase):
def testGetNotPresentFile(self): def testGetNotPresentFile(self):
"""Test if the getFile method returns None for not present file request""" """Test if the getFile method returns None for not present file request"""
requested_file = self.oodocument.getFile('not_present.xml') requested_file = self.oodocument.getFile('not_present.xml')
self.assertEqual(requested_file, '') self.assertEqual(requested_file, b'')
def testParseContent(self): def testParseContent(self):
"""Test if the _parsed_content attribute is the parsed content.xml""" """Test if the _parsed_content attribute is the parsed content.xml"""
# XXX not sure it is good to store parsed document everytime # XXX not sure it is good to store parsed document everytime
self.assertTrue(isinstance(self.oodocument.parsed_content, etree._Element)) self.assertIsInstance(self.oodocument.parsed_content, etree._Element)
self.assertTrue(self.oodocument.parsed_content.tag.endswith( self.assertTrue(self.oodocument.parsed_content.tag.endswith(
'document-content')) 'document-content'))
......
############################################################################## ##############################################################################
# coding: utf-8
# #
# Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved. # Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com> # Gabriel M. Monnerat <gabriel@tiolive.com>
...@@ -31,13 +30,12 @@ ...@@ -31,13 +30,12 @@
from os.path import join, exists from os.path import join, exists
from os import remove from os import remove
from base64 import encodestring, decodestring from base64 import encodebytes, decodebytes
from StringIO import StringIO from io import BytesIO
from lxml import etree from lxml import etree
from types import DictType
from zipfile import ZipFile, is_zipfile from zipfile import ZipFile, is_zipfile
from cloudooo.tests.cloudoooTestCase import TestCase from cloudooo.tests.cloudoooTestCase import TestCase
from cloudooo.tests.backportUnittest import expectedFailure from unittest import expectedFailure
import magic import magic
from cloudooo.handler.ooo.tests.testOooMimemapper import text_expected_tuple, presentation_expected_tuple from cloudooo.handler.ooo.tests.testOooMimemapper import text_expected_tuple, presentation_expected_tuple
...@@ -51,9 +49,14 @@ class TestAllowedExtensions(TestCase): ...@@ -51,9 +49,14 @@ class TestAllowedExtensions(TestCase):
ui_name. The request is by document type as text""" ui_name. The request is by document type as text"""
text_request = {'document_type': "text"} text_request = {'document_type': "text"}
text_allowed_list = self.proxy.getAllowedExtensionList(text_request) text_allowed_list = self.proxy.getAllowedExtensionList(text_request)
# XXX slightly different allowed formats with document_type !?
_text_expected_tuple = text_expected_tuple + (
('docm', 'Word 2007–365 VBA'),
('webp', 'WEBP - WebP Image'),
)
self.assertEqual( self.assertEqual(
sorted([tuple(x) for x in text_allowed_list]), sorted([tuple(x) for x in text_allowed_list]),
sorted(text_expected_tuple)) sorted(_text_expected_tuple))
def testGetAllowedPresentationExtensionListByType(self): def testGetAllowedPresentationExtensionListByType(self):
"""Verify if getAllowedExtensionList returns is a list with extension and """Verify if getAllowedExtensionList returns is a list with extension and
...@@ -61,14 +64,17 @@ class TestAllowedExtensions(TestCase): ...@@ -61,14 +64,17 @@ class TestAllowedExtensions(TestCase):
request_dict = {'document_type': "presentation"} request_dict = {'document_type': "presentation"}
presentation_allowed_list = self.proxy.getAllowedExtensionList(request_dict) presentation_allowed_list = self.proxy.getAllowedExtensionList(request_dict)
self.assertTrue(presentation_allowed_list) self.assertTrue(presentation_allowed_list)
for arg in presentation_allowed_list: self.assertEqual(
self.assertIn(tuple(arg), presentation_expected_tuple) sorted([tuple(x) for x in presentation_allowed_list]),
sorted(presentation_expected_tuple))
def testGetAllowedExtensionListByExtension(self): def testGetAllowedExtensionListByExtension(self):
"""Verify if getAllowedExtensionList returns is a list with extension and """Verify if getAllowedExtensionList returns is a list with extension and
ui_name. The request is by extension""" ui_name. The request is by extension"""
doc_allowed_list = self.proxy.getAllowedExtensionList({'extension': "doc"}) doc_allowed_list = self.proxy.getAllowedExtensionList({'extension': "doc"})
self.assertEqual(sorted([tuple(x) for x in doc_allowed_list]), sorted(text_expected_tuple)) self.assertEqual(
sorted([tuple(x) for x in doc_allowed_list]),
sorted(text_expected_tuple))
def testGetAllowedExtensionListByMimetype(self): def testGetAllowedExtensionListByMimetype(self):
"""Verify if getAllowedExtensionList returns is a list with extension and """Verify if getAllowedExtensionList returns is a list with extension and
...@@ -100,14 +106,17 @@ class TestConversion(TestCase): ...@@ -100,14 +106,17 @@ class TestConversion(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
# Try convert one document for a invalid format
(open(join('data', 'test.doc')).read(), 'doc', 'xyz'),
# Try convert one document to format not possible
(open(join('data', 'test.odp')).read(), 'odp', 'doc'),
] ]
# Try convert one document to an invalid format
with open(join('data', 'test.doc'), 'rb') as f:
scenario_list.append((f.read(), 'doc', 'xyz'))
# Try convert one video to format not possible
with open(join('data', 'test.odp'), 'rb') as f:
scenario_list.append((f.read(), 'odp', 'doc'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid OOofiles""" """Test fail convertion of Invalid OOofiles"""
...@@ -128,16 +137,20 @@ class TestConversion(TestCase): ...@@ -128,16 +137,20 @@ class TestConversion(TestCase):
]) ])
def ConvertScenarioList(self): def ConvertScenarioList(self):
return [ scenario_list = []
# Test run_convert method # Test run_convert method
('test.doc', open(join('data', 'test.doc')).read(), 200, '', with open(join('data', 'test.doc'), 'rb') as f:
scenario_list.append(
('test.doc', f.read(), 200, '',
['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text' ['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text'
), ))
# Test run_convert method with invalid file # Test run_convert method with invalid file
('test.doc', open(join('data', 'test.doc')).read()[:30], 200, '', with open(join('data', 'test.doc'), 'rb') as f:
scenario_list.append(
('test.doc', f.read()[:-300], 402, '',
['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text' ['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text'
), ))
] return scenario_list
def testRunConvertMethod(self): def testRunConvertMethod(self):
"""Test run_convert method""" """Test run_convert method"""
...@@ -193,8 +206,10 @@ class TestGetMetadata(TestCase): ...@@ -193,8 +206,10 @@ class TestGetMetadata(TestCase):
def testupdateFileMetadataUpdateSomeMetadata(self): def testupdateFileMetadataUpdateSomeMetadata(self):
"""Test server using method updateFileMetadata when the same metadata is """Test server using method updateFileMetadata when the same metadata is
updated""" updated"""
odf_data = self.proxy.updateFileMetadata(encodestring( with open(join('data', 'testMetadata.odt'), 'rb') as f:
open(join('data', 'testMetadata.odt')).read()), data = f.read()
odf_data = self.proxy.updateFileMetadata(
encodebytes(data).decode(),
'odt', 'odt',
dict(Reference="testSetMetadata", Something="ABC")) dict(Reference="testSetMetadata", Something="ABC"))
new_odf_data = self.proxy.updateFileMetadata(odf_data, new_odf_data = self.proxy.updateFileMetadata(odf_data,
...@@ -211,14 +226,15 @@ class TestGetMetadata(TestCase): ...@@ -211,14 +226,15 @@ class TestGetMetadata(TestCase):
# and the support to this kind of tests will be dropped. # and the support to this kind of tests will be dropped.
def testRunGenerateMethod(self): def testRunGenerateMethod(self):
"""Test run_generate method""" """Test run_generate method"""
data = open(join('data', 'test.odt'), 'r').read() with open(join('data', 'test.odt'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.odt', generate_result = self.proxy.run_generate('test.odt',
encodestring(data), encodebytes(data).decode(),
None, 'pdf', None, 'pdf',
'application/vnd.oasis.opendocument.text') 'application/vnd.oasis.opendocument.text')
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
self.assertEqual(type(response_dict), DictType) self.assertIsInstance(response_dict, dict)
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/pdf') self.assertEqual(response_dict['mime'], 'application/pdf')
...@@ -229,18 +245,20 @@ class TestGenerate(TestCase): ...@@ -229,18 +245,20 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdsToHTML(self): def testRunGenerateMethodConvertOdsToHTML(self):
"""Test run_generate method from ods to html. This test is to validate """Test run_generate method from ods to html. This test is to validate
a bug convertions to html""" a bug convertions to html"""
with open(join('data', 'test.ods'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.ods', generate_result = self.proxy.run_generate('test.ods',
encodestring( encodebytes(data).decode(),
open(join('data', 'test.ods')).read()),
None, 'html', None, 'html',
"application/vnd.oasis.opendocument.spreadsheet") "application/vnd.oasis.opendocument.spreadsheet")
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
self.assertEqual(type(response_dict), DictType) self.assertIsInstance(response_dict, dict)
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip') self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip") output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data'])) with open(output_url, 'wb') as f:
f.write(decodebytes(response_dict['data'].encode()))
self.assertTrue(is_zipfile(output_url)) self.assertTrue(is_zipfile(output_url))
filename_list = [file.filename for file in ZipFile(output_url).filelist] filename_list = [file.filename for file in ZipFile(output_url).filelist]
for filename in filename_list: for filename in filename_list:
...@@ -254,14 +272,15 @@ class TestGenerate(TestCase): ...@@ -254,14 +272,15 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdsToMsXslx(self): def testRunGenerateMethodConvertOdsToMsXslx(self):
"""Test run_generate method from ods to ms.xlsx. This test is to validate """Test run_generate method from ods to ms.xlsx. This test is to validate
a bug convertions to html""" a bug convertions to html"""
with open(join('data', 'test.ods'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.ods', generate_result = self.proxy.run_generate('test.ods',
encodestring( encodebytes(data).decode(),
open(join('data', 'test.ods')).read()),
None, 'ms.xlsx', None, 'ms.xlsx',
"application/vnd.oasis.opendocument.spreadsheet") "application/vnd.oasis.opendocument.spreadsheet")
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
self.assertEqual(type(response_dict), DictType) self.assertIsInstance(response_dict, dict)
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') self.assertEqual(response_dict['mime'], 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
...@@ -270,29 +289,29 @@ class TestGenerate(TestCase): ...@@ -270,29 +289,29 @@ class TestGenerate(TestCase):
def testPNGFileToConvertOdpToHTML(self): def testPNGFileToConvertOdpToHTML(self):
"""Test run_generate method from odp with png to html. """Test run_generate method from odp with png to html.
This test if returns good png files""" This test if returns good png files"""
with open(join('data', 'test_png.odp'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test_png.odp', generate_result = self.proxy.run_generate('test_png.odp',
encodestring( encodebytes(data).decode(),
open(join('data', 'test_png.odp')).read()),
None, 'html', None, 'html',
'application/vnd.oasis.opendocument.presentation') 'application/vnd.oasis.opendocument.presentation')
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
self.assertEqual(type(response_dict), DictType) self.assertIsInstance(response_dict, dict)
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip') self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip") output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data'])) with open(output_url, 'wb') as f:
f.write(decodebytes(response_dict['data'].encode()))
self.assertTrue(is_zipfile(output_url)) self.assertTrue(is_zipfile(output_url))
zipfile = ZipFile(output_url) with ZipFile(output_url) as zipfile:
try:
png_path = join(self.tmp_url, "img0.png") png_path = join(self.tmp_url, "img0.png")
zipfile.extractall(self.tmp_url) zipfile.extractall(self.tmp_url)
content_type = self._getFileType(encodestring(open(png_path).read())) with open(png_path, 'rb') as f:
content_type = self._getFileType(encodebytes(f.read()).decode())
self.assertEqual(content_type, 'image/png') self.assertEqual(content_type, 'image/png')
m = magic.Magic() m = magic.Magic()
self.assertTrue("8-bit/color RGB" in m.from_file(png_path)) self.assertIn("8-bit/color RGB", m.from_file(png_path))
finally:
zipfile.close()
if exists(output_url): if exists(output_url):
remove(output_url) remove(output_url)
...@@ -301,18 +320,20 @@ class TestGenerate(TestCase): ...@@ -301,18 +320,20 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdpToHTML(self): def testRunGenerateMethodConvertOdpToHTML(self):
"""Test run_generate method from odp to html. This test is to validate """Test run_generate method from odp to html. This test is to validate
a bug convertions to html""" a bug convertions to html"""
with open(join('data', 'test.odp'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.odp', generate_result = self.proxy.run_generate('test.odp',
encodestring( encodebytes(data).decode(),
open(join('data', 'test.odp')).read()),
None, 'html', None, 'html',
'application/vnd.oasis.opendocument.presentation') 'application/vnd.oasis.opendocument.presentation')
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
self.assertEqual(type(response_dict), DictType) self.assertIsInstance(response_dict, dict)
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip') self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip") output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data'])) with open(output_url, 'wb') as f:
f.write(decodebytes(response_dict['data'].encode()))
self.assertTrue(is_zipfile(output_url)) self.assertTrue(is_zipfile(output_url))
filename_list = [file.filename for file in ZipFile(output_url).filelist] filename_list = [file.filename for file in ZipFile(output_url).filelist]
for filename in filename_list: for filename in filename_list:
...@@ -329,13 +350,13 @@ class TestGenerate(TestCase): ...@@ -329,13 +350,13 @@ class TestGenerate(TestCase):
@expectedFailure @expectedFailure
def testRunGenerateMethodFailResponse(self): def testRunGenerateMethodFailResponse(self):
"""Test run_generate method with invalid document""" """Test run_generate method with invalid document"""
data = open(join('data', 'test.odt'), 'r').read()[:100] data = open(join('data', 'test.odt')).read()[:100]
generate_result = self.proxy.run_generate('test.odt', generate_result = self.proxy.run_generate('test.odt',
encodestring(data), encodebytes(data).decode(),
None, 'pdf', 'application/vnd.oasis.opendocument.text') None, 'pdf', 'application/vnd.oasis.opendocument.text')
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
self.assertEqual(response_code, 402) self.assertEqual(response_code, 402)
self.assertEqual(type(response_dict), DictType) self.assertIsInstance(response_dict, dict)
self.assertEqual(response_dict, {}) self.assertEqual(response_dict, {})
self.assertTrue(response_message.startswith('Traceback')) self.assertTrue(response_message.startswith('Traceback'))
...@@ -343,9 +364,10 @@ class TestGenerate(TestCase): ...@@ -343,9 +364,10 @@ class TestGenerate(TestCase):
class TestSetMetadata(TestCase): class TestSetMetadata(TestCase):
def testRunSetMetadata(self): def testRunSetMetadata(self):
"""Test run_setmetadata method, updating the same metadata""" """Test run_setmetadata method, updating the same metadata"""
with open(join('data', 'testMetadata.odt'), 'rb') as f:
data = f.read()
setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt', setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt',
encodestring( encodebytes(data).decode(),
open(join('data', 'testMetadata.odt')).read()),
{"Title": "testSetMetadata", "Description": "Music"}) {"Title": "testSetMetadata", "Description": "Music"})
response_code, response_dict, response_message = setmetadata_result response_code, response_dict, response_message = setmetadata_result
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
...@@ -372,9 +394,10 @@ class TestSetMetadata(TestCase): ...@@ -372,9 +394,10 @@ class TestSetMetadata(TestCase):
def testRunSetMetadataFailResponse(self): def testRunSetMetadataFailResponse(self):
"""Test run_setmetadata method with invalid document""" """Test run_setmetadata method with invalid document"""
with open(join('data', 'testMetadata.odt'), 'rb') as f:
data = f.read()[:100]
setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt', setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt',
encodestring( encodebytes(data).decode(),
open(join('data', 'testMetadata.odt')).read()[:100]),
{"Title": "testSetMetadata", "Description": "Music"}) {"Title": "testSetMetadata", "Description": "Music"})
response_code, response_dict, response_message = setmetadata_result response_code, response_dict, response_message = setmetadata_result
self.assertEqual(response_code, 402) self.assertEqual(response_code, 402)
...@@ -403,8 +426,10 @@ class TestGetTableItemList(TestCase): ...@@ -403,8 +426,10 @@ class TestGetTableItemList(TestCase):
table_list = [['Developers', ''], table_list = [['Developers', ''],
['Prices', 'Table 1: Prices table from Mon Restaurant'], ['Prices', 'Table 1: Prices table from Mon Restaurant'],
['SoccerTeams', 'Tabela 2: Soccer Teams']] ['SoccerTeams', 'Tabela 2: Soccer Teams']]
with open("data/granulate_table_test.odt", "rb") as f:
data = f.read()
granulated_table = self.proxy.getTableItemList( granulated_table = self.proxy.getTableItemList(
encodestring(open("data/granulate_table_test.odt").read()), encodebytes(data).decode(),
"odt") "odt")
self.assertEqual(table_list, granulated_table) self.assertEqual(table_list, granulated_table)
...@@ -413,18 +438,21 @@ class TestGetTableItemList(TestCase): ...@@ -413,18 +438,21 @@ class TestGetTableItemList(TestCase):
table_list = [['Table1', ''], table_list = [['Table1', ''],
['Table2', 'Table 1: Prices table from Mon Restaurant'], ['Table2', 'Table 1: Prices table from Mon Restaurant'],
['Table3', 'Tabela 2: Soccer Teams']] ['Table3', 'Tabela 2: Soccer Teams']]
with open("data/granulate_table_test.doc", "rb") as f:
data = f.read()
granulated_table = self.proxy.getTableItemList( granulated_table = self.proxy.getTableItemList(
encodestring(open("data/granulate_table_test.doc").read()), encodebytes(data).decode(),
"doc") "doc")
self.assertEqual(table_list, granulated_table) self.assertEqual(table_list, granulated_table)
def testGetTableFromOdt(self): def testGetTableFromOdt(self):
"""Test if getTableItemList can get a item of some granulated table from odt file""" """Test if getTableItemList can get a item of some granulated table from odt file"""
data = encodestring(open("./data/granulate_table_test.odt").read()) with open("./data/granulate_table_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
granulated_table = self.proxy.getTableItemList(data, "odt") granulated_table = self.proxy.getTableItemList(data, "odt")
table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0], table_item = decodebytes(self.proxy.getTable(data, granulated_table[1][0],
"odt")) "odt").encode())
content_xml_str = ZipFile(StringIO(table_item)).read('content.xml') content_xml_str = ZipFile(BytesIO(table_item)).read('content.xml')
content_xml = etree.fromstring(content_xml_str) content_xml = etree.fromstring(content_xml_str)
table_list = content_xml.xpath('//table:table', table_list = content_xml.xpath('//table:table',
namespaces=content_xml.nsmap) namespaces=content_xml.nsmap)
...@@ -435,12 +463,13 @@ class TestGetTableItemList(TestCase): ...@@ -435,12 +463,13 @@ class TestGetTableItemList(TestCase):
def testGetTableFromDoc(self): def testGetTableFromDoc(self):
"""Test if getTableItemList can get a item of some granulated table from doc file""" """Test if getTableItemList can get a item of some granulated table from doc file"""
data = encodestring(open("./data/granulate_table_test.doc").read()) with open("./data/granulate_table_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
granulated_table = self.proxy.getTableItemList(data, "doc") granulated_table = self.proxy.getTableItemList(data, "doc")
self.proxy.getTable(data, granulated_table[1][0], "doc") self.proxy.getTable(data, granulated_table[1][0], "doc")
table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0], table_item = decodebytes(self.proxy.getTable(data, granulated_table[1][0],
"doc")) "doc").encode())
content_xml_str = ZipFile(StringIO(table_item)).read('content.xml') content_xml_str = ZipFile(BytesIO(table_item)).read('content.xml')
content_xml = etree.fromstring(content_xml_str) content_xml = etree.fromstring(content_xml_str)
table_list = content_xml.xpath('//table:table', table_list = content_xml.xpath('//table:table',
namespaces=content_xml.nsmap) namespaces=content_xml.nsmap)
...@@ -451,24 +480,29 @@ class TestGetTableItemList(TestCase): ...@@ -451,24 +480,29 @@ class TestGetTableItemList(TestCase):
def testGetColumnItemListFromOdt(self): def testGetColumnItemListFromOdt(self):
"""Test if getColumnItemList can get the list of column item from odt file""" """Test if getColumnItemList can get the list of column item from odt file"""
with open("./data/granulate_table_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
columns = self.proxy.getColumnItemList( columns = self.proxy.getColumnItemList(
encodestring(open("./data/granulate_table_test.odt").read()), data,
"SoccerTeams", "SoccerTeams",
"odt") "odt")
self.assertEqual([[0, 'Name'], [1, 'Country']], columns) self.assertEqual([[0, 'Name'], [1, 'Country']], columns)
def testGetColumnItemListFromDoc(self): def testGetColumnItemListFromDoc(self):
"""Test if getColumnItemList can get the list of column item from doc file""" """Test if getColumnItemList can get the list of column item from doc file"""
with open("./data/granulate_table_test.doc", "rb") as f:
data = encodebytes(f.read()).decode()
#in the doc format the tables lose their names #in the doc format the tables lose their names
columns = self.proxy.getColumnItemList( columns = self.proxy.getColumnItemList(
encodestring(open("./data/granulate_table_test.doc").read()), data,
"Table3", "Table3",
"doc") "doc")
self.assertEqual([[0, 'Name'], [1, 'Country']], columns) self.assertEqual([[0, 'Name'], [1, 'Country']], columns)
def testGetLineItemListFromOdt(self): def testGetLineItemListFromOdt(self):
"""Test if getLineItemList can get the list of lines items from odt file""" """Test if getLineItemList can get the list of lines items from odt file"""
data = encodestring(open("./data/granulate_table_test.odt").read()) with open("./data/granulate_table_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
line_item_list = self.proxy.getLineItemList(data, "Developers", "odt") line_item_list = self.proxy.getLineItemList(data, "Developers", "odt")
self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'], self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'],
['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'], ['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'],
...@@ -477,7 +511,8 @@ class TestGetTableItemList(TestCase): ...@@ -477,7 +511,8 @@ class TestGetTableItemList(TestCase):
def testGetLineItemListFromDoc(self): def testGetLineItemListFromDoc(self):
"""Test if getLineItemList can get the list of lines items from doc file""" """Test if getLineItemList can get the list of lines items from doc file"""
data = encodestring(open("./data/granulate_table_test.doc").read()) with open("./data/granulate_table_test.doc", "rb") as f:
data = encodebytes(f.read()).decode()
line_item_list = self.proxy.getLineItemList(data, "Table1", "doc") line_item_list = self.proxy.getLineItemList(data, "Table1", "doc")
self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'], self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'],
['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'], ['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'],
...@@ -488,52 +523,61 @@ class TestGetTableItemList(TestCase): ...@@ -488,52 +523,61 @@ class TestGetTableItemList(TestCase):
class TestImagetItemList(TestCase): class TestImagetItemList(TestCase):
def testGetImageItemListFromOdt(self): def testGetImageItemListFromOdt(self):
"""Test if getImageItemList can get the list of images items from odt file""" """Test if getImageItemList can get the list of images items from odt file"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
image_list = self.proxy.getImageItemList(data, "odt") image_list = self.proxy.getImageItemList(data, "odt")
self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''], self.assertEqual(
['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'], [['10000000000000C80000009C38276C51.jpg', ''],
['10000201000000C80000004E85B3F70C71E07CE8.png', ''], ['10000201000000C80000004E7B947D46.png', 'TioLive Logo'],
['2000004F0000423300001370ADF6545B2997B448.svm', 'Python Logo'], ['10000201000000C80000004E7B947D46.png', ''],
['10000201000000C80000004E85B3F70C71E07CE8.png', 'Again TioLive Logo']], ['2000004F00004233000013707E7DE37A.svm', 'Python Logo'],
['10000201000000C80000004E7B947D46.png', 'Again TioLive Logo']],
image_list) image_list)
# the image filenames are the ones from the original document
with open("./data/granulate_test.odt", "rb") as f:
self.assertIn('Pictures/10000000000000C80000009C38276C51.jpg', ZipFile(f).namelist())
def testGetImageItemListFromDoc(self): def testGetImageItemListFromDoc(self):
"""Test if getImageItemList can get the list of images items from doc file""" """Test if getImageItemList can get the list of images items from doc file"""
data = encodestring(open("./data/granulate_test.doc").read()) with open("./data/granulate_test.doc", "rb") as f:
data = encodebytes(f.read()).decode()
image_list = self.proxy.getImageItemList(data, "doc") image_list = self.proxy.getImageItemList(data, "doc")
self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''], # the doc has been converted to odt, so the image ids are
['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'], # not really predictable (they seem to depend on Libreoffice version)
['10000201000000C80000004E85B3F70C71E07CE8.png', ''], self.assertIn('.jpg', image_list[0][0])
['2000031600004233000013702113A0E70B910778.wmf', 'Python Logo'], self.assertIn('TioLive Logo', [i[1] for i in image_list])
['10000201000000C80000004E85B3F70C71E07CE8.png', 'Again TioLive Logo']],
image_list)
def testGetImageFromOdt(self): def testGetImageFromOdt(self):
"""Test if getImage can get a image from odt file after zip""" """Test if getImage can get a image from odt file after zip"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
zip = ZipFile(StringIO(decodestring(data))) data = encodebytes(f.read()).decode()
image_id = '10000201000000C80000004E7B947D46.png' with ZipFile('./data/granulate_test.odt') as zip:
original_image = zip.read('Pictures/%s' % image_id) original_image = zip.read('Pictures/10000201000000C80000004E7B947D46.png')
geted_image = decodestring(self.proxy.getImage(data, image_id, "odt")) geted_image = decodebytes(self.proxy.getImage(data, '10000201000000C80000004E7B947D46.png', "odt").encode())
self.assertEqual(original_image, geted_image) self.assertEqual(original_image, geted_image)
def testGetImageFromDoc(self): def testGetImageFromDoc(self):
"""Test if getImage can get a image from doc file after zip""" """Test if getImage can get a image from doc file after zip"""
data = encodestring(open("./data/granulate_test.doc").read()) with open("./data/granulate_test.doc", "rb") as f:
data = encodebytes(f.read()).decode()
#This conversion is necessary to catch the image from the doc file; #This conversion is necessary to catch the image from the doc file;
#so compare with the server return. #so compare with the server return.
data_odt = self.proxy.convertFile(data, 'doc', 'odt', False) data_odt = self.proxy.convertFile(data, 'doc', 'odt', False)
zip = ZipFile(StringIO(decodestring(data_odt))) zip = ZipFile(BytesIO(decodebytes(data_odt.encode())))
image_id = '10000000000000C80000009CBF079A6E41EE290C.jpg' png_filename = [
original_image = zip.read('Pictures/%s' % image_id) name for name in zip.namelist()
geted_image = decodestring(self.proxy.getImage(data, image_id, "doc")) if name.startswith('Pictures/')
and name.endswith('.png')][0]
image_id = png_filename[len('Pictures/'):]
original_image = zip.read(png_filename)
geted_image = decodebytes(self.proxy.getImage(data, image_id, "doc").encode())
self.assertEqual(original_image, geted_image) self.assertEqual(original_image, geted_image)
class TestParagraphItemList(TestCase): class TestParagraphItemList(TestCase):
def testGetParagraphItemList(self): def testGetParagraphItemList(self):
"""Test if getParagraphItemList can get paragraphs correctly from document""" """Test if getParagraphItemList can get paragraphs correctly from document"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
paragraph_list = self.proxy.getParagraphItemList(data, "odt") paragraph_list = self.proxy.getParagraphItemList(data, "odt")
self.assertEqual([[0, 'P3'], [1, 'P1'], [2, 'P12'], [3, 'P6'], [4, 'P7'], self.assertEqual([[0, 'P3'], [1, 'P1'], [2, 'P12'], [3, 'P6'], [4, 'P7'],
[5, 'P8'], [6, 'P6'], [7, 'P6'], [8, 'P13'], [9, 'P9'], [5, 'P8'], [6, 'P6'], [7, 'P6'], [8, 'P13'], [9, 'P9'],
...@@ -545,7 +589,8 @@ class TestParagraphItemList(TestCase): ...@@ -545,7 +589,8 @@ class TestParagraphItemList(TestCase):
def testGetParagraphItem(self): def testGetParagraphItem(self):
"""Test if getParagraph can get a paragraph""" """Test if getParagraph can get a paragraph"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
paragraph = self.proxy.getParagraph(data, 1, "odt") paragraph = self.proxy.getParagraph(data, 1, "odt")
self.assertEqual(['', 'P1'], paragraph) self.assertEqual(['', 'P1'], paragraph)
...@@ -553,7 +598,8 @@ class TestParagraphItemList(TestCase): ...@@ -553,7 +598,8 @@ class TestParagraphItemList(TestCase):
class TestChapterItemList(TestCase): class TestChapterItemList(TestCase):
def testGetChapterItemList(self): def testGetChapterItemList(self):
"""Test if getChapterItemList can get the chapters list correctly from document""" """Test if getChapterItemList can get the chapters list correctly from document"""
data = encodestring(open("./data/granulate_chapters_test.odt").read()) with open("./data/granulate_chapters_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
chapter_list = self.proxy.getChapterItemList(data, "odt") chapter_list = self.proxy.getChapterItemList(data, "odt")
self.assertEqual([[0, 'Title 0'], [1, 'Title 1'], [2, 'Title 2'], self.assertEqual([[0, 'Title 0'], [1, 'Title 1'], [2, 'Title 2'],
[3, 'Title 3'], [4, 'Title 4'], [5, 'Title 5'], [3, 'Title 3'], [4, 'Title 4'], [5, 'Title 5'],
...@@ -562,7 +608,8 @@ class TestChapterItemList(TestCase): ...@@ -562,7 +608,8 @@ class TestChapterItemList(TestCase):
def testGetChapterItem(self): def testGetChapterItem(self):
"""Test if getChapterItem can get a chapter""" """Test if getChapterItem can get a chapter"""
data = encodestring(open("./data/granulate_chapters_test.odt").read()) with open("./data/granulate_chapters_test.odt", "rb") as f:
data = encodebytes(f.read()).decode()
chapter = self.proxy.getChapterItem(1, data, "odt") chapter = self.proxy.getChapterItem(1, data, "odt")
self.assertEqual(['Title 1', 1], chapter) self.assertEqual(['Title 1', 1], chapter)
...@@ -573,40 +620,44 @@ class TestCSVEncoding(TestCase): ...@@ -573,40 +620,44 @@ class TestCSVEncoding(TestCase):
* the fields delimiter is guessed by python csv module. * the fields delimiter is guessed by python csv module.
""" """
def test_decode_ascii(self): def test_decode_ascii(self):
data = encodestring(open("./data/csv_ascii.csv").read()) with open("./data/csv_ascii.csv", "rb") as f:
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) data = encodebytes(f.read()).decode()
converted = decodebytes(self.proxy.convertFile(data, "csv", "html").encode())
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(BytesIO(converted), parser)
self.assertEqual( self.assertEqual(
["test", "1234"], ["test", "1234"],
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
def test_decode_utf8(self): def test_decode_utf8(self):
data = encodestring(open("./data/csv_utf8.csv").read()) with open("./data/csv_utf8.csv", "rb") as f:
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) data = encodebytes(f.read()).decode()
converted = decodebytes(self.proxy.convertFile(data, "csv", "html").encode())
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(BytesIO(converted), parser)
self.assertEqual( self.assertEqual(
[u"Jérome", u"ジェローム"], ["Jérome", "ジェローム"],
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
self.assertEqual( self.assertEqual(
[u"नमस्ते", u"여보세요"], ["नमस्ते", "여보세요"],
[x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text])
def test_decode_latin9(self): def test_decode_latin9(self):
data = encodestring(open("./data/csv_latin9.csv").read()) with open("./data/csv_latin9.csv", "rb") as f:
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) data = encodebytes(f.read()).decode()
converted = decodebytes(self.proxy.convertFile(data, "csv", "html").encode())
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(BytesIO(converted), parser)
self.assertEqual( self.assertEqual(
[u"Jérome", u"1€"], ["Jérome", "1€"],
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
def test_separator_semicolon(self): def test_separator_semicolon(self):
data = encodestring(open("./data/csv_semicolon.csv").read()) with open("./data/csv_semicolon.csv", "rb") as f:
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) data = encodebytes(f.read()).decode()
converted = decodebytes(self.proxy.convertFile(data, "csv", "html").encode())
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(BytesIO(converted), parser)
self.assertEqual( self.assertEqual(
['a a', '1'], ['a a', '1'],
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
...@@ -615,10 +666,11 @@ class TestCSVEncoding(TestCase): ...@@ -615,10 +666,11 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text])
def test_separator_tab(self): def test_separator_tab(self):
data = encodestring(open("./data/tsv.tsv").read()) with open("./data/tsv.tsv", "rb") as f:
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) data = encodebytes(f.read()).decode()
converted = decodebytes(self.proxy.convertFile(data, "csv", "html").encode())
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(BytesIO(converted), parser)
self.assertEqual( self.assertEqual(
['a', 'b'], ['a', 'b'],
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
...@@ -627,10 +679,10 @@ class TestCSVEncoding(TestCase): ...@@ -627,10 +679,10 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text])
def test_empty_csv(self): def test_empty_csv(self):
data = encodestring("") data = ''
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) converted = decodebytes(self.proxy.convertFile(data, "csv", "html").encode())
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(BytesIO(converted), parser)
self.assertEqual( self.assertEqual(
[], [],
[x.text for x in tree.getroot().findall('.//td')]) [x.text for x in tree.getroot().findall('.//td')])
...@@ -50,7 +50,8 @@ class TestUnoConverter(HandlerTestCase): ...@@ -50,7 +50,8 @@ class TestUnoConverter(HandlerTestCase):
""" """ """ """
openoffice.acquire() openoffice.acquire()
self.hostname, self.port = openoffice.getAddress() self.hostname, self.port = openoffice.getAddress()
data = open("data/test.odt", 'r').read() with open("data/test.odt", "rb") as f:
data = f.read()
self.document = FileSystemDocument(self.tmp_url, data, 'odt') self.document = FileSystemDocument(self.tmp_url, data, 'odt')
def tearDown(self): def tearDown(self):
...@@ -78,6 +79,7 @@ class TestUnoConverter(HandlerTestCase): ...@@ -78,6 +79,7 @@ class TestUnoConverter(HandlerTestCase):
"--source_format=%s" % "odt", "--source_format=%s" % "odt",
"--mimemapper=%s" % mimemapper_pickled] "--mimemapper=%s" % mimemapper_pickled]
stdout, stderr = Popen(command, stdout, stderr = Popen(command,
text=True,
stdout=PIPE, stdout=PIPE,
stderr=PIPE).communicate() stderr=PIPE).communicate()
self.assertEqual(stderr, '') self.assertEqual(stderr, '')
......
...@@ -67,11 +67,10 @@ class TestUnoMimeMapper(HandlerTestCase): ...@@ -67,11 +67,10 @@ class TestUnoMimeMapper(HandlerTestCase):
"--port=%s" % self.openoffice_port] "--port=%s" % self.openoffice_port]
stdout, stderr = Popen(command, stdout, stderr = Popen(command,
stdout=PIPE, stdout=PIPE,
stderr=PIPE).communicate() stderr=PIPE,
text=True).communicate()
self.assertEqual(stderr, '') self.assertEqual(stderr, '')
filter_dict, type_dict = json.loads(stdout) filter_dict, type_dict = json.loads(stdout)
self.assertTrue('filter_dict' in locals())
self.assertTrue('type_dict' in locals())
self.assertNotEqual(filter_dict.get('writer8'), None) self.assertNotEqual(filter_dict.get('writer8'), None)
self.assertEqual(type_dict.get('writer8').get('Name'), 'writer8') self.assertEqual(type_dict.get('writer8').get('Name'), 'writer8')
self.assertNotEqual(filter_dict.get('writer8'), None) self.assertNotEqual(filter_dict.get('writer8'), None)
...@@ -88,11 +87,10 @@ class TestUnoMimeMapper(HandlerTestCase): ...@@ -88,11 +87,10 @@ class TestUnoMimeMapper(HandlerTestCase):
"--port=%s" % self.openoffice_port] "--port=%s" % self.openoffice_port]
stdout, stderr = Popen(command, stdout, stderr = Popen(command,
stdout=PIPE, stdout=PIPE,
stderr=PIPE).communicate() stderr=PIPE,
text=True).communicate()
self.assertEqual(stderr, '') self.assertEqual(stderr, '')
filter_dict, type_dict = json.loads(stdout) filter_dict, type_dict = json.loads(stdout)
self.assertTrue('filter_dict' in locals())
self.assertTrue('type_dict' in locals())
self.assertNotEqual(filter_dict.get('writer8'), None) self.assertNotEqual(filter_dict.get('writer8'), None)
self.assertEqual(type_dict.get('writer8').get('Name'), 'writer8') self.assertEqual(type_dict.get('writer8').get('Name'), 'writer8')
self.assertNotEqual(filter_dict.get('writer8'), None) self.assertNotEqual(filter_dict.get('writer8'), None)
...@@ -101,7 +99,7 @@ class TestUnoMimeMapper(HandlerTestCase): ...@@ -101,7 +99,7 @@ class TestUnoMimeMapper(HandlerTestCase):
def testWithoutOpenOffice(self): def testWithoutOpenOffice(self):
"""Test when the openoffice is stopped""" """Test when the openoffice is stopped"""
error_msg = "couldn\'t connect to socket (Success)\n" error_msg = "couldn\'t connect to socket"
hostname, host = openoffice.getAddress() hostname, host = openoffice.getAddress()
openoffice.stop() openoffice.stop()
python = path.join(self.office_binary_path, "python") python = path.join(self.office_binary_path, "python")
...@@ -117,9 +115,10 @@ class TestUnoMimeMapper(HandlerTestCase): ...@@ -117,9 +115,10 @@ class TestUnoMimeMapper(HandlerTestCase):
for i in range(10): for i in range(10):
stdout, stderr = Popen(command, stdout, stderr = Popen(command,
stdout=PIPE, stdout=PIPE,
stderr=PIPE).communicate() stderr=PIPE,
text=True).communicate()
self.assertEqual(stdout, '') self.assertEqual(stdout, '')
self.assertTrue(stderr.endswith(error_msg), stderr) self.assertIn(error_msg, stderr)
openoffice.start() openoffice.start()
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
# #
############################################################################## ##############################################################################
import contextlib
from socket import socket, error from socket import socket, error
from errno import EADDRINUSE from errno import EADDRINUSE
from time import sleep from time import sleep
...@@ -47,7 +48,8 @@ def removeDirectory(path): ...@@ -47,7 +48,8 @@ def removeDirectory(path):
def socketStatus(hostname, port): def socketStatus(hostname, port):
"""Verify if the address is busy.""" """Verify if the address is busy."""
try: try:
socket().bind((hostname, port),) with contextlib.closing(socket()) as sock:
sock.bind((hostname, port))
# False if the is free # False if the is free
return False return False
except error as err: except error as err:
...@@ -61,7 +63,7 @@ def waitStartDaemon(daemon, attempts): ...@@ -61,7 +63,7 @@ def waitStartDaemon(daemon, attempts):
for num in range(attempts): for num in range(attempts):
if daemon.status(): if daemon.status():
return True return True
elif daemon.pid() is None: elif daemon.hasExited():
return False return False
sleep(1) sleep(1)
return False return False
...@@ -70,7 +72,7 @@ def waitStartDaemon(daemon, attempts): ...@@ -70,7 +72,7 @@ def waitStartDaemon(daemon, attempts):
def waitStopDaemon(daemon, attempts=5): def waitStopDaemon(daemon, attempts=5):
"""Wait a certain time to stop the daemon.""" """Wait a certain time to stop the daemon."""
for num in range(attempts): for num in range(attempts):
if not daemon.status(): if not daemon.status() or daemon.hasExited():
return True return True
sleep(1) sleep(1)
return False return False
......
...@@ -36,11 +36,13 @@ from cloudooo.util import logger, parseContentType ...@@ -36,11 +36,13 @@ from cloudooo.util import logger, parseContentType
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from tempfile import mktemp from tempfile import mktemp
from pyPdf import PdfFileWriter, PdfFileReader
from pyPdf.generic import NameObject, createStringObject from pypdf import PdfWriter, PdfReader
from pypdf.generic import NameObject, createStringObject
@implementer(IHandler) @implementer(IHandler)
class Handler(object): class Handler:
"""PDF Handler is used to handler inputed pdf document.""" """PDF Handler is used to handler inputed pdf document."""
def __init__(self, base_folder_url, data, source_format, **kw): def __init__(self, base_folder_url, data, source_format, **kw):
...@@ -52,7 +54,7 @@ class Handler(object): ...@@ -52,7 +54,7 @@ class Handler(object):
def convert(self, destination_format=None, **kw): def convert(self, destination_format=None, **kw):
""" Convert a pdf document """ """ Convert a pdf document """
# TODO: use pyPdf # TODO: use pyPdf
logger.debug("PDFConvert: %s > %s" % (self.document.source_format, destination_format)) logger.debug("PDFConvert: %s > %s", self.document.source_format, destination_format)
output_url = mktemp(suffix=".%s" % destination_format, output_url = mktemp(suffix=".%s" % destination_format,
dir=self.document.directory_name) dir=self.document.directory_name)
command = ["pdftotext", self.document.getUrl(), output_url] command = ["pdftotext", self.document.getUrl(), output_url]
...@@ -77,8 +79,9 @@ class Handler(object): ...@@ -77,8 +79,9 @@ class Handler(object):
stdout=PIPE, stdout=PIPE,
stderr=PIPE, stderr=PIPE,
close_fds=True, close_fds=True,
text=True,
env=self.environment).communicate() env=self.environment).communicate()
info_list = filter(None, stdout.split("\n")) info_list = [_f for _f in stdout.split("\n") if _f]
metadata = {} metadata = {}
for info in iter(info_list): for info in iter(info_list):
info = info.split(":") info = info.split(":")
...@@ -94,8 +97,8 @@ class Handler(object): ...@@ -94,8 +97,8 @@ class Handler(object):
metadata -- expected an dictionary with metadata. metadata -- expected an dictionary with metadata.
""" """
# TODO: date as "D:20090401124817-04'00'" ASN.1 for ModDate and CreationDate # TODO: date as "D:20090401124817-04'00'" ASN.1 for ModDate and CreationDate
input_pdf = PdfFileReader(open(self.document.getUrl(), "rb")) input_pdf = PdfReader(self.document.getUrl())
output_pdf = PdfFileWriter() output_pdf = PdfWriter()
modification_date = metadata.pop("ModificationDate", None) modification_date = metadata.pop("ModificationDate", None)
if modification_date: if modification_date:
...@@ -103,13 +106,13 @@ class Handler(object): ...@@ -103,13 +106,13 @@ class Handler(object):
if type(metadata.get('Keywords', None)) is list: if type(metadata.get('Keywords', None)) is list:
metadata['Keywords'] = metadata['Keywords'].join(' ') metadata['Keywords'] = metadata['Keywords'].join(' ')
args = {} args = {}
for key, value in list(metadata.items()): for key, value in metadata.items():
args[NameObject('/' + key.capitalize())] = createStringObject(value) args[NameObject('/' + key.capitalize())] = createStringObject(value)
output_pdf._info.getObject().update(args) output_pdf._info.get_object().update(args)
for page_num in range(input_pdf.getNumPages()): for page in input_pdf.pages:
output_pdf.addPage(input_pdf.getPage(page_num)) output_pdf.add_page(page)
output_stream = io.BytesIO() output_stream = io.BytesIO()
output_pdf.write(output_stream) output_pdf.write(output_stream)
...@@ -124,7 +127,7 @@ class Handler(object): ...@@ -124,7 +127,7 @@ class Handler(object):
... ...
] ]
""" """
source_mimetype = parseContentType(source_mimetype).gettype() source_mimetype = parseContentType(source_mimetype).get_content_type()
if source_mimetype in ("application/pdf", "pdf"): if source_mimetype in ("application/pdf", "pdf"):
return [("text/plain", "Plain Text")] return [("text/plain", "Plain Text")]
return [] return []
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
from cloudooo.handler.pdf.handler import Handler from cloudooo.handler.pdf.handler import Handler
from cloudooo.tests.handlerTestCase import HandlerTestCase from cloudooo.tests.handlerTestCase import HandlerTestCase
from types import DictType
class TestHandler(HandlerTestCase): class TestHandler(HandlerTestCase):
...@@ -41,23 +40,26 @@ class TestHandler(HandlerTestCase): ...@@ -41,23 +40,26 @@ class TestHandler(HandlerTestCase):
def testConvertPDFtoText(self): def testConvertPDFtoText(self):
"""Test conversion of pdf to txt""" """Test conversion of pdf to txt"""
pdf_document = open("data/test.pdf").read() with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw) handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
txt_document = handler.convert("txt") txt_document = handler.convert("txt")
self.assertTrue(txt_document.startswith("UNG Docs Architecture")) self.assertTrue(txt_document.startswith(b"UNG Docs Architecture"))
def testgetMetadata(self): def testgetMetadata(self):
"""Test if the metadata are extracted correctly""" """Test if the metadata are extracted correctly"""
pdf_document = open("data/test.pdf").read() with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw) handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(type(metadata), DictType) self.assertIsInstance(metadata, dict)
self.assertNotEqual(metadata, {}) self.assertNotEqual(metadata, {})
self.assertEqual(metadata["title"], 'Free Cloud Alliance Presentation') self.assertEqual(metadata["title"], 'Free Cloud Alliance Presentation')
def testsetMetadata(self): def testsetMetadata(self):
"""Test if the metadata is inserted correctly""" """Test if the metadata is inserted correctly"""
pdf_document = open("data/test.pdf").read() with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw) handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
metadata_dict = {"title": "Set Metadata Test", "creator": "gabriel\'@"} metadata_dict = {"title": "Set Metadata Test", "creator": "gabriel\'@"}
new_document = handler.setMetadata(metadata_dict) new_document = handler.setMetadata(metadata_dict)
......
...@@ -45,14 +45,17 @@ class TestServer(TestCase): ...@@ -45,14 +45,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
]
# Try convert one video for a invalid format # Try convert one video for a invalid format
(open(join('data', 'test.pdf')).read(), 'pdf', 'xyz'), with open(join('data', 'test.pdf'), 'rb') as f:
scenario_list.append((f.read(), 'pdf', 'xyz'))
# Try convert one video to format not possible # Try convert one video to format not possible
(open(join('data', 'test.pdf')).read(), 'pdf', 'ogv'), with open(join('data', 'test.pdf'), 'rb') as f:
] scenario_list.append((f.read(), 'pdf', 'ogv'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid pdf files""" """Test fail convertion of Invalid pdf files"""
......
...@@ -40,7 +40,7 @@ def keyNameToOption(key_name, prefix=""): ...@@ -40,7 +40,7 @@ def keyNameToOption(key_name, prefix=""):
return "--" + prefix + key_name.replace("_", "-") return "--" + prefix + key_name.replace("_", "-")
@implementer(IHandler) @implementer(IHandler)
class Handler(object): class Handler:
"""ImageMagic Handler is used to handler images.""" """ImageMagic Handler is used to handler images."""
def __init__(self, base_folder_url, data, source_format, **kw): def __init__(self, base_folder_url, data, source_format, **kw):
...@@ -66,7 +66,7 @@ class Handler(object): ...@@ -66,7 +66,7 @@ class Handler(object):
def convert(self, destination_format=None, **kw): def convert(self, destination_format=None, **kw):
"""Convert a image""" """Convert a image"""
logger.debug("wkhtmltopdf convert: %s > %s" % (self.file.source_format, destination_format)) logger.debug("wkhtmltopdf convert: %s > %s", self.file.source_format, destination_format)
output_path = self.makeTempFile(destination_format) output_path = self.makeTempFile(destination_format)
command = self.makeWkhtmltopdfCommandList( command = self.makeWkhtmltopdfCommandList(
self.convertPathToUrl(self.file.getUrl()), self.convertPathToUrl(self.file.getUrl()),
...@@ -109,7 +109,7 @@ class Handler(object): ...@@ -109,7 +109,7 @@ class Handler(object):
... ...
] ]
""" """
source_mimetype = parseContentType(source_mimetype).gettype() source_mimetype = parseContentType(source_mimetype).get_content_type()
if source_mimetype in ("text/html", "htm", "html"): if source_mimetype in ("text/html", "htm", "html"):
return [("application/pdf", "PDF - Portable Document Format")] return [("application/pdf", "PDF - Portable Document Format")]
return [] return []
...@@ -272,10 +272,12 @@ class Handler(object): ...@@ -272,10 +272,12 @@ class Handler(object):
return option_list return option_list
def makeDataPathArgumentOptionList(self, *args, **kw): def makeDataPathArgumentOptionList(self, *args, **kw):
return self.makeDataUrlArgumentOptionList(*args, url_type="path", **kw) kw['url_type'] = "path"
return self.makeDataUrlArgumentOptionList(*args, **kw)
def makeDataFileArgumentOptionList(self, *args, **kw): def makeDataFileArgumentOptionList(self, *args, **kw):
return self.makeDataUrlArgumentOptionList(*args, url_type="file", **kw) kw['url_type'] = "file"
return self.makeDataUrlArgumentOptionList(*args, **kw)
def makeRepeatableDataUrlArgumentOptionList(self, allowed_option_list, def makeRepeatableDataUrlArgumentOptionList(self, allowed_option_list,
option_dict, **kw): option_dict, **kw):
......
...@@ -40,7 +40,8 @@ class TestHandler(HandlerTestCase): ...@@ -40,7 +40,8 @@ class TestHandler(HandlerTestCase):
self.kw = dict(env=dict(PATH=self.env_path)) self.kw = dict(env=dict(PATH=self.env_path))
def _testBase(self, html_path, **conversion_kw): def _testBase(self, html_path, **conversion_kw):
html_file = open(html_path).read() with open(html_path, 'rb') as f:
html_file = f.read()
handler = Handler(self.tmp_url, html_file, "html", **self.kw) handler = Handler(self.tmp_url, html_file, "html", **self.kw)
pdf_file = handler.convert("pdf", **conversion_kw) pdf_file = handler.convert("pdf", **conversion_kw)
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
...@@ -67,16 +68,18 @@ class TestHandler(HandlerTestCase): ...@@ -67,16 +68,18 @@ class TestHandler(HandlerTestCase):
def testConvertHtmlWithTableOfContent(self): def testConvertHtmlWithTableOfContent(self):
"""Test conversion of html with an additional table of content""" """Test conversion of html with an additional table of content"""
with open("data/test_toc.xsl", 'rb') as f:
xsl_style_sheet_data = f.read()
self._testBase( self._testBase(
"data/test_with_toc.html", "data/test_with_toc.html",
toc=True, toc=True,
xsl_style_sheet_data=b64encode(open("data/test_toc.xsl").read()), xsl_style_sheet_data=b64encode(xsl_style_sheet_data),
) )
# XXX how to check for table of content presence ? # XXX how to check for table of content presence ?
def testsetMetadata(self): def testsetMetadata(self):
""" Test if metadata are inserted correclty """ """ Test if metadata are inserted correclty """
handler = Handler(self.tmp_url, "", "png", **self.kw) handler = Handler(self.tmp_url, b"", "png", **self.kw)
self.assertRaises(NotImplementedError, handler.setMetadata) self.assertRaises(NotImplementedError, handler.setMetadata)
def testGetAllowedConversionFormatList(self): def testGetAllowedConversionFormatList(self):
......
...@@ -44,10 +44,11 @@ class TestServer(TestCase): ...@@ -44,10 +44,11 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
# Try convert one html for a invalid format
(open(join('data', 'test_with_png_dataurl.html')).read(), 'html', 'xyz'),
] ]
# Try convert one html for a invalid format
with open(join('data', 'test_with_png_dataurl.html'), 'rb') as f:
scenario_list.append((f.read(), 'html', 'xyz'))
return scenario_list
...@@ -157,7 +157,7 @@ yformat_tuple = ("docy", "xlsy", "ppty") ...@@ -157,7 +157,7 @@ yformat_tuple = ("docy", "xlsy", "ppty")
@implementer(IHandler) @implementer(IHandler)
class Handler(object): class Handler:
""" """
X2T Handler is used to convert Microsoft Office 2007 documents to OnlyOffice X2T Handler is used to convert Microsoft Office 2007 documents to OnlyOffice
documents. documents.
...@@ -183,7 +183,7 @@ class Handler(object): ...@@ -183,7 +183,7 @@ class Handler(object):
def convert(self, destination_format=None, **kw): def convert(self, destination_format=None, **kw):
""" Convert the inputed file to output as format that were informed """ """ Convert the inputed file to output as format that were informed """
source_format = self.file.source_format source_format = self.file.source_format
logger.debug("x2t convert: %s > %s" % (source_format, destination_format)) logger.debug("x2t convert: %s > %s", source_format, destination_format)
# init vars and xml configuration file # init vars and xml configuration file
in_format = format_code_map[source_format] in_format = format_code_map[source_format]
...@@ -198,7 +198,7 @@ class Handler(object): ...@@ -198,7 +198,7 @@ class Handler(object):
output_data = None output_data = None
if source_format in yformat_tuple: if source_format in yformat_tuple:
if self._data.startswith("PK\x03\x04"): if self._data.startswith(b"PK\x03\x04"):
os.mkdir(input_dir) os.mkdir(input_dir)
unzip(self.file.getUrl(), input_dir) unzip(self.file.getUrl(), input_dir)
input_file_name = os.path.join(input_dir, "body.txt") input_file_name = os.path.join(input_dir, "body.txt")
...@@ -227,7 +227,7 @@ class Handler(object): ...@@ -227,7 +227,7 @@ class Handler(object):
root = ElementTree.Element('root') root = ElementTree.Element('root')
for key, value in config.items(): for key, value in config.items():
ElementTree.SubElement(root, key).text = value ElementTree.SubElement(root, key).text = value
ElementTree.ElementTree(root).write(config_file, encoding='utf-8', xml_declaration=True, ElementTree.ElementTree(root).write(config_file, encoding='unicode', xml_declaration=True,
default_namespace=None, method="xml") default_namespace=None, method="xml")
# run convertion binary # run convertion binary
...@@ -280,7 +280,7 @@ class Handler(object): ...@@ -280,7 +280,7 @@ class Handler(object):
def getMetadata(self, base_document=False): def getMetadata(self, base_document=False):
r"""Returns a dictionary with all metadata of document. r"""Returns a dictionary with all metadata of document.
""" """
if self._source_format in yformat_tuple and self._data.startswith("PK\x03\x04"): if self._source_format in yformat_tuple and self._data.startswith(b"PK\x03\x04"):
if base_document: if base_document:
openxml_format = yformat_map[self._source_format] openxml_format = yformat_map[self._source_format]
data = self.convert(yformat_map[self._source_format]) data = self.convert(yformat_map[self._source_format])
...@@ -305,7 +305,7 @@ class Handler(object): ...@@ -305,7 +305,7 @@ class Handler(object):
""" """
if metadata is None: if metadata is None:
metadata = {} metadata = {}
if self._source_format in yformat_tuple and self._data.startswith("PK\x03\x04"): if self._source_format in yformat_tuple and self._data.startswith(b"PK\x03\x04"):
root_dir = self.file.directory_name root_dir = self.file.directory_name
output_file_name = os.path.join(root_dir, "tmp") output_file_name = os.path.join(root_dir, "tmp")
try: try:
...@@ -321,7 +321,8 @@ class Handler(object): ...@@ -321,7 +321,8 @@ class Handler(object):
absolute_path = os.path.join(root, file_name) absolute_path = os.path.join(root, file_name)
file_name = os.path.join(relative_root, file_name) file_name = os.path.join(relative_root, file_name)
zipfile.write(absolute_path, file_name) zipfile.write(absolute_path, file_name)
output_data = open(output_file_name).read() with open(output_file_name, 'rb') as f:
output_data = f.read()
finally: finally:
os.unlink(output_file_name) os.unlink(output_file_name)
return output_data return output_data
...@@ -329,7 +330,7 @@ class Handler(object): ...@@ -329,7 +330,7 @@ class Handler(object):
return OOoHandler(self.base_folder_url, self._data, self._source_format, **self._init_kw).setMetadata(metadata) return OOoHandler(self.base_folder_url, self._data, self._source_format, **self._init_kw).setMetadata(metadata)
@staticmethod @staticmethod
def getAllowedConversionFormatList(source_mimetype): def getAllowedConversionFormatList(source_mimetype:str):
"""Returns a list content_type and their titles which are supported """Returns a list content_type and their titles which are supported
by enabled handlers. by enabled handlers.
...@@ -337,7 +338,7 @@ class Handler(object): ...@@ -337,7 +338,7 @@ class Handler(object):
... ...
] ]
""" """
source_mimetype = parseContentType(source_mimetype).gettype() source_mimetype = parseContentType(source_mimetype).get_content_type()
if source_mimetype in ("docy", "application/x-asc-text"): if source_mimetype in ("docy", "application/x-asc-text"):
return [ return [
("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Word 2007 Document"), ("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Word 2007 Document"),
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
from zipfile import ZipFile from zipfile import ZipFile
from cStringIO import StringIO from io import BytesIO
from cloudooo.handler.x2t.handler import Handler from cloudooo.handler.x2t.handler import Handler
from cloudooo.tests.handlerTestCase import HandlerTestCase from cloudooo.tests.handlerTestCase import HandlerTestCase
...@@ -44,80 +44,94 @@ class TestHandler(HandlerTestCase): ...@@ -44,80 +44,94 @@ class TestHandler(HandlerTestCase):
def testConvertXlsx(self): def testConvertXlsx(self):
"""Test conversion of xlsx to xlsy and back""" """Test conversion of xlsx to xlsy and back"""
y_data = Handler(self.tmp_url, open("data/test.xlsx").read(), "xlsx", **self.kw).convert("xlsy") with open("data/test.xlsx", "rb") as f:
y_body_data = ZipFile(StringIO(y_data)).open("body.txt").read() data = f.read()
self.assertTrue(y_body_data.startswith("XLSY;v10;0;"), "%r... does not start with 'XLSY;v10;0;'" % (y_body_data[:20],)) y_data = Handler(self.tmp_url, data, "xlsx", **self.kw).convert("xlsy")
y_body_data = ZipFile(BytesIO(y_data)).open("body.txt").read()
self.assertTrue(y_body_data.startswith(b"XLSY;v10;0;"), "{!r}... does not start with 'XLSY;v10;0;'".format(y_body_data[:20]))
x_data = Handler(self.tmp_url, y_data, "xlsy", **self.kw).convert("xlsx") x_data = Handler(self.tmp_url, y_data, "xlsy", **self.kw).convert("xlsx")
# magic inspired by https://github.com/minad/mimemagic/pull/19/files # magic inspired by https://github.com/minad/mimemagic/pull/19/files
self.assertIn("xl/", x_data[:2000]) self.assertIn(b"xl/", x_data[:2000])
def testConvertXlsy(self): def testConvertXlsy(self):
"""Test conversion of xlsy to xlsx and back""" """Test conversion of xlsy to xlsx and back"""
x_data = Handler(self.tmp_url, open("data/test_body.xlsy").read(), "xlsy", **self.kw).convert("xlsx") with open("data/test_body.xlsy", "rb") as f:
self.assertIn("xl/", x_data[:2000]) data = f.read()
x_data = Handler(self.tmp_url, open("data/test.xlsy").read(), "xlsy", **self.kw).convert("xlsx") x_data = Handler(self.tmp_url, data, "xlsy", **self.kw).convert("xlsx")
self.assertIn("xl/", x_data[:2000]) self.assertIn(b"xl/", x_data[:2000])
with open("data/test.xlsy", "rb") as f:
data = f.read()
x_data = Handler(self.tmp_url, data, "xlsy", **self.kw).convert("xlsx")
self.assertIn(b"xl/", x_data[:2000])
y_data = Handler(self.tmp_url, x_data, "xlsx", **self.kw).convert("xlsy") y_data = Handler(self.tmp_url, x_data, "xlsx", **self.kw).convert("xlsy")
y_body_data = ZipFile(StringIO(y_data)).open("body.txt").read() y_body_data = ZipFile(BytesIO(y_data)).open("body.txt").read()
self.assertTrue(y_body_data.startswith("XLSY;v10;0;"), "%r... does not start with 'XLSY;v10;0;'" % (y_body_data[:20],)) self.assertTrue(y_body_data.startswith(b"XLSY;v10;0;"), "{!r}... does not start with 'XLSY;v10;0;'".format(y_body_data[:20]))
def testConvertDocx(self): def testConvertDocx(self):
"""Test conversion of docx to docy and back""" """Test conversion of docx to docy and back"""
y_data = Handler(self.tmp_url, open("data/test_with_image.docx").read(), "docx", **self.kw).convert("docy") with open("data/test_with_image.docx", "rb") as f:
y_zip = ZipFile(StringIO(y_data)) data = f.read()
y_data = Handler(self.tmp_url, data, "docx", **self.kw).convert("docy")
y_zip = ZipFile(BytesIO(y_data))
y_body_data = y_zip.open("body.txt").read() y_body_data = y_zip.open("body.txt").read()
self.assertTrue(y_body_data.startswith("DOCY;v10;0;"), "%r... does not start with 'DOCY;v10;0;'" % (y_body_data[:20],)) self.assertTrue(y_body_data.startswith(b"DOCY;v10;0;"), "{!r}... does not start with 'DOCY;v10;0;'".format(y_body_data[:20]))
y_zip.open("media/image1.png") y_zip.open("media/image1.png")
x_data = Handler(self.tmp_url, y_data, "docy", **self.kw).convert("docx") x_data = Handler(self.tmp_url, y_data, "docy", **self.kw).convert("docx")
# magic inspired by https://github.com/minad/mimemagic/pull/19/files # magic inspired by https://github.com/minad/mimemagic/pull/19/files
self.assertIn("word/", x_data[:2000]) self.assertIn(b"word/", x_data[:2000])
def testConvertDocy(self): def testConvertDocy(self):
"""Test conversion of docy to docx and back""" """Test conversion of docy to docx and back"""
x_data = Handler(self.tmp_url, open("data/test_with_image.docy").read(), "docy", **self.kw).convert("docx") with open("data/test_with_image.docy", "rb") as f:
self.assertIn("word/", x_data[:2000]) data = f.read()
x_data = Handler(self.tmp_url, data, "docy", **self.kw).convert("docx")
self.assertIn(b"word/", x_data[:2000])
y_data = Handler(self.tmp_url, x_data, "docx", **self.kw).convert("docy") y_data = Handler(self.tmp_url, x_data, "docx", **self.kw).convert("docy")
y_zip = ZipFile(StringIO(y_data)) y_zip = ZipFile(BytesIO(y_data))
y_body_data = y_zip.open("body.txt").read() y_body_data = y_zip.open("body.txt").read()
self.assertTrue(y_body_data.startswith("DOCY;v10;0;"), "%r... does not start with 'DOCY;v10;0;'" % (y_body_data[:20],)) self.assertTrue(y_body_data.startswith(b"DOCY;v10;0;"), "{!r}... does not start with 'DOCY;v10;0;'".format(y_body_data[:20]))
y_zip.open("media/image1.png") y_zip.open("media/image1.png")
def testgetMetadata(self): def testgetMetadata(self):
"""Test getMetadata from yformats""" """Test getMetadata from yformats"""
handler = Handler(self.tmp_url, "", "xlsy", **self.kw) handler = Handler(self.tmp_url, b"", "xlsy", **self.kw)
self.assertEqual(handler.getMetadata(), { self.assertEqual(handler.getMetadata(), {
u'CreationDate': u'00/00/0000 00:00:00', 'CreationDate': '00/00/0000 00:00:00',
u'ImplementationName': u'com.sun.star.comp.comphelper.OPropertyBag', 'ImplementationName': 'com.sun.star.comp.comphelper.OPropertyBag',
u'MIMEType': u'text/plain', 'MIMEType': 'text/plain',
u'ModificationDate': u'00/00/0000 00:00:00', 'ModificationDate': '00/00/0000 00:00:00',
u'PrintDate': u'00/00/0000 00:00:00', 'PrintDate': '00/00/0000 00:00:00',
u'TemplateDate': u'00/00/0000 00:00:00', 'TemplateDate': '00/00/0000 00:00:00',
}) })
handler = Handler(self.tmp_url, open("data/test_with_metadata.xlsy").read(), "xlsy", **self.kw) with open("data/test_with_metadata.xlsy", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, data, "xlsy", **self.kw)
self.assertEqual(handler.getMetadata(), { self.assertEqual(handler.getMetadata(), {
u'CreationDate': u'31/01/2018 21:09:10', 'CreationDate': '31/01/2018 21:09:10',
u'Keywords': [u'\u0442\u0435\u0441\u0442', u'\u0441\u0430\u0431\u0436\u0435\u043a\u0442'], 'Keywords': ['\u0442\u0435\u0441\u0442', '\u0441\u0430\u0431\u0436\u0435\u043a\u0442'],
'MIMEType': 'application/x-asc-spreadsheet', 'MIMEType': 'application/x-asc-spreadsheet',
u'ModificationDate': u'31/01/2018 21:22:36', 'ModificationDate': '31/01/2018 21:22:36',
u'PrintDate': u'00/00/0000 00:00:00', 'PrintDate': '00/00/0000 00:00:00',
u'Subject': u'\u0432\u044b\u043a\u043b\u044e\u0447\u0438 \u0442\u0435\u043b\u0435\u0432\u0438\u0437\u043e\u0440', 'Subject': '\u0432\u044b\u043a\u043b\u044e\u0447\u0438 \u0442\u0435\u043b\u0435\u0432\u0438\u0437\u043e\u0440',
u'TemplateDate': u'00/00/0000 00:00:00', 'TemplateDate': '00/00/0000 00:00:00',
u'Title': u'kesha'}) 'Title': 'kesha'})
def testsetMetadata(self): def testsetMetadata(self):
"""Test setMetadata for yformats""" """Test setMetadata for yformats"""
handler = Handler(self.tmp_url, open("data/test_with_metadata.xlsy").read(), "xlsy", **self.kw) with open("data/test_with_metadata.xlsy", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, data, "xlsy", **self.kw)
new_mime_data = handler.setMetadata({ new_mime_data = handler.setMetadata({
"Title": "test title", "Title": "test title",
"Subject": "test subject", "Subject": "test subject",
"Keywords": "test keywords", "Keywords": "test keywords",
}) })
handler = Handler(self.tmp_url, new_mime_data, "xlsy", **self.kw) handler = Handler(self.tmp_url, new_mime_data, "xlsy", **self.kw)
self.assertEqual(handler.getMetadata(), {u'Keywords': u'test keywords', 'MIMEType': 'application/x-asc-spreadsheet', u'Title': u'test title', u'Subject': u'test subject'}) self.assertEqual(handler.getMetadata(), {'Keywords': 'test keywords', 'MIMEType': 'application/x-asc-spreadsheet', 'Title': 'test title', 'Subject': 'test subject'})
def testGetAllowedConversionFormatList(self): def testGetAllowedConversionFormatList(self):
"""Test all combination of mimetype """Test all combination of mimetype
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
############################################################################## ##############################################################################
import io import io
import zipfile import zipfile
from base64 import decodestring, encodestring from base64 import decodebytes, encodebytes
from os.path import join from os.path import join
from cloudooo.tests.cloudoooTestCase import TestCase from cloudooo.tests.cloudoooTestCase import TestCase
...@@ -52,38 +52,40 @@ class TestServer(TestCase): ...@@ -52,38 +52,40 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
# Try convert one xlsx for a invalid format
(open(join('data', 'test.xlsx')).read(), 'xlsx', 'xyz'),
] ]
# Try convert one xlsx for a invalid format
with open(join('data', 'test.xlsx'), 'rb') as f:
scenario_list.append((f.read(), 'xlsx', 'xyz'))
return scenario_list
def test_xlsx_to_xlsy(self): def test_xlsx_to_xlsy(self):
with open(join('data', 'test.xlsx')) as f: with open(join('data', 'test.xlsx'), 'rb') as f:
xlsx_data = f.read() xlsx_data = f.read()
xlsy_data = self.proxy.convertFile( xlsy_data = self.proxy.convertFile(
encodestring(xlsx_data), encodebytes(xlsx_data).decode(),
'xlsx', 'xlsx',
'xlsy', 'xlsy',
False False
) )
self.assertEqual( self.assertEqual(
sorted(zipfile.ZipFile(io.BytesIO(decodestring(xlsy_data))).namelist()), sorted(zipfile.ZipFile(io.BytesIO(decodebytes(xlsy_data.encode()))).namelist()),
sorted(['Editor.xlsx', 'body.txt', 'metadata.json']) sorted(['Editor.xlsx', 'body.txt', 'metadata.json'])
) )
def test_docx_to_docy(self): def test_docx_to_docy(self):
with open(join('data', 'test_with_image.docx')) as f: with open(join('data', 'test_with_image.docx'), 'rb') as f:
docx_data = f.read() docx_data = f.read()
docy_data = self.proxy.convertFile( docy_data = self.proxy.convertFile(
encodestring(docx_data), encodebytes(docx_data).decode(),
'docx', 'docx',
'docy', 'docy',
False False
) )
self.assertEqual( self.assertEqual(
sorted(zipfile.ZipFile(io.BytesIO(decodestring(docy_data))).namelist()), sorted(zipfile.ZipFile(io.BytesIO(decodebytes(docy_data.encode()))).namelist()),
sorted(['body.txt', 'media/image1.png', 'metadata.json']) sorted(['body.txt', 'media/image1.png', 'metadata.json'])
) )
# -*- coding: utf-8 -*-
############################################################################## ##############################################################################
# #
# Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved. # Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
...@@ -30,10 +29,12 @@ ...@@ -30,10 +29,12 @@
############################################################################## ##############################################################################
import mimetypes import mimetypes
import xmlrpc
from mimetypes import guess_type, guess_extension from mimetypes import guess_type, guess_extension
from base64 import encodestring, decodestring from binascii import a2b_base64
from base64 import encodebytes
from zope.interface import implementer from zope.interface import implementer
from interfaces.manager import IManager, IERP5Compatibility from .interfaces.manager import IManager, IERP5Compatibility
from cloudooo.util import logger, parseContentType from cloudooo.util import logger, parseContentType
from cloudooo.interfaces.granulate import ITableGranulator from cloudooo.interfaces.granulate import ITableGranulator
from cloudooo.interfaces.granulate import IImageGranulator from cloudooo.interfaces.granulate import IImageGranulator
...@@ -79,9 +80,11 @@ def BBB_guess_extension(mimetype, title=None): ...@@ -79,9 +80,11 @@ def BBB_guess_extension(mimetype, title=None):
"Flat XML ODF Text Document": ".fodt", "Flat XML ODF Text Document": ".fodt",
"MET - OS/2 Metafile": ".met", "MET - OS/2 Metafile": ".met",
"Microsoft Excel 2007-2013 XML": ".ms.xlsx", "Microsoft Excel 2007-2013 XML": ".ms.xlsx",
"Excel 2007–365": ".ms.xlsx",
"Microsoft PowerPoint 2007-2013 XML": ".ms.pptx", "Microsoft PowerPoint 2007-2013 XML": ".ms.pptx",
"Microsoft PowerPoint 2007-2013 XML AutoPlay": ".ms.ppsx", "Microsoft PowerPoint 2007-2013 XML AutoPlay": ".ms.ppsx",
"Microsoft Word 2007-2013 XML": ".ms.docx", "Microsoft Word 2007-2013 XML": ".ms.docx",
"Word 2007–365": ".ms.docx",
}.get(title, None) or { }.get(title, None) or {
# mediatype : extension # mediatype : extension
"application/msword": ".doc", "application/msword": ".doc",
...@@ -92,7 +95,7 @@ def BBB_guess_extension(mimetype, title=None): ...@@ -92,7 +95,7 @@ def BBB_guess_extension(mimetype, title=None):
"text/html": ".html", "text/html": ".html",
"text/plain": ".txt", "text/plain": ".txt",
"image/jpeg": ".jpg", "image/jpeg": ".jpg",
}.get(parseContentType(mimetype).gettype(), None) or guess_extension(mimetype) }.get(parseContentType(mimetype).get_content_type(), None) or guess_extension(mimetype)
@implementer(IManager, IERP5Compatibility, ITableGranulator, IImageGranulator, @implementer(IManager, IERP5Compatibility, ITableGranulator, IImageGranulator,
ITextGranulator) ITextGranulator)
...@@ -106,8 +109,12 @@ class Manager(object): ...@@ -106,8 +109,12 @@ class Manager(object):
self.mimetype_registry = kw.pop("mimetype_registry") self.mimetype_registry = kw.pop("mimetype_registry")
self.handler_dict = kw.pop("handler_dict") self.handler_dict = kw.pop("handler_dict")
def convertFile(self, file, source_format, destination_format, zip=False, def _check_file_type(self, file):
refresh=False, conversion_kw={}): if isinstance(file, xmlrpc.client.Binary):
raise TypeError('`file` must be provided as a string with the file content encoded as base64')
def convertFile(self, file:str, source_format:str, destination_format:str, zip=False,
refresh=False, conversion_kw={}) -> str:
"""Returns the converted file in the given format. """Returns the converted file in the given format.
Keywords arguments: Keywords arguments:
file -- File as string in base64 file -- File as string in base64
...@@ -116,6 +123,8 @@ class Manager(object): ...@@ -116,6 +123,8 @@ class Manager(object):
zip -- Boolean Attribute. If true, returns the file in the form of a zip -- Boolean Attribute. If true, returns the file in the form of a
zip archive zip archive
""" """
self._check_file_type(file)
kw = self.kw.copy() kw = self.kw.copy()
kw.update(zip=zip, refresh=refresh) kw.update(zip=zip, refresh=refresh)
# XXX Force the use of wkhtmltopdf handler if converting from html to pdf # XXX Force the use of wkhtmltopdf handler if converting from html to pdf
...@@ -136,57 +145,59 @@ class Manager(object): ...@@ -136,57 +145,59 @@ class Manager(object):
self.mimetype_registry, self.mimetype_registry,
self.handler_dict) self.handler_dict)
handler = handler_class(self._path_tmp_dir, handler = handler_class(self._path_tmp_dir,
decodestring(file), a2b_base64(file),
source_format, source_format,
**kw) **kw)
decode_data = handler.convert(destination_format, **conversion_kw) decode_data = handler.convert(destination_format, **conversion_kw)
return encodestring(decode_data) return encodebytes(decode_data).decode()
def updateFileMetadata(self, file, source_format, metadata_dict): def updateFileMetadata(self, file:str, source_format:str, metadata_dict:dict) -> str:
"""Receives the string of document and a dict with metadatas. The metadata """Receives the string of document and a dict with metadatas. The metadata
is added in document. is added in document.
e.g e.g
self.updateFileMetadata(data = encodestring(data), metadata = \ self.updateFileMetadata(data = encodebytes(data), metadata = \
{"title":"abc","description":...}) {"title":"abc","description":...})
return encodestring(document_with_metadata) return encodebytes(document_with_metadata)
""" """
self._check_file_type(file)
handler_class = getHandlerClass(source_format, handler_class = getHandlerClass(source_format,
None, None,
self.mimetype_registry, self.mimetype_registry,
self.handler_dict) self.handler_dict)
handler = handler_class(self._path_tmp_dir, handler = handler_class(self._path_tmp_dir,
decodestring(file), a2b_base64(file),
source_format, source_format,
**self.kw) **self.kw)
metadata_dict = dict([(key.capitalize(), value) \ metadata_dict = {key.capitalize(): value \
for key, value in metadata_dict.iteritems()]) for key, value in metadata_dict.items()}
decode_data = handler.setMetadata(metadata_dict) decode_data = handler.setMetadata(metadata_dict)
return encodestring(decode_data) return encodebytes(decode_data).decode()
def getFileMetadataItemList(self, file, source_format, base_document=False): def getFileMetadataItemList(self, file:str, source_format:str, base_document=False) -> dict[str, str]:
"""Receives the string of document as encodestring and returns a dict with """Receives the string of document as encodebytes and returns a dict with
metadatas. metadatas.
e.g. e.g.
self.getFileMetadataItemList(data = encodestring(data)) self.getFileMetadataItemList(data = encodebytes(data).decode())
return {'Title': 'abc','Description': 'comments', 'Data': None} return {'Title': 'abc','Description': 'comments', 'Data': None}
If converted_data is True, the ODF of data is added in dictionary. If converted_data is True, the ODF of data is added in dictionary.
e.g e.g
self.getFileMetadataItemList(data = encodestring(data), True) self.getFileMetadataItemList(data = encodebytes(data).decode(), True)
return {'Title': 'abc','Description': 'comments', 'Data': string_ODF} return {'Title': 'abc','Description': 'comments', 'Data': string_ODF}
Note that all keys of the dictionary have the first word in uppercase. Note that all keys of the dictionary have the first word in uppercase.
""" """
self._check_file_type(file)
handler_class = getHandlerClass(source_format, handler_class = getHandlerClass(source_format,
None, None,
self.mimetype_registry, self.mimetype_registry,
self.handler_dict) self.handler_dict)
handler = handler_class(self._path_tmp_dir, handler = handler_class(self._path_tmp_dir,
decodestring(file), a2b_base64(file),
source_format, source_format,
**self.kw) **self.kw)
metadata_dict = handler.getMetadata(base_document) metadata_dict = handler.getMetadata(base_document)
metadata_dict['Data'] = encodestring(metadata_dict.get('Data', '')) metadata_dict['Data'] = encodebytes(metadata_dict.get('Data', b'')).decode()
return metadata_dict return metadata_dict
def getAllowedExtensionList(self, request_dict={}): def getAllowedExtensionList(self, request_dict={}):
...@@ -217,7 +228,7 @@ class Manager(object): ...@@ -217,7 +228,7 @@ class Manager(object):
else: else:
return [('', '')] return [('', '')]
def getAllowedConversionFormatList(self, source_mimetype): def getAllowedConversionFormatList(self, source_mimetype:str) -> list:
r"""Returns a list content_type and their titles which are supported r"""Returns a list content_type and their titles which are supported
by enabled handlers. by enabled handlers.
...@@ -275,8 +286,7 @@ class Manager(object): ...@@ -275,8 +286,7 @@ class Manager(object):
del response_dict['meta']['Data'] del response_dict['meta']['Data']
return (200, response_dict, "") return (200, response_dict, "")
except Exception as e: except Exception as e:
import traceback; logger.error('Error converting to %s', extension, exc_info=True)
logger.error(traceback.format_exc())
return (402, {}, e.args[0]) return (402, {}, e.args[0])
def run_setmetadata(self, filename='', data=None, meta=None, def run_setmetadata(self, filename='', data=None, meta=None,
...@@ -292,8 +302,7 @@ class Manager(object): ...@@ -292,8 +302,7 @@ class Manager(object):
response_dict['data'] = self.updateFileMetadata(data, extension, meta) response_dict['data'] = self.updateFileMetadata(data, extension, meta)
return (200, response_dict, '') return (200, response_dict, '')
except Exception as e: except Exception as e:
import traceback; logger.error('Error setting metadata', exc_info=True)
logger.error(traceback.format_exc())
return (402, {}, e.args[0]) return (402, {}, e.args[0])
def run_getmetadata(self, filename='', data=None, meta=None, def run_getmetadata(self, filename='', data=None, meta=None,
...@@ -312,8 +321,7 @@ class Manager(object): ...@@ -312,8 +321,7 @@ class Manager(object):
response_dict['meta']['title'] = response_dict['meta']['Title'] response_dict['meta']['title'] = response_dict['meta']['Title']
return (200, response_dict, '') return (200, response_dict, '')
except Exception as e: except Exception as e:
import traceback; logger.error('Error getting metadata', exc_info=True)
logger.error('run_getmetadata: ' + traceback.format_exc())
return (402, {}, e.args[0]) return (402, {}, e.args[0])
def run_generate(self, filename='', data=None, meta=None, extension=None, def run_generate(self, filename='', data=None, meta=None, extension=None,
...@@ -354,11 +362,10 @@ class Manager(object): ...@@ -354,11 +362,10 @@ class Manager(object):
mimetypes.types_map.get('.%s' % extension.split('.')[-1])) mimetypes.types_map.get('.%s' % extension.split('.')[-1]))
return (200, response_dict, "") return (200, response_dict, "")
except Exception as e: except Exception as e:
import traceback; logger.error('Error in generate from %s to %s', orig_format, extension, exc_info=True)
logger.error(traceback.format_exc())
return (402, response_dict, str(e)) return (402, response_dict, str(e))
def getAllowedTargetItemList(self, content_type): def getAllowedTargetItemList(self, content_type:str):
"""Wrapper getAllowedExtensionList but returns a dict. """Wrapper getAllowedExtensionList but returns a dict.
This is a Backwards compatibility provided for ERP5 Project, in order to This is a Backwards compatibility provided for ERP5 Project, in order to
keep compatibility with OpenOffice.org Daemon. keep compatibility with OpenOffice.org Daemon.
...@@ -374,28 +381,30 @@ class Manager(object): ...@@ -374,28 +381,30 @@ class Manager(object):
response_dict['response_data'] = extension_list response_dict['response_data'] = extension_list
return (200, response_dict, '') return (200, response_dict, '')
except Exception as e: except Exception as e:
logger.error(e) logger.error('Error in getting target item list from %s', content_type, exc_info=True)
return (402, {}, e.args[0]) return (402, {}, e.args[0])
def _getOOGranulator(self, data, source_format="odt"): def _getOOGranulator(self, data:str, source_format="odt"):
"""Returns an instance of the handler OOGranulator after convert the """Returns an instance of the handler OOGranulator after convert the
data to 'odt'""" data to 'odt'
data is a str with the actual data encoded in base64
"""
GRANULATABLE_FORMAT_LIST = ("odt",) GRANULATABLE_FORMAT_LIST = ("odt",)
if source_format not in GRANULATABLE_FORMAT_LIST: if source_format not in GRANULATABLE_FORMAT_LIST:
data = self.convertFile(data, source_format, data = self.convertFile(data, source_format,
GRANULATABLE_FORMAT_LIST[0], zip=False) GRANULATABLE_FORMAT_LIST[0], zip=False)
return OOGranulator(decodestring(data), GRANULATABLE_FORMAT_LIST[0]) return OOGranulator(a2b_base64(data), GRANULATABLE_FORMAT_LIST[0])
def getTableItemList(self, data, source_format="odt"): def getTableItemList(self, data, source_format="odt"):
"""Returns the list of table IDs in the form of (id, title).""" """Returns the list of table IDs in the form of (id, title)."""
document = self._getOOGranulator(data, source_format) document = self._getOOGranulator(data, source_format)
return document.getTableItemList() return document.getTableItemList()
def getTable(self, data, id, source_format="odt"): def getTable(self, data, id, source_format="odt") -> str:
"""Returns the table into a new 'format' file.""" """Returns the table into a new 'format' file."""
document = self._getOOGranulator(data, source_format) document = self._getOOGranulator(data, source_format)
#the file will be convert; so, the source_format will be always 'odt' #the file will be convert; so, the source_format will be always 'odt'
return encodestring(document.getTable(id, 'odt')) return encodebytes(document.getTable(id, 'odt')).decode()
def getColumnItemList(self, data, table_id, source_format): def getColumnItemList(self, data, table_id, source_format):
"""Return the list of columns in the form of (id, title).""" """Return the list of columns in the form of (id, title)."""
...@@ -407,17 +416,16 @@ class Manager(object): ...@@ -407,17 +416,16 @@ class Manager(object):
document = self._getOOGranulator(data, source_format) document = self._getOOGranulator(data, source_format)
return document.getLineItemList(table_id) return document.getLineItemList(table_id)
def getImageItemList(self, data, source_format): def getImageItemList(self, data:str, source_format:str):
"""Return the list of images in the form of (id, title).""" """Return the list of images in the form of (id, title)."""
data = self.convertFile(data, source_format, 'odt', zip=False)
document = self._getOOGranulator(data, source_format) document = self._getOOGranulator(data, source_format)
return document.getImageItemList() return document.getImageItemList()
def getImage(self, data, image_id, source_format, format=None, def getImage(self, data:str, image_id:str, source_format:str, format=None,
resolution=None, **kw): resolution=None, **kw) -> str:
"""Return the given image.""" """Return the given image."""
document = self._getOOGranulator(data, source_format) document = self._getOOGranulator(data, source_format)
return encodestring(document.getImage(image_id, format, resolution, **kw)) return encodebytes(document.getImage(image_id, format, resolution, **kw)).decode()
def getParagraphItemList(self, data, source_format): def getParagraphItemList(self, data, source_format):
"""Returns the list of paragraphs in the form of (id, class) where class """Returns the list of paragraphs in the form of (id, class) where class
......
...@@ -51,7 +51,7 @@ def application(global_config, **local_config): ...@@ -51,7 +51,7 @@ def application(global_config, **local_config):
""" """
prefix = 'env-' prefix = 'env-'
environment_dict = {} environment_dict = {}
for parameter_name, value in local_config.iteritems(): for parameter_name, value in local_config.items():
if parameter_name.startswith(prefix): if parameter_name.startswith(prefix):
value = value or '' value = value or ''
variable_name = parameter_name[len(prefix):] variable_name = parameter_name[len(prefix):]
...@@ -81,7 +81,7 @@ def application(global_config, **local_config): ...@@ -81,7 +81,7 @@ def application(global_config, **local_config):
mimetype_registry = local_config.get("mimetype_registry", "") mimetype_registry = local_config.get("mimetype_registry", "")
local_config["mimetype_registry"] = handler_mapping_list = \ local_config["mimetype_registry"] = handler_mapping_list = \
filter(None, mimetype_registry.split("\n")) [_f for _f in mimetype_registry.split("\n") if _f]
ooo_disable_filter_list = [] ooo_disable_filter_list = []
for filter_name in local_config.get("ooo_disable_filter_list", "").split("\n"): for filter_name in local_config.get("ooo_disable_filter_list", "").split("\n"):
...@@ -109,6 +109,6 @@ def application(global_config, **local_config): ...@@ -109,6 +109,6 @@ def application(global_config, **local_config):
handler_dict[handler] = module.Handler handler_dict[handler] = module.Handler
local_config['handler_dict'] = handler_dict local_config['handler_dict'] = handler_dict
from manager import Manager from .manager import Manager
cloudooo_manager = Manager(cloudooo_path_tmp_dir, **local_config) cloudooo_manager = Manager(cloudooo_path_tmp_dir, **local_config)
return WSGIXMLRPCApplication(instance=cloudooo_manager) return WSGIXMLRPCApplication(instance=cloudooo_manager)
# Backport of Python 2.7 unittest chosen parts to be able to use the
# "skip" decorators, and the associated ExpectedFailure and
# UnexpectedSuccess.
#
# Implementation is mostly a direct translation from Python r75708
# grep for "BACK" comments for backport-specific remarks.
import unittest
import sys
import time
class SkipTest(Exception):
"""
Raise this exception in a test to skip it.
Usually you can use TestResult.skip() or one of the skipping decorators
instead of raising this directly.
"""
pass
class _ExpectedFailure(Exception):
"""
Raise this when a test is expected to fail.
This is an implementation detail.
"""
def __init__(self, exc_info):
Exception.__init__(self)
self.exc_info = exc_info
class _UnexpectedSuccess(Exception):
"""
The test was supposed to fail, but it didn't!
"""
pass
class SetupSiteError(Exception):
"""
The ERP5 Site could not have been setup.
This is raised when the site could not have been created in a previous
test. We want this to count as an error, but we do not want this to happear
in traceback for readability.
"""
pass
def _id(obj):
return obj
def skip(reason):
"""
Unconditionally skip a test.
"""
def decorator(test_item):
if isinstance(test_item, type) and issubclass(test_item, TestCase):
test_item.__unittest_skip__ = True
test_item.__unittest_skip_why__ = reason
return test_item
def skip_wrapper(*args, **kwargs):
raise SkipTest(reason)
skip_wrapper.__name__ = test_item.__name__
skip_wrapper.__doc__ = test_item.__doc__
return skip_wrapper
return decorator
def skipIf(condition, reason):
"""
Skip a test if the condition is true.
"""
if condition:
return skip(reason)
return _id
def skipUnless(condition, reason):
"""
Skip a test unless the condition is true.
"""
if not condition:
return skip(reason)
return _id
def expectedFailure(func):
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
raise _ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
class TestCase(unittest.TestCase):
"""We redefine here the run() method, and add a skipTest() method.
"""
failureException = AssertionError
def run(self, result=None):
orig_result = result
if result is None:
result = self.defaultTestResult()
# BACK: Not necessary for Python < 2.7:
# TestResult.startTestRun does not exist yet
# startTestRun = getattr(result, 'startTestRun', None)
# if startTestRun is not None:
# startTestRun()
# BACK: Not needed for Python < 2.7
# unittest.addCleanup does not exist yet
# self._resultForDoCleanups = result
result.startTest(self)
if getattr(self.__class__, "__unittest_skip__", False):
# If the whole class was skipped.
try:
result.addSkip(self, self.__class__.__unittest_skip_why__)
finally:
result.stopTest(self)
return
testMethod = getattr(self, self._testMethodName)
try:
success = False
try:
self.setUp()
except SkipTest as e:
result.addSkip(self, str(e))
except SetupSiteError as e:
result.errors.append(None)
except BaseException as e:
result.addError(self, sys.exc_info())
if isinstance(e, (KeyboardInterrupt, SystemExit)):
raise
else:
try:
testMethod()
except self.failureException:
result.addFailure(self, sys.exc_info())
except _ExpectedFailure as e:
result.addExpectedFailure(self, e.exc_info)
except _UnexpectedSuccess:
result.addUnexpectedSuccess(self)
except SkipTest as e:
result.addSkip(self, str(e))
except BaseException as e:
result.addError(self, sys.exc_info())
if isinstance(e, (KeyboardInterrupt, SystemExit)):
raise
else:
success = True
try:
self.tearDown()
except BaseException as e:
result.addError(self, sys.exc_info())
if isinstance(e, (KeyboardInterrupt, SystemExit)):
raise
success = False
# BACK: Not needed for Python < 2.7
# unittest.addCleanup does not exist yet
# cleanUpSuccess = self.doCleanups()
# success = success and cleanUpSuccess
if success:
result.addSuccess(self)
finally:
result.stopTest(self)
# BACK: Not necessary for Python < 2.7
# TestResult.stopTestRun does not exist yet
# if orig_result is None:
# stopTestRun = getattr(result, 'stopTestRun', None)
# if stopTestRun is not None:
# stopTestRun()
def skipTest(self, reason):
"""Skip this test."""
raise SkipTest(reason)
if not hasattr(unittest.TestResult, 'addSkip'): # BBB: Python < 2.7
unittest.TestResult._orig_init__ = unittest.TestResult.__init__.im_func
def __init__(self):
self._orig_init__()
self.skipped = []
self.expectedFailures = []
self.unexpectedSuccesses = []
def addSkip(self, test, reason):
"""Called when a test is skipped."""
self.skipped.append((test, reason))
if self.showAll:
self.stream.writeln("skipped %s" % repr(reason))
elif self.dots:
self.stream.write("s")
self.stream.flush()
def addExpectedFailure(self, test, err):
"""Called when an expected failure/error occured."""
self.expectedFailures.append(
(test, self._exc_info_to_string(err, test)))
if self.showAll:
self.stream.writeln("expected failure")
elif self.dots:
self.stream.write("x")
self.stream.flush()
def addUnexpectedSuccess(self, test):
"""Called when a test was expected to fail, but succeed."""
self.unexpectedSuccesses.append(test)
if self.showAll:
self.stream.writeln("unexpected success")
elif self.dots:
self.stream.write("u")
self.stream.flush()
for f in __init__, addSkip, addExpectedFailure, addUnexpectedSuccess:
setattr(unittest.TestResult, f.__name__, f)
def getDescription(self, test):
doc_first_line = test.shortDescription()
if self.descriptions and doc_first_line:
return '\n'.join((str(test), doc_first_line))
else:
return str(test)
unittest._TextTestResult.getDescription = getDescription
class _TextTestResult(unittest._TextTestResult):
def wasSuccessful(self):
"Tells whether or not this result was a success"
return not (self.failures or self.errors or self.unexpectedSuccesses)
def printErrors(self):
if self.dots or self.showAll:
self.stream.writeln()
# 'None' correspond to redundant errors due to site creation errors,
# and we do not display them here.
self.printErrorList('ERROR', filter(None, self.errors))
self.printErrorList('FAIL', self.failures)
if self.unexpectedSuccesses:
self.stream.writeln(self.separator1)
for test in self.unexpectedSuccesses:
self.stream.writeln("SUCCESS: %s" % self.getDescription(test))
class TextTestRunner(unittest.TextTestRunner):
def _makeResult(self):
return _TextTestResult(self.stream, self.descriptions, self.verbosity)
def run(self, test):
result = self._makeResult()
startTime = time.time()
# BACK: 2.7 implementation wraps run with result.(start|stop)TestRun
try:
test(result)
except KeyboardInterrupt:
pass
stopTime = time.time()
timeTaken = stopTime - startTime
result.printErrors()
self.stream.writeln(result.separator2)
run = result.testsRun
self.stream.writeln("Ran %d test%s in %.3fs" %
(run, run != 1 and "s" or "", timeTaken))
self.stream.writeln()
results = map(len, (result.expectedFailures,
result.unexpectedSuccesses,
result.skipped))
expectedFails, unexpectedSuccesses, skipped = results
infos = []
if not result.wasSuccessful():
self.stream.write("FAILED")
failed, errored = map(len, (result.failures, result.errors))
if failed:
infos.append("failures=%d" % failed)
if errored:
infos.append("errors=%d" % errored)
else:
self.stream.write("OK")
if skipped:
infos.append("skipped=%d" % skipped)
if expectedFails:
infos.append("expected failures=%d" % expectedFails)
if unexpectedSuccesses:
infos.append("unexpected successes=%d" % unexpectedSuccesses)
if infos:
self.stream.writeln(" (%s)" % (", ".join(infos),))
else:
self.stream.write("\n")
return result
...@@ -30,23 +30,21 @@ ...@@ -30,23 +30,21 @@
import unittest import unittest
from os import environ, path from os import environ, path
from ConfigParser import ConfigParser from configparser import ConfigParser
from xmlrpclib import ServerProxy, Fault from xmlrpc.client import ServerProxy, Fault
from magic import Magic from magic import Magic
from types import DictType from base64 import encodebytes, decodebytes
from base64 import encodestring, decodestring
from cloudooo.tests import backportUnittest
config = ConfigParser() config = ConfigParser()
def make_suite(test_case): def make_suite(test_case):
"""Function is used to run all tests together""" """Function is used to run all tests together"""
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(test_case)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(test_case))
return suite return suite
class TestCase(backportUnittest.TestCase): class TestCase(unittest.TestCase):
def setUp(self): def setUp(self):
server_cloudooo_conf = environ.get("server_cloudooo_conf", None) server_cloudooo_conf = environ.get("server_cloudooo_conf", None)
...@@ -58,16 +56,20 @@ class TestCase(backportUnittest.TestCase): ...@@ -58,16 +56,20 @@ class TestCase(backportUnittest.TestCase):
#create temporary path for some files #create temporary path for some files
self.working_path = config.get("app:main", "working_path") self.working_path = config.get("app:main", "working_path")
self.tmp_url = path.join(self.working_path, "tmp") self.tmp_url = path.join(self.working_path, "tmp")
self.proxy = ServerProxy(("http://%s:%s/RPC2" % (self.hostname, self.port)),\ self.proxy = ServerProxy(("http://{}:{}/RPC2".format(self.hostname, self.port)),\
allow_none=True) allow_none=True)
self.addCleanup(self.proxy('close'))
self.afterSetUp() self.afterSetUp()
def afterSetUp(self): def afterSetUp(self):
"""Must be overwrite into subclass in case of need """ """Must be overwrite into subclass in case of need """
def _getFileType(self, output_data): def _getFileType(self, output_data:str) -> str:
"""get file type of `output_data`
output_data is base64
"""
mime = Magic(mime=True) mime = Magic(mime=True)
mimetype = mime.from_buffer(decodestring(output_data)) mimetype = mime.from_buffer(decodebytes(output_data.encode()))
return mimetype return mimetype
def _testConvertFile(self, input_url, source_format, destination_format, def _testConvertFile(self, input_url, source_format, destination_format,
...@@ -75,7 +77,10 @@ class TestCase(backportUnittest.TestCase): ...@@ -75,7 +77,10 @@ class TestCase(backportUnittest.TestCase):
""" Generic test for converting file """ """ Generic test for converting file """
fault_list = [] fault_list = []
try: try:
output_data = self.proxy.convertFile(encodestring(open(input_url).read()), with open(input_url, 'rb') as f:
data = f.read()
output_data = self.proxy.convertFile(
encodebytes(data).decode(),
source_format, source_format,
destination_format, destination_format,
zip) zip)
...@@ -92,47 +97,51 @@ class TestCase(backportUnittest.TestCase): ...@@ -92,47 +97,51 @@ class TestCase(backportUnittest.TestCase):
message = '\n'.join([template_message % fault for fault in fault_list]) message = '\n'.join([template_message % fault for fault in fault_list])
self.fail('Failed Conversions:\n' + message) self.fail('Failed Conversions:\n' + message)
def _testFaultConversion(self, data, source_format, destination_format): def _testFaultConversion(self, data:bytes, source_format:str, destination_format:str):
""" Generic test for fail converting""" """ Generic test for fail converting"""
self.assertRaises(Fault, self.proxy.convertFile, (data, self.assertRaises(Fault, self.proxy.convertFile, (data,
source_format, source_format,
destination_format)) destination_format))
def _testGetMetadata(self, input_url, source_format, expected_metadata, def _testGetMetadata(self, input_url:str, source_format:str, expected_metadata:dict,
base_document=False): base_document=False):
""" Generic tes for getting metadata file""" """ Generic tes for getting metadata file"""
with open(input_url, 'rb') as f:
input_data = f.read()
metadata_dict = self.proxy.getFileMetadataItemList( metadata_dict = self.proxy.getFileMetadataItemList(
encodestring(open(input_url).read()), encodebytes(input_data).decode(),
source_format, source_format,
base_document) base_document)
for key,value in expected_metadata.iteritems(): for key,value in expected_metadata.items():
self.assertEqual(metadata_dict[key], value) self.assertEqual(metadata_dict[key], value)
def _testFaultGetMetadata(self, data, source_format): def _testFaultGetMetadata(self, data:bytes, source_format:str):
""" Generic test for fail converting""" """ Generic test for fail converting"""
self.assertRaises(Fault, self.proxy.getFileMetadataItemList, (data, self.assertRaises(Fault, self.proxy.getFileMetadataItemList, (data,
source_format)) source_format))
def _testUpdateMetadata(self, input_url, source_format, metadata_dict): def _testUpdateMetadata(self, input_url:str, source_format:str, metadata_dict:dict):
""" Generic test for setting metadata for file """ """ Generic test for setting metadata for file """
output_data = self.proxy.updateFileMetadata(encodestring(open(input_url).read()), with open(input_url, 'rb') as f:
input_data = f.read()
output_data = self.proxy.updateFileMetadata(encodebytes(input_data).decode(),
source_format, source_format,
metadata_dict) metadata_dict)
new_metadata_dict = self.proxy.getFileMetadataItemList( new_metadata_dict = self.proxy.getFileMetadataItemList(
output_data, output_data,
source_format, source_format,
False) False)
for key,value in metadata_dict.iteritems(): for key, value in metadata_dict.items():
self.assertEqual(new_metadata_dict[key], value) self.assertEqual(new_metadata_dict[key], value)
def _testRunConvert(self, filename, data, expected_response_code, def _testRunConvert(self, filename:str, data:bytes, expected_response_code:int,
response_dict_data,response_dict_keys, response_dict_data, response_dict_keys:list[str],
expected_response_message, response_dict_meta=None): expected_response_message, response_dict_meta=None):
"""Generic test for run_convert""" """Generic test for run_convert"""
response_code, response_dict, response_message = \ response_code, response_dict, response_message = \
self.proxy.run_convert(filename, encodestring(data)) self.proxy.run_convert(filename, encodebytes(data).decode())
self.assertEqual(response_code, expected_response_code) self.assertEqual(response_code, expected_response_code)
self.assertEqual(type(response_dict), DictType) self.assertIsInstance(response_dict, dict)
if expected_response_code == 402: if expected_response_code == 402:
self.assertEqual(response_dict, {}) self.assertEqual(response_dict, {})
self.assertTrue(response_message.endswith(expected_response_message), self.assertTrue(response_message.endswith(expected_response_message),
...@@ -152,6 +161,7 @@ class TestCase(backportUnittest.TestCase): ...@@ -152,6 +161,7 @@ class TestCase(backportUnittest.TestCase):
def runConversionList(self, scenarios): def runConversionList(self, scenarios):
for scenario in scenarios: for scenario in scenarios:
with self.subTest(scenario):
self._testConvertFile(*scenario) self._testConvertFile(*scenario)
def GetMetadataScenarioList(self): def GetMetadataScenarioList(self):
...@@ -163,6 +173,7 @@ class TestCase(backportUnittest.TestCase): ...@@ -163,6 +173,7 @@ class TestCase(backportUnittest.TestCase):
def runGetMetadataList(self, scenarios): def runGetMetadataList(self, scenarios):
for scenario in scenarios: for scenario in scenarios:
with self.subTest(scenario):
self._testGetMetadata(*scenario) self._testGetMetadata(*scenario)
def UpdateMetadataScenarioList(self): def UpdateMetadataScenarioList(self):
...@@ -174,6 +185,7 @@ class TestCase(backportUnittest.TestCase): ...@@ -174,6 +185,7 @@ class TestCase(backportUnittest.TestCase):
def runUpdateMetadataList(self, scenarios): def runUpdateMetadataList(self, scenarios):
for scenario in scenarios: for scenario in scenarios:
with self.subTest(scenario):
self._testUpdateMetadata(*scenario) self._testUpdateMetadata(*scenario)
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
...@@ -185,6 +197,7 @@ class TestCase(backportUnittest.TestCase): ...@@ -185,6 +197,7 @@ class TestCase(backportUnittest.TestCase):
def runFaultConversionList(self, scenarios): def runFaultConversionList(self, scenarios):
for scenario in scenarios: for scenario in scenarios:
with self.subTest(scenario):
self._testFaultConversion(*scenario) self._testFaultConversion(*scenario)
def FaultGetMetadataScenarioList(self): def FaultGetMetadataScenarioList(self):
...@@ -196,6 +209,7 @@ class TestCase(backportUnittest.TestCase): ...@@ -196,6 +209,7 @@ class TestCase(backportUnittest.TestCase):
def runFaultGetMetadataList(self, scenarios): def runFaultGetMetadataList(self, scenarios):
for scenario in scenarios: for scenario in scenarios:
with self.subTest(scenario):
self._testFaultGetMetadata(*scenario) self._testFaultGetMetadata(*scenario)
def ConvertScenarioList(self): def ConvertScenarioList(self):
...@@ -207,5 +221,6 @@ class TestCase(backportUnittest.TestCase): ...@@ -207,5 +221,6 @@ class TestCase(backportUnittest.TestCase):
def runConvertScenarioList(self, scenarios): def runConvertScenarioList(self, scenarios):
for scenario in scenarios: for scenario in scenarios:
with self.subTest(scenario):
self._testRunConvert(*scenario) self._testRunConvert(*scenario)
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
import unittest import unittest
import sys import sys
from os import environ, path, mkdir, putenv from os import environ, path, mkdir, putenv
from ConfigParser import ConfigParser from configparser import ConfigParser
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.mimemapper import mimemapper from cloudooo.handler.ooo.mimemapper import mimemapper
...@@ -42,7 +42,7 @@ config = ConfigParser() ...@@ -42,7 +42,7 @@ config = ConfigParser()
def make_suite(test_case): def make_suite(test_case):
"""Function is used to run all tests together""" """Function is used to run all tests together"""
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(test_case)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(test_case))
return suite return suite
...@@ -84,8 +84,8 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None): ...@@ -84,8 +84,8 @@ def startFakeEnvironment(start_openoffice=True, conf_path=None):
if start_openoffice: if start_openoffice:
default_language = config.get('app:main', default_language = config.get('app:main',
'openoffice_user_interface_language', False, 'openoffice_user_interface_language', raw=False,
{'openoffice_user_interface_language': 'en'}) vars={'openoffice_user_interface_language': 'en'})
openoffice.loadSettings(hostname, openoffice.loadSettings(hostname,
openoffice_port, openoffice_port,
working_path, working_path,
...@@ -110,27 +110,6 @@ def stopFakeEnvironment(stop_openoffice=True): ...@@ -110,27 +110,6 @@ def stopFakeEnvironment(stop_openoffice=True):
openoffice.stop() openoffice.stop()
return True return True
if 1:
from cloudooo.handler.ooo.application.openoffice import OpenOffice
from cloudooo.handler.ooo.util import waitStartDaemon, waitStopDaemon
from subprocess import Popen, PIPE
# patch OpenOffice._startProcess not to put bogus output to stderr,
# that prevents detecting the end of unit test.
def _startProcess(self, command, env):
"""Start OpenOffice.org process"""
for i in range(5):
self.stop()
waitStopDaemon(self, self.timeout)
self.process = Popen(command, stderr=PIPE,
close_fds=True,
env=env)
if not waitStartDaemon(self, self.timeout):
continue
if self._testOpenOffice(self.hostname, self.port):
return
OpenOffice._startProcess = _startProcess
class HandlerTestCase(unittest.TestCase): class HandlerTestCase(unittest.TestCase):
"""Test Case to load cloudooo conf.""" """Test Case to load cloudooo conf."""
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
import sys import sys
from pkg_resources import resource_filename from pkg_resources import resource_filename
import logging
import unittest import unittest
from time import sleep from time import sleep
from subprocess import Popen from subprocess import Popen
from ConfigParser import ConfigParser from configparser import ConfigParser
from argparse import ArgumentParser from argparse import ArgumentParser
from os import chdir, path, environ, curdir, remove from os import chdir, path, environ, curdir, remove
from cloudooo.tests import backportUnittest
from glob import glob from glob import glob
import psutil import psutil
from signal import SIGQUIT from signal import SIGQUIT
...@@ -23,10 +23,7 @@ def wait_use_port(pid, timeout_limit=30): ...@@ -23,10 +23,7 @@ def wait_use_port(pid, timeout_limit=30):
return False return False
def exit(msg): logger = logging.getLogger(__name__)
sys.stderr.write(msg)
sys.exit(0)
def run(): def run():
description = "Unit Test Runner for Handlers" description = "Unit Test Runner for Handlers"
...@@ -34,12 +31,18 @@ def run(): ...@@ -34,12 +31,18 @@ def run():
parser.add_argument('server_cloudooo_conf') parser.add_argument('server_cloudooo_conf')
parser.add_argument('test_name') parser.add_argument('test_name')
parser.add_argument('--timeout_limit', dest='timeout_limit', parser.add_argument('--timeout_limit', dest='timeout_limit',
type=long, default=30, type=int, default=30,
help="Timeout to waiting for the cloudooo stop") help="Timeout to waiting for the cloudooo stop")
parser.add_argument('--paster_path', dest='paster_path', parser.add_argument('--paster_path', dest='paster_path',
default='paster', default='paster',
help="Path to Paster script") help="Path to Paster script")
parser.add_argument('-v', '--verbose', action='store_true', help='Enable logging')
parser.add_argument(
'-D', '--debug', action='store_true',
help='Enable pdb on errors/failures') # XXX but does not show test output
namespace = parser.parse_args() namespace = parser.parse_args()
if namespace.verbose:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',)
environment_path = glob(path.join(resource_filename("cloudooo", "handler"), '*', 'tests')) environment_path = glob(path.join(resource_filename("cloudooo", "handler"), '*', 'tests'))
sys.path.extend(environment_path) sys.path.extend(environment_path)
server_cloudooo_conf = namespace.server_cloudooo_conf server_cloudooo_conf = namespace.server_cloudooo_conf
...@@ -50,32 +53,38 @@ def run(): ...@@ -50,32 +53,38 @@ def run():
environ['server_cloudooo_conf'] = server_cloudooo_conf environ['server_cloudooo_conf'] = server_cloudooo_conf
paster_path = namespace.paster_path paster_path = namespace.paster_path
python_extension = '.py'
if test_name[-3:] == python_extension:
test_name = test_name[:-3]
handler_path = None
for env_handler_path in environment_path:
full_path = path.join(env_handler_path, '%s%s' % (test_name,
python_extension))
if path.exists(full_path):
handler_path = env_handler_path
break
if handler_path is None:
exit("%s does not exists\n" % full_path)
from cloudooo.tests.handlerTestCase import startFakeEnvironment from cloudooo.tests.handlerTestCase import startFakeEnvironment
from cloudooo.tests.handlerTestCase import stopFakeEnvironment from cloudooo.tests.handlerTestCase import stopFakeEnvironment
config = ConfigParser() config = ConfigParser()
config.read(server_cloudooo_conf) config.read(server_cloudooo_conf)
if namespace.debug:
# XXX not really correct but enough to get a pdb prompt
suite = unittest.defaultTestLoader.loadTestsFromName(test_name)
module = __import__(list(suite)[0].__module__)
if module is unittest:
module = __import__(list(list(suite)[0])[0].__module__)
else:
module = __import__(test_name) module = __import__(test_name)
suite = unittest.defaultTestLoader.loadTestsFromModule(module)
handler_path = path.dirname(module.__file__)
DAEMON = getattr(module, 'DAEMON', False) DAEMON = getattr(module, 'DAEMON', False)
OPENOFFICE = getattr(module, 'OPENOFFICE', False) OPENOFFICE = getattr(module, 'OPENOFFICE', False)
TestRunner = backportUnittest.TextTestRunner def run_suite():
suite = unittest.defaultTestLoader.loadTestsFromModule(module) if namespace.debug:
import functools
suite.run = functools.partial(suite.run, debug=True)
try:
unittest.TextTestRunner(
verbosity=2,
warnings=None if sys.warnoptions else 'default',
).run(suite)
except:
import pdb; pdb.post_mortem()
raise
if DAEMON: if DAEMON:
log_file = '%s/cloudooo_test.log' % config.get('app:main', log_file = '%s/cloudooo_test.log' % config.get('app:main',
...@@ -85,20 +94,24 @@ def run(): ...@@ -85,20 +94,24 @@ def run():
command = [paster_path, 'serve', '--log-file', log_file, command = [paster_path, 'serve', '--log-file', log_file,
server_cloudooo_conf] server_cloudooo_conf]
process = Popen(command) process = Popen(command)
logger.debug("Started daemon %s", command)
wait_use_port(process.pid) wait_use_port(process.pid)
logger.debug("Daemon ready")
chdir(handler_path) chdir(handler_path)
try: try:
TestRunner(verbosity=2).run(suite) run_suite()
finally: finally:
process.send_signal(SIGQUIT) process.send_signal(SIGQUIT)
process.wait() process.wait()
elif OPENOFFICE: elif OPENOFFICE:
chdir(handler_path) chdir(handler_path)
logger.debug("Starting fake environment")
startFakeEnvironment(conf_path=server_cloudooo_conf) startFakeEnvironment(conf_path=server_cloudooo_conf)
logger.debug("Fake environment ready")
try: try:
TestRunner(verbosity=2).run(suite) run_suite()
finally: finally:
stopFakeEnvironment() stopFakeEnvironment()
else: else:
chdir(handler_path) chdir(handler_path)
TestRunner(verbosity=2).run(suite) run_suite()
...@@ -39,7 +39,7 @@ from erp5.util.testsuite import SubprocessError ...@@ -39,7 +39,7 @@ from erp5.util.testsuite import SubprocessError
from erp5.util import taskdistribution from erp5.util import taskdistribution
def _parsingErrorHandler(data, _): def _parsingErrorHandler(data, _):
print >> sys.stderr, 'Error parsing data:', repr(data) print('Error parsing data:', repr(data), file=sys.stderr)
taskdistribution.patchRPCParser(_parsingErrorHandler) taskdistribution.patchRPCParser(_parsingErrorHandler)
class TestSuite(BaseTestSuite): class TestSuite(BaseTestSuite):
...@@ -115,7 +115,7 @@ def run(): ...@@ -115,7 +115,7 @@ def run():
if test_result is not None: if test_result is not None:
assert revision == test_result.revision, (revision, test_result.revision) assert revision == test_result.revision, (revision, test_result.revision)
while suite.acquire(): while suite.acquire():
test = test_result.start(suite.running.keys()) test = test_result.start(list(suite.running.keys()))
if test is not None: if test is not None:
suite.start(test.name, lambda status_dict, __test=test: suite.start(test.name, lambda status_dict, __test=test:
__test.stop(**status_dict)) __test.stop(**status_dict))
......
...@@ -28,12 +28,12 @@ ...@@ -28,12 +28,12 @@
# #
############################################################################## ##############################################################################
import email
import email.message
import logging import logging
import mimetypes import mimetypes
import pkg_resources import pkg_resources
import os import os
import mimetools
import cStringIO
from zipfile import ZipFile, ZIP_DEFLATED from zipfile import ZipFile, ZIP_DEFLATED
logger = logging.getLogger('Cloudooo') logger = logging.getLogger('Cloudooo')
...@@ -96,7 +96,7 @@ convertStringToBool = ('false', 'true').index ...@@ -96,7 +96,7 @@ convertStringToBool = ('false', 'true').index
def zipTree(destination, *tree_path_list): def zipTree(destination, *tree_path_list):
""" """
destination may be a path or a StringIO destination may be a path or a file-like
tree_path_list is a list that may contain a path or a couple(path, archive_root) tree_path_list is a list that may contain a path or a couple(path, archive_root)
""" """
...@@ -126,17 +126,13 @@ def unzip(source, destination): ...@@ -126,17 +126,13 @@ def unzip(source, destination):
with ZipFile(source) as zipfile: with ZipFile(source) as zipfile:
zipfile.extractall(destination) zipfile.extractall(destination)
def parseContentType(content_type): def parseContentType(content_type:str) -> email.message.EmailMessage:
"""Parses `text/plain;charset="utf-8"` to a mimetools.Message object. """Parses `text/plain;charset="utf-8"` to an email object.
Note: Content type or MIME type are built like `maintype/subtype[;params]`. Note: Content type or MIME type are built like `maintype/subtype[;params]`.
parsed_content_type = parseContentType('text/plain;charset="utf-8"') parsed_content_type = parseContentType('text/plain;charset="utf-8"')
parsed_content_type.gettype() -> 'text/plain' parsed_content_type.get_content_type() -> 'text/plain'
parsed_content_type.getmaintype() -> 'text' parsed_content_type.get_charset() -> 'utf-8'
parsed_content_type.getsubtype() -> 'plain'
parsed_content_type.getplist() -> 'charset="utf-8"'
parsed_content_type.getparam('charset') -> 'utf-8'
parsed_content_type.typeheader -> 'text/plain;charset="utf-8"'
""" """
return mimetools.Message(cStringIO.StringIO("Content-Type:" + content_type.replace("\r\n", "\r\n\t"))) return email.message_from_string("Content-Type:" + content_type.replace("\r\n", "\r\n\t"))
...@@ -12,15 +12,29 @@ ...@@ -12,15 +12,29 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher import logging
from xmlrpc.server import SimpleXMLRPCDispatcher
logger = logging.getLogger(__name__)
class WSGIXMLRPCApplication(object): class ErrorLoggingXMLRPCDispatcher(SimpleXMLRPCDispatcher):
"""A XMLRPC Dispatcher which logs errors
"""
def _dispatch(self, method, params):
try:
return super()._dispatch(method, params)
except:
logger.exception("Error calling %s", method)
raise
class WSGIXMLRPCApplication:
"""Application to handle requests to the XMLRPC service""" """Application to handle requests to the XMLRPC service"""
def __init__(self, instance=None, methods=[]): def __init__(self, instance=None, methods=[]):
"""Create windmill xmlrpc dispatcher""" """Create windmill xmlrpc dispatcher"""
self.dispatcher = SimpleXMLRPCDispatcher(allow_none=True, self.dispatcher = ErrorLoggingXMLRPCDispatcher(
allow_none=True,
encoding=None) encoding=None)
if instance is not None: if instance is not None:
self.dispatcher.register_instance(instance) self.dispatcher.register_instance(instance)
...@@ -34,7 +48,7 @@ class WSGIXMLRPCApplication(object): ...@@ -34,7 +48,7 @@ class WSGIXMLRPCApplication(object):
return self.handle_POST(environ, start_response) return self.handle_POST(environ, start_response)
else: else:
start_response("400 Bad request", [('Content-Type', 'text/plain')]) start_response("400 Bad request", [('Content-Type', 'text/plain')])
return [''] return [b'']
def handle_POST(self, environ, start_response): def handle_POST(self, environ, start_response):
"""Handles the HTTP POST request. """Handles the HTTP POST request.
...@@ -60,9 +74,10 @@ class WSGIXMLRPCApplication(object): ...@@ -60,9 +74,10 @@ class WSGIXMLRPCApplication(object):
response = self.dispatcher._marshaled_dispatch( response = self.dispatcher._marshaled_dispatch(
data, getattr(self.dispatcher, '_dispatch', None) data, getattr(self.dispatcher, '_dispatch', None)
) )
response += '\n' response += b'\n'
except: # This should only happen if the module is buggy except: # This should only happen if the module is buggy
# internal error, report as HTTP server error # internal error, report as HTTP server error
logger.exception("Error serving request")
start_response("500 Server error", [('Content-Type', start_response("500 Server error", [('Content-Type',
'text/plain')]) 'text/plain')])
return [] return []
......
...@@ -12,7 +12,7 @@ install_requires = [ ...@@ -12,7 +12,7 @@ install_requires = [
'zope.interface', 'zope.interface',
'PasteDeploy', 'PasteDeploy',
'PasteScript[WSGIUtils]', 'PasteScript[WSGIUtils]',
'pyPdf', 'pyPdf>=3.6.0',
'psutil>=3.0.0', 'psutil>=3.0.0',
'lxml', 'lxml',
'python-magic', 'python-magic',
...@@ -28,7 +28,7 @@ setup(name='cloudooo', ...@@ -28,7 +28,7 @@ setup(name='cloudooo',
"Topic :: System :: Networking", "Topic :: System :: Networking",
"Topic :: System :: Operating System Kernels :: Linux", "Topic :: System :: Operating System Kernels :: Linux",
"Topic :: Internet :: WWW/HTTP :: WSGI", "Topic :: Internet :: WWW/HTTP :: WSGI",
"Programming Language :: Python :: 2.6", "Programming Language :: Python :: 3",
"Natural Language :: English", "Natural Language :: English",
"Framework :: Paste"], "Framework :: Paste"],
keywords='xmlrpc openoffice wsgi paste python', keywords='xmlrpc openoffice wsgi paste python',
...@@ -40,6 +40,7 @@ setup(name='cloudooo', ...@@ -40,6 +40,7 @@ setup(name='cloudooo',
include_package_data=True, include_package_data=True,
zip_safe=False, zip_safe=False,
install_requires=install_requires, install_requires=install_requires,
python_requires='>=3.8',
entry_points=""" entry_points="""
[paste.app_factory] [paste.app_factory]
main = cloudooo.paster_application:application main = cloudooo.paster_application:application
......
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