From 76a60919d9d7697fa40665e2e3c80f1e83728142 Mon Sep 17 00:00:00 2001
From: PrimozGodec
Date: Fri, 27 Oct 2023 13:22:18 +0200
Subject: [PATCH] Release version 0.12.1 Addons - Do not use cache when no
permission to write cache
---
.../application/tests/test_addons_utils.py | 20 +++++++++++++++-
orangecanvas/application/utils/addons.py | 24 ++++++++++++-------
2 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/orangecanvas/application/tests/test_addons_utils.py b/orangecanvas/application/tests/test_addons_utils.py
index 6c3d0d56..b9fd1556 100644
--- a/orangecanvas/application/tests/test_addons_utils.py
+++ b/orangecanvas/application/tests/test_addons_utils.py
@@ -1,6 +1,10 @@
+import os
import unittest
+from tempfile import mkdtemp
from pkg_resources import Requirement
+from requests import Session
+from requests_cache import CachedSession
from orangecanvas.application.utils.addons import (
Available,
@@ -9,7 +13,7 @@
installable_from_json_response,
installable_items,
is_updatable,
- prettify_name,
+ prettify_name, _session,
)
from orangecanvas.config import Distribution
@@ -92,6 +96,20 @@ def test_prettify_name(self):
self.assertEqual('Image Analytics', prettify_name('Orange3-ImageAnalytics'))
self.assertEqual('Survival Analysis', prettify_name('Orange3-Survival-Analysis'))
+ def test_session(self):
+ # when now permission error accessing cache dir
+ self.assertIsInstance(_session(), CachedSession)
+ # when no permissions to cache dir
+
+ temp_dir = mkdtemp()
+ if os.name == "nt":
+ # Windows
+ os.chmod(temp_dir, 0o700)
+ else:
+ # macOS and Linux
+ os.chmod(temp_dir, 0o444)
+ self.assertIsInstance(_session(temp_dir), Session)
+
if __name__ == "__main__":
unittest.main()
diff --git a/orangecanvas/application/utils/addons.py b/orangecanvas/application/utils/addons.py
index 985f6d48..75cc2860 100644
--- a/orangecanvas/application/utils/addons.py
+++ b/orangecanvas/application/utils/addons.py
@@ -9,6 +9,7 @@
from collections import deque
from datetime import timedelta
from enum import Enum
+from sqlite3 import OperationalError
from types import SimpleNamespace
from typing import AnyStr, Callable, List, NamedTuple, Optional, Tuple, TypeVar, Union
@@ -296,14 +297,21 @@ def _session(cachedir=None):
if cachedir is None:
cachedir = QStandardPaths.writableLocation(QStandardPaths.CacheLocation)
cachedir = os.path.join(cachedir, "networkcache")
- session = requests_cache.CachedSession(
- os.path.join(cachedir, "requests.sqlite"),
- backend="sqlite",
- cache_control=True,
- expire_after=timedelta(days=1),
- stale_if_error=True,
- )
- return session
+ try:
+ return requests_cache.CachedSession(
+ os.path.join(cachedir, "requests.sqlite"),
+ backend="sqlite",
+ cache_control=True,
+ expire_after=timedelta(days=1),
+ stale_if_error=True,
+ )
+ except OperationalError as ex:
+ # if no permission to write in dir or read cache file return uncached session
+ log.info(
+ f"Cache file creation/opening failed with: '{str(ex)}'. "
+ f"Using requests.Session instead of cached session."
+ )
+ return requests.Session()
def optional_map(func: Callable[[A], B]) -> Callable[[Optional[A]], Optional[B]]: