2020-09-27 21:44:37 +00:00
|
|
|
from pathlib import Path
|
2020-04-17 10:31:47 +00:00
|
|
|
import sys
|
2020-10-29 12:00:58 +00:00
|
|
|
import os
|
2021-06-27 20:29:41 +00:00
|
|
|
import re
|
|
|
|
from deemix.utils import canWrite
|
2020-02-20 14:42:12 +00:00
|
|
|
|
2020-09-27 21:44:37 +00:00
|
|
|
homedata = Path.home()
|
2020-10-29 12:00:58 +00:00
|
|
|
userdata = ""
|
|
|
|
musicdata = ""
|
|
|
|
|
2021-06-27 20:29:41 +00:00
|
|
|
def checkPath(path):
|
|
|
|
if path == "": return ""
|
|
|
|
if not path.is_dir(): return ""
|
|
|
|
if not canWrite(path): return ""
|
|
|
|
return path
|
2020-10-29 12:00:58 +00:00
|
|
|
|
2020-02-20 14:42:12 +00:00
|
|
|
def getConfigFolder():
|
2021-06-27 20:29:41 +00:00
|
|
|
global userdata
|
|
|
|
if userdata != "": return userdata
|
|
|
|
if os.getenv("XDG_CONFIG_HOME") and userdata == "":
|
|
|
|
userdata = Path(os.getenv("XDG_CONFIG_HOME"))
|
|
|
|
userdata = checkPath(userdata)
|
|
|
|
if os.getenv("APPDATA") and userdata == "":
|
|
|
|
userdata = Path(os.getenv("APPDATA"))
|
|
|
|
userdata = checkPath(userdata)
|
|
|
|
if sys.platform.startswith('darwin') and userdata == "":
|
|
|
|
userdata = homedata / 'Library' / 'Application Support'
|
|
|
|
userdata = checkPath(userdata)
|
|
|
|
if userdata == "":
|
|
|
|
userdata = homedata / '.config'
|
|
|
|
userdata = checkPath(userdata)
|
|
|
|
|
|
|
|
if userdata == "": userdata = Path(os.getcwd()) / 'config'
|
|
|
|
else: userdata = userdata / 'deemix'
|
|
|
|
|
|
|
|
if os.getenv("DEEMIX_DATA_DIR"):
|
|
|
|
userdata = Path(os.getenv("DEEMIX_DATA_DIR"))
|
2020-04-17 10:31:47 +00:00
|
|
|
return userdata
|
2020-10-29 12:00:58 +00:00
|
|
|
|
|
|
|
def getMusicFolder():
|
2021-06-27 20:29:41 +00:00
|
|
|
global musicdata
|
|
|
|
if musicdata != "": return musicdata
|
|
|
|
if os.getenv("XDG_MUSIC_DIR") and musicdata == "":
|
|
|
|
musicdata = Path(os.getenv("XDG_MUSIC_DIR"))
|
|
|
|
musicdata = checkPath(musicdata)
|
|
|
|
if (homedata / '.config' / 'user-dirs.dirs').is_file() and musicdata == "":
|
2021-12-21 11:40:35 +00:00
|
|
|
with open(homedata / '.config' / 'user-dirs.dirs', 'r', encoding="utf-8") as f:
|
2021-06-27 20:29:41 +00:00
|
|
|
userDirs = f.read()
|
2021-07-16 12:49:10 +00:00
|
|
|
musicdata_search = re.search(r"XDG_MUSIC_DIR=\"(.*)\"", userDirs)
|
|
|
|
if musicdata_search:
|
|
|
|
musicdata = musicdata_search.group(1)
|
|
|
|
musicdata = Path(os.path.expandvars(musicdata))
|
|
|
|
musicdata = checkPath(musicdata)
|
2021-06-27 20:29:41 +00:00
|
|
|
if os.name == 'nt' and musicdata == "":
|
2021-12-21 14:13:27 +00:00
|
|
|
try:
|
|
|
|
musicKeys = ['My Music', '{4BD8D571-6D19-48D3-BE97-422220080E43}']
|
|
|
|
regData = os.popen(r'reg.exe query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"').read().split('\r\n')
|
|
|
|
for i, line in enumerate(regData):
|
|
|
|
if line == "": continue
|
|
|
|
if i == 1: continue
|
|
|
|
line = line.split(' ')
|
|
|
|
if line[1] in musicKeys:
|
|
|
|
musicdata = Path(line[3])
|
|
|
|
break
|
|
|
|
musicdata = checkPath(musicdata)
|
|
|
|
except Exception:
|
|
|
|
musicdata = ""
|
2021-06-27 20:29:41 +00:00
|
|
|
if musicdata == "":
|
|
|
|
musicdata = homedata / 'Music'
|
|
|
|
musicdata = checkPath(musicdata)
|
|
|
|
|
|
|
|
if musicdata == "": musicdata = Path(os.getcwd()) / 'music'
|
|
|
|
else: musicdata = musicdata / 'deemix Music'
|
|
|
|
|
|
|
|
if os.getenv("DEEMIX_MUSIC_DIR"):
|
|
|
|
musicdata = Path(os.getenv("DEEMIX_MUSIC_DIR"))
|
2020-10-29 12:00:58 +00:00
|
|
|
return musicdata
|