mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-10 01:17:42 +01:00
added error handling in case of corrupt json config files.
This commit is contained in:
parent
5e69ce4c40
commit
9041ad7daa
2 changed files with 32 additions and 12 deletions
|
@ -1,3 +1,3 @@
|
||||||
__version__ = "4.1.8.a7"
|
__version__ = "4.1.8.a8"
|
||||||
__codename__ = "Groundhog Day"
|
__codename__ = "Groundhog Day"
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ 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):
|
||||||
|
try:
|
||||||
logging.info("{} Load ".format(self.name))
|
logging.info("{} Load ".format(self.name))
|
||||||
with open(self.path) as json_file:
|
with open(self.path) as json_file:
|
||||||
data = json.load(json_file)
|
data = json.load(json_file)
|
||||||
|
@ -47,6 +48,25 @@ class BasicController:
|
||||||
logging.info("{} Starting ".format(self.name))
|
logging.info("{} Starting ".format(self.name))
|
||||||
await self.start(item.id)
|
await self.start(item.id)
|
||||||
await self.push_udpate()
|
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):
|
async def save(self):
|
||||||
logging.info("{} Save ".format(self.name))
|
logging.info("{} Save ".format(self.name))
|
||||||
|
|
Loading…
Reference in a new issue