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):
self.name = "wilty"
self.version = self.name + " 0.1"
self.version = self.name + " 0.2"
self.save_dir = ".botany"
self.plant_file = "_plant_data.json"
self.visit_file = "visitors.json"
self.harvest_file = "harvest_file.json"
self.water_user = self.name
self.users = {}
self.plants = {}
self.live_plants = {}
self.stats_model = {
@ -94,7 +95,7 @@ class Wilty():
sv_dir = "/home/" + u.pw_name + "/" + self.save_dir + "/"
if os.path.exists(sv_dir) and \
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):
"""Given the username, calculates the last time since the user's
@ -118,33 +119,37 @@ class Wilty():
states.
"""
self.getUsers()
for u in self.plants:
for u in self.users:
sv_dir = "/home/" + u + "/" + self.save_dir + "/"
# Get plant data
with open(sv_dir + u + self.plant_file) as plant_fh:
plant_json = plant_fh.read()
self.plants[u] = json.loads(plant_json)
# Get visitor data
if os.access(sv_dir + self.visit_file, os.R_OK) and \
try:
# Load plant data if json is valid syntax
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):
with open(sv_dir + self.visit_file) as visit_fh:
visit_json = visit_fh.read()
self.plants[u]["visitors"] = json.loads(visit_json)
self.plants[u]["allow_visit"] = True
else:
self.plants[u]["allow_visit"] = False
self.plants[u]["visitors"] = []
# Get harvest data
if os.access(sv_dir + self.harvest_file, os.R_OK):
with open(sv_dir + self.harvest_file) as harvest_fh:
harvest_json = harvest_fh.read()
self.plants[u]["harvests"] = json.loads(harvest_json)
else:
self.plants[u]["harvests"] = {}
# Update plant state and live plants dictionary
self.checkPlant(u)
if not self.plants[u]["is_dead"]:
self.live_plants[u] = self.plants[u]
with open(sv_dir + self.visit_file) as visit_fh:
visit_json = visit_fh.read()
self.plants[u]["visitors"] = json.loads(visit_json)
self.plants[u]["allow_visit"] = True
else:
self.plants[u]["allow_visit"] = False
self.plants[u]["visitors"] = []
# Get harvest data
if os.access(sv_dir + self.harvest_file, os.R_OK):
with open(sv_dir + self.harvest_file) as harvest_fh:
harvest_json = harvest_fh.read()
self.plants[u]["harvests"] = json.loads(harvest_json)
else:
self.plants[u]["harvests"] = {}
# Update plant state and live plants dictionary
self.checkPlant(u)
if not self.plants[u]["is_dead"]:
self.live_plants[u] = self.plants[u]
except json.decoder.JSONDecodeError:
pass
def waterRandomPlant(self):
"""Randomly select a live plant to water."""