polished finding music directory to follow the freedesktop standard
This commit is contained in:
parent
6d26be674b
commit
73c75c6598
@ -1,4 +1,7 @@
|
|||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from sys import platform
|
from sys import platform
|
||||||
import logging
|
import logging
|
||||||
from os.path import expandvars
|
from os.path import expandvars
|
||||||
@ -14,27 +17,37 @@ def get_xdg_music_directory() -> Path:
|
|||||||
Thanks to Distant Thunder, as well as Kevin Gruber for making that pull request:
|
Thanks to Distant Thunder, as well as Kevin Gruber for making that pull request:
|
||||||
https://github.com/HeIIow2/music-downloader/pull/6
|
https://github.com/HeIIow2/music-downloader/pull/6
|
||||||
|
|
||||||
:return:
|
XDG_USER_DIRS_FILE reference:
|
||||||
|
https://freedesktop.org/wiki/Software/xdg-user-dirs/
|
||||||
|
https://web.archive.org/web/20230322012953/https://freedesktop.org/wiki/Software/xdg-user-dirs/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# XDG_USER_DIRS_FILE reference: https://freedesktop.org/wiki/Software/xdg-user-dirs/
|
xdg_user_dirs_file = os.environ.get("XDG_CONFIG_HOME") or Path(Path.home(), ".config", "user-dirs.dirs")
|
||||||
xdg_user_dirs_file = Path(Path.home(), ".config", "user-dirs.dirs")
|
xdg_user_dirs_default_file = Path("/etc/xdg/user-dirs.defaults")
|
||||||
|
|
||||||
try:
|
def get_music_dir_from_xdg_file(xdg_file_path: os.PathLike) -> Optional[Path]:
|
||||||
with open(xdg_user_dirs_file, 'r') as f:
|
try:
|
||||||
data = "[XDG_USER_DIRS]\n" + f.read()
|
with open(xdg_file_path, 'r') as f:
|
||||||
config = configparser.ConfigParser(allow_no_value=True)
|
data = "[XDG_USER_DIRS]\n" + f.read()
|
||||||
config.read_string(data)
|
config = configparser.ConfigParser(allow_no_value=True)
|
||||||
xdg_config = config['XDG_USER_DIRS']
|
config.read_string(data)
|
||||||
return Path(expandvars(xdg_config['xdg_music_dir'].strip('"')))
|
xdg_config = config['XDG_USER_DIRS']
|
||||||
|
return Path(expandvars(xdg_config['xdg_music_dir'].strip('"')))
|
||||||
|
|
||||||
except (FileNotFoundError, KeyError) as e:
|
except (FileNotFoundError, KeyError) as e:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Missing file or No entry found for \"xdg_music_dir\" in: \"{xdg_user_dirs_file}\".\n"
|
f"Missing file or No entry found for \"xdg_music_dir\" in: \"{xdg_file_path}\".\n"
|
||||||
f"Will fallback on default \"$HOME/Music\"."
|
)
|
||||||
)
|
logging.debug(str(e))
|
||||||
logging.debug(str(e))
|
|
||||||
|
|
||||||
|
music_dir = get_music_dir_from_xdg_file(xdg_user_dirs_file)
|
||||||
|
if music_dir is not None:
|
||||||
|
return music_dir
|
||||||
|
music_dir = get_music_dir_from_xdg_file(xdg_user_dirs_default_file)
|
||||||
|
if music_dir is not None:
|
||||||
|
return music_dir
|
||||||
|
|
||||||
|
logging.warning(f"couldn't find a XDG music dir, falling back to: {DEFAULT_MUSIC_DIRECTORY}")
|
||||||
return DEFAULT_MUSIC_DIRECTORY
|
return DEFAULT_MUSIC_DIRECTORY
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user