add default to open_json_file

This commit is contained in:
jomo
2014-07-27 06:01:52 +02:00
parent e277b923a7
commit a854fa2039
2 changed files with 5 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
from helpers import * from helpers import *
friends_filename = "plugins/redstoner-utils.py.dir/files/friends.json" friends_filename = "plugins/redstoner-utils.py.dir/files/friends.json"
friends = open_json_file(friends_filename) # {Player_UUID:[List_of_friend_uuids]} friends = open_json_file(friends_filename, {}) # {Player_UUID:[List_of_friend_uuids]}
friend_join_sound = "random.orb" friend_join_sound = "random.orb"

View File

@@ -119,16 +119,18 @@ def played_before(player):
return True return True
def open_json_file(filename): def open_json_file(filename, default):
""" """
opens the given json file and returns an object or returns None on error opens the given json file and returns an object or returns None on error
filename is the path + name of the file. filename is the path + name of the file.
""" """
data = None
try: try:
with open(filename) as obj: with open(filename) as obj:
return json_loads(obj.read()) data = json_loads(obj.read())
except Exception, e: except Exception, e:
error("Failed to read from %s: %s" % (filename, e)) error("Failed to read from %s: %s" % (filename, e))
return (default if data is None else data)
def save_json_file(filename, obj): def save_json_file(filename, obj):