-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.py
187 lines (153 loc) · 6.6 KB
/
pack.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
import io
import os
import zlib
import pyTOSC.IterableObject as IObj
from configparser import ConfigParser
import xml.etree.ElementTree as ET
import copy
import uuid
import math
def VerifyConfig(config_name: str):
try:
config = io.open(config_name,'r')
config.close()
return True
except:
return False
def Pack(xml_name):
file = io.open('version', 'r')
version = file.read()
file.close()
file = io.open(xml_name, 'r')
data = file.read()
file.close()
replacements = 0
lua_name = ''
lua_script = ''
submodule_start = 0
submodule_next = 0
submodule_name = ''
submodule_script = ''
start = ''
end = ''
root = ET.fromstring(data)
xml_properties = root.findall(".//property")
# Load config files
main_config = ConfigParser()
main_config.read('config.ini')
build_config_name = main_config.get('Configs', 'Builds', fallback='')
layout_config_name = main_config.get('Configs', 'Layout', fallback='')
channels_config_name = main_config.get('Configs', 'Channels', fallback='')
tabs_config_name = main_config.get('Configs', 'Tabs', fallback='')
cameras_config_name = main_config.get('Configs', 'Cameras', fallback='')
# First pass to replace scripts
for p in xml_properties:
if p[0].text == 'script' and p[1].text[0] == '$':
lua_name = p[1].text[2:-1]
print(f'Found script reference: {lua_name}')
try:
lua_file = io.open(os.path.normpath(f'Lua/{lua_name}'), 'r')
lua_script = lua_file.read()
lua_file.close()
except:
print(f'Unable to open file \'{lua_name}\'\nCheck that file is in Lua folder')
print('Could not complete packing! Closing...')
exit()
# Check luaScript for submodules and insert them as needed
while submodule_start != -1:
submodule_start = lua_script.find('--Submodule.include(\'')
if submodule_start > -1:
start = lua_script[0 : submodule_start]
end = lua_script[submodule_start + 21 :]
submodule_next = end.find('\')')
submodule_name = end[0:submodule_next]
if submodule_next > -1:
print(f'Found submodule reference: \'{submodule_name}\'')
try:
lua_file = io.open(os.path.normpath(f'Lua/Submodules/{submodule_name}'), 'r')
submodule_script = lua_file.read()
lua_file.close()
except:
print(f'Unable to open submodule file \'{submodule_name}\'\nCheck that file is in Lua\Submodules folder')
print('Could not complete packing! Closing...')
exit()
lua_script = lua_script.replace(f'--Submodule.include(\'{submodule_name}\')', submodule_script)
replacements += 1
# Replace script reference with actual script
p[1].text = lua_script
replacements += 1
print(f'Total script replacements: {replacements}\n')
# Second pass to duplicate iterable objects
valid_layout = VerifyConfig(layout_config_name)
iterable_buttons = None
if valid_layout:
passed_channels_config = ""
if VerifyConfig(channels_config_name):
print(f'Button config found: {channels_config_name}')
passed_channels_config = channels_config_name
else:
passed_channels_config = "Default"
iterable_buttons = IObj.IterableButton(root, layout_config_name, passed_channels_config)
iterable_buttons.Iterate()
if VerifyConfig(tabs_config_name):
print(f'Tabs config found: {tabs_config_name}')
auto_pagers = IObj.AutoPager(root, tabs_config_name)
auto_pagers.Iterate()
if valid_layout and VerifyConfig(cameras_config_name):
print(f'Camera config found: {cameras_config_name}')
iterable_cameras = IObj.IterableCamera(root, layout_config_name, cameras_config_name)
iterable_cameras.Iterate()
# Late-Fill Buttons
if iterable_buttons:
iterable_buttons.LateFill()
main_data = ET.tostring(root, encoding='unicode')
# Build specific changes
builds = []
if VerifyConfig(build_config_name):
build_config = ConfigParser()
build_config.read('builds.ini')
builds = build_config.sections()
print(f'Build configurations found: {builds}\n')
else:
print('No valid build config specified\n')
if len(builds) > 0:
build_index = 0
for build in builds:
build_name = f'Build/{xml_name[0:-4]} ({build}) {version}.tosc'
temp_data = main_data
print(build)
# Replace string '$BUILD_NAME' with actual build name. This can automate relabeling things
temp_data = temp_data.replace('$BUILD_NAME', build)
# Replace string '$VERSION' with version
temp_data = temp_data.replace('$VERSION', f'Version {version}')
build_root = ET.fromstring(temp_data)
properties = build_config[build].items()
for property in properties:
xml_properties = build_root.findall(".//property")
for p in xml_properties:
if p[0].text == property[0].upper():
print(f'Found {p[0].text}')
print(f'Replacing value "{p[1].text}" with "{property[1]}"')
p[1].text = property[1]
# Unique UUIDs for each build
if build_index > 0:
nodes = build_root.findall(".//node")
for node in nodes:
node.set('ID', str(uuid.uuid4()))
build_data = ET.tostring(build_root)
compressed = zlib.compress(build_data)
file = io.open(build_name, 'wb')
file.write(compressed)
file.close()
print(f'Wrote to {build_name}')
print('----')
build_index += 1
else:
build_name = f'Build/{xml_name[0:-4]} {version}.tosc'
build_data = main_data.encode('UTF-8')
compressed = zlib.compress(build_data)
file = io.open(build_name, 'wb')
file.write(compressed)
file.close()
print(f'Wrote to {build_name}')
print('----')