1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-01 21:02:38 +00:00

Added json functionality to helper methods

This commit is contained in:
Myned 2017-10-16 14:06:31 -04:00
parent e6e9b59791
commit e1a582be05

View file

@ -1,5 +1,5 @@
import asyncio import asyncio
import json import json as jsn
import os import os
import pickle as pkl import pickle as pkl
import subprocess import subprocess
@ -19,12 +19,12 @@ print('\nPID : {}\n'.format(os.getpid()))
try: try:
with open('config.json') as infile: with open('config.json') as infile:
config = json.load(infile) config = jsn.load(infile)
print('config.json loaded.') print('config.json loaded.')
except FileNotFoundError: except FileNotFoundError:
with open('config.json', 'w') as outfile: with open('config.json', 'w') as outfile:
json.dump({'client_id': 0, 'listed_ids': [0], 'owner_id': 0, 'permissions': 126016, 'prefix': ',', jsn.dump({'client_id': 0, 'owner_id': 0, 'permissions': 126016, 'playing': 'a game', 'prefix': ',',
'shutdown_channel': 0, 'startup_channel': 0, 'token': 'str'}, outfile, indent=4, sort_keys=True) 'shutdown_channel': 0, 'startup_channel': 0, 'token': 'str'}, outfile, indent=4, sort_keys=True)
raise FileNotFoundError( raise FileNotFoundError(
'Config file not found: config.json created with abstract values. Restart run.py with correct values.') 'Config file not found: config.json created with abstract values. Restart run.py with correct values.')
@ -42,14 +42,22 @@ def setdefault(filename, default=None):
return pkl.load(iofile) return pkl.load(iofile)
def load(filename): def load(filename, *, json=False):
with open(filename, 'rb') as infile: if not json:
return pkl.load(infile) with open(filename, 'rb') as infile:
return pkl.load(infile)
else:
with open(filename) as infile:
return jsn.load(infile)
def dump(obj, filename): def dump(obj, filename, *, json=False):
with open(filename, 'wb') as outfile: if not json:
pkl.dump(obj, outfile) with open(filename, 'wb') as outfile:
pkl.dump(obj, outfile)
else:
with open(filename, 'w') as outfile:
jsn.dump(obj, outfile, indent=4, sort_keys=True)
settings = setdefault('settings.pkl', {'del_ctx': []}) settings = setdefault('settings.pkl', {'del_ctx': []})