Skip to content

Commit

Permalink
Merge pull request #236 from PrimozGodec/replace-cachecontrol
Browse files Browse the repository at this point in the history
[ENH] Replace cachecontrol with requests_cache
  • Loading branch information
ales-erjavec committed Sep 8, 2023
2 parents 2157982 + 25b622d commit 644dd65
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
13 changes: 0 additions & 13 deletions orangecontrib/imageanalytics/local_embedder.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
from os.path import join

import cachecontrol.caches
import requests

from Orange.canvas.config import cache_dir
from Orange.misc.utils.embedder_utils import EmbedderCache
from Orange.util import dummy_callback
from orangecontrib.imageanalytics.utils.embedder_utils import ImageLoader
Expand All @@ -18,13 +12,6 @@ def __init__(self, model, model_settings):

self._target_image_size = model_settings["target_image_size"]

self._session = cachecontrol.CacheControl(
requests.session(),
cache=cachecontrol.caches.FileCache(
join(cache_dir(), __name__ + ".ImageEmbedder.httpcache")
),
)

self._image_loader = ImageLoader()
self._cache = EmbedderCache(model)

Expand Down
23 changes: 12 additions & 11 deletions orangecontrib/imageanalytics/utils/embedder_utils.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import ftplib
import logging
from datetime import timedelta
from io import BytesIO
from os.path import join
from urllib.error import URLError
from urllib.parse import urlparse
from urllib.request import urlopen

import cachecontrol.caches
import numpy as np
import requests
from AnyQt.QtCore import QStandardPaths
from PIL import ImageFile
from PIL.Image import LANCZOS
from PIL.Image import open as open_image
from requests.exceptions import RequestException

from Orange.misc.environ import cache_dir
from requests_cache import CachedSession

log = logging.getLogger(__name__)
ImageFile.LOAD_TRUNCATED_IMAGES = True


class ImageLoader:
def __init__(self):
self._session = cachecontrol.CacheControl(
requests.session(),
cache=cachecontrol.caches.FileCache(
join(cache_dir(), __name__ + ".ImageEmbedder.httpcache")
),
cache_dir = QStandardPaths.writableLocation(QStandardPaths.CacheLocation)
cache_path = join(cache_dir, "networkcache", "image_loader.sqlite")
self._session = CachedSession(
cache_path,
backend="sqlite",
cache_control=True,
expire_after=timedelta(days=1),
stale_if_error=True,
)

def load_image_or_none(self, file_path, target_size=None):
Expand Down Expand Up @@ -65,7 +66,7 @@ def _load_image_from_url_or_local_path(self, file_path):
urlparts = urlparse(file_path)
if urlparts.scheme in ("http", "https"):
try:
file = self._session.get(file_path, stream=True).raw
file = BytesIO(self._session.get(file_path).content)
except RequestException:
log.warning("Image skipped", exc_info=True)
return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from unittest.mock import patch
from urllib.error import URLError

import numpy as np
from PIL.Image import Image
from numpy.testing import assert_array_equal
from requests import RequestException

from orangecontrib.imageanalytics.utils.embedder_utils import ImageLoader
Expand Down Expand Up @@ -49,6 +51,10 @@ def test_load_images_url(self) -> None:
"""
image = self.image_loader.load_image_or_none(self.im_url)
self.assertTrue(isinstance(image, Image))
image_array = np.array(image)
# manually checked values
assert_array_equal(image_array[147, 181], [103, 10, 0])
assert_array_equal(image_array[305, 331], [21, 1, 2])

image = self.image_loader.load_image_or_none(self.im_paths[0],
target_size=(255, 255))
Expand All @@ -59,7 +65,7 @@ def test_load_images_url(self) -> None:
image = self.image_loader.load_image_or_none(self.im_url + "a")
self.assertIsNone(image)

@patch("requests.sessions.Session.get", side_effect=RequestException)
@patch("requests_cache.CachedSession.get", side_effect=RequestException)
def test_load_images_url_request_exception(self, _) -> None:
"""
Handle loading images from http, https type urls
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def include_documentation(local_dir, install_dir):
classifiers=CLASSIFIERS,
install_requires=[
"AnyQt",
"cachecontrol[filecache]",
"ndf >=0.1.4",
"numpy >=1.16",
"Orange3 >=3.34.0",
Expand All @@ -104,6 +103,7 @@ def include_documentation(local_dir, install_dir):
# constraint when requiring Orange> 3.35
"pandas <2.1",
"requests",
"requests_cache",
"scipy",
],
extras_require={
Expand Down

0 comments on commit 644dd65

Please sign in to comment.