From 024d85316da47ef669031683f96e551a8240f8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= <462445+alensiljak@users.noreply.github.com> Date: Sun, 25 May 2025 15:42:30 +0200 Subject: [PATCH 1/7] Replacing shelve with diskcache diskcache is a better multi-platform and multi-threaded alternative to shelve, for caching. The difference is that the current cache needs to be deleted. It is using a directory instead of a filename. --- beanprice/price.py | 4 ++-- pyproject.toml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/beanprice/price.py b/beanprice/price.py index 7e75e7a..ceef7b9 100644 --- a/beanprice/price.py +++ b/beanprice/price.py @@ -8,7 +8,7 @@ import datetime import functools from os import path -import shelve +import diskcache import tempfile import hashlib import re @@ -565,7 +565,7 @@ def setup_cache(cache_filename: Optional[str], clear_cache: bool): flag = "n" global _CACHE - _CACHE = shelve.open(cache_filename, flag=flag) # type: ignore + _CACHE = diskcache.Cache(cache_filename, flag=flag) _CACHE.expiration = DEFAULT_EXPIRATION # type: ignore diff --git a/pyproject.toml b/pyproject.toml index a518423..df85e66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ dependencies = [ 'python-dateutil >= 2.6.0', 'requests >= 2.0', 'curl_cffi>=0.6.5', + "diskcache>=5.6.3", ] [project.scripts] From 730291589f9240146bf6c639e5af6c8608f40b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= <462445+alensiljak@users.noreply.github.com> Date: Sun, 25 May 2025 21:16:46 +0200 Subject: [PATCH 2/7] reorder imports --- beanprice/price.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beanprice/price.py b/beanprice/price.py index ceef7b9..4f6ea39 100644 --- a/beanprice/price.py +++ b/beanprice/price.py @@ -8,7 +8,6 @@ import datetime import functools from os import path -import diskcache import tempfile import hashlib import re @@ -16,6 +15,7 @@ import logging from concurrent import futures from typing import Any, Dict, List, Optional, NamedTuple, Tuple +import diskcache from dateutil import tz From 89c63475d5ae4b1f621e0de6fa99388cb750aa76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= <462445+alensiljak@users.noreply.github.com> Date: Sun, 25 May 2025 21:23:14 +0200 Subject: [PATCH 3/7] adding diskcache to requirements.txt --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9fa3f63..175180b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ beancount>=3.0.0 python-dateutil>=2.6.0 requests>=2.0 -curl_cffi>=0.6.5 \ No newline at end of file +curl_cffi>=0.6.5 +diskcache>=5.6.3 \ No newline at end of file From 7633391dad3a4e0410ce5d0ab877c8797b3c228c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= <462445+alensiljak@users.noreply.github.com> Date: Sun, 25 May 2025 21:30:14 +0200 Subject: [PATCH 4/7] add dev dependencies to pyproject --- pyproject.toml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index df85e66..741383c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,13 @@ bean-price = 'beanprice.price:main' homepage = 'https://github.com/beancount/beanprice' issues = 'https://github.com/beancount/beanprice/issues' +[dependency-groups] +dev = [ + "pytest>=8.3.5", + "pylint==3.3.3", + "mypy==1.14.1" +] + [tool.setuptools.packages] find = {} From 38e1b1efe6497fb2cce75270c24051832b579e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= <462445+alensiljak@users.noreply.github.com> Date: Sun, 25 May 2025 21:37:16 +0200 Subject: [PATCH 5/7] explicitly clear cache, not just close --- beanprice/price.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/beanprice/price.py b/beanprice/price.py index 4f6ea39..1f463ac 100644 --- a/beanprice/price.py +++ b/beanprice/price.py @@ -573,6 +573,8 @@ def reset_cache(): """Reset the cache to its uninitialized state.""" global _CACHE if _CACHE is not None: + if _CACHE.clear is not None: + _CACHE.clear() _CACHE.close() _CACHE = None From cb8c3706f260fb1e6ee047ab1422b0d2b1189c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= <462445+alensiljak@users.noreply.github.com> Date: Sun, 25 May 2025 21:41:52 +0200 Subject: [PATCH 6/7] removing existing cache file on setup so that a cache directory can be created. --- beanprice/price.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/beanprice/price.py b/beanprice/price.py index 1f463ac..83eb09b 100644 --- a/beanprice/price.py +++ b/beanprice/price.py @@ -8,6 +8,7 @@ import datetime import functools from os import path +import os import tempfile import hashlib import re @@ -557,6 +558,11 @@ def setup_cache(cache_filename: Optional[str], clear_cache: bool): if not cache_filename: return + # Handle switch to diskcache. Check for existing cache file and remove it, + # so that diskcache can create a directory with the same name. + if os.path.exists(cache_filename): + os.remove(cache_filename) + logging.info('Using price cache at "%s" (with indefinite expiration)', cache_filename) flag = "c" From 2d7cc1c7f1ffcff75c752b93660cd406c8bb95f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= <462445+alensiljak@users.noreply.github.com> Date: Sun, 25 May 2025 21:45:19 +0200 Subject: [PATCH 7/7] delete only files --- beanprice/price.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beanprice/price.py b/beanprice/price.py index 83eb09b..66348f4 100644 --- a/beanprice/price.py +++ b/beanprice/price.py @@ -560,7 +560,7 @@ def setup_cache(cache_filename: Optional[str], clear_cache: bool): # Handle switch to diskcache. Check for existing cache file and remove it, # so that diskcache can create a directory with the same name. - if os.path.exists(cache_filename): + if os.path.exists(cache_filename) and os.path.isfile(cache_filename): os.remove(cache_filename) logging.info('Using price cache at "%s" (with indefinite expiration)', cache_filename)