diff options
-rw-r--r-- | poezio/config.py | 20 | ||||
-rw-r--r-- | poezio/plugin_manager.py | 15 | ||||
-rw-r--r-- | poezio/xdg.py | 39 |
3 files changed, 45 insertions, 29 deletions
diff --git a/poezio/config.py b/poezio/config.py index fe60be63..8549df71 100644 --- a/poezio/config.py +++ b/poezio/config.py @@ -23,6 +23,7 @@ from os import environ, makedirs, path, remove from shutil import copy2 from pathlib import Path from poezio.args import parse_args +from poezio import xdg DEFAULT_CONFIG = { 'Poezio': { @@ -506,22 +507,6 @@ def file_ok(filepath): return bool(val) -def get_default_config_dir(): - """ - returns the default configuration directory path - """ - try: - config_home = Path(environ.get('XDG_CONFIG_HOME')) - except TypeError: - # XDG_CONFIG_HOME isn’t defined, fallback to ~/.config - pass - else: - if config_home.is_absolute(): - return config_home / 'poezio' - # HOME has already been checked to be non-None in test_env(). - return Path.home() / '.config' / 'poezio' - - def check_create_cache_dir(): """ create the cache directory if it doesn't exist @@ -578,8 +563,7 @@ def check_config(): def run_cmdline_args(): "Parse the command line arguments" global options - CONFIG_PATH = get_default_config_dir() - options = parse_args(CONFIG_PATH) + options = parse_args(xdg.CONFIG_HOME) # Copy a default file if none exists if not options.filename.is_file(): diff --git a/poezio/plugin_manager.py b/poezio/plugin_manager.py index bb629e0a..a5b0d8ab 100644 --- a/poezio/plugin_manager.py +++ b/poezio/plugin_manager.py @@ -7,9 +7,10 @@ plugin env. import os from os import path +from pathlib import Path import logging -from poezio import tabs +from poezio import tabs, xdg from poezio.core.structs import Command, Completion from poezio.plugin import PluginAPI from poezio.config import config @@ -326,10 +327,7 @@ class PluginManager(object): Create the plugins_conf_dir """ plugins_conf_dir = config.get('plugins_conf_dir') - if not plugins_conf_dir: - self.plugins_conf_dir = config.get_default_config_dir() / 'plugins' - else: - self.plugins_conf_dir = Path(plugins_conf_dir).expanduser() + self.plugins_conf_dir = Path(plugins_conf_dir).expanduser() if plugins_conf_dir else xdg.CONFIG_HOME / 'plugins' self.check_create_plugins_conf_dir() def check_create_plugins_conf_dir(self): @@ -353,12 +351,7 @@ class PluginManager(object): Set the plugins_dir on start """ plugins_dir = config.get('plugins_dir') - plugins_dir = plugins_dir or\ - path.join(os.environ.get('XDG_DATA_HOME') or\ - path.join(os.environ.get('HOME'), - '.local', 'share'), - 'poezio', 'plugins') - self.plugins_dir = path.expanduser(plugins_dir) + plugins_dir = Path(plugins_dir).expanduser() if plugins_dir else xdg.DATA_HOME / 'plugins' self.check_create_plugins_dir() def check_create_plugins_dir(self): 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') |