2018-11-16 20:35:59 +01:00
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
from core.api.property import Property
|
|
|
|
|
|
|
|
__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:
|
|
|
|
with open(fname, 'rt') as f:
|
|
|
|
data = yaml.load(f)
|
|
|
|
return data
|
|
|
|
except:
|
|
|
|
pass
|
2018-11-01 21:27:37 +01:00
|
|
|
|
|
|
|
|
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)
|
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})
|
|
|
|
pprint(result, width=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|