From 7fd0bfaa0707eced95708436cd93b375c2bf3f8c Mon Sep 17 00:00:00 2001 From: RemixDev Date: Thu, 29 Oct 2020 13:00:58 +0100 Subject: [PATCH] Made -p cli argument create only that folder and not the default one Moved default download folder inside music Check XDG first, then fallback to untranslated 'Music' folder This fixes #82 --- deemix/app/__init__.py | 4 ++-- deemix/app/cli.py | 4 +--- deemix/app/settings.py | 32 +++++++++++++++++++++++--------- deemix/utils/localpaths.py | 29 +++++++++++++++++++++++------ 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/deemix/app/__init__.py b/deemix/app/__init__.py index 66da18c..a7e85bb 100644 --- a/deemix/app/__init__.py +++ b/deemix/app/__init__.py @@ -4,8 +4,8 @@ from deemix.app.queuemanager import QueueManager from deemix.app.spotifyhelper import SpotifyHelper class deemix: - def __init__(self, configFolder=None): - self.set = Settings(configFolder) + def __init__(self, configFolder=None, overwriteDownloadFolder=None): + self.set = Settings(configFolder, overwriteDownloadFolder=overwriteDownloadFolder) self.dz = Deezer() self.dz.set_accept_language(self.set.settings.get('tagsLanguage')) self.sp = SpotifyHelper(configFolder) diff --git a/deemix/app/cli.py b/deemix/app/cli.py index 45b945b..379e11f 100644 --- a/deemix/app/cli.py +++ b/deemix/app/cli.py @@ -6,10 +6,8 @@ from deemix.utils import checkFolder class cli(deemix): def __init__(self, downloadpath, configFolder=None): - super().__init__(configFolder) + super().__init__(configFolder, overwriteDownloadFolder=downloadpath) if downloadpath: - if checkFolder(downloadpath): - self.set.settings['downloadLocation'] = str(downloadpath) print("Using folder: "+self.set.settings['downloadLocation']) def downloadLink(self, url, bitrate=None): diff --git a/deemix/app/settings.py b/deemix/app/settings.py index f13247c..bbc6f14 100644 --- a/deemix/app/settings.py +++ b/deemix/app/settings.py @@ -47,7 +47,7 @@ class FeaturesOption(): """Move to track title""" DEFAULT_SETTINGS = { - "downloadLocation": str(localpaths.getHomeFolder() / 'deemix Music'), + "downloadLocation": str(localpaths.getMusicFolder()), "tracknameTemplate": "%artist% - %title%", "albumTracknameTemplate": "%tracknumber% - %title%", "playlistTracknameTemplate": "%position% - %artist% - %title%", @@ -128,7 +128,7 @@ DEFAULT_SETTINGS = { } class Settings: - def __init__(self, configFolder=None): + def __init__(self, configFolder=None, overwriteDownloadFolder=None): self.settings = {} self.configFolder = Path(configFolder or localpaths.getConfigFolder()) @@ -144,11 +144,27 @@ class Settings: with open(self.configFolder / 'config.json', 'r') as configFile: self.settings = json.load(configFile) - # Make sure the download path exsits - if not checkFolder(self.settings['downloadLocation']) and self.settings['downloadLocation'] != "": - self.settings['downloadLocation'] = "" + # Check for overwriteDownloadFolder + # This prevents the creation of the original download folder when + # using overwriteDownloadFolder + originalDownloadFolder = self.settings['downloadLocation'] + if overwriteDownloadFolder: + overwriteDownloadFolder = str(overwriteDownloadFolder) + self.settings['downloadLocation'] = overwriteDownloadFolder - self.settingsCheck() + # Make sure the download path exsits, fallback to default + invalidDownloadFolder = False + if self.settings['downloadLocation'] == "" or not checkFolder(self.settings['downloadLocation']): + self.settings['downloadLocation'] = DEFAULT_SETTINGS['downloadLocation'] + originalDownloadFolder = self.settings['downloadLocation'] + invalidDownloadFolder = True + + # Check the settings and save them if something changed + if self.settingsCheck() > 0 or invalidDownloadFolder: + makedirs(self.settings['downloadLocation'], exist_ok=True) + self.settings['downloadLocation'] = originalDownloadFolder # Prevents the saving of the overwritten path + self.saveSettings() + self.settings['downloadLocation'] = overwriteDownloadFolder or originalDownloadFolder # Restores the correct path # LOGFILES @@ -196,11 +212,9 @@ class Settings: changes += 1 if self.settings['downloadLocation'] == "": self.settings['downloadLocation'] = DEFAULT_SETTINGS['downloadLocation'] - makedirs(self.settings['downloadLocation'], exist_ok=True) changes += 1 for template in ['tracknameTemplate', 'albumTracknameTemplate', 'playlistTracknameTemplate', 'playlistNameTemplate', 'artistNameTemplate', 'albumNameTemplate', 'playlistFilenameTemplate', 'coverImageTemplate', 'artistImageTemplate', 'paddingSize']: if self.settings[template] == "": self.settings[template] = DEFAULT_SETTINGS[template] changes += 1 - if changes > 0: - self.saveSettings() + return changes diff --git a/deemix/utils/localpaths.py b/deemix/utils/localpaths.py index b836ede..8ad00e0 100644 --- a/deemix/utils/localpaths.py +++ b/deemix/utils/localpaths.py @@ -1,21 +1,38 @@ from pathlib import Path import sys -from os import getenv +import os -userdata = "" homedata = Path.home() +userdata = "" +musicdata = "" -if getenv("APPDATA"): - userdata = Path(getenv("APPDATA")) / "deemix" + +if os.getenv("XDG_CONFIG_HOME"): + userdata = Path(os.getenv("XDG_CONFIG_HOME")) / 'deemix' +elif os.getenv("APPDATA"): + userdata = Path(os.getenv("APPDATA")) / "deemix" elif sys.platform.startswith('darwin'): userdata = homedata / 'Library' / 'Application Support' / 'deemix' -elif getenv("XDG_CONFIG_HOME"): - userdata = Path(getenv("XDG_CONFIG_HOME")) / 'deemix' else: userdata = homedata / '.config' / 'deemix' +if os.getenv("XDG_MUSIC_DIR"): + musicdata = Path(os.getenv("XDG_MUSIC_DIR")) / "deemix Music" +elif os.name == 'nt': + import winreg + sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders' + music_guid = '{4BD8D571-6D19-48D3-BE97-422220080E43}' + with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key: + location = winreg.QueryValueEx(key, music_guid)[0] + musicdata = Path(location) / "deemix Music" +else: + musicdata = homedata / "Music" / "deemix Music" + def getHomeFolder(): return homedata def getConfigFolder(): return userdata + +def getMusicFolder(): + return musicdata