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,33 +119,37 @@ 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()
self.plants[u] = json.loads(plant_json) try:
# Get visitor data # Load plant data if json is valid syntax
if os.access(sv_dir + self.visit_file, os.R_OK) and \ self.plants[u] = json.loads(plant_json)
# Get visitor data
if os.access(sv_dir + self.visit_file, os.R_OK) and \
os.access(sv_dir + self.visit_file, os.W_OK): os.access(sv_dir + self.visit_file, os.W_OK):
with open(sv_dir + self.visit_file) as visit_fh: with open(sv_dir + self.visit_file) as visit_fh:
visit_json = visit_fh.read() visit_json = visit_fh.read()
self.plants[u]["visitors"] = json.loads(visit_json) self.plants[u]["visitors"] = json.loads(visit_json)
self.plants[u]["allow_visit"] = True self.plants[u]["allow_visit"] = True
else: else:
self.plants[u]["allow_visit"] = False self.plants[u]["allow_visit"] = False
self.plants[u]["visitors"] = [] self.plants[u]["visitors"] = []
# Get harvest data # Get harvest data
if os.access(sv_dir + self.harvest_file, os.R_OK): if os.access(sv_dir + self.harvest_file, os.R_OK):
with open(sv_dir + self.harvest_file) as harvest_fh: with open(sv_dir + self.harvest_file) as harvest_fh:
harvest_json = harvest_fh.read() harvest_json = harvest_fh.read()
self.plants[u]["harvests"] = json.loads(harvest_json) self.plants[u]["harvests"] = json.loads(harvest_json)
else: else:
self.plants[u]["harvests"] = {} self.plants[u]["harvests"] = {}
# Update plant state and live plants dictionary # Update plant state and live plants dictionary
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."""