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 ...@@ -32,10 +32,27 @@ from base64 import encodestring, decodestring
from zope.interface import implements from zope.interface import implements
from interfaces.manager import IManager, IERP5Compatibility from interfaces.manager import IManager, IERP5Compatibility
from handler.ooo.handler import OOHandler 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 handler.ooo.mimemapper import mimemapper
from utils.utils import logger 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): class Manager(object):
"""Manipulates requisitons of client and temporary files in file system.""" """Manipulates requisitons of client and temporary files in file system."""
implements(IManager, IERP5Compatibility) implements(IManager, IERP5Compatibility)
...@@ -44,6 +61,7 @@ class Manager(object): ...@@ -44,6 +61,7 @@ class Manager(object):
"""Need pass the path where the temporary document will be created.""" """Need pass the path where the temporary document will be created."""
self._path_tmp_dir = path_tmp_dir self._path_tmp_dir = path_tmp_dir
self.kw = kw self.kw = kw
self.mimetype_registry = self.kw.pop("mimetype_registry")
def convertFile(self, file, source_format, destination_format, zip=False, def convertFile(self, file, source_format, destination_format, zip=False,
refresh=False): refresh=False):
...@@ -60,10 +78,13 @@ class Manager(object): ...@@ -60,10 +78,13 @@ class Manager(object):
"or is invalid" % destination_format) "or is invalid" % destination_format)
self.kw['zip'] = zip self.kw['zip'] = zip
self.kw['refresh'] = refresh self.kw['refresh'] = refresh
document = OOHandler(self._path_tmp_dir, handler = getHandlerObject(source_format,
decodestring(file), destination_format,
source_format, self.mimetype_registry)
**self.kw) document = handler(self._path_tmp_dir,
decodestring(file),
source_format,
**self.kw)
decode_data = document.convert(destination_format) decode_data = document.convert(destination_format)
return encodestring(decode_data) return encodestring(decode_data)
...@@ -100,10 +121,13 @@ class Manager(object): ...@@ -100,10 +121,13 @@ class Manager(object):
{"title":"abc","description":...}) {"title":"abc","description":...})
return encodestring(document_with_metadata) return encodestring(document_with_metadata)
""" """
document = OOHandler(self._path_tmp_dir, handler = getHandlerObject(source_format,
decodestring(file), "*",
source_format, self.mimetype_registry)
**self.kw) document = handler(self._path_tmp_dir,
decodestring(file),
source_format,
**self.kw)
metadata_dict = dict([(key.capitalize(), value) \ metadata_dict = dict([(key.capitalize(), value) \
for key, value in metadata_dict.iteritems()]) for key, value in metadata_dict.iteritems()])
decode_data = document.setMetadata(metadata_dict) decode_data = document.setMetadata(metadata_dict)
...@@ -123,12 +147,15 @@ class Manager(object): ...@@ -123,12 +147,15 @@ class Manager(object):
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.
""" """
document = OOHandler(self._path_tmp_dir, handler = getHandlerObject(source_format,
decodestring(file), "*",
source_format, self.mimetype_registry)
**self.kw) document = handler(self._path_tmp_dir,
decodestring(file),
source_format,
**self.kw)
metadata_dict = document.getMetadata(base_document) metadata_dict = document.getMetadata(base_document)
metadata_dict['Data'] = encodestring(metadata_dict['Data']) metadata_dict['Data'] = encodestring(metadata_dict.get('Data', ''))
return metadata_dict return metadata_dict
def getAllowedExtensionList(self, request_dict={}): def getAllowedExtensionList(self, request_dict={}):
......
...@@ -110,6 +110,9 @@ def application(global_config, **local_config): ...@@ -110,6 +110,9 @@ def application(global_config, **local_config):
mimemapper.loadFilterList(application_hostname, mimemapper.loadFilterList(application_hostname,
openoffice_port, **kw) openoffice_port, **kw)
openoffice.release() openoffice.release()
kw["mimetype_registry"] = filter(None,
local_config.get("mimetype_registry", "").split("\n"))
kw["env"] = environment_dict
from manager import Manager from manager import Manager
cloudooo_manager = Manager(cloudooo_path_tmp_dir, **kw) cloudooo_manager = Manager(cloudooo_path_tmp_dir, **kw)
return WSGIXMLRPCApplication(instance=cloudooo_manager) return WSGIXMLRPCApplication(instance=cloudooo_manager)
...@@ -41,6 +41,14 @@ openoffice_port = 4062 ...@@ -41,6 +41,14 @@ openoffice_port = 4062
# specify preferrable executable locations # specify preferrable executable locations
# env-PATH = /opt/erp5/trunk/parts/imagemagick/bin:/opt/erp5/trunk/parts/w3m/bin # 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] [server:main]
use = egg:PasteScript#wsgiutils use = egg:PasteScript#wsgiutils
host = 0.0.0.0 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