Move XDG basedir functions to the poezio.xdg module.
This commit is contained in:
parent
7978481edf
commit
da12fe7d6a
3 changed files with 45 additions and 29 deletions
|
@ -23,6 +23,7 @@ from os import environ, makedirs, path, remove
|
||||||
from shutil import copy2
|
from shutil import copy2
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from poezio.args import parse_args
|
from poezio.args import parse_args
|
||||||
|
from poezio import xdg
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
'Poezio': {
|
'Poezio': {
|
||||||
|
@ -506,22 +507,6 @@ def file_ok(filepath):
|
||||||
return bool(val)
|
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():
|
def check_create_cache_dir():
|
||||||
"""
|
"""
|
||||||
create the cache directory if it doesn't exist
|
create the cache directory if it doesn't exist
|
||||||
|
@ -578,8 +563,7 @@ def check_config():
|
||||||
def run_cmdline_args():
|
def run_cmdline_args():
|
||||||
"Parse the command line arguments"
|
"Parse the command line arguments"
|
||||||
global options
|
global options
|
||||||
CONFIG_PATH = get_default_config_dir()
|
options = parse_args(xdg.CONFIG_HOME)
|
||||||
options = parse_args(CONFIG_PATH)
|
|
||||||
|
|
||||||
# Copy a default file if none exists
|
# Copy a default file if none exists
|
||||||
if not options.filename.is_file():
|
if not options.filename.is_file():
|
||||||
|
|
|
@ -7,9 +7,10 @@ plugin env.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from os import path
|
from os import path
|
||||||
|
from pathlib import Path
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from poezio import tabs
|
from poezio import tabs, xdg
|
||||||
from poezio.core.structs import Command, Completion
|
from poezio.core.structs import Command, Completion
|
||||||
from poezio.plugin import PluginAPI
|
from poezio.plugin import PluginAPI
|
||||||
from poezio.config import config
|
from poezio.config import config
|
||||||
|
@ -326,10 +327,7 @@ class PluginManager(object):
|
||||||
Create the plugins_conf_dir
|
Create the plugins_conf_dir
|
||||||
"""
|
"""
|
||||||
plugins_conf_dir = config.get('plugins_conf_dir')
|
plugins_conf_dir = config.get('plugins_conf_dir')
|
||||||
if not plugins_conf_dir:
|
self.plugins_conf_dir = Path(plugins_conf_dir).expanduser() if plugins_conf_dir else xdg.CONFIG_HOME / 'plugins'
|
||||||
self.plugins_conf_dir = config.get_default_config_dir() / 'plugins'
|
|
||||||
else:
|
|
||||||
self.plugins_conf_dir = Path(plugins_conf_dir).expanduser()
|
|
||||||
self.check_create_plugins_conf_dir()
|
self.check_create_plugins_conf_dir()
|
||||||
|
|
||||||
def check_create_plugins_conf_dir(self):
|
def check_create_plugins_conf_dir(self):
|
||||||
|
@ -353,12 +351,7 @@ class PluginManager(object):
|
||||||
Set the plugins_dir on start
|
Set the plugins_dir on start
|
||||||
"""
|
"""
|
||||||
plugins_dir = config.get('plugins_dir')
|
plugins_dir = config.get('plugins_dir')
|
||||||
plugins_dir = plugins_dir or\
|
plugins_dir = Path(plugins_dir).expanduser() if plugins_dir else xdg.DATA_HOME / 'plugins'
|
||||||
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)
|
|
||||||
self.check_create_plugins_dir()
|
self.check_create_plugins_dir()
|
||||||
|
|
||||||
def check_create_plugins_dir(self):
|
def check_create_plugins_dir(self):
|
||||||
|
|
39
poezio/xdg.py
Normal file
39
poezio/xdg.py
Normal file
|
@ -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')
|
Loading…
Reference in a new issue