From accabdf4dc01d58e00c90a5a53e4b6fc11430a35 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Tue, 26 May 2020 12:22:32 +0200 Subject: [PATCH] Implemented --portable option --- .gitignore | 4 +++- app.py | 22 +++++++++++++++------- deemix_gui.py | 17 +++++++++++++---- server.py | 14 ++++++++------ 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 881e5a6..60f7f89 100644 --- a/.gitignore +++ b/.gitignore @@ -25,5 +25,7 @@ yarn-error.log* *.sln *.sw? -# Private configs +# Development stuff /config.py +deemix +config diff --git a/app.py b/app.py index 9c178f0..8062243 100644 --- a/app.py +++ b/app.py @@ -12,24 +12,31 @@ from os import remove settings = {} spotifyHelper = None chartsList = [] +configFolder = "" -def initialize(): +def initialize(portable): global settings global spotifyHelper global defaultSettings - settings = initSettings() + global configFolder + if portable: + configFolder = portable + else: + configFolder = getConfigFolder() + settings = initSettings(configFolder=configFolder) defaultSettings = getDefaultSettings() - spotifyHelper = SpotifyHelper() + spotifyHelper = SpotifyHelper(configFolder=configFolder) def shutdown(interface=None): + global configFolder if settings['saveDownloadQueue']: (queue, queueComplete, queueList, currentItem) = getQueue() if len(queueList) > 0: if currentItem != "": queue.insert(0, currentItem) - with open(path.join(getConfigFolder(), 'queue.json'), 'w') as f: + with open(path.join(configFolder, 'queue.json'), 'w') as f: json.dump({ 'queue': queue, 'queueComplete': queueComplete, @@ -40,11 +47,12 @@ def shutdown(interface=None): interface.send("toast", {'msg': "Server is closed."}) def loadDownloadQueue(dz, interface=None): - if path.isfile(path.join(getConfigFolder(), 'queue.json')): + global configFolder + if path.isfile(path.join(configFolder, 'queue.json')): if interface: interface.send('toast', {'msg': "Restoring download queue", 'icon': 'loading', 'dismiss': False, 'id': 'restoring_queue'}) - with open(path.join(getConfigFolder(), 'queue.json'), 'r') as f: + with open(path.join(configFolder, 'queue.json'), 'r') as f: qd = json.load(f) if interface: interface.send('init_downloadQueue', @@ -52,7 +60,7 @@ def loadDownloadQueue(dz, interface=None): if interface: interface.send('toast', {'msg': "Download queue restored!", 'icon': 'done', 'dismiss': True, 'id': 'restoring_queue'}) - remove(path.join(getConfigFolder(), 'queue.json')) + remove(path.join(configFolder, 'queue.json')) restoreQueue(qd['queue'], qd['queueComplete'], qd['queueList'], dz, interface) def getDownloadFolder(): diff --git a/deemix_gui.py b/deemix_gui.py index b4bee27..9f080c5 100644 --- a/deemix_gui.py +++ b/deemix_gui.py @@ -32,17 +32,26 @@ def save_position(): if __name__ == '__main__': url = "127.0.0.1" + port = 9666 if len(sys.argv) >= 2: - port = int(sys.argv[1]) + try: + port = int(sys.argv[1]) + except ValueError: + pass + if '--portable' in sys.argv: + portable = path.join(path.dirname(path.realpath(__file__)), 'config') else: - port = 9666 - t = Thread(target=run_server, args=(port, )) + portable = None + t = Thread(target=run_server, args=(port, portable)) t.daemon = True t.start() while not url_ok(url, port): sleep(1) - configFolder = getConfigFolder() + if portable: + configFolder = portable + else: + configFolder = getConfigFolder() if path.isfile(path.join(configFolder, '.UIposition')): try: diff --git a/server.py b/server.py index c9a750c..ec7ac96 100644 --- a/server.py +++ b/server.py @@ -10,7 +10,6 @@ from flask_socketio import SocketIO, emit import app from deemix.api.deezer import Deezer from deemix.app.MessageInterface import MessageInterface -from deemix.utils import localpaths # Workaround for MIME type error in certain Windows installs # https://github.com/pallets/flask/issues/1045#issuecomment-42202749 @@ -65,10 +64,8 @@ logging.getLogger('socketio').setLevel(logging.ERROR) logging.getLogger('engineio').setLevel(logging.ERROR) #server.logger.disabled = True -app.initialize() firstConnection = True - @server.route('/') def landing(): return render_template('index.html') @@ -93,7 +90,7 @@ def on_connect(): emit('init_settings', (settings, spotifyCredentials, defaultSettings)) emit('init_autologin') - arl_file_path = path.join(localpaths.getConfigFolder(), '.arl') + arl_file_path = path.join(app.configFolder, '.arl') if serverwide_arl and path.isfile(arl_file_path): with open(arl_file_path, 'r') as file: arl = file.readline().rstrip("\n") @@ -260,7 +257,8 @@ def openDownloadsFolder(): elif sys.platform == 'win32': subprocess.check_call(['explorer', folder]) -def run_server(port): +def run_server(port, portable=None): + app.initialize(portable) print("Starting server at http://127.0.0.1:" + str(port)) socketio.run(server, host='0.0.0.0', port=port) @@ -272,4 +270,8 @@ if __name__ == '__main__': port = int(sys.argv[1]) except ValueError: pass - run_server(port) + if '--portable' in sys.argv: + portable = path.join(path.dirname(path.realpath(__file__)), 'config') + else: + portable = None + run_server(port, portable)