From 5086039e6392523fad5ef3de2326eca7bf8b6832 Mon Sep 17 00:00:00 2001 From: Dhanush Kovi <99819848+dkvc@users.noreply.github.com> Date: Fri, 17 Feb 2023 20:09:47 +0000 Subject: [PATCH] feat: respect XDG spec for configuration files (#300) --- toggl/cli/commands.py | 2 -- toggl/utils/config.py | 14 +++++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/toggl/cli/commands.py b/toggl/cli/commands.py index 60d4947..952ec28 100644 --- a/toggl/cli/commands.py +++ b/toggl/cli/commands.py @@ -15,8 +15,6 @@ from toggl.cli import helpers, types from toggl.cli.themes import themes -DEFAULT_CONFIG_PATH = '~/.togglrc' - logger = logging.getLogger('toggl.cli.commands') click_completion.init() diff --git a/toggl/utils/config.py b/toggl/utils/config.py index e14a2e4..805e81e 100644 --- a/toggl/utils/config.py +++ b/toggl/utils/config.py @@ -8,6 +8,7 @@ import click import requests from pbr import version +from pathlib import Path from toggl.utils import metas, bootstrap, migrations from toggl import exceptions @@ -62,7 +63,18 @@ class IniConfigMixin: 'version': IniEntry('version', str), } - DEFAULT_CONFIG_PATH = os.path.expanduser('~/.togglrc') + _old_file_path = Path.expanduser(Path('~/.togglrc')) + + if "XDG_CONFIG_HOME" in os.environ: + _new_file_path = Path(os.environ["XDG_CONFIG_HOME"]).joinpath(".togglrc") + + if _new_file_path.exists() or not _old_file_path.exists(): + DEFAULT_CONFIG_PATH = _new_file_path + else: + DEFAULT_CONFIG_PATH = _old_file_path + + else: + DEFAULT_CONFIG_PATH = _old_file_path def __init__(self, config_path=sentinel, **kwargs): # type: (typing.Optional[str], **typing.Any) -> None self._config_path = self.DEFAULT_CONFIG_PATH if config_path == sentinel else config_path