diff --git a/beanprice/price.py b/beanprice/price.py index 7e75e7a..66348f4 100644 --- a/beanprice/price.py +++ b/beanprice/price.py @@ -8,7 +8,7 @@ import datetime import functools from os import path -import shelve +import os import tempfile import hashlib import re @@ -16,6 +16,7 @@ import logging from concurrent import futures from typing import Any, Dict, List, Optional, NamedTuple, Tuple +import diskcache from dateutil import tz @@ -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) and os.path.isfile(cache_filename): + os.remove(cache_filename) + logging.info('Using price cache at "%s" (with indefinite expiration)', cache_filename) flag = "c" @@ -565,7 +571,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 @@ -573,6 +579,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 diff --git a/pyproject.toml b/pyproject.toml index a518423..741383c 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] @@ -42,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 = {} 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