-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathDrawIoConverter.py
271 lines (218 loc) · 8.38 KB
/
DrawIoConverter.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# by Flaxbeard
# version 0.2.1, 7/23/21
import re
import os
from html import unescape
import tkinter as tk
import math
from tkinter.scrolledtext import ScrolledText
# Base structure of a focus
BASE_FOCUS = '''
shared_focus = {
id = <tag>
icon = GFX_generic_suspend_constitution
cost = 2
x = <x>
y = <y>
completion_reward = {
log = "[GetDateText]: [Root.GetName]: Focus <tag>"
}
<prereqs><exclusives>}
'''
# Regex to find focuses in the draw.io file (boxes), these have a stroke
BOX_REGEX = re.compile(r'(<mxCell.*value((?!strokeColor=none).)*?>[\s\S]*?</mxCell>)', re.MULTILINE)
# Regex to find arrows (have a source and target set to boxes)
ARROW_REGEX = re.compile(r'(<mxCell.*source.*target.*>)')
# Regex to extract various properties of arrows
SOURCE_REGEX = re.compile(r'source="([^"]*)"')
TARGET_REGEX = re.compile(r'target="([^"]*)"')
# Regex to extract various properties of focus boxes
VALUE_REGEX = re.compile(r'value="([^"]*)"')
ID_REGEX = re.compile(r'id="([^"]*)"')
X_REGEX = re.compile(r' x="([^"]*)"')
Y_REGEX = re.compile(r' y="([^"]*)"')
# Regex to find and remove HTML tags or excess spaces
TAG_REGEX = re.compile(r'<.*?>')
SPACE_REGEX = re.compile(r'\s+')
# Regex to filter characters that can't be used in a focus ID
NON_ALPHANUMERIC_REGEX = re.compile(r'[^a-zA-Z0-9_]+')
# Represents a single focus
class Focus:
def __init__(self, focus_id, focus_tag, focus_name = None):
self.id = focus_id
self.tag = focus_tag
self.name = focus_name
self.prerequisites = []
self.dashed_prerequisites = []
self.exclusives = []
self.relative = None
self.raw_x = 0
self.raw_y = 0
self.x = 0
self.y = 0
self.rel_x = 0
self.rel_y = 0
class App(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.select_image_file = ''
self.select_music_file = ''
self.master = master
self.pack()
self.create_widgets()
# Set up GUI
def create_widgets(self):
self.run_script = tk.Button(self)
self.run_script['text'] = 'Run Program'
self.run_script['command'] = self.run_app
self.run_script.pack(side='top', pady=5)
self.country_tag_label = tk.Label(self)
self.country_tag_label['text'] = 'Country Tag\n(EQS, GRI, CTH)'
self.country_tag_label['wraplength'] = 170
self.country_tag_label.pack(side='top', pady=5)
self.country_tag = tk.Entry(self)
self.country_tag.pack(side='top', pady=5)
self.file_name_focus_label = tk.Label(self)
self.file_name_focus_label['text'] = 'National Focus File Name'
self.file_name_focus_label['wraplength'] = 170
self.file_name_focus_label.pack(side='top', pady=5)
self.file_name_focus = tk.Entry(self)
self.file_name_focus.pack(side='top', pady=5)
self.file_name_loc_label = tk.Label(self)
self.file_name_loc_label['text'] = 'Localisation File Name\n(with _l_english)'
self.file_name_loc_label['wraplength'] = 170
self.file_name_loc_label.pack(side='top', pady=5)
self.file_name_loc = tk.Entry(self)
self.file_name_loc.pack(side='top', pady=5)
self.horiz_spacing_label = tk.Label(self)
self.horiz_spacing_label['text'] = 'Horizontal Spacing (Higher is Tighter):'
self.horiz_spacing_label.pack(side='top', pady=5)
self.horiz_spacing = tk.Scale(self, from_=25, to=300, orient=tk.HORIZONTAL, resolution = 1, command=self.update_positions, length=400)
self.horiz_spacing.set(70)
self.horiz_spacing.pack(side='top', pady=5)
self.vert_spacing_label = tk.Label(self)
self.vert_spacing_label['text'] = 'Vertical Spacing (Higher is Tighter):'
self.vert_spacing_label.pack(side='top', pady=5)
self.vert_spacing = tk.Scale(self, from_=25, to=300, orient=tk.HORIZONTAL, resolution = 1, command=self.update_positions, length=400)
self.vert_spacing.set(80)
self.vert_spacing.pack(side='top', pady=5)
self.drawio_data_label = tk.Label(self)
self.drawio_data_label['text'] = 'Draw.io Data\n(Copied from Extras -> Edit Diagram)'
self.drawio_data_label.pack(side='top', pady=5)
self.drawio_data = ScrolledText(self, width=50, height=15, wrap="word")
self.drawio_data.pack(side='top', pady=5)
def update_positions(self, _ = 0):
self.horiz_spacing_amnt = self.horiz_spacing.get()
self.vert_spacing_amnt = self.vert_spacing.get()
def run_app(self):
country_tag = self.country_tag.get()
file_name_focus = self.file_name_focus.get()
file_name_loc = self.file_name_loc.get()
foci = {}
i_contents = self.drawio_data.get(1.0, tk.END).strip()
# Process all focus boxes
for e in BOX_REGEX.findall(i_contents):
focus_id = ID_REGEX.findall(e[0])[0]
# Extract focus name
text = VALUE_REGEX.findall(e[0])[0]
text = unescape(text).replace(' ', ' ')
text = re.sub(TAG_REGEX, ' ', text)
text = re.sub(SPACE_REGEX, ' ', text)
text = text.strip()
# Break if no name (false positive)
if not text or not e:
continue
xs = X_REGEX.findall(e[0])
ys = Y_REGEX.findall(e[0])
if not xs:
xs = [0]
if not ys:
ys = [0]
focus_x = xs[0]
focus_y = ys[0]
focus_name = text
focus_tag = re.sub(NON_ALPHANUMERIC_REGEX, '', text.replace(' ', '_').lower())
focus_tag = f'{country_tag}_{focus_tag}'
focus = Focus(focus_id, focus_tag, focus_name)
focus.raw_x = float(focus_x)
focus.raw_y = float(focus_y)
foci[focus_id] = focus
# Process all arrows
for e in ARROW_REGEX.findall(i_contents):
source = SOURCE_REGEX.findall(e)[0]
target = TARGET_REGEX.findall(e)[0]
s_focus = foci[source]
t_focus = foci[target]
# Arrows between objects on the same y-level indicate mutually exclusive
if abs(s_focus.raw_y - t_focus.raw_y) < 10:
if target not in s_focus.exclusives:
s_focus.exclusives.append(target)
t_focus.exclusives.append(source)
else:
# Dashed arrows indicate that either focus can be a prereq, otherwise normal prereq
if 'dash' in e:
t_focus.dashed_prerequisites.append(source)
else:
t_focus.prerequisites.append(source)
# Find the uppermost and leftmost focus, to set the origin point
base_x = math.inf
base_y = math.inf
for focus_id in foci:
focus = foci[focus_id]
if focus.raw_x < base_x:
base_x = focus.raw_x
if focus.raw_y < base_y:
base_y = focus.raw_y
# Set the x and y position in terms of focus tree positioning based on the diagram position
for focus_id in foci:
foci[focus_id].x = round((foci[focus_id].raw_x - base_x) / self.horiz_spacing_amnt)
foci[focus_id].y = round((foci[focus_id].raw_y - base_y) / self.vert_spacing_amnt)
# Use relative positioning where possible
for focus_id in foci:
focus = foci[focus_id]
all_prereqs = focus.prerequisites + focus.dashed_prerequisites
if len(all_prereqs) > 0:
focus.relative = all_prereqs[0]
focus.rel_x = round((focus.raw_x - foci[focus.relative].raw_x) / self.horiz_spacing_amnt)
focus.rel_y = round((focus.raw_y - foci[focus.relative].raw_y) / self.vert_spacing_amnt)
# Output the foci
out = ''
for focus_id in foci:
focus = foci[focus_id]
# Assemble prerequisite code
prereqs_code = ''
for prereq in focus.prerequisites:
prereqs_code += f'\tprerequisite = {{ focus = {foci[prereq].tag} }}\n'
if len(focus.dashed_prerequisites) > 0:
prereqs_code += '\tprerequisite = {\n'
for prereq in focus.dashed_prerequisites:
prereqs_code += f'\t\tfocus = {foci[prereq].tag}\n'
prereqs_code += '\t}\n'
# Assemble mutually exclusive code
exclusives_code = ''
for exclusive in focus.exclusives:
exclusives_code += f'\tmutually_exclusive = {{ focus = {foci[exclusive].tag} }}\n'
focus_code = BASE_FOCUS.replace('<tag>', focus.tag)
# Assemble positioning code
if focus.relative:
prereqs_code = f'\trelative_position_id = {foci[focus.relative].tag}\n' + prereqs_code
focus_code = focus_code.replace('<x>', str(focus.rel_x)).replace('<y>', str(focus.rel_y))
else:
focus_code = focus_code.replace('<x>', str(focus.x)).replace('<y>', str(focus.y))
# Output focus code
focus_code = focus_code.replace('<prereqs>', prereqs_code).replace('<exclusives>', exclusives_code)
out += focus_code
# Output focus loc
out_loc = 'l_english:\n'
for focus_id in foci:
focus = foci[focus_id]
out_loc += f' {focus.tag}: "{focus.name}"\n'
with open(f'common/national_focus/{file_name_focus}.txt', 'w') as o_file:
o_file.write(out)
with open(f'localisation/english/{file_name_loc}.yml', 'w', encoding='utf-8-sig') as o_file:
o_file.write(out_loc)
root = tk.Tk()
root.geometry('500x772')
root.title('Draw.io Converter')
app = App(master=root)
app.mainloop()