summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2012-07-23 21:58:30 +0100
committerThomas Kluyver <takowl@gmail.com>2012-07-23 21:58:30 +0100
commit225e9885c85215beb162994e886ee6d528a91998 (patch)
tree9ae0bf8b7b8bbf97f5057c6b1ae90f50bc6b3beb
parent890841fb910112853306e2d6b4163cce12262fd5 (diff)
Add function xdg.BaseDirectory.save_cache_path()
fd.o bug #26458
-rw-r--r--test/test-basedirectory.py10
-rw-r--r--xdg/BaseDirectory.py12
2 files changed, 22 insertions, 0 deletions
diff --git a/test/test-basedirectory.py b/test/test-basedirectory.py
index a001bbe..cc285df 100644
--- a/test/test-basedirectory.py
+++ b/test/test-basedirectory.py
@@ -40,6 +40,16 @@ class BaseDirectoryTest(unittest.TestCase):
finally:
shutil.rmtree(tmpdir)
+ def test_save_cache_path(self):
+ tmpdir = tempfile.mkdtemp()
+ try:
+ environ['XDG_CACHE_HOME'] = tmpdir
+ reload(BaseDirectory)
+ datapath = BaseDirectory.save_cache_path("foo")
+ self.assertEqual(datapath, os.path.join(tmpdir, "foo"))
+ finally:
+ shutil.rmtree(tmpdir)
+
def test_load_first_config(self):
tmpdir = tempfile.mkdtemp()
tmpdir2 = tempfile.mkdtemp()
diff --git a/xdg/BaseDirectory.py b/xdg/BaseDirectory.py
index 5022f18..2ad2390 100644
--- a/xdg/BaseDirectory.py
+++ b/xdg/BaseDirectory.py
@@ -70,6 +70,18 @@ def save_data_path(*resource):
os.makedirs(path)
return path
+def save_cache_path(*resource):
+ """Ensure $XDG_CACHE_HOME/<resource>/ exists, and return its path.
+ 'resource' is the name of some shared resource. Use this when updating
+ a shared (between programs) database. Use the xdg_cache_home variable
+ for loading."""
+ resource = os.path.join(*resource)
+ assert not resource.startswith('/')
+ path = os.path.join(xdg_cache_home, resource)
+ if not os.path.isdir(path):
+ os.makedirs(path)
+ return path
+
def load_config_paths(*resource):
"""Returns an iterator which gives each directory named 'resource' in the
configuration search path. Information provided by earlier directories should