Skip to content

Commit

Permalink
Merge branch 'xdg-dirs' of https://github.com/silenc3r/nvpy
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuki0xff committed Dec 10, 2022
2 parents ea23492 + 2845428 commit a9f33dc
Showing 1 changed file with 11 additions and 1 deletion.
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 @@ -106,6 +107,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 @@ -115,6 +117,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 @@ -267,12 +274,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 @@ -974,7 +984,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

0 comments on commit a9f33dc

Please sign in to comment.