summaryrefslogtreecommitdiff
path: root/poezio/xdg.py
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-07-04 11:42:04 +0200
committerEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-07-04 12:07:31 +0200
commitda12fe7d6aa9ee9a710e914185a45f843180e42e (patch)
tree5f573f8b3eef50cf27114f3c2d0eebd90e2d9e4e /poezio/xdg.py
parent7978481edf172fa6d308932a640e1a9bccdd0d4f (diff)
downloadpoezio-da12fe7d6aa9ee9a710e914185a45f843180e42e.tar.gz
poezio-da12fe7d6aa9ee9a710e914185a45f843180e42e.tar.bz2
poezio-da12fe7d6aa9ee9a710e914185a45f843180e42e.tar.xz
poezio-da12fe7d6aa9ee9a710e914185a45f843180e42e.zip
Move XDG basedir functions to the poezio.xdg module.
Diffstat (limited to 'poezio/xdg.py')
-rw-r--r--poezio/xdg.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/poezio/xdg.py b/poezio/xdg.py
new file mode 100644
index 00000000..79dc24a9
--- /dev/null
+++ b/poezio/xdg.py
@@ -0,0 +1,39 @@
+# Copyright 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+#
+# This file is part of Poezio.
+#
+# Poezio is free software: you can redistribute it and/or modify
+# it under the terms of the zlib license. See the COPYING file.
+
+"""
+Implements the XDG base directory specification.
+
+https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
+"""
+
+from pathlib import Path
+from os import environ
+
+# $HOME has already been checked to not be None in test_env().
+DEFAULT_PATHS = {
+ 'XDG_CONFIG_HOME': Path.home() / '.config',
+ 'XDG_DATA_HOME': Path.home() / '.local' / 'share',
+ 'XDG_CACHE_HOME': Path.home() / '.cache',
+}
+
+def _get_directory(variable: str):
+ """
+ returns the default configuration directory path
+ """
+ if variable not in DEFAULT_PATHS:
+ raise ValueError('Invalid XDG basedir variable')
+ xdg = environ.get(variable)
+ if xdg is not None:
+ xdg_path = Path(xdg)
+ if xdg_path.is_absolute():
+ return xdg_path / 'poezio'
+ return DEFAULT_PATHS[variable] / 'poezio'
+
+CONFIG_HOME = _get_directory('XDG_CONFIG_HOME')
+DATA_HOME = _get_directory('XDG_DATA_HOME')
+CACHE_HOME = _get_directory('XDG_CACHE_HOME')