deemix-py/deemix/app/settings.py

78 lines
2.3 KiB
Python
Raw Normal View History

2020-02-20 14:42:12 +00:00
#!/usr/bin/env python3
import json
import os.path as path
2020-05-18 10:31:56 +00:00
from os import makedirs
2020-05-20 14:36:01 +00:00
import random
import string
2020-02-20 14:42:12 +00:00
import deemix.utils.localpaths as localpaths
settings = {}
defaultSettings = {}
2020-02-20 14:42:12 +00:00
2020-05-20 14:36:01 +00:00
def initSettings(bot_mode = False):
global settings
global defaultSettings
currentFolder = path.abspath(path.dirname(__file__))
configFolder = localpaths.getConfigFolder()
2020-05-18 10:31:56 +00:00
makedirs(configFolder, exist_ok=True)
with open(path.join(currentFolder, 'default.json'), 'r') as d:
defaultSettings = json.load(d)
2020-05-20 14:36:01 +00:00
defaultSettings['downloadLocation'] = ""
if not path.isfile(path.join(configFolder, 'config.json')):
with open(path.join(configFolder, 'config.json'), 'w') as f:
json.dump(defaultSettings, f, indent=2)
with open(path.join(configFolder, 'config.json'), 'r') as configFile:
settings = json.load(configFile)
settingsCheck()
2020-05-20 14:36:01 +00:00
if bot_mode:
print("Im using bot mode")
settings['downloadLocation'] = randomString(12)
elif settings['downloadLocation'] == "":
print("I'm using normal mode")
settings['downloadLocation'] = path.join(localpaths.getHomeFolder(), 'deemix Music')
2020-05-18 10:31:56 +00:00
saveSettings(settings)
makedirs(settings['downloadLocation'], exist_ok=True)
return settings
2020-02-20 14:42:12 +00:00
def getSettings():
global settings
return settings
2020-02-20 14:42:12 +00:00
def getDefaultSettings():
global defaultSettings
return defaultSettings
2020-02-20 14:42:12 +00:00
def saveSettings(newSettings):
global settings
settings = newSettings
with open(path.join(localpaths.getConfigFolder(), 'config.json'), 'w') as configFile:
json.dump(settings, configFile, indent=2)
return True
def settingsCheck():
global settings
global defaultSettings
changes = 0
for x in defaultSettings:
if not x in settings or type(settings[x]) != type(defaultSettings[x]):
settings[x] = defaultSettings[x]
changes += 1
for x in defaultSettings['tags']:
if not x in settings['tags'] or type(settings['tags'][x]) != type(defaultSettings['tags'][x]):
settings['tags'][x] = defaultSettings['tags'][x]
changes += 1
if changes > 0:
saveSettings(settings)
2020-05-20 14:36:01 +00:00
def randomString(stringLength=8):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))