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"

View file

@ -34,6 +34,7 @@ class BasicController:
return self.resource(data.get("id"), data.get("name"), type=data.get("type"), props=Props(data.get("props", {})) )
async def load(self):
try:
logging.info("{} Load ".format(self.name))
with open(self.path) as json_file:
data = json.load(json_file)
@ -47,6 +48,25 @@ class BasicController:
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()
async def save(self):
logging.info("{} Save ".format(self.name))