From 8a721d5bbcf1ad94079e19ab7599f53d57db8ffc Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Thu, 22 Feb 2024 14:06:06 +0000 Subject: [PATCH] Fix for paths starting with tilde used for cache directory --- docs/release_notes/version_0.5_updates.rst | 7 +++++++ earthkit/data/core/caching.py | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/release_notes/version_0.5_updates.rst b/docs/release_notes/version_0.5_updates.rst index 5030092a..d607591d 100644 --- a/docs/release_notes/version_0.5_updates.rst +++ b/docs/release_notes/version_0.5_updates.rst @@ -1,6 +1,13 @@ Version 0.5 Updates ///////////////////////// +Version 0.5.3 +=============== + +Fixes +++++++ +- fixed issue when paths starting with ~ used for :ref:`cache directories ` were not correctly expanded + Version 0.5.2 =============== diff --git a/earthkit/data/core/caching.py b/earthkit/data/core/caching.py index 28e3b495..a6266e97 100644 --- a/earthkit/data/core/caching.py +++ b/earthkit/data/core/caching.py @@ -643,6 +643,12 @@ def maximum_cache_disk_usage(self): def file_in_cache_directory(self, path): return path.startswith(self.directory()) + @staticmethod + def _expand_path(path): + if path is not None: + path = os.path.expanduser(path) + return path + class EmptyCachePolicy(CachePolicy): _name = "empty" @@ -680,7 +686,9 @@ def managed(self): def directory(self): if self._dir is None: if self._dir is None: - root_dir = self._settings.get("temporary-directory-root") + root_dir = self._expand_path( + self._settings.get("temporary-directory-root") + ) self._dir = temp_directory(dir=root_dir) return self._dir.path @@ -706,7 +714,7 @@ class UserCachePolicy(CachePolicy): def __init__(self): super().__init__() - path = self._settings.get("user-cache-directory") + path = self._expand_path(self._settings.get("user-cache-directory")) if not os.path.exists(path): os.makedirs(path, exist_ok=True) @@ -748,7 +756,9 @@ class TmpCachePolicy(UserCachePolicy): def __init__(self): super().__init__() - root_dir = self._settings.get("temporary-cache-directory-root") + root_dir = self._expand_path( + self._settings.get("temporary-cache-directory-root") + ) self._dir = temp_directory(dir=root_dir) def directory(self):