craftbeerpi4-pione/core/utils/utils.py

37 lines
732 B
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
import json
from json import JSONEncoder
2018-11-01 21:27:37 +01:00
import yaml
2018-11-01 19:50:04 +01:00
from core.database.model import DBModel, ActorModel
2018-11-01 21:27:37 +01:00
def load_config(fname):
with open(fname, 'rt') as f:
data = yaml.load(f)
# TODO: add config validation
return data
2018-11-01 19:50:04 +01:00
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
2018-11-01 21:27:37 +01:00
2018-11-01 19:50:04 +01:00
def json_dumps(obj):
2018-11-01 21:27:37 +01:00
return json.dumps(obj, cls=ComplexEncoder)