Fix invalid json error handling
parent
de77a2b1eb
commit
dbc209543f
11
wilty.py
11
wilty.py
|
@ -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,11 +119,13 @@ 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()
|
||||
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 \
|
||||
|
@ -145,6 +148,8 @@ class Wilty():
|
|||
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."""
|
||||
|
|
Loading…
Reference in New Issue