Commit 7ebe0217 authored by Gabriel Monnerat's avatar Gabriel Monnerat

Initial commit to multi handler. Addcode to select handler according to...

Initial commit to multi handler. Addcode to select handler according to mimetype registry declared in cloudooo.conf. The next step is refactor this code and add specific tests

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@43561 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 46a05a84
......@@ -32,10 +32,27 @@ from base64 import encodestring, decodestring
from zope.interface import implements
from interfaces.manager import IManager, IERP5Compatibility
from handler.ooo.handler import OOHandler
from handler.pdf.handler import PDFHandler
from handler.ffmpeg.handler import FFMPEGHandler
from handler.ooo.mimemapper import mimemapper
from utils.utils import logger
from fnmatch import fnmatch
import mimetypes
handler_dict = {"pdf": PDFHandler, "ooo": OOHandler, "ffmpeg": FFMPEGHandler}
def getHandlerObject(source_format, destination_format, mimetype_registry):
"""Select handler according to source_format and destination_format"""
source_mimetype = mimetypes.types_map.get('.%s' % source_format, "*")
destination_mimetype = mimetypes.types_map.get('.%s' % destination_format, '*')
for pattern in mimetype_registry:
registry_list = pattern.split()
if fnmatch(source_mimetype, registry_list[0]) and \
fnmatch(destination_mimetype, registry_list[1]):
return handler_dict[registry_list[2]]
return handler_dict["ooo"]
class Manager(object):
"""Manipulates requisitons of client and temporary files in file system."""
implements(IManager, IERP5Compatibility)
......@@ -44,6 +61,7 @@ class Manager(object):
"""Need pass the path where the temporary document will be created."""
self._path_tmp_dir = path_tmp_dir
self.kw = kw
self.mimetype_registry = self.kw.pop("mimetype_registry")
def convertFile(self, file, source_format, destination_format, zip=False,
refresh=False):
......@@ -60,7 +78,10 @@ class Manager(object):
"or is invalid" % destination_format)
self.kw['zip'] = zip
self.kw['refresh'] = refresh
document = OOHandler(self._path_tmp_dir,
handler = getHandlerObject(source_format,
destination_format,
self.mimetype_registry)
document = handler(self._path_tmp_dir,
decodestring(file),
source_format,
**self.kw)
......@@ -100,7 +121,10 @@ class Manager(object):
{"title":"abc","description":...})
return encodestring(document_with_metadata)
"""
document = OOHandler(self._path_tmp_dir,
handler = getHandlerObject(source_format,
"*",
self.mimetype_registry)
document = handler(self._path_tmp_dir,
decodestring(file),
source_format,
**self.kw)
......@@ -123,12 +147,15 @@ class Manager(object):
Note that all keys of the dictionary have the first word in uppercase.
"""
document = OOHandler(self._path_tmp_dir,
handler = getHandlerObject(source_format,
"*",
self.mimetype_registry)
document = handler(self._path_tmp_dir,
decodestring(file),
source_format,
**self.kw)
metadata_dict = document.getMetadata(base_document)
metadata_dict['Data'] = encodestring(metadata_dict['Data'])
metadata_dict['Data'] = encodestring(metadata_dict.get('Data', ''))
return metadata_dict
def getAllowedExtensionList(self, request_dict={}):
......
......@@ -110,6 +110,9 @@ def application(global_config, **local_config):
mimemapper.loadFilterList(application_hostname,
openoffice_port, **kw)
openoffice.release()
kw["mimetype_registry"] = filter(None,
local_config.get("mimetype_registry", "").split("\n"))
kw["env"] = environment_dict
from manager import Manager
cloudooo_manager = Manager(cloudooo_path_tmp_dir, **kw)
return WSGIXMLRPCApplication(instance=cloudooo_manager)
......@@ -41,6 +41,14 @@ openoffice_port = 4062
# specify preferrable executable locations
# env-PATH = /opt/erp5/trunk/parts/imagemagick/bin:/opt/erp5/trunk/parts/w3m/bin
#
# Mimetype Registry
# It is used to select the handler that will be used in conversion
#
mimetype_registry =
application/pdf * pdf
video/* * ffmpeg
[server:main]
use = egg:PasteScript#wsgiutils
host = 0.0.0.0
......
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