Commit a3248e0a authored by Gabriel Monnerat's avatar Gabriel Monnerat

refactor code to load the handler list defined on cloudooo.conf. Later, if one...

refactor code to load the handler list defined on cloudooo.conf. Later, if one handler is not defined, this handler is not enable on cloudooo

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@43738 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 043d9978
...@@ -44,7 +44,8 @@ from cloudooo.interfaces.handler import IHandler ...@@ -44,7 +44,8 @@ from cloudooo.interfaces.handler import IHandler
from types import TypeType from types import TypeType
def getHandlerObject(source_format, destination_format, mimetype_registry): def getHandlerObject(source_format, destination_format,
mimetype_registry, handler_dict):
"""Select handler according to source_format and destination_format""" """Select handler according to source_format and destination_format"""
source_mimetype = mimetypes.types_map.get('.%s' % source_format, "*") source_mimetype = mimetypes.types_map.get('.%s' % source_format, "*")
destination_mimetype = mimetypes.types_map.get('.%s' % destination_format, "*") destination_mimetype = mimetypes.types_map.get('.%s' % destination_format, "*")
...@@ -52,17 +53,12 @@ def getHandlerObject(source_format, destination_format, mimetype_registry): ...@@ -52,17 +53,12 @@ def getHandlerObject(source_format, destination_format, mimetype_registry):
registry_list = pattern.split() registry_list = pattern.split()
if fnmatch(source_mimetype, registry_list[0]) and \ if fnmatch(source_mimetype, registry_list[0]) and \
(fnmatch(destination_mimetype, registry_list[1]) or destination_format is None): (fnmatch(destination_mimetype, registry_list[1]) or destination_format is None):
handler_name = "cloudooo.handler.%s.handler" % registry_list[2] handler_name = registry_list[2]
__import__(handler_name) import_name = "cloudooo.handler.%s.handler" % handler_name
handler = sys.modules[handler_name] if import_name not in sys.modules:
# XXX - Ugly and slow way to find the Handler Object __import__(import_name)
for name in iter(dir(handler)): handler = sys.modules[import_name]
if not name.endswith("Handler"): return getattr(handler, handler_dict[registry_list[2]])
continue
obj = getattr(handler, name)
if type(obj) == TypeType and IHandler.implementedBy(obj):
return obj
raise ValueError("No Handler Enabled for this conversion")
class Manager(object): class Manager(object):
...@@ -75,6 +71,7 @@ class Manager(object): ...@@ -75,6 +71,7 @@ class Manager(object):
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") self.mimetype_registry = self.kw.pop("mimetype_registry")
self.handler_dict = self.kw.pop("handler_dict")
def convertFile(self, file, source_format, destination_format, zip=False, def convertFile(self, file, source_format, destination_format, zip=False,
refresh=False): refresh=False):
...@@ -90,7 +87,8 @@ class Manager(object): ...@@ -90,7 +87,8 @@ class Manager(object):
self.kw['refresh'] = refresh self.kw['refresh'] = refresh
handler = getHandlerObject(source_format, handler = getHandlerObject(source_format,
destination_format, destination_format,
self.mimetype_registry) self.mimetype_registry,
self.handler_dict)
document = handler(self._path_tmp_dir, document = handler(self._path_tmp_dir,
decodestring(file), decodestring(file),
source_format, source_format,
...@@ -108,7 +106,8 @@ class Manager(object): ...@@ -108,7 +106,8 @@ class Manager(object):
""" """
handler = getHandlerObject(source_format, handler = getHandlerObject(source_format,
None, None,
self.mimetype_registry) self.mimetype_registry,
self.handler_dict)
document = handler(self._path_tmp_dir, document = handler(self._path_tmp_dir,
decodestring(file), decodestring(file),
source_format, source_format,
...@@ -134,7 +133,8 @@ class Manager(object): ...@@ -134,7 +133,8 @@ class Manager(object):
""" """
handler = getHandlerObject(source_format, handler = getHandlerObject(source_format,
None, None,
self.mimetype_registry) self.mimetype_registry,
self.handler_dict)
document = handler(self._path_tmp_dir, document = handler(self._path_tmp_dir,
decodestring(file), decodestring(file),
source_format, source_format,
......
...@@ -111,7 +111,14 @@ def application(global_config, **local_config): ...@@ -111,7 +111,14 @@ def application(global_config, **local_config):
openoffice_port, **kw) openoffice_port, **kw)
openoffice.release() openoffice.release()
kw["mimetype_registry"] = filter(None, kw["mimetype_registry"] = filter(None,
local_config.get("mimetype_registry", "").split("\n")) local_config.get("mimetype_registry",
"").split("\n"))
kw["handler_dict"] = {}
handler_mapping_list = local_config.get("handler_mapping", "").split("\n")
for line in filter(None, handler_mapping_list):
key, value = line.strip().split()
kw["handler_dict"][key] = value
kw["env"] = environment_dict 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)
......
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