From a9a09c58e06b6f333676ade4c6ade3eb4f391a2d Mon Sep 17 00:00:00 2001 From: RemixDev Date: Thu, 8 Oct 2020 13:26:17 +0200 Subject: [PATCH] Added folderCheck for download folder folderCheck checks if the folder exsists and it's writable --- deemix/__init__.py | 2 +- deemix/app/cli.py | 5 +++-- deemix/app/settings.py | 14 ++++++++++---- deemix/utils/__init__.py | 9 +++++++++ setup.py | 2 +- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/deemix/__init__.py b/deemix/__init__.py index 2d79367..3cd3b75 100644 --- a/deemix/__init__.py +++ b/deemix/__init__.py @@ -1,3 +1,3 @@ #!/usr/bin/env python3 -__version__ = "1.5.12" +__version__ = "1.5.13" diff --git a/deemix/app/cli.py b/deemix/app/cli.py index 32c638b..45b945b 100644 --- a/deemix/app/cli.py +++ b/deemix/app/cli.py @@ -2,13 +2,14 @@ from pathlib import Path from os import makedirs from deemix.app import deemix +from deemix.utils import checkFolder class cli(deemix): def __init__(self, downloadpath, configFolder=None): super().__init__(configFolder) if downloadpath: - self.set.settings['downloadLocation'] = str(downloadpath) - makedirs(downloadpath, exist_ok=True) + 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 74dcd74..d2bae7d 100644 --- a/deemix/app/settings.py +++ b/deemix/app/settings.py @@ -3,6 +3,7 @@ from pathlib import Path from os import makedirs, listdir from deemix import __version__ as deemixVersion from deemix.api.deezer import TrackFormats +from deemix.utils import checkFolder import logging import datetime import platform @@ -142,10 +143,11 @@ class Settings: with open(self.configFolder / 'config.json', 'r') as configFile: self.settings = json.load(configFile) - self.settingsCheck() - # Make sure the download path exsits - makedirs(self.settings['downloadLocation'], exist_ok=True) + if not checkFolder(self.settings['downloadLocation']) and self.settings['downloadLocation'] != "": + self.settings['downloadLocation'] = "" + + self.settingsCheck() # LOGFILES @@ -173,6 +175,9 @@ class Settings: def saveSettings(self, newSettings=None, dz=None): if newSettings: if dz and newSettings.get('tagsLanguage') != self.settings.get('tagsLanguage'): dz.set_accept_language(newSettings.get('tagsLanguage')) + if newSettings.get('downloadLocation') != self.settings.get('downloadLocation') and not checkFolder(newSettings.get('downloadLocation')): + newSettings['downloadLocation'] = DEFAULT_SETTINGS['downloadLocation'] + makedirs(newSettings['downloadLocation'], exist_ok=True) self.settings = newSettings with open(self.configFolder / 'config.json', 'w') as configFile: json.dump(self.settings, configFile, indent=2) @@ -189,7 +194,8 @@ class Settings: self.settings['tags'][set] = DEFAULT_SETTINGS['tags'][set] changes += 1 if self.settings['downloadLocation'] == "": - self.settings['downloadLocation'] = str(localpaths.getHomeFolder() / 'deemix Music') + 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] == "": diff --git a/deemix/utils/__init__.py b/deemix/utils/__init__.py index ef9bc4c..159c226 100644 --- a/deemix/utils/__init__.py +++ b/deemix/utils/__init__.py @@ -1,6 +1,7 @@ import re import string from deemix.api.deezer import TrackFormats +import os def generateReplayGainString(trackGain): return "{0:.2f} dB".format((float(trackGain) + 18.4) * -1) @@ -120,3 +121,11 @@ def uniqueArray(arr): if iPrinc!=iRest and namePrinc.lower() in nRest.lower(): del arr[iRest] return arr + +def checkFolder(folder): + try: + os.makedirs(folder, exist_ok=True) + except Exception as e: + print(str(e)) + return False + return os.access(folder, os.W_OK) diff --git a/setup.py b/setup.py index c26c0df..56137b0 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text() setup( name="deemix", - version="1.5.12", + version="1.5.13", description="A barebone deezer downloader library", long_description=README, long_description_content_type="text/markdown",