import os.path, os import json, codecs ##---------------------------------------------------------------------------------------## class Config(object): ##---------------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------## def __init__(self): ##-----------------------------------------------------------------------------------## print("Config initialisiert") ##-----------------------------------------------------------------------------------## def defaultSettings(self): ##-----------------------------------------------------------------------------------## print("Returning Default-Settings") data = { "yeelight" : { "dunkel" : "1", "hell" : "100", "warm" : "1700", "normal" : "3000", "kalt" : "6500" } } return data ##-----------------------------------------------------------------------------------## def defaultDevices(self): ##-----------------------------------------------------------------------------------## print("Returning Default-Devices") data = {} return data ##-----------------------------------------------------------------------------------## def read(self, type): ##-----------------------------------------------------------------------------------## path = os.path.dirname(os.path.abspath(__file__)) path = path.replace('/app', '') data = None if(type == "settings"): print("Reading 'settings.json'") if(os.path.isfile(path + "/config/settings.json")): with open(path + '/config/settings.json', encoding='utf-8') as data_file: try: data = json.loads(data_file.read()) except: print("settings.json konnte nicht gelesen werden") data = self.defaultSettings() self.writeConfig("settings", data) print("Settings loaded") else: data =self.defaultSettings() self.writeConfig("settings", data) else: print("Reading 'devices.json'") if(os.path.isfile(path + "/config/devices.json")): with open(path + '/config/devices.json', encoding='utf-8') as data_file: try: data = json.loads(data_file.read()) except: print("devices.json konnte nicht gelesen werden") data = self.defaultDevices() self.writeConfig("devices", data) print("Devices loaded") else: data = self.defaultDevices() self.writeConfig("devices", data) return data ##-----------------------------------------------------------------------------------## def writeConfig(self, type, data): ##-----------------------------------------------------------------------------------## path = os.path.dirname(os.path.abspath(__file__)) path = path.replace('/app', '') #print(path) if(type == "settings"): print("Writing 'settings.json'") with open(path + '/config/settings.json', 'w') as outfile: json.dump(data, outfile, indent=4) print("Settings written") else: print("Writing 'devices.json'") with open(path + '/config/devices.json', 'w') as outfile: json.dump(data, outfile, indent=4) print("Devices written")