[Erp5-report] r43633 gabriel - in /erp5/trunk/utils/cloudooo/cloudooo: ./ handler/imagemagi...

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Feb 23 21:16:34 CET 2011


Author: gabriel
Date: Wed Feb 23 21:16:34 2011
New Revision: 43633

URL: http://svn.erp5.org?rev=43633&view=rev
Log:
Initial commit to imagemagick handler

Added:
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/__init__.py
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/handler.py
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/__init__.py
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/data/
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/data/test.png   (with props)
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/runImageMagickHandlerUnitTest.py
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testImageMagickHandler.py
    erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testServer.py
Modified:
    erp5/trunk/utils/cloudooo/cloudooo/manager.py
    erp5/trunk/utils/cloudooo/cloudooo/mime.types

Added: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/__init__.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/__init__.py?rev=43633&view=auto
==============================================================================
    (empty)

Added: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/handler.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/handler.py?rev=43633&view=auto
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/handler.py (added)
+++ erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/handler.py [utf8] Wed Feb 23 21:16:34 2011
@@ -0,0 +1,88 @@
+##############################################################################
+#
+# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
+#                    Gabriel M. Monnerat <gabriel at tiolive.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+##############################################################################
+
+import re
+from zope.interface import implements
+from cloudooo.interfaces.handler import IHandler
+from cloudooo.file import File
+from subprocess import Popen, PIPE
+from tempfile import mktemp
+
+
+class ImageMagickHandler(object):
+  """ImageMagicHandler is used to handler images."""
+
+  implements(IHandler)
+
+  def __init__(self, base_folder_url, data, source_format, **kw):
+    """ Load pdf document """
+    self.base_folder_url = base_folder_url
+    self.document = File(base_folder_url, data, source_format)
+    self.environment = kw.get("env", {})
+
+  def convert(self, destination_format=None, **kw):
+    """Convert a image"""
+    output_url = mktemp(suffix='.%s' % destination_format,
+                        dir=self.base_folder_url)
+    command = ["convert", self.document.getUrl(), output_url]
+    stdout, stderr = Popen(command,
+                          stdout=PIPE,
+                          stderr=PIPE,
+                          env=self.environment).communicate()
+    try:
+      return open(output_url).read()
+    finally:
+      self.document.trash()
+
+  def getMetadata(self, base_document=False):
+    """Returns a dictionary with all metadata of document.
+    along with the metadata.
+    """
+    command = ["identify", "-verbose", self.document.getUrl()]
+    stdout, stderr = Popen(command,
+                          stdout=PIPE,
+                          stderr=PIPE,
+                          env=self.environment).communicate()
+    
+    metadata_dict = {}
+    for std in stdout.split("\n"):
+      std = std.strip()
+      if re.search("^[a-zA-Z]", std):
+        if std.count(":") > 1:
+          key, value = re.compile(".*\:\ ").split(std)
+        else:
+          key, value = std.split(":")
+        metadata_dict[key] = value.strip()
+    return metadata_dict
+
+  def setMetadata(self, metadata={}):
+    """Returns image with new metadata.
+    Keyword arguments:
+    metadata -- expected an dictionary with metadata.
+    """
+    raise NotImplementedError 

Added: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/__init__.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/__init__.py?rev=43633&view=auto
==============================================================================
    (empty)

Added: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/data/test.png
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/data/test.png?rev=43633&view=auto
==============================================================================
Binary file - no diff available.

Propchange: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/data/test.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/runImageMagickHandlerUnitTest.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/runImageMagickHandlerUnitTest.py?rev=43633&view=auto
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/runImageMagickHandlerUnitTest.py (added)
+++ erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/runImageMagickHandlerUnitTest.py [utf8] Wed Feb 23 21:16:34 2011
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+##############################################################################
+#
+# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
+#                    Gabriel M. Monnerat <gabriel at tiolive.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+##############################################################################
+
+from cloudooo.handler.tests import runHandlerUnitTest
+
+def run():
+  runHandlerUnitTest.run("imagemagick")

