Skip to content

Commit

Permalink
Add support for Python 2 (IN-7070)
Browse files Browse the repository at this point in the history
  • Loading branch information
thms-rmb committed Aug 3, 2022
1 parent d78aed8 commit e12fc1b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 91 deletions.
39 changes: 10 additions & 29 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,21 @@ name: CI

on:
push:
branches: master
branches: [master]
pull_request:
branches: master
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 3 * * 6'
workflow_dispatch:
inputs:
reason:
description: 'Reason'
required: false
default: 'Manual trigger'
branches: [master]

jobs:
Tests:
runs-on: ubuntu-20.04
services:
redis:
image: redis:6
ports:
- 6379:6379
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
requirements-level: [pypi]
cache-service: [redis]
python-version: [2.7, 3.7, 3.8, 3.9]

env:
CACHE: ${{ matrix.cache-service }}
Expand All @@ -45,24 +39,11 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Generate dependencies
run: |
pip install wheel requirements-builder
requirements-builder -e "$EXTRAS" --level=${{ matrix.requirements-level }} setup.py > .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt
- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('.${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt') }}

- name: Install dependencies
run: |
pip install -r .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt
pip install ".[$EXTRAS]"
pip install wheel "blinker>=1.4" "Werkzeug==0.14.1" "Flask==0.10.1" "Flask-RESTful>=0.3.7" "pillow>=4.0" "six>=1.7.2" "pytest>=4.6.11,<5.0.0" "mock>=3.0.5,<4.0.0" "flask-testing>=0.6.0,<0.8.0" "itsdangerous<2.0" "Jinja2<3.1" "sphinx>=1.8.6,<2.0.0" "redis>=3.5.3,<4.0.0"
pip install -e .
pip freeze
docker --version
docker-compose --version
- name: Run tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion flask_iiif/cache/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from datetime import datetime

from cachelib.redis import RedisCache
from werkzeug.contrib.cache import RedisCache
from flask import current_app
from redis import StrictRedis

Expand Down
2 changes: 1 addition & 1 deletion flask_iiif/cache/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from datetime import datetime

from cachelib.simple import SimpleCache
from werkzeug.contrib.cache import SimpleCache

from .cache import ImageCache

Expand Down
10 changes: 5 additions & 5 deletions flask_iiif/restful.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get(self, version, uuid):
iiif_before_info_request.send(self, version=version, uuid=uuid)

# build the image key
key = "iiif:info:{0}/{1}".format(version, uuid)
key = u"iiif:info:{0}/{1}".format(version, uuid)

# Check if its cached
try:
Expand Down Expand Up @@ -146,7 +146,7 @@ def get(self, version, uuid, region, size, rotation, quality, image_format):
IIIFImageAPIWrapper.validate_api(**api_parameters)

