forked from sugarlabs/turtleblocksjs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pluginify.py
222 lines (176 loc) · 6.45 KB
/
pluginify.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
# Copyright (C) 2015 Sam Parkinson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import sys
import json
HELP = '''Usage:
python pluginify.py (file)
or
python pluginify.py (file) > plugin.json
Converts an RTP (readable TurtleBlocks plugin) file into a JSON file
to load into Turtle Blocks JS. For more information, run
`python pluginify.py syntax`
'''
SYNTAX = '''
In an RST file, new blocks are not defined with braces (as is typical of
Javascript); rather they start whenever you add //* *// and their scope is
until the next block starts (or the end of file in the case that it is
the last block).
To add a comment, just type //* comment *// followed by your multi-
line comment. pluginify ignores comments, i.e., they are not added in
the JSON plugin.
Example:
//* comment *// Your single or multi line comment here...
Global variables are defined in a section //* globals *//.
Example:
//* globals *//
var calories = 0;
var protein = 0;
var carbohydrate = 0;
var fiber = 0;
var fat = 0;
Define all the global variables under the section
//* block-globals *//.
These definitions will be added to all the code blocks in the created
JSON output. Note that these "globals" included in each block but are
local in context to each block.
Example: You can define a common API Key to be used by all blocks.
//* block-globals *//
var mashapeKey = '(keycode)'; in globals.
You can also declare global variables specific to argument blocks that
get applied to a set of similar blocks, e.g., the variables under
//* arg-globals *// will be added to all the arg blocks.
Example:
//* arg-globals *//
var block = blocks.blockList[blk];
var connections = block.connections;
You can declare functions for parameter blocks to be evaluated when
the block labels are updated with the //* parameter:(blockname) *//
tag.
Example:
//* parameter:loudness *//
if (mic == null) {errorMsg("The microphone is not available.");
value = 0;
} else {
value = Math.round(mic.getLevel() * 1000);
}
To define a block you need to type: //* block:(blockname) *//
Example:
//* block:power *//
var block = new ProtoBlock('power');
block.palette = palettes.dict['maths'];
blocks.protoBlockDict['power'] = block;
block.twoArgMathBlock();
block.defaults.push(10, 2);
block.staticLabels.push('power', 'base', 'exp.');
You should also define a setter for a parameter block (if appropriate).
You can do this using the setter setion:
//* setter:myValue *//
myValue = value;
updateDisplayOfMyValue();
Macros are defined in in //* macro:(blockname) *//
Example:
//* macro: black *//
[[0, "setshade", 0, 0, [null, 1, null]], [1, ["number", {"value": 0}], 0, 0, [0]]]
Note the use of double quotes in the JSON-encoded object.
Graphical elements (icons, colors) are defined in the own sections:
Palette icons are defined as //* palette-icon:(palette name) *//
Example:
//* palette-icon:food *//
<svg ...> ... </svg>
Similarly for block colors:
Example:
//* palette-fill:food *// #FFFFFF
//* palette-stroke:food *// #A0A0A0
//* palette-highlight:food *// #D5D5D5
Plugins can specify code to be executed on load, on start, and on stop.
Example:
//* onload:foo *//
your code here...
NOTE: name of on load, on start, and on stop sections must match the
name of one of the plugin blocks.
'''
def clear():
global NAMES, JS_TYPES, IMAGES
NAMES = {
'flow': 'FLOWPLUGINS',
'arg': 'ARGPLUGINS',
'block': 'BLOCKPLUGINS',
'macro': 'MACROPLUGINS',
'parameter': 'PARAMETERPLUGINS',
'setter': 'SETTERPLUGINS',
'onload': 'ONLOAD',
'onstart': 'ONSTART',
'onstop': 'ONSTOP',
'palette-icon': 'PALETTEPLUGINS',
'palette-fill': 'PALETTEFILLCOLORS',
'palette-stroke': 'PALETTESTROKECOLORS',
'palette-highlight': 'PALETTEHIGHLIGHTCOLORS',
'palette-stroke-highlight': 'HIGHLIGHTSTROKECOLORS'}
JS_TYPES = ('flow', 'arg', 'block', 'macro', 'parameter', 'setter', 'onload', 'onstart', 'onstop')
# 'blkName': 'imageData',
IMAGES = {}
def pluginify(data):
clear()
sections_list = data.split('//*')
sections_pairs = []
specific_globals = {x: '' for x in JS_TYPES}
globals_ = None
for section in sections_list:
match = re.match('(.*)\*\/\/([^\0]*)', section.strip())
if match:
if match.group(1).strip() == 'globals':
globals_ = match.group(2).strip()
elif match.group(1).strip().endswith('-globals'):
type_, _ = match.group(1).strip().split('-')
specific_globals[type_] = specific_globals[type_] + \
match.group(2).strip()
elif match.group(1).strip() == 'comment':
continue
else:
sections_pairs.append((match.group(1).strip(),
match.group(2).strip()))
outp = {}
if globals_ is not None:
outp['GLOBALS'] = globals_.replace('\n', '').replace('var ', '')
for key, value in sections_pairs:
if len(key.split(':')) != 2:
raise ValueError('Section names should have 1 colon (type:name)')
type_, name = key.split(':')
if type_ in JS_TYPES:
value = specific_globals[type_] + value
value = value.replace('\n', '')
if type_ in NAMES:
type_ = NAMES[type_]
if type_ not in outp:
outp[type_] = {}
outp[type_][name] = value
if type_ == 'image':
# TODO: Detect if its png
IMAGES[name] = 'data:image/svg+xml;utf8,' + value
if IMAGES:
outp['IMAGES'] = IMAGES
return json.dumps(outp, indent=4)
if __name__ == '__main__':
if len(sys.argv) != 2:
print HELP
elif sys.argv[1] in ('help', '-h', '--help'):
print HELP
elif sys.argv[1] == 'syntax':
print SYNTAX
else:
with open(sys.argv[1]) as f:
data = f.read()
print pluginify(data)