Added: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testImageMagickHandler.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testImageMagickHandler.py?rev=43633&view=auto
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testImageMagickHandler.py (added)
+++ erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testImageMagickHandler.py [utf8] Wed Feb 23 21:16:34 2011
@@ -0,0 +1,66 @@
+##############################################################################
+#
+# Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
+#                    Gabriel M. Monnerat <gabriel at tiolive.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+##############################################################################
+
+
+import unittest
+import magic
+from cloudooo.handler.imagemagick.handler import ImageMagickHandler
+from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
+
+
+class TestImageMagickHandler(HandlerTestCase):
+
+  def afterSetUp(self):
+    self.kw = dict(env=dict(PATH=self.env_path))
+
+  def testConvertPNGtoJPG(self):
+    """Test conversion of png to jpg"""
+    png_file = open("data/test.png").read()
+    handler = ImageMagickHandler(self.tmp_url, png_file, "png", **self.kw)
+    jpg_file = handler.convert("jpg")
+    mime = magic.Magic(mime=True)
+    jpg_mimetype = mime.from_buffer(jpg_file)
+    self.assertEquals("image/jpeg", jpg_mimetype)
+
+  def testgetMetadataFromImage(self):
+    """Test if metadata is extracted from image correctly"""
+    png_file = open("data/test.png").read()
+    handler = ImageMagickHandler(self.tmp_url, png_file, "png", **self.kw)
+    metadata = handler.getMetadata()
+    self.assertEquals(metadata.get("Compression"), "Zip")
+    self.assertEquals(metadata.get("Colorspace"), "RGB")
+    self.assertEquals(metadata.get("Matte color"), "grey74")
+
+  def testsetMetadata(self):
+    """ Test if metadata are inserted correclty """
+    handler = ImageMagickHandler(self.tmp_url, "", "png", **self.kw)
+    self.assertRaises(NotImplementedError, handler.setMetadata)
+
+
+def test_suite():
+  return make_suite(TestImageMagickHandler)

Added: erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testServer.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testServer.py?rev=43633&view=auto
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testServer.py (added)
+++ erp5/trunk/utils/cloudooo/cloudooo/handler/imagemagick/tests/testServer.py [utf8] Wed Feb 23 21:16:34 2011
@@ -0,0 +1,64 @@
+##############################################################################
+#
+# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
+#                    Gabriel M. Monnerat <gabriel at tiolive.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+##############################################################################
+
+from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
+from xmlrpclib import ServerProxy
+from os.path import join
+from base64 import encodestring, decodestring
+from magic import Magic
+
+DAEMON = True
+
+
+class TestServer(HandlerTestCase):
+  """Test XmlRpc Server. Needs cloudooo server started"""
+
+  def afterSetUp(self):
+    """Creates a connection with cloudooo server"""
+    self.proxy = ServerProxy("http://%s:%s/RPC2" % \
+        (self.hostname, self.cloudooo_port), allow_none=True)
+
+  def testConvertPNGtoJPG(self):
+    """Converts png to jpg"""
+    data = open(join('data', 'test.png'), 'r').read()
+    document = self.proxy.convertFile(encodestring(data),
+                                      "png",
+                                      "jpg")
+    mime = Magic(mime=True)
+    mimetype = mime.from_buffer(decodestring(document))
+    self.assertEquals(mimetype, "image/jpeg")
+  
+  def testGetMetadataFromPNG(self):
+    """test if metadata are extracted correctly"""
+    data = open(join('data', 'test.png'), 'r').read()
+    metadata = self.proxy.getFileMetadataItemList(encodestring(data), "png")
+    self.assertEquals(metadata["Compression"], 'Zip')
+
+
+def test_suite():
+  return make_suite(TestServer)

Modified: erp5/trunk/utils/cloudooo/cloudooo/manager.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/manager.py?rev=43633&r1=43632&r2=43633&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/manager.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/manager.py [utf8] Wed Feb 23 21:16:34 2011
@@ -34,6 +34,7 @@ from interfaces.manager import IManager,
 from cloudooo.handler.ooo.handler import OOHandler
 from cloudooo.handler.pdf.handler import PDFHandler
 from cloudooo.handler.ffmpeg.handler import FFMPEGHandler
+from cloudooo.handler.imagemagick.handler import ImageMagickHandler
 from handler.ooo.mimemapper import mimemapper
 from utils.utils import logger
 from fnmatch import fnmatch
@@ -41,7 +42,10 @@ import mimetypes
 import pkg_resources
 
 
-HANDLER_DICT = {"pdf": PDFHandler, "ooo": OOHandler, "ffmpeg": FFMPEGHandler}
+HANDLER_DICT = {"pdf": PDFHandler,
+                "ooo": OOHandler,
+                "ffmpeg": FFMPEGHandler,
+                "imagemagick": ImageMagickHandler}
 
 
 def getHandlerObject(source_format, destination_format, mimetype_registry):

Modified: erp5/trunk/utils/cloudooo/cloudooo/mime.types
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/mime.types?rev=43633&r1=43632&r2=43633&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/mime.types [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/mime.types [utf8] Wed Feb 23 21:16:34 2011
@@ -18,5 +18,8 @@ application/vnd.oasis.opendocument.text 
 # Image
 #
 image/gif                                                 gif
+#
+# Audio
+#
 audio/mp4                                                 mp4
 audio/mp3                                                 mp3



More information about the Erp5-report mailing list