-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlite.py
More file actions
179 lines (142 loc) · 5.74 KB
/
lite.py
File metadata and controls
179 lines (142 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
### conversion from full bodied formats to lite formats and vice versa
import traceback
import json
import copy
from src.thingDescriptionCollection import ThingDescriptionCollection
def convert_td_genLite(td:dict) -> dict:
if "security" in td:
del td["security"]
if "securityDefinitions" in td:
del td["securityDefinitions"]
if "forms" in td:
del td["forms"]
if "uriVariables" in td:
del td["uriVariables"]
if "@context" in td:
del td["@context"]
if "@type" in td:
del td["@type"]
if "properties" in td:
for i in td["properties"]:
if "forms" in td["properties"][i]:
del td["properties"][i]["forms"]
if "actions" in td:
for i in td["actions"]:
if "forms" in td["actions"][i]:
del td["actions"][i]["forms"]
if "events" in td:
for i in td["events"]:
if "forms" in td["events"][i]:
del td["events"][i]["forms"]
del_list = []
for k, v in td.items():
if (v == "") or (v == {}) or (v == []):
del_list.append(k)
for i in del_list:
del td[i]
return td
def convert_tds_genLite(tds:list) -> list:
for td in tds:
td = convert_td_genLite(td)
return tds
def getID(tds: list, type: str, name: str) -> str | None:
for td in tds:
if (type in td) and (name in td[type]):
return td["id"]
return None
def convert_flow_lite(flow:list,tds:dict) -> list:
for node in flow:
if "name" in node:
del node["name"]
if "x" in node:
del node["x"]
if "y" in node:
del node["y"]
if "z" in node:
del node["z"]
if "disabled" in node:
del node["disabled"]
if "redeploy" in node:
del node["redeploy"]
if "thingDirectoryURI" in node:
del node["thingDirectoryURI"]
if node["type"] == "system-event-node":
node["type"] = "sys-evt"
node["thingID"] = getID(tds, "events", node["thingEvent"])
del node["thingEventValue"]
if node["type"] == "system-property-node":
node["type"] = "sys-prp"
node["thingID"] = getID(tds, "properties", node["thingProperty"])
del node["thingPropertyValue"]
if node["type"] == "system-action-node":
node["type"] = "sys-act"
node["thingID"] = getID(tds, "actions", node["thingAction"])
del node["thingActionValue"]
for node in flow:
if node["type"] == "tab":
flow.remove(node)
break
return flow
def convert_flow_full(flow:list, tds:dict, tddURI) -> list:
tdc = ThingDescriptionCollection(tds=tds)
try:
for node in flow:
node["z"] = "test flow"
match node["type"]:
case "sys-evt":
node["type"] = "system-event-node"
node["thingDirectoryURI"] = tddURI
event = tdc.selectElement("events", node["thingEvent"], id=node["thingID"])
description = ""
if "description" in event:
description = event["description"]
node["thingEventValue"] = json.dumps({
"uri": tddURI + "/things/" + node["thingID"],
"output": event["data"],
"description": description,
"event": node["thingEvent"]
})
case "sys-prp":
node["type"] = "system-property-node"
node["thingDirectoryURI"] = tddURI
prop = tdc.selectElement("properties", node["thingProperty"], id=node["thingID"])
description = ""
if "description" in prop:
description = prop["description"]
node["thingPropertyValue"]= json.dumps({
"uri": prop["forms"][0]["href"],
"type": prop["type"],
"description": description,
"property": node["thingProperty"]
})
case "sys-act":
node["type"] = "system-action-node"
node["thingDirectoryURI"] = tddURI
action = copy.deepcopy(tdc.selectElement("actions", node["thingAction"], id=node["thingID"]))
description = ""
if "description" in action:
description = action["description"]
output = {}
if "output" in action:
output = action["output"]
params = {}
if "input" in action:
params = action["input"]
if params["type"] == "object":
params = params["properties"]
for k, v in params.items():
params[k] = v["type"]
node["thingActionValue"] = json.dumps({
"uri": action["forms"][0]["href"],
"params": params,
"output": output,
"description": description,
"action": node["thingAction"]
})
flow.append({"type":"tab","label":"Flow 1","id":"test flow"})
return flow
except Exception as e:
print(e)
traceback.print_exception(type(e), e, e.__traceback__)
print("Error in converting flow")
raise Exception("failed to convert flow to full format, check all variables are correct.")