From 333436f50fec40c2a0300e15aedeee5103adffe0 Mon Sep 17 00:00:00 2001 From: Aaron Lichtman Date: Sun, 2 Jul 2023 06:35:53 -0700 Subject: [PATCH] Make $XDG_CONFIG_HOME the new default vit config location --- CUSTOMIZE.md | 8 +++++--- requirements.txt | 1 + vit/config_parser.py | 13 ++++++------- vit/loader.py | 37 +++++++++++++++++++++++++------------ vit/xdg.py | 2 +- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/CUSTOMIZE.md b/CUSTOMIZE.md index ba9b502..5098563 100644 --- a/CUSTOMIZE.md +++ b/CUSTOMIZE.md @@ -8,9 +8,11 @@ VIT provides a user directory that allows for configuring basic settings *(via ` VIT searches for the user directory in this order of priority: -1. The ```VIT_DIR``` environment variable -2. ```~/.vit``` (the default location) -3. A ```vit``` directory in any valid [XDG base directory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) +1. The `VIT_DIR` environment variable +2. `~/.vit` (the old default location) +3. `$XDG_CONFIG_HOME/vit`, following the [XDG base directory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) + +If no config exists, you'll be prompted to create one (in `$XDG_CONFIG_HOME/vit`). #### Taskwarrior configuration diff --git a/requirements.txt b/requirements.txt index b898f91..35525cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ tasklib>=2.4.3 urwid>=2.1.2 backports.zoneinfo;python_version<"3.9" +xdg-base-dirs>=6.0.0 diff --git a/vit/config_parser.py b/vit/config_parser.py index dcc3afc..1fb5f18 100644 --- a/vit/config_parser.py +++ b/vit/config_parser.py @@ -17,7 +17,6 @@ SORT_ORDER_CHARACTERS = ['+', '-'] SORT_COLLATE_CHARACTERS = ['/'] -VIT_CONFIG_FILE = 'config.ini' FILTER_EXCLUSION_REGEX = re.compile(r'^limit:') FILTER_PARENS_REGEX = re.compile(r'([\(\)])') CONFIG_BOOLEAN_TRUE_REGEX = re.compile(r'1|yes|true', re.IGNORECASE) @@ -102,16 +101,16 @@ def __init__(self, loader): self.loader = loader self.config = configparser.SafeConfigParser() self.config.optionxform = str - self.user_config_dir = self.loader.user_config_dir - self.user_config_filepath = '%s/%s' % (self.user_config_dir, VIT_CONFIG_FILE) - if not self.config_file_exists(self.user_config_filepath): - self.optional_create_config_file(self.user_config_filepath) - self.config.read(self.user_config_filepath) + self.vit_config = self.loader.user_config_file + if not self.config_file_exists(self.vit_config): + self.optional_create_config_file(self.vit_config) + self.config.read(self.vit_config) self.taskrc_path = self.get_taskrc_path() self.validate_taskrc() self.defaults = DEFAULTS self.set_config_data() + def set_config_data(self): self.subproject_indentable = self.is_subproject_indentable() self.row_striping_enabled = self.is_row_striping_enabled() @@ -203,7 +202,7 @@ def get_taskrc_path(self): taskrc_path = os.path.expanduser('TASKRC' in env.user and env.user['TASKRC'] or self.get('taskwarrior', 'taskrc')) if not os.path.exists(taskrc_path): - xdg_dir = xdg.get_xdg_config_dir(taskrc_path, "task") + xdg_dir = xdg.check_for_existing_xdg_configs("task") if xdg_dir: taskrc_path = os.path.join(xdg_dir, "taskrc") diff --git a/vit/loader.py b/vit/loader.py index 42fe3e6..da01175 100644 --- a/vit/loader.py +++ b/vit/loader.py @@ -1,21 +1,34 @@ import os -try: - import importlib.util -except: - import imp +import importlib.util +from pathlib import Path +from xdg_base_dirs import xdg_config_home -from vit import env, xdg - -DEFAULT_VIT_DIR = '~/.vit' class Loader: + def __init__(self): - self.user_config_dir = os.path.expanduser('VIT_DIR' in env.user and env.user['VIT_DIR'] or DEFAULT_VIT_DIR) + VIT_CONFIG_FILE = "config.ini" + # Set the correct vit config file location. + # VIT searches for the user directory in this order of priority: + # 1. The VIT_DIR environment variable + # 2. ~/.vit (the old default location) + # 3. A vit directory in any valid XDG base directory (the new default location) + old_default_vit_config = os.path.join(os.path.expanduser("~/.vit"), VIT_CONFIG_FILE) + if os.getenv("VIT_DIR"): + default_vit_dir = os.getenv("VIT_DIR") + config_path = os.path.join(default_vit_dir, VIT_CONFIG_FILE) + os.makedirs(default_vit_dir, exist_ok=True) + self.user_config_file = config_path + elif os.path.exists(old_default_vit_config): + self.user_config_file = old_default_vit_config + else: + vit_xdg_config_home = os.path.join(xdg_config_home(), 'vit') + xdg_config_file = os.path.join(vit_xdg_config_home, VIT_CONFIG_FILE) + os.makedirs(vit_xdg_config_home, exist_ok=True) + self.user_config_file = xdg_config_file + + self.user_config_dir = os.path.dirname(self.user_config_file) - if not os.path.exists(self.user_config_dir): - xdg_dir = xdg.get_xdg_config_dir(self.user_config_dir, "vit") - if xdg_dir: - self.user_config_dir = xdg_dir def load_user_class(self, module_type, module_name, class_name): module = '%s.%s' % (module_type, module_name) diff --git a/vit/xdg.py b/vit/xdg.py index 7494cbc..b7e9f99 100644 --- a/vit/xdg.py +++ b/vit/xdg.py @@ -3,7 +3,7 @@ from vit import env -def get_xdg_config_dir(user_config_dir, resource): +def check_for_existing_xdg_configs(resource): xdg_config_home = env.user.get("XDG_CONFIG_HOME") or os.path.join( os.path.expanduser("~"), ".config" )