mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-10 01:17:42 +01:00
36 lines
732 B
Python
36 lines
732 B
Python
import json
|
|
from json import JSONEncoder
|
|
|
|
import yaml
|
|
|
|
from core.database.model import DBModel, ActorModel
|
|
|
|
|
|
def load_config(fname):
|
|
with open(fname, 'rt') as f:
|
|
data = yaml.load(f)
|
|
# TODO: add config validation
|
|
return data
|
|
|
|
|
|
class ComplexEncoder(JSONEncoder):
|
|
def default(self, obj):
|
|
|
|
try:
|
|
if isinstance(obj, DBModel):
|
|
return obj.__dict__
|
|
|
|
elif isinstance(obj, ActorModel):
|
|
return None
|
|
|
|
elif hasattr(obj, "callback"):
|
|
return obj()
|
|
else:
|
|
return None
|
|
except TypeError as e:
|
|
pass
|
|
return None
|
|
|
|
|
|
def json_dumps(obj):
|
|
return json.dumps(obj, cls=ComplexEncoder)
|