@@ -31,6 +31,8 @@ import * as commonStorage from '../storage/common_storage';
31
31
export class ExtendedPythonGenerator extends PythonGenerator {
32
32
private currentModule : commonStorage . Module | null = null ;
33
33
private mapWorkspaceIdToExportedBlocks : { [ key : string ] : Block [ ] } = Object . create ( null ) ;
34
+ protected methods_ : { [ key : string ] : string } = Object . create ( null ) ;
35
+
34
36
35
37
constructor ( ) {
36
38
super ( 'Python' ) ;
@@ -158,6 +160,9 @@ export class ExtendedPythonGenerator extends PythonGenerator {
158
160
addImport ( importModule : string ) : void {
159
161
this . definitions_ [ 'import_' + importModule ] = 'import ' + importModule ;
160
162
}
163
+ addMethod ( methodName : string , code : string ) : void {
164
+ this . methods_ [ methodName ] = code ;
165
+ }
161
166
162
167
classParentFromModuleType ( moduleType : string ) : string {
163
168
if ( moduleType == commonStorage . MODULE_TYPE_PROJECT ) {
@@ -186,31 +191,32 @@ export class ExtendedPythonGenerator extends PythonGenerator {
186
191
// Convert the definitions dictionary into a list.
187
192
const imports = [ ] ;
188
193
const definitions = [ ] ;
194
+
189
195
for ( let name in this . definitions_ ) {
190
196
const def = this . definitions_ [ name ] ;
191
197
if ( def . match ( / ^ ( f r o m \s + \S + \s + ) ? i m p o r t \s + \S + / ) ) {
192
198
imports . push ( def ) ;
193
- } else {
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
+ const methods = [ ] ;
204
+ for ( let name in this . methods_ ) {
205
+ methods . push ( this . methods_ [ name ] )
206
+ }
207
+
208
+ this . definitions_ = Object . create ( null ) ;
209
+ this . functionNames_ = Object . create ( null ) ;
210
+ this . methods_ = Object . create ( null ) ;
211
+
203
212
this . isInitialized = false ;
204
213
205
214
let class_def = "class " + className + "(" + classParent + "):\n" ;
206
- if ( ! code ) {
207
- code = "pass" ;
208
- }
209
215
210
216
this . nameDB_ ! . reset ( ) ;
211
217
const allDefs = imports . join ( '\n' ) + '\n\n' + definitions . join ( '\n\n' ) ;
212
218
return allDefs . replace ( / \n \n + / g, '\n\n' ) . replace ( / \n * $ / , '\n\n\n' ) + class_def +
213
- this . prefixLines ( code , this . INDENT ) ;
219
+ this . prefixLines ( methods . join ( '\n\n' ) , this . INDENT ) ;
214
220
}
215
221
}
216
222
0 commit comments