add default to open_json_file
This commit is contained in:
@@ -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"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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):
|
||||||
|
|||||||
Reference in New Issue
Block a user