diff --git a/doreah/settings.py b/doreah/settings.py index a8d0b16..8c9ddf1 100644 --- a/doreah/settings.py +++ b/doreah/settings.py @@ -120,9 +120,9 @@ def config(defaultextension=".ini",files=["settings.ini","settings.conf","config 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: lines = origfile.readlines() @@ -162,10 +162,17 @@ def config(defaultextension=".ini",files=["settings.ini","settings.conf","config #print("Found key") newline = origline.split("=",1) #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) 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: newfile.write("".join(newlines)) diff --git a/maloja b/maloja index a118939..dee8c18 100755 --- a/maloja +++ b/maloja @@ -22,6 +22,7 @@ SOURCE_URL = "https://github.com/krateng/maloja/archive/master.zip" 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" @@ -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.") key = input() if key == "": key = None - settings.update_settings("settings/settings.ini",{"LASTFM_API_KEY":key}) - # TODO: Make sure we set the setting even if file doesnt exist + settings.update_settings("settings/settings.ini",{"LASTFM_API_KEY":key},create_new=True) else: print("Last.FM API key found.") diff --git a/server.py b/server.py index 897a6dc..bd4599c 100755 --- a/server.py +++ b/server.py @@ -25,6 +25,7 @@ from urllib.error import * 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") diff --git a/settings/default.ini b/settings/default.ini index edcd42a..717bbe9 100644 --- a/settings/default.ini +++ b/settings/default.ini @@ -5,4 +5,4 @@ API_PORT = 42011 [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.