craftbeerpi4-pione/core/utils/utils.py

73 lines
2.7 KiB
Python
Raw Normal View History

2018-11-16 20:35:59 +01:00
from pprint import pprint
2018-12-29 00:27:19 +01:00
from cbpi_api import *
2018-11-18 15:40:10 +01:00
from core.utils.encoder import ComplexEncoder
2018-11-16 20:35:59 +01:00
__all__ = ['load_config',"json_dumps", "parse_props"]
2018-11-04 00:47:26 +01:00
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):
2018-11-16 20:35:59 +01:00
try:
2018-12-29 00:27:19 +01:00
2018-11-16 20:35:59 +01:00
with open(fname, 'rt') as f:
data = yaml.load(f)
2018-12-29 00:27:19 +01:00
2018-11-16 20:35:59 +01:00
return data
2018-12-29 00:27:19 +01:00
except Exception as e:
2018-11-16 20:35:59 +01:00
pass
2018-11-01 21:27:37 +01:00
2018-11-01 19:50:04 +01:00
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)
2018-11-16 20:35:59 +01:00
def parse_props(self, cls):
name = cls.__name__
result = {"name": name, "class": cls, "properties": [], "actions": []}
tmpObj = cls()
members = [attr for attr in dir(tmpObj) if not callable(getattr(tmpObj, attr)) and not attr.startswith("__")]
for m in members:
if isinstance(tmpObj.__getattribute__(m), Property.Number):
t = tmpObj.__getattribute__(m)
result["properties"].append(
{"name": m, "label": t.label, "type": "number", "configurable": t.configurable, "description": t.description, "default_value": t.default_value})
elif isinstance(tmpObj.__getattribute__(m), Property.Text):
t = tmpObj.__getattribute__(m)
result["properties"].append(
{"name": m, "label": t.label, "type": "text", "configurable": t.configurable, "default_value": t.default_value, "description": t.description})
elif isinstance(tmpObj.__getattribute__(m), Property.Select):
t = tmpObj.__getattribute__(m)
result["properties"].append(
{"name": m, "label": t.label, "type": "select", "configurable": True, "options": t.options, "description": t.description})
elif isinstance(tmpObj.__getattribute__(m), Property.Actor):
t = tmpObj.__getattribute__(m)
result["properties"].append({"name": m, "label": t.label, "type": "actor", "configurable": t.configurable, "description": t.description})
elif isinstance(tmpObj.__getattribute__(m), Property.Sensor):
t = tmpObj.__getattribute__(m)
result["properties"].append({"name": m, "label": t.label, "type": "sensor", "configurable": t.configurable, "description": t.description})
elif isinstance(tmpObj.__getattribute__(m), Property.Kettle):
t = tmpObj.__getattribute__(m)
result["properties"].append({"name": m, "label": t.label, "type": "kettle", "configurable": t.configurable, "description": t.description})
for method_name, method in cls.__dict__.items():
if hasattr(method, "action"):
key = method.__getattribute__("key")
parameters = method.__getattribute__("parameters")
result["actions"].append({"method": method_name, "label": key, "parameters": parameters})
2018-12-29 00:27:19 +01:00
2018-11-16 20:35:59 +01:00