Fix invalid json error handling

trunk
mio 2019-03-07 19:57:47 +00:00
parent de77a2b1eb
commit dbc209543f
1 changed files with 29 additions and 24 deletions

View File

@ -9,12 +9,13 @@ class Wilty():
def __init__(self): def __init__(self):
self.name = "wilty" self.name = "wilty"
self.version = self.name + " 0.1" self.version = self.name + " 0.2"
self.save_dir = ".botany" self.save_dir = ".botany"
self.plant_file = "_plant_data.json" self.plant_file = "_plant_data.json"
self.visit_file = "visitors.json" self.visit_file = "visitors.json"
self.harvest_file = "harvest_file.json" self.harvest_file = "harvest_file.json"
self.water_user = self.name self.water_user = self.name
self.users = {}
self.plants = {} self.plants = {}
self.live_plants = {} self.live_plants = {}
self.stats_model = { self.stats_model = {
@ -94,7 +95,7 @@ class Wilty():
sv_dir = "/home/" + u.pw_name + "/" + self.save_dir + "/" sv_dir = "/home/" + u.pw_name + "/" + self.save_dir + "/"
if os.path.exists(sv_dir) and \ if os.path.exists(sv_dir) and \
os.access(sv_dir + u.pw_name + self.plant_file, os.R_OK): os.access(sv_dir + u.pw_name + self.plant_file, os.R_OK):
self.plants[u.pw_name] = {} self.users[u.pw_name] = {}
def checkPlant(self, user): def checkPlant(self, user):
"""Given the username, calculates the last time since the user's """Given the username, calculates the last time since the user's
@ -118,11 +119,13 @@ class Wilty():
states. states.
""" """
self.getUsers() self.getUsers()
for u in self.plants: for u in self.users:
sv_dir = "/home/" + u + "/" + self.save_dir + "/" sv_dir = "/home/" + u + "/" + self.save_dir + "/"
# Get plant data # Get plant data
with open(sv_dir + u + self.plant_file) as plant_fh: with open(sv_dir + u + self.plant_file) as plant_fh:
plant_json = plant_fh.read() plant_json = plant_fh.read()
try:
# Load plant data if json is valid syntax
self.plants[u] = json.loads(plant_json) self.plants[u] = json.loads(plant_json)
# Get visitor data # Get visitor data
if os.access(sv_dir + self.visit_file, os.R_OK) and \ if os.access(sv_dir + self.visit_file, os.R_OK) and \
@ -145,6 +148,8 @@ class Wilty():
self.checkPlant(u) self.checkPlant(u)
if not self.plants[u]["is_dead"]: if not self.plants[u]["is_dead"]:
self.live_plants[u] = self.plants[u] self.live_plants[u] = self.plants[u]
except json.decoder.JSONDecodeError:
pass
def waterRandomPlant(self): def waterRandomPlant(self):
"""Randomly select a live plant to water.""" """Randomly select a live plant to water."""