@@ -158,6 +158,9 @@ export class ExtendedPythonGenerator extends PythonGenerator {
158
158
addImport ( importModule : string ) : void {
159
159
this . definitions_ [ 'import_' + importModule ] = 'import ' + importModule ;
160
160
}
161
+ addMethod ( methodName : string , code : string ) : void {
162
+ this . definitions_ [ '%' + methodName ] = code ;
163
+ }
161
164
162
165
classParentFromModuleType ( moduleType : string ) : string {
163
166
if ( moduleType == commonStorage . MODULE_TYPE_PROJECT ) {
@@ -186,31 +189,27 @@ export class ExtendedPythonGenerator extends PythonGenerator {
186
189
// Convert the definitions dictionary into a list.
187
190
const imports = [ ] ;
188
191
const definitions = [ ] ;
192
+ const methods = [ ] ;
189
193
for ( let name in this . definitions_ ) {
190
194
const def = this . definitions_ [ name ] ;
191
195
if ( def . match ( / ^ ( f r o m \s + \S + \s + ) ? i m p o r t \s + \S + / ) ) {
192
196
imports . push ( def ) ;
193
- } else {
197
+ } else if ( name . match ( / ^ % .* / ) ) {
198
+ methods . push ( def ) ;
199
+ } else {
194
200
definitions . push ( def ) ;
195
201
}
196
202
}
197
- // Call Blockly.CodeGenerator's finish. This is required to be done this way
198
- // because we derive from PythonGenerator which dervies from CodeGenerator
199
- // This section except for the class_def part is all copied from Blockly's
200
- // PythonGenerator. It can't be derived because it needs the class insertion
201
- // in the middle.
202
- code = Blockly . CodeGenerator . prototype . finish ( code ) ;
203
+ this . definitions_ = Object . create ( null ) ;
204
+ this . functionNames_ = Object . create ( null ) ;
203
205
this . isInitialized = false ;
204
206
205
207
let class_def = "class " + className + "(" + classParent + "):\n" ;
206
- if ( ! code ) {
207
- code = "pass" ;
208
- }
209
208
210
209
this . nameDB_ ! . reset ( ) ;
211
210
const allDefs = imports . join ( '\n' ) + '\n\n' + definitions . join ( '\n\n' ) ;
212
211
return allDefs . replace ( / \n \n + / g, '\n\n' ) . replace ( / \n * $ / , '\n\n\n' ) + class_def +
213
- this . prefixLines ( code , this . INDENT ) ;
212
+ this . prefixLines ( methods . join ( '\n\n' ) , this . INDENT ) ;
214
213
}
215
214
}
216
215
0 commit comments