1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Better settings handling

This commit is contained in:
Krateng 2019-03-28 16:18:31 +01:00
parent 03efd4fa62
commit 43adda8169
4 changed files with 14 additions and 6 deletions

View File

@ -120,9 +120,9 @@ def config(defaultextension=".ini",files=["settings.ini","settings.conf","config
else: return [allsettings.get(k) for k in keys] else: return [allsettings.get(k) for k in keys]
def update_settings(file,settings): def update_settings(file,settings,create_new=False):
if not os.path.exists(file): return if not os.path.exists(file): open(file,"w").close()
with open(file,"r") as origfile: with open(file,"r") as origfile:
lines = origfile.readlines() lines = origfile.readlines()
@ -162,10 +162,17 @@ def config(defaultextension=".ini",files=["settings.ini","settings.conf","config
#print("Found key") #print("Found key")
newline = origline.split("=",1) newline = origline.split("=",1)
#print({"linepart":newline[1],"keytoreplace":val,"new":settings[key]}) #print({"linepart":newline[1],"keytoreplace":val,"new":settings[key]})
newline[1] = newline[1].replace(val,str(settings[key])) newline[1] = newline[1].replace(val,str(settings[key]),1)
newline = "=".join(newline) newline = "=".join(newline)
newlines.append(newline) newlines.append(newline)
del settings[key]
if create_new:
# settings that were not present in the file
for key in settings:
newlines.append(key + " = " + settings[key] + "\n")
with open(file,"w") as newfile: with open(file,"w") as newfile:
newfile.write("".join(newlines)) newfile.write("".join(newlines))

4
maloja
View File

@ -22,6 +22,7 @@ SOURCE_URL = "https://github.com/krateng/maloja/archive/master.zip"
settings.config(files=["settings/default.ini","settings/settings.ini"]) settings.config(files=["settings/default.ini","settings/settings.ini"])
#settings.update("settings/default.ini","settings/settings.ini")
def blue(txt): return "\033[94m" + txt + "\033[0m" def blue(txt): return "\033[94m" + txt + "\033[0m"
@ -51,8 +52,7 @@ def setup():
print("Please enter your Last.FM API key. If you do not want to display artist and track images, simply leave this empty and press Enter.") print("Please enter your Last.FM API key. If you do not want to display artist and track images, simply leave this empty and press Enter.")
key = input() key = input()
if key == "": key = None if key == "": key = None
settings.update_settings("settings/settings.ini",{"LASTFM_API_KEY":key}) settings.update_settings("settings/settings.ini",{"LASTFM_API_KEY":key},create_new=True)
# TODO: Make sure we set the setting even if file doesnt exist
else: else:
print("Last.FM API key found.") print("Last.FM API key found.")

View File

@ -25,6 +25,7 @@ from urllib.error import *
settings.config(files=["settings/default.ini","settings/settings.ini"]) settings.config(files=["settings/default.ini","settings/settings.ini"])
#settings.update("settings/default.ini","settings/settings.ini")
MAIN_PORT, DATABASE_PORT = settings.get_settings("WEB_PORT","API_PORT") MAIN_PORT, DATABASE_PORT = settings.get_settings("WEB_PORT","API_PORT")

View File

@ -5,4 +5,4 @@ API_PORT = 42011
[Third Party Services] [Third Party Services]
LASTFM_API_KEY = "ASK" LASTFM_API_KEY = "ASK" # "ASK" signifies that the user has not yet indicated to not use any key at all.