diff --git a/CHANGELOG.md b/CHANGELOG.md index 51c13ae..42066a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ Backwards compatibility is not guaranteed in versions <1.0.0. * Phase information is exposed at the fight level in this client (`FFLogsFight.phases`), *not* at the report level * Fix quotes not being properly escaped in query filters +* System temporary directories are now used for query caches + * As a result, the `cache_directory` param on the client is now deprecated * Bump cryptography dependency (vulnerability fix) ## v2.0.2 diff --git a/fflogsapi/client.py b/fflogsapi/client.py index ae1135f..489f5f7 100644 --- a/fflogsapi/client.py +++ b/fflogsapi/client.py @@ -4,6 +4,7 @@ import os import pickle +import tempfile from copy import deepcopy from functools import wraps from time import time @@ -25,6 +26,8 @@ from .user_auth import UserModeAuthMixin from .world.client_extensions import WorldMixin +from warnings import warn + def ensure_token(func): ''' @@ -127,9 +130,17 @@ def __init__( self._query_cache = {} self.cache_expiry = cache_expiry self.cache_queries = enable_caching - self.cache_dir = cache_directory self.ignore_cache_expiry = ignore_cache_expiry + # deprecation warning for cache_directory use + if cache_directory != './fflogs-querycache': + warn('Custom cache directories are deprecated in favor of system temp dirs.' + ' Consider removing usage of cache_directory when instantiating FFLogsClient.', + category=FutureWarning) + else: + # future behavior + self.cache_dir = os.path.join(tempfile.gettempdir(), 'fflogsapi') + if enable_caching: if not os.path.exists(self.cache_dir): os.makedirs(self.cache_dir) diff --git a/tests/client/test_caching.py b/tests/client/test_caching.py index ff88dab..f3e9d6c 100644 --- a/tests/client/test_caching.py +++ b/tests/client/test_caching.py @@ -1,5 +1,6 @@ import os import unittest +import tempfile from time import sleep, time from fflogsapi.client import FFLogsClient @@ -13,25 +14,22 @@ class CacheTest(unittest.TestCase): WARNING: Running these tests will delete all existing query caches! ''' - - CACHE_DIR = './querycache' CACHE_EXPIRY = 2 # seconds @classmethod def setUpClass(cls) -> None: - # The cache directory must be empty before starting these tests (if it exists) - if os.path.exists(cls.CACHE_DIR): - for fn in os.listdir(cls.CACHE_DIR): - os.remove(os.path.join(cls.CACHE_DIR, fn)) - os.rmdir(cls.CACHE_DIR) - cls.client = FFLogsClient( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, - cache_directory=cls.CACHE_DIR, cache_expiry=cls.CACHE_EXPIRY ) + # The cache directory must be empty before starting these tests (if it exists) + if os.path.exists(cls.client.cache_dir): + for fn in os.listdir(cls.client.cache_dir): + os.remove(os.path.join(cls.client.cache_dir, fn)) + os.rmdir(cls.client.cache_dir) + @classmethod def tearDownClass(cls) -> None: cls.client.close() @@ -54,8 +52,8 @@ def test_cache_saving(self) -> None: The client should be able to save a file containing cached query results ''' self.client.save_cache(silent=False) - self.assertTrue(os.path.exists(self.CACHE_DIR)) - cache_expiry = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR)))[0] + self.assertTrue(os.path.exists(self.client.cache_dir)) + cache_expiry = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir)))[0] self.assertAlmostEqual(time() + self.CACHE_EXPIRY, cache_expiry, places=1) def test_extend_cache(self) -> None: @@ -65,7 +63,7 @@ def test_extend_cache(self) -> None: self.client.extend_cache(2) self.client.save_cache() - expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR))) + expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir))) ok = False for expiry in expiries: if round(time() + self.CACHE_EXPIRY + 2, 1) == round(expiry, 1): @@ -79,7 +77,7 @@ def test_clean_cache(self) -> None: ''' self.client.save_cache() - expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR))) + expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir))) # is soonest even a word? soonest_expire = min(expiries) @@ -92,7 +90,7 @@ def test_clean_cache(self) -> None: self.assertGreaterEqual(time(), soonest_expire) self.client.clean_cache() - expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR))) + expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir))) self.assertNotIn(soonest_expire, expiries)