-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprof_template.py
executable file
·204 lines (170 loc) · 5 KB
/
prof_template.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
import os
import zipfile
DEV_COMMANDS = {
1: 'ENTER',
2: 'POWER ON',
3: 'POWER OFF',
4: 'POWER TOGGLE',
5: 'CHANNEL UP',
6: 'CHANNEL DOWN',
7: 'PAUSE',
8: 'PLAY',
9: 'RECORD',
10: 'DIGIT 0',
11: 'DIGIT 1',
12: 'DIGIT 2',
13: 'DIGIT 3',
14: 'DIGIT 4',
15: 'DIGIT 5',
16: 'DIGIT 6',
17: 'DIGIT 7',
18: 'DIGIT 8',
19: 'DIGIT 9',
20: 'STOP',
21: 'REVERSE',
22: 'FORWARD',
23: 'SKIP',
24: 'VOLUME UP',
25: 'VOLUME DOWN'
}
NODEDEF_HEADER = """<nodeDefs>
<nodeDef id="SMPLHUB" nls="SHUB">
<editors />
<sts>
<st id="ST" editor="_2_0_R_0_1" />
</sts>
<cmds>
<sends />
<accepts>
<cmd id="DISCOVER" />
</accepts>
</cmds>
</nodeDef>
<nodeDef id="DEVICE" nls="DEV">
<editors />
<sts>
<st id="ST" editor="DCMD" />
</sts>
<cmds>
<sends />
<accepts>
<cmd id="DON" />
<cmd id="DOF" />
<cmd id="PTOGGLE" />
<cmd id="SEND_CMD" init="ST" >
<p id="" editor="DCMD" />
</cmd>
</accepts>
</cmds>
</nodeDef>
"""
NODEDEF_FOOTER = """
</nodeDefs>"""
NLS_HEADER = """# controller
ND-SMPLHUB-NAME = SimpleHub
ND-SMPLHUB-ICON = GenericCtl
CMD-SHUB-DISCOVER-NAME = Re-Discover
ST-SHUB-ST-NAME = NodeServer Online
ST-ROOM-ST-NAME = Last command
CMD-ROOM-SET_ACTIVITY-NAME = Run Activity
# device
ND-DEVICE-NAME = Device
ND-DEVICE-ICON = GenericRsp
ST-DEV-ST-NAME = Last command
CMD-DEV-DON-NAME = Power On
CMD-DEV-DOF-NAME = Power Off
CMD-DEV-PTOGGLE-NAME = Power Toggle
CMD-DEV-SEND_CMD-NAME = Send Command
"""
NLS_FOOTER = """
"""
EDITORS_HEADER = """<editors>
"""
EDITORS_FOOTER = """
</editors>"""
NODEDEF_ROOM_TEMPL = """
<nodeDef id="ROOM%d" nls="ROOM">
<editors />
<sts>
<st id="ST" editor="R%dCMD" />
</sts>
<cmds>
<sends />
<accepts>
<cmd id="SET_ACTIVITY" init="ST" >
<p id="" editor="R%dCMD" />
</cmd>
</accepts>
</cmds>
</nodeDef>
"""
NLS_ROOM_ND_TEMPL = """
ND-ROOM%d-NAME = %s
ND-ROOM%d-ICON = GenericRspCtl
"""
EDITORS_TEMPL = """
<editor id="R%dCMD">
<range uom="25" min="%d" max="%d" nls="R%dACT" />
</editor>
"""
EDITORS_TEMPL_D = """
<editor id="DCMD">
<range uom="25" min="1" max="%d" nls="DCMD" />
</editor>
"""
def extract_index(json):
try:
# Also convert to int since update_time will be string. When comparing
# strings, "10" is smaller than "2".
return int(json['idx'])
except KeyError:
return 0
def write_nls(file_ptr, data):
file_ptr.write(NLS_ROOM_ND_TEMPL % (data['index'], data['name'], data['index']))
actarray = []
for act_id, act_data in data['activities'].items():
actarray.append({'ridx': data['index'], 'idx': act_data['index'], 'name': act_data['name']})
actarray.sort(key=extract_index)
for nls in actarray:
file_ptr.write('R%dACT-%d = %s\n' % (nls['ridx'], nls['idx'], nls['name']))
def write_room_nd(file_ptr, data):
file_ptr.write(NODEDEF_ROOM_TEMPL % (data['index'], data['index'], data['index']))
def write_editor(file_ptr, data):
file_ptr.write(EDITORS_TEMPL % (data['index'], 1, len(data['activities']), data['index']))
def write_profile(home=None):
os.makedirs('profile/nls', exist_ok=True)
os.makedirs('profile/editor', exist_ok=True)
os.makedirs('profile/nodedef', exist_ok=True)
editors_file = open('profile/editor/editors.xml', 'w')
editors_file.write(EDITORS_HEADER)
editors_file.write(EDITORS_TEMPL_D % len(DEV_COMMANDS))
nodedef_file = open('profile/nodedef/nodedefs.xml', 'w')
nodedef_file.write(NODEDEF_HEADER)
nls_file = open('profile/nls/en_us.txt', 'w')
nls_file.write(NLS_HEADER)
for dcmd_id, dcmd_name in DEV_COMMANDS.items():
nls_file.write('DCMD-%d = %s\n' % (dcmd_id, dcmd_name))
if home is not None:
for room_id, data in home['rooms'].items():
write_room_nd(nodedef_file, data)
write_editor(editors_file, data)
write_nls(nls_file, data)
nodedef_file.write(NODEDEF_FOOTER)
nodedef_file.close()
nls_file.write(NLS_FOOTER)
nls_file.close()
editors_file.write(EDITORS_FOOTER)
editors_file.close()
write_profile_zip()
def write_profile_zip():
src = 'profile'
abs_src = os.path.abspath(src)
with zipfile.ZipFile('profile.zip', 'w') as zf:
for dirname, subdirs, files in os.walk(src):
if '/.' not in dirname:
for filename in files:
if filename.endswith('.xml') or filename.endswith('.txt'):
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
zf.write(absname, arcname)
zf.close()