craftbeerpi4-pione/core/utils/utils.py
2018-11-01 21:27:37 +01:00

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)