[MediaWiki-commits] [Gerrit] TIFF engine - change (thumbor/tiff-engine)

2015-12-23 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: TIFF engine
..


TIFF engine

Bug: T120203
Change-Id: I3a1c12707f329e7e49937b51de049719aacae597
---
A LICENSE
A requirements.txt
A setup.py
A tox.ini
A wikimedia_thumbor_tiff_engine/__init__.py
5 files changed, 123 insertions(+), 0 deletions(-)

Approvals:
  Ori.livneh: Looks good to me, approved
  Filippo Giunchedi: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/LICENSE b/LICENSE
new file mode 100644
index 000..133846d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 Gilles Dubuc, Wikimedia Foundation
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 000..6f55aaa
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+thumbor
+wikimedia_thumbor_base_engine
\ No newline at end of file
diff --git a/setup.py b/setup.py
new file mode 100644
index 000..26cb9aa
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+
+from setuptools import setup, find_packages
+
+
+setup(
+name='wikimedia_thumbor_tiff_engine',
+version='0.1.1',
+url='https://github.com/wikimedia/thumbor-tiff-engine',
+license='MIT',
+author='Gilles Dubuc, Wikimedia Foundation',
+description='Thumbor TIFF engine',
+packages=find_packages(),
+include_package_data=True,
+zip_safe=False,
+platforms='any',
+install_requires=[
+'thumbor',
+'wikimedia_thumbor_base_engine'
+],
+extras_require={
+'tests': [
+'pyvows',
+'coverage',
+],
+},
+classifiers=[
+'Development Status :: 4 - Beta',
+'Intended Audience :: Developers',
+'License :: OSI Approved :: MIT License',
+'Operating System :: OS Independent',
+'Programming Language :: Python',
+'Topic :: Software Development :: Libraries :: Python Modules'
+]
+)
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 000..9e359d2
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,12 @@
+[tox]
+minversion = 1.6
+skipsdist = True
+envlist = flake8
+
+[testenv]
+setenv = VIRTUAL_ENV={envdir}
+deps = -r{toxinidir}/requirements.txt
+
+[testenv:flake8]
+commands = flake8 {posargs}
+deps = flake8
diff --git a/wikimedia_thumbor_tiff_engine/__init__.py 
b/wikimedia_thumbor_tiff_engine/__init__.py
new file mode 100644
index 000..642dcc2
--- /dev/null
+++ b/wikimedia_thumbor_tiff_engine/__init__.py
@@ -0,0 +1,52 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# thumbor imaging service
+# https://github.com/thumbor/thumbor/wiki
+
+# Licensed under the MIT license:
+# http://www.opensource.org/licenses/mit-license
+# Copyright (c) 2011 globo.com timeh...@corp.globo.com
+# Copyright (c) 2015 Wikimedia Foundation
+
+# TIFF engine
+
+from wikimedia_thumbor_base_engine import BaseWikimediaEngine
+
+
+BaseWikimediaEngine.add_format(
+'image/tiff',
+'.tiff',
+lambda buffer: buffer.startswith('II*\x00') or buffer.startswith('MM\x00*')
+)
+
+
+class Engine(BaseWikimediaEngine):
+def should_run(self, extension, buffer):
+return extension == '.tiff'
+
+def create_image(self, buffer):
+self.tiff_buffer = buffer
+img = super(Engine, self).create_image(buffer)
+
+try:
+page = self.context.request.page
+img.seek(page - 1)
+except (AttributeError, EOFError):
+page = 1
+img.seek(0)
+
+self.extension = '.jpg'
+
+return img
+
+def read(self, extension=None, quality=None):
+if extension == '.tiff' and quality is None:
+# We're saving the source, let's save the TIFF
+return self.tiff_buffer
+
+# Beyond this point we're saving the JPG result
+if extension == '.tiff':
+extension = '.jpg'
+
+

[MediaWiki-commits] [Gerrit] TIFF engine - change (thumbor/tiff-engine)

2015-12-06 Thread Gilles (Code Review)
Gilles has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/257187

Change subject: TIFF engine
..

TIFF engine

Bug: T120203
Change-Id: I3a1c12707f329e7e49937b51de049719aacae597
---
A LICENSE
A requirements.txt
A setup.py
A tox.ini
A wikimedia_thumbor_tiff_engine/__init__.py
5 files changed, 131 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/thumbor/tiff-engine 
refs/changes/87/257187/1

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 000..133846d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 Gilles Dubuc, Wikimedia Foundation
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 000..39195db
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+thumbor
\ No newline at end of file
diff --git a/setup.py b/setup.py
new file mode 100644
index 000..9112cdb
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+
+from setuptools import setup, find_packages
+
+
+setup(
+name='wikimedia_thumbor_tiff_engine',
+version='0.1.1',
+url='https://github.com/wikimedia/thumbor-tiff-engine',
+license='MIT',
+author='Gilles Dubuc, Wikimedia Foundation',
+description='Thumbor TIFF engine',
+packages=find_packages(),
+include_package_data=True,
+zip_safe=False,
+platforms='any',
+install_requires=[
+'thumbor',
+],
+extras_require={
+'tests': [
+'pyvows',
+'coverage',
+],
+},
+classifiers=[
+'Development Status :: 4 - Beta',
+'Intended Audience :: Developers',
+'License :: OSI Approved :: MIT License',
+'Operating System :: OS Independent',
+'Programming Language :: Python',
+'Topic :: Software Development :: Libraries :: Python Modules'
+]
+)
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 000..9e359d2
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,12 @@
+[tox]
+minversion = 1.6
+skipsdist = True
+envlist = flake8
+
+[testenv]
+setenv = VIRTUAL_ENV={envdir}
+deps = -r{toxinidir}/requirements.txt
+
+[testenv:flake8]
+commands = flake8 {posargs}
+deps = flake8
diff --git a/wikimedia_thumbor_tiff_engine/__init__.py 
b/wikimedia_thumbor_tiff_engine/__init__.py
new file mode 100644
index 000..bf7c183
--- /dev/null
+++ b/wikimedia_thumbor_tiff_engine/__init__.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# thumbor imaging service
+# https://github.com/thumbor/thumbor/wiki
+
+# Licensed under the MIT license:
+# http://www.opensource.org/licenses/mit-license
+# Copyright (c) 2011 globo.com timeh...@corp.globo.com
+# Copyright (c) 2015 Wikimedia Foundation
+
+# TIFF engine
+
+from thumbor.engines import BaseEngine
+from thumbor.engines.pil import Engine as PilEngine
+from thumbor.utils import EXTENSION
+
+
+# Unfortunately there is no elegant way to extend Thumbor to support
+# a new MIME type, which is why this monkey-patching is done here
+EXTENSION['image/tiff'] = '.tiff'
+
+old_get_mimetype = BaseEngine.get_mimetype
+
+@classmethod
+def new_get_mimetype(cls, buffer):
+if (buffer.startswith('II*\x00') or buffer.startswith('MM\x00*')):
+return 'image/tiff'
+
+return old_get_mimetype(buffer)
+
+BaseEngine.get_mimetype = new_get_mimetype
+
+class Engine(PilEngine):
+def should_run(self, extension, buffer):
+return (extension == '.tiff')
+
+def create_image(self, buffer):
+self.tiff_buffer = buffer
+img = super(Engine, self).create_image(buffer)
+
+try:
+page = self.context.request.page
+img.seek(page - 1)
+except (AttributeError, EOFError):
+page = 1
+img.seek(0)
+
+self.extension = '.jpg'
+
+return img
+
+def read(self, extension=None, quality=None):
+if (extension ==