2020-04-17 12:45:22 +00:00
|
|
|
import json
|
|
|
|
import os
|
2020-04-18 11:32:14 +00:00
|
|
|
from threading import Thread
|
2020-04-17 12:45:22 +00:00
|
|
|
|
2020-04-19 11:52:42 +00:00
|
|
|
filename = os.path.expanduser(r"~/Documents/fishy_config.json")
|
2020-04-17 12:45:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.config_dict = json.loads(open(filename).read()) if os.path.exists(filename) else dict()
|
|
|
|
|
2020-04-19 11:52:42 +00:00
|
|
|
def get(self, key, default=None, save=True):
|
2020-04-17 12:45:22 +00:00
|
|
|
if key in self.config_dict:
|
|
|
|
return self.config_dict[key]
|
2020-04-19 11:52:42 +00:00
|
|
|
|
|
|
|
if save:
|
|
|
|
self.set(key, default)
|
2020-04-17 12:45:22 +00:00
|
|
|
return default
|
|
|
|
|
2020-04-18 11:32:14 +00:00
|
|
|
def set(self, key, value, save=True):
|
2020-04-17 12:45:22 +00:00
|
|
|
self.config_dict[key] = value
|
2020-04-18 11:32:14 +00:00
|
|
|
if save:
|
|
|
|
self.save_config()
|
2020-04-17 12:45:22 +00:00
|
|
|
|
|
|
|
def save_config(self):
|
2020-04-18 11:32:14 +00:00
|
|
|
def save():
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write(json.dumps(self.config_dict))
|
|
|
|
|
|
|
|
Thread(target=save).start()
|