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

Add basic support for XDG dirs #222

Merged
merged 1 commit into from
Dec 10, 2022
Merged
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
12 changes: 11 additions & 1 deletion nvpy/nvpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from . import events
from http.client import HTTPException
import pathlib
import platform

try:
import markdown # type:ignore
Expand Down Expand Up @@ -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!
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand Down