added error handling in case of corrupt json config files.

This commit is contained in:
avollkopf 2023-04-08 11:55:49 +02:00
parent 5e69ce4c40
commit 9041ad7daa
2 changed files with 32 additions and 12 deletions

View file

@ -1,3 +1,3 @@
__version__ = "4.1.8.a7" __version__ = "4.1.8.a8"
__codename__ = "Groundhog Day" __codename__ = "Groundhog Day"

View file

@ -34,19 +34,39 @@ class BasicController:
return self.resource(data.get("id"), data.get("name"), type=data.get("type"), props=Props(data.get("props", {})) ) return self.resource(data.get("id"), data.get("name"), type=data.get("type"), props=Props(data.get("props", {})) )
async def load(self): async def load(self):
logging.info("{} Load ".format(self.name)) try:
with open(self.path) as json_file: logging.info("{} Load ".format(self.name))
data = json.load(json_file) with open(self.path) as json_file:
data['data'].sort(key=lambda x: x.get('name').upper()) data = json.load(json_file)
data['data'].sort(key=lambda x: x.get('name').upper())
for i in data["data"]: for i in data["data"]:
self.data.append(self.create(i)) self.data.append(self.create(i))
if self.autostart is True:
for item in self.data:
logging.info("{} Starting ".format(self.name))
await self.start(item.id)
await self.push_udpate()
except Exception as e:
logging.warning("Invalid {} file - Creating empty file".format(self.path))
os.remove(self.path)
with open(self.path, "w") as file:
json.dump(dict( data=[]), file, indent=4, sort_keys=True)
with open(self.path) as json_file:
data = json.load(json_file)
data['data'].sort(key=lambda x: x.get('name').upper())
for i in data["data"]:
self.data.append(self.create(i))
if self.autostart is True:
for item in self.data:
logging.info("{} Starting ".format(self.name))
await self.start(item.id)
await self.push_udpate()
if self.autostart is True:
for item in self.data:
logging.info("{} Starting ".format(self.name))
await self.start(item.id)
await self.push_udpate()
async def save(self): async def save(self):
logging.info("{} Save ".format(self.name)) logging.info("{} Save ".format(self.name))