Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make $XDG_CONFIG_HOME/vit the new default vit config location #341

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions CUSTOMIZE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tasklib>=2.4.3
urwid>=2.1.2
backports.zoneinfo;python_version<"3.9"
xdg-base-dirs>=6.0.0
13 changes: 6 additions & 7 deletions vit/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")

Expand Down
37 changes: 25 additions & 12 deletions vit/loader.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion vit/xdg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down