summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kluyver <thomas@kluyver.me.uk>2022-06-05 12:19:52 +0100
committerThomas Kluyver <thomas@kluyver.me.uk>2022-06-05 12:19:52 +0100
commitee7025341c006ad1bd44f078cc537926e7c0484c (patch)
tree0fd454441e42ab979e077ff18cf4d740bc4f1480
parentf097a66923a65e93640c48da83e6e9cfbddd86ba (diff)
parent6d1a550711b40edf44e25babd04477b4676c0b61 (diff)
Merge branch 'pr8'
-rw-r--r--docs/basedirectory.rst9
-rw-r--r--test/test-basedirectory.py10
-rw-r--r--xdg/BaseDirectory.py14
3 files changed, 33 insertions, 0 deletions
diff --git a/docs/basedirectory.rst b/docs/basedirectory.rst
index 247c02f..6a6b188 100644
--- a/docs/basedirectory.rst
+++ b/docs/basedirectory.rst
@@ -51,6 +51,15 @@ Cache directory
$XDG_CACHE_HOME or the default, ``~/.cache``
+State directory
+---------------
+
+.. autofunction:: save_state_path
+
+.. data:: xdg_state_home
+
+ $XDG_STATE_HOME or the default, ``~/.local/state``
+
Runtime directory
-----------------
diff --git a/test/test-basedirectory.py b/test/test-basedirectory.py
index c7a49c5..954f573 100644
--- a/test/test-basedirectory.py
+++ b/test/test-basedirectory.py
@@ -51,6 +51,16 @@ class BaseDirectoryTest(unittest.TestCase):
finally:
shutil.rmtree(tmpdir)
+ def test_save_state_path(self):
+ tmpdir = tempfile.mkdtemp()
+ try:
+ environ['XDG_STATE_HOME'] = tmpdir
+ reload(BaseDirectory)
+ statepath = BaseDirectory.save_state_path("foo")
+ self.assertEqual(statepath, 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 a7c31b1..3c3afe8 100644
--- a/xdg/BaseDirectory.py
+++ b/xdg/BaseDirectory.py
@@ -43,6 +43,9 @@ xdg_config_dirs = [xdg_config_home] + \
xdg_cache_home = os.environ.get('XDG_CACHE_HOME') or \
os.path.join(_home, '.cache')
+xdg_state_home = os.environ.get('XDG_STATE_HOME') or \
+ os.path.join(_home, '.local', 'state')
+
xdg_data_dirs = [x for x in xdg_data_dirs if x]
xdg_config_dirs = [x for x in xdg_config_dirs if x]
@@ -81,6 +84,17 @@ def save_cache_path(*resource):
os.makedirs(path)
return path
+def save_state_path(*resource):
+ """Ensure ``$XDG_STATE_HOME/<resource>/`` exists, and return its path.
+ 'resource' should normally be the name of your application or a shared
+ resource."""
+ resource = os.path.join(*resource)
+ assert not resource.startswith('/')
+ path = os.path.join(xdg_state_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