-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui_elements.py
217 lines (162 loc) · 6.79 KB
/
ui_elements.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import re
import aiofiles
from aiofiles import os
import json
import aiohttp
import jinja2
from unit_conversions import unit_factor
def get_uuid(magic = [0]):
# very ugy hack but we need something unique
# idealy should be seperate counter for each request etc
magic[0] += 1
return "UUDI_" + str(magic[0])
class Element:
""" base class for a node in the widget tree """
def __init__(self, name):
self.name = name
self.nodes = []
self.parent = None
def render(self):
""" returns a str of the html that encodes this element (and child elements).
Put the elements javascript inline with the html
"""
pass
def add_child(self, child):
""" adds a child element in the element tree """
self.nodes.append(child)
child.parent = self
@property
def top(self):
node = self
while True:
try:
node = node.parent
except AttributeError:
break
return node
@staticmethod
def load_template(filename):
with open(filename, 'r') as file:
return file.read()
@staticmethod
def format(pre_format, **kwargs):
""" poor mans html template system. {{var}} in the template can be over written with kwargs[var]"""
# swaps double and single curly braces allowing us to use python str.format() method
curly = { "{{":"{", "}}":"}", "{":"{{", "}":"}}" }
substrings = sorted(curly, key=len, reverse=True)
regex = re.compile('|'.join(map(re.escape, substrings)))
to_formant = regex.sub(lambda match: curly[match.group(0)], pre_format)
return to_formant.format(**kwargs)
class Graph(Element):
pass
def render(self):
with open("templates/graphs.template.html") as file:
graphs = jinja2.Template(file.read())
def get_id_and_desc(node, path):
return {"id": ".".join(path[1:]), "desc": node["desc"] }
return graphs.render( list_ids = self.top.flat_meta(get_id_and_desc), title = self.name )
class SquibTable(Element):
def __init__(self, name, line_ids):
super().__init__(name)
self.line_ids = line_ids
def render(self):
with open("templates/table.squibs.template.html") as file:
box = jinja2.Template(file.read())
items = [ {"id":id,
"desc":self.top.get_meta(id, "desc"), } for id in self.line_ids]
return box.render( {"list_ids": items, "title": self.name} )
class ValveTable(Element):
def __init__(self, name, line_ids):
super().__init__(name)
self.line_ids = line_ids
def render(self):
with open("templates/table.valves.template.html") as file:
box = jinja2.Template(file.read())
items = [ {"id":id,
"desc":self.top.get_meta(id, "desc"), } for id in self.line_ids]
test = box.render( {"list_ids": items, "title": self.name, "uuid": get_uuid() } )
return test
class RawSensorTable(Element):
def __init__(self, name, line_ids, units = None):
super().__init__(name)
self.line_ids = line_ids
if units == None:
self.units = {id: None for id in line_ids }
else:
self.units = {id: s for id,s in zip(line_ids, units) }
def render(self):
with open("templates/table.sensors.template.html") as file:
box = jinja2.Template(file.read())
items = []
for id in self.line_ids:
if self.units[id] is None:
unit = self.top.get_meta(id, "unit")
else:
source, target = self.units[id].split("->")
# assert source == self.top.get_meta(id, "raw.unit")
unit = target
items.append( {"id":id,
"conv": unit_factor( self.units[id] ),
"unit": unit,
"desc": self.top.get_meta(id, "desc"), })
test = box.render( {"list_ids": items, "title": self.name} )
return test
class FillMassController(Element):
def __init__(self):
super().__init__("Load Mass")
def render(self):
with open("templates/load_mass.template.html") as file:
box = jinja2.Template(file.read())
test = box.render( {"metaslate": self.top.metaslate} )
return test
class DataTable(Element):
def __init__(self, name, line_ids):
super().__init__(name)
self.line_ids = line_ids
def render(self):
with open("templates/table.data.template.html") as file:
box = jinja2.Template(file.read())
items = [ {"id":id,
"unit":self.top.get_meta(id, "unit"),
"desc":self.top.get_meta(id, "desc"), } for id in self.line_ids]
test = box.render( {"list_ids": items, "title": self.name} )
return test
class MiniGraph(Element):
def __init__(self, name, line_ids, time_seconds = 60, units = None):
super().__init__(name)
self.line_ids = line_ids
self.colors = ["#348ABD", "#A60628", "#7A68A6", "#467821", "#CF4457", "#188487", "#E24A33" ]
self.time_seconds = time_seconds
if units == None:
self.units = {id: None for id in line_ids }
else:
self.units = {id: s for id,s in zip(line_ids, units) }
def render(self):
with open("templates/mini_graph.template.html") as file:
box = jinja2.Template(file.read())
items = [ {"id":id,
"conv": unit_factor( self.units[id] ),
# "unit":self.top.get_meta(id, "unit"),
"unit": self.top.get_meta(id, "unit") if self.units[id] is None else self.units[id].split("->")[1],
"desc":self.top.get_meta(id, "desc"),
"color":self.colors[i] } for i, id in enumerate(self.line_ids)]
test = box.render( {"list_ids": items, "title": self.name, "total_points": self.time_seconds * 20, "uuid": get_uuid() } )
return test
class Sequence(Element):
def __init__(self, name, line_ids):
super().__init__(name)
self.line_ids = line_ids
def render(self):
with open("templates/sequencing.template.html") as file:
box = jinja2.Template(file.read())
state_machines = []
writable_values = []
for id, unit in self.line_ids.items():
if type(unit) == type([]):
state_machines.append( {"id":id, "unit": unit, "desc": self.top.get_meta(id, "desc"), "len": len(unit) } )
else:
writable_values.append( {"id":id, "unit": unit, "desc": self.top.get_meta(id, "desc"), } )
test = box.render( {"state_machines": state_machines,
"writable_values": writable_values,
"title": self.name} )
return test