Commit 377ccb42 authored by Gabriel Monnerat's avatar Gabriel Monnerat

Initial commit to mimetypes registry. This file is used to select the handler...

Initial commit to mimetypes registry. This file is used to select the handler before each conversion.

- add test to check if the file with mimetypes is loaded correctly.
- refactor code to load mime.types before the server starts


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@43627 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 9829e4ac
...@@ -29,9 +29,9 @@ ...@@ -29,9 +29,9 @@
import unittest import unittest
import logging import logging
from cloudooo.utils.utils import logger, configureLogger, \ from cloudooo.utils.utils import logger, configureLogger, \
convertStringToBool convertStringToBool, loadMimetypeList
from cloudooo.handler.tests.handlerTestCase import make_suite from cloudooo.handler.tests.handlerTestCase import make_suite
import mimetypes
class TestUtils(unittest.TestCase): class TestUtils(unittest.TestCase):
"""Test Utils""" """Test Utils"""
...@@ -53,6 +53,14 @@ class TestUtils(unittest.TestCase): ...@@ -53,6 +53,14 @@ class TestUtils(unittest.TestCase):
self.assertEquals(convertStringToBool('faLse'), False) self.assertEquals(convertStringToBool('faLse'), False)
self.assertEquals(convertStringToBool(''), None) self.assertEquals(convertStringToBool(''), None)
def testLoadMimetypelist(self):
"""Test if the file with mimetypes is loaded correctly"""
self.assertEquals(mimetypes.types_map.get(".ogv"), None)
self.assertEquals(mimetypes.types_map.get(".3gp"), None)
loadMimetypeList()
self.assertEquals(mimetypes.types_map.get(".ogv"), "application/ogv")
self.assertEquals(mimetypes.types_map.get(".3gp"), "video/3gpp")
def test_suite(): def test_suite():
return make_suite(TestUtils) return make_suite(TestUtils)
......
...@@ -31,16 +31,18 @@ from mimetypes import guess_all_extensions, guess_extension ...@@ -31,16 +31,18 @@ from mimetypes import guess_all_extensions, guess_extension
from base64 import encodestring, decodestring 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 cloudooo.handler.ooo.handler import OOHandler
from handler.pdf.handler import PDFHandler from cloudooo.handler.pdf.handler import PDFHandler
from handler.ffmpeg.handler import FFMPEGHandler from cloudooo.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 from fnmatch import fnmatch
import mimetypes import mimetypes
import pkg_resources
handler_dict = {"pdf": PDFHandler, "ooo": OOHandler, "ffmpeg": FFMPEGHandler} HANDLER_DICT = {"pdf": PDFHandler, "ooo": OOHandler, "ffmpeg": FFMPEGHandler}
def getHandlerObject(source_format, destination_format, mimetype_registry): def getHandlerObject(source_format, destination_format, mimetype_registry):
"""Select handler according to source_format and destination_format""" """Select handler according to source_format and destination_format"""
...@@ -50,8 +52,9 @@ def getHandlerObject(source_format, destination_format, mimetype_registry): ...@@ -50,8 +52,9 @@ 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]): fnmatch(destination_mimetype, registry_list[1]):
return handler_dict[registry_list[2]] return HANDLER_DICT[registry_list[2]]
return handler_dict["ooo"] 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."""
......
#
# Video
#
application/ogv ogv
application/ogg ogg
video/3gpp 3gp
video/avi avi
video/mpg mpg
video/mpeg mpeg
video/mp4v-es mp4
#
# Text
#
application/vnd.oasis.opendocument.presentation odp
application/vnd.oasis.opendocument.spreadsheet ods
application/vnd.oasis.opendocument.text odt
#
# Image
#
image/gif gif
audio/mp4 mp4
audio/mp3 mp3
...@@ -33,7 +33,7 @@ import os ...@@ -33,7 +33,7 @@ import os
import cloudooo.handler.ooo.monitor as monitor import cloudooo.handler.ooo.monitor as monitor
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.wsgixmlrpcapplication import WSGIXMLRPCApplication from cloudooo.wsgixmlrpcapplication import WSGIXMLRPCApplication
from cloudooo.utils.utils import convertStringToBool, configureLogger from cloudooo.utils import utils
from cloudooo.handler.ooo.mimemapper import mimemapper from cloudooo.handler.ooo.mimemapper import mimemapper
...@@ -69,8 +69,8 @@ def application(global_config, **local_config): ...@@ -69,8 +69,8 @@ def application(global_config, **local_config):
value = '%s:%s' % (value, current_value) value = '%s:%s' % (value, current_value)
environment_dict[variable_name] = value environment_dict[variable_name] = value
gc.enable() gc.enable()
debug_mode = convertStringToBool(local_config.get('debug_mode')) debug_mode = utils.convertStringToBool(local_config.get('debug_mode'))
configureLogger(debug_mode=debug_mode) utils.configureLogger(debug_mode=debug_mode)
# path of directory to run cloudooo # path of directory to run cloudooo
working_path = local_config.get('working_path') working_path = local_config.get('working_path')
if not path.exists(working_path): if not path.exists(working_path):
...@@ -93,7 +93,7 @@ def application(global_config, **local_config): ...@@ -93,7 +93,7 @@ def application(global_config, **local_config):
environment_dict=environment_dict, environment_dict=environment_dict,
) )
openoffice.start() openoffice.start()
utils.loadMimetypeList()
monitor.load(local_config) monitor.load(local_config)
timeout_response = int(local_config.get('timeout_response')) timeout_response = int(local_config.get('timeout_response'))
kw = dict(uno_path=local_config.get('uno_path'), kw = dict(uno_path=local_config.get('uno_path'),
......
...@@ -48,6 +48,7 @@ openoffice_port = 4062 ...@@ -48,6 +48,7 @@ openoffice_port = 4062
mimetype_registry = mimetype_registry =
application/pdf * pdf application/pdf * pdf
video/* * ffmpeg video/* * ffmpeg
image/* * imagemagick
[server:main] [server:main]
use = egg:PasteScript#wsgiutils use = egg:PasteScript#wsgiutils
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
############################################################################## ##############################################################################
import logging import logging
import mimetypes
import pkg_resources
logger = logging.getLogger('Cloudooo') logger = logging.getLogger('Cloudooo')
...@@ -46,6 +48,12 @@ PYTHON_ENVIRONMENT = [ ...@@ -46,6 +48,12 @@ PYTHON_ENVIRONMENT = [
] ]
def loadMimetypeList():
mime_types_url = pkg_resources.resource_filename("cloudooo",
"mime.types")
mimetypes.init(files=[mime_types_url,])
def configureLogger(level=None, debug_mode=False): def configureLogger(level=None, debug_mode=False):
"""Configure logger. """Configure logger.
Keyword arguments: Keyword arguments:
......
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