-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.py
executable file
·236 lines (198 loc) · 7.17 KB
/
build.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
#!/usr/bin/env python3
"""
Build blocks and translations strings.
It's a hack. Needs refactoring!
Example usage:
$ ./build.py en es de
Will generate new blocks in the "blocks" directory derived from the JSON
definitions in the definitions directory. Translation strings for languages
with the given language codes will be created in the "messages" directory.
These will initially contain the English text defined in the original JSON.
"""
import sys
import json
import copy
import re
from os import listdir, makedirs
from os.path import isfile, join, exists
from string import Template
TRANS_TOKEN = '____{}____'
HEAD = """'use strict';
goog.provide('Blockly.Blocks.colour');
goog.require('Blockly.Blocks');
goog.require('Blockly.constants');
Blockly.FieldColour.COLOURS = ['#f00', '#e00', '#d00', '#c00', '#b00', '#a00',
'#800', '#600', '#400', '#000'];
Blockly.FieldColour.COLUMNS = 5;
"""
DEFINITION = Template("""Blockly.Blocks['$name'] = {
init: function() {
this.jsonInit($definition);
}
};
""")
EGGSMELL = Template("""<xml id="blockly-toolbox" style="display: none">
$category</xml>
""")
EGGSMELL_CATEGORY = Template(""" <category name="$name" colour="$colour">
$blocks </category>
""")
EGGSMELL_BLOCK = Template(""" <block type="$name">$shadows</block>\n""")
EGGSMELL_SHADOWS = Template('<value name="$shadow_name">'
'<shadow type="$shadow_type">'
'<field name="$shadow_item_name">'
'$shadow_value'
'</field>'
'</shadow>'
'</value>')
SHADOW_SPECS = {
'Number': {
'type': 'math_number',
'name': 'NUM',
'value': '0',
},
'String': {
'type': 'text',
'name': 'TEXT',
'value': 'Some text',
},
}
SHADOW_OVERRIDES = {
'microbit_display_set_pixel': {
'value': '9',
},
'microbit_display_scroll': {
'message': 'Hello, World!',
},
'microbit_microbit_sleep': {
'duration': '1000',
},
'microbit_music_set_tempo': {
'ticks': '4',
'bpm': '120',
},
'microbit_speech_say': {
'english': 'Exterminate!',
},
'microbit_speech_pronounce': {
'phonemes': '/HEH5EH4EH3EH2EH2EH3EH4EH5EHLP.',
},
'microbit_speech_sing': {
'song': '#115DOWWWW',
}
}
LANGUAGE_HEAD = Template("""'use strict';
goog.provide('Blockly.Msg.$language');
goog.require('Blockly.Msg');
""")
def get_categories(path='definitions'):
"""
Return data structures representing the various block definitions.
"""
files = sorted([f for f in listdir(path) if
isfile(join(path, f)) and f.endswith('.js')])
result = []
for f in files:
with open(join(path, f)) as def_file:
result.append({
'name': f.replace('.js', '').capitalize(),
'definitions': json.loads(def_file.read())
})
return result
def get_code(definitions, colour):
"""
Given a list of definitions will return the associated code.
"""
code = []
for definition in definitions:
new_def = copy.deepcopy(definition)
name = new_def['type']
del new_def['type']
new_def['colour'] = colour
code.append(DEFINITION.substitute(name=name,
definition=json.dumps(new_def)))
code = ''.join(code)
matcher = re.compile(r'\"____(?P<name>[\w]+)____\"')
matched = matcher.findall(code)
for name in matched:
code = code.replace('"' + TRANS_TOKEN.format(name) + '"',
'Blockly.Msg.{}'.format(name))
return code
def get_eggsmell(categories):
"""
Gets XML (pronounced eggsmell) for toolbox. Seriously, this is far quicker
than faffing about with XML.
"""
rendered = []
for category in categories:
rendered_defs = []
for definition in category['definitions']:
name = definition['type']
shadows = ''
for arg in definition.get('args0', []):
if arg['type'] == 'input_value' and \
arg['check'] in SHADOW_SPECS:
spec = SHADOW_SPECS[arg['check']]
s_name = arg['name']
s_type = spec['type']
s_item_name = spec['name']
s_val = spec['value']
if name in SHADOW_OVERRIDES:
if s_name in SHADOW_OVERRIDES[name]:
s_val = SHADOW_OVERRIDES[name][s_name]
shadows += EGGSMELL_SHADOWS.substitute(shadow_name=s_name,
shadow_type=s_type,
shadow_item_name=s_item_name,
shadow_value=s_val)
rendered_defs.append(EGGSMELL_BLOCK.substitute(name=name,
shadows=shadows))
blocks = ''.join(rendered_defs)
rendered.append(EGGSMELL_CATEGORY.substitute(name=category['name'],
colour=category['colour'],
blocks=blocks))
return EGGSMELL.substitute(category=''.join(rendered))
def generate_translation(definition):
"""
Given a JSON representation of a definition will return a list of
JavaScript statements defining the attributes to be translated.
"""
result = []
fields = ['tooltip', 'helpUrl']
template = "Blockly.Msg.{} = {};\n"
name = definition['type']
for k in definition.keys():
trans_name = '{}_{}'.format(name, k).upper()
if k in fields:
result.append(template.format(trans_name, repr(definition[k])))
definition[k] = TRANS_TOKEN.format(trans_name)
elif k.startswith('message'):
result.append(template.format(trans_name, repr(definition[k])))
definition[k] = TRANS_TOKEN.format(trans_name)
return result
def main(languages):
categories = get_categories()
colour = 0
step = 360 // len(categories)
for i, category in enumerate(categories):
category['colour'] = colour
colour = step * (i + 1)
for lang in languages:
lang_content = []
lang_content.append(LANGUAGE_HEAD.substitute(language=lang))
for category in categories:
for definition in category['definitions']:
lang_content.extend(generate_translation(definition))
translation_strings = ''.join(lang_content)
directory = join('messages', lang)
if not exists(directory):
mkdirs(directory)
with open(join(directory, 'messages.js'), 'w') as msgs:
msgs.write(translation_strings)
with open('blocks/microbit.js', 'w') as output:
output.write(HEAD)
for category in categories:
output.write(get_code(category['definitions'], category['colour']))
with open('toolbox.xml', 'w') as toolbox:
toolbox.write(get_eggsmell(categories))
if __name__ == '__main__':
main(sys.argv[1:])