# build the image key
key = "iiif:{0}/{1}/{2}/{3}/{4}.{5}".format(
key = u"iiif:{0}/{1}/{2}/{3}/{4}.{5}".format(
uuid, region, size, quality, rotation, image_format
)

Expand Down Expand Up @@ -203,13 +203,11 @@ def get(self, version, uuid, region, size, rotation, quality, image_format):
send_file_kwargs = {"mimetype": mimetype}
# last_modified is not supported before flask 0.12
additional_headers = []
if last_modified:
send_file_kwargs.update(last_modified=last_modified)

if "dl" in request.args:
filename = secure_filename(request.args.get("dl", ""))
if filename.lower() in {"", "1", "true"}:
filename = "{0}-{1}-{2}-{3}-{4}.{5}".format(
filename = u"{0}-{1}-{2}-{3}-{4}.{5}".format(
uuid, region, size, quality, rotation, image_format
)
send_file_kwargs.update(
Expand All @@ -222,6 +220,8 @@ def get(self, version, uuid, region, size, rotation, quality, image_format):
if if_modified_since and if_modified_since >= last_modified:
return Response(status=304)
response = send_file(to_serve, **send_file_kwargs)
if last_modified:
response.last_modified = last_modified
if additional_headers:
response.headers.extend(additional_headers)
return response
9 changes: 0 additions & 9 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ set -o errexit
# Quit on unbound symbols
set -o nounset

# Always bring down docker services
function cleanup() {
eval "$(docker-services-cli down --env)"
}
trap cleanup EXIT


python -m check_manifest
python -m sphinx.cmd.build -qnNW docs docs/_build/html
eval "$(docker-services-cli up --cache ${CACHE:-redis} --env)"
python -m pytest
tests_exit_code=$?
exit "$tests_exit_code"
22 changes: 9 additions & 13 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@ classifiers =
[options]
include_package_data = True
packages = find:
python_requires = >=3.7
zip_safe = False
install_requires =
blinker>=1.4
cachelib>=0.1
Flask>=1.0.4
Werkzeug==0.14.1
Flask==0.10.1
Flask-RESTful>=0.3.7
pillow>=4.0
six>=1.7.2

[options.extras_require]
tests =
pytest-black>=0.3.0,<0.3.10
flask-testing>=0.6.0
pytest-invenio>=1.4.0
werkzeug>=0.15.3
sphinx>=4.5
redis>=3.5
mock>=3.0.5,<4.0.0
pytest>=4.6.11,<5.0.0
flask-testing>=0.6.0,<0.8.0
itsdangerous<2.0
sphinx>=1.8.6,<2.0.0
redis>=3.5.3,<4.0.0

[build_sphinx]
source-dir = docs/
Expand All @@ -51,14 +50,11 @@ all_files = 1
[bdist_wheel]
universal = 1

[isort]
profile=black

[check-manifest]
ignore =
*-requirements.txt

[tool:pytest]
addopts = --black --isort --pydocstyle --doctest-glob="*.rst" --doctest-modules --cov=flask_iiif --cov-report=term-missing
addopts = --doctest-glob="*.rst" --doctest-modules
testpaths = tests flask_iiif
filterwarnings = ignore::pytest.PytestDeprecationWarning
7 changes: 7 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
class IIIFTestCase(TestCase):
"""IIIF REST test case."""

def assertRaisesRegexCompat(self, *args, **kwargs):
"""Allow to call assertRaisesRegex in a 2-and-3 compatible way."""
if hasattr(self, "assertRaisesRegex"):
self.assertRaisesRegex(*args, **kwargs)
else:
self.assertRaisesRegexp(*args, **kwargs)

def create_app(self):
"""Create the app."""
from flask_restful import Api
Expand Down
34 changes: 17 additions & 17 deletions tests/test_restful_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"""Test REST API."""

from io import BytesIO
from unittest.mock import patch

from flask import url_for
from mock import patch
from PIL import Image
from werkzeug.utils import secure_filename

Expand All @@ -34,10 +34,10 @@ def test_api_info(self):
from flask import jsonify

id_v1 = url_for(
"iiifimagebase", uuid="valid:id-üni", version="v1", _external=True
"iiifimagebase", uuid=u"valid:id-üni", version="v1", _external=True
)
id_v2 = url_for(
"iiifimagebase", uuid="valid:id-üni", version="v2", _external=True
"iiifimagebase", uuid=u"valid:id-üni", version="v2", _external=True
)

expected = {
Expand Down Expand Up @@ -69,15 +69,15 @@ def test_api_info(self):
get_the_response = self.get(
"iiifimageinfo",
urlargs=dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
),
)
self.assert200(get_the_response)
get_the_response = self.get(
"iiifimageinfo",
urlargs=dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
),
)
Expand All @@ -86,7 +86,7 @@ def test_api_info(self):
get_the_response = self.get(
"iiifimageinfo",
urlargs=dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v1",
),
)
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_api_internal_server_error(self):
get_the_response = self.get(
"iiifimageapi",
urlargs=dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
region="full",
size="full",
Expand All @@ -141,7 +141,7 @@ def test_api_iiif_validation_error(self):
get_the_response = self.get(
"iiifimageapi",
urlargs=dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v1",
region="200",
size="full",
Expand All @@ -162,7 +162,7 @@ def test_api_stream_image(self):
get_the_response = self.get(
"iiifimageapi",
urlargs=dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
region="full",
size="full",
Expand All @@ -183,7 +183,7 @@ def test_api_stream_image(self):
get_the_response = self.get(
"iiifimageapi",
urlargs=dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
region="full",
size="full",
Expand All @@ -197,7 +197,7 @@ def test_api_stream_image(self):
self.assertEqual(get_the_response.status_code, 304)

urlargs = dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
region="200,200,200,200",
size="300,300",
Expand All @@ -212,7 +212,7 @@ def test_api_stream_image(self):
)
self.assert200(get_the_response)

default_name = "{name}-200200200200-300300-color-50.pdf".format(
default_name = u"{name}-200200200200-300300-color-50.pdf".format(
name=secure_filename(urlargs["uuid"])
)
for dl, name in (
Expand Down Expand Up @@ -250,7 +250,7 @@ def test_api_decorator(self):
def test_api_abort_all_methods_except_get(self):
"""Abort all methods but GET."""
data = dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
region="full",
size="full",
Expand All @@ -274,7 +274,7 @@ def test_api_cache_control(self):
"""Test cache-control headers"""

urlargs = dict(
uuid="valid:id-üni",
uuid=u"valid:id-üni",
version="v2",
region="200,200,200,200",
size="300,300",
Expand All @@ -283,7 +283,7 @@ def test_api_cache_control(self):
image_format="pdf",
)

key = "iiif:{0}/{1}/{2}/{3}/{4}.{5}".format(
key = u"iiif:{0}/{1}/{2}/{3}/{4}.{5}".format(
urlargs["uuid"],
urlargs["region"],
urlargs["size"],
Expand Down Expand Up @@ -355,10 +355,10 @@ def test_cache_ignore_errors(self):
cache = self.app.config["IIIF_CACHE_HANDLER"].cache
with patch.object(cache, "get", side_effect=Exception("test fail")):
# Without ignoring errors
self.assertRaisesRegex(
self.assertRaisesRegexCompat(
Exception, "test fail", self.get, "iiifimageinfo", urlargs=info_args
)
self.assertRaisesRegex(
self.assertRaisesRegexCompat(
Exception, "test fail", self.get, "iiifimageapi", urlargs=api_args
)

Expand Down
Loading

0 comments on commit e12fc1b

Please sign in to comment.