From 2845428b24d6181db23c6c5f3c17908978e1e2e2 Mon Sep 17 00:00:00 2001 From: Dawid Zych Date: Fri, 8 Oct 2021 23:40:22 +0200 Subject: [PATCH] Add basic support for XDG dirs Read configuration from `XDG_CONFIG_HOME/nvpy.cfg` (usually `~/.config/nvpy.cfg`) if exits. This file has the highest priority and overwrites settings from other configuration files. Save user settings in `XDG_CACHE_HOME/nvpy_settings` (usually `~/.cache/nvpy_settings`) on linux. --- nvpy/nvpy.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/nvpy/nvpy.py b/nvpy/nvpy.py index 476442e..a648a69 100755 --- a/nvpy/nvpy.py +++ b/nvpy/nvpy.py @@ -50,6 +50,7 @@ from . import events from http.client import HTTPException import pathlib +import platform try: import markdown # type:ignore @@ -105,6 +106,7 @@ def __init__(self, app_dir: str, cfg: typing.Optional[str]): @param app_dir: the directory containing nvpy.py @param cfg: path to configuration file """ + is_linux = platform.system() == "Linux" self.app_dir = app_dir # cross-platform way of getting home dir! @@ -114,6 +116,11 @@ def __init__(self, app_dir: str, cfg: typing.Optional[str]): # the file that we write user settings to, which is different # from the configuration files self.settings_file = os.path.join(home, '.nvpy_settings') + if is_linux: + env_dir = os.environ.get("XDG_CACHE_HOME") + cache_dir = pathlib.Path(env_dir) if env_dir and os.path.isabs(env_dir) else pathlib.Path.home() / ".cache" + self.settings_file = cache_dir / "nvpy_settings" + defaults = { 'app_dir': app_dir, @@ -266,12 +273,15 @@ def _load_cfg(self, defaults: dict, cfg: typing.Optional[str]) -> typing.Tuple[t else: # Later config files overwrite earlier files try a number of alternatives. home = pathlib.Path.home() + env_dir = os.environ.get("XDG_CONFIG_HOME") + xdg_config_home = pathlib.Path(env_dir) if env_dir and os.path.isabs(env_dir) else home / ".config" cfg_files = [ pathlib.Path(self.app_dir) / 'nvpy.cfg', home / 'nvpy.cfg', home / '.nvpy.cfg', home / '.nvpy', home / '.nvpyrc', + xdg_config_home / 'nvpy.cfg', ] return cp.read(cfg_files), cp @@ -972,7 +982,7 @@ def get_appdir(): def parse_cmd_line_args(args: typing.Optional[typing.List] = None) -> argparse.Namespace: """ Parse command line arguments - + Args: args: List of command line arguments. If args is not specified, takes args from sys.args.