@@ -33,11 +33,13 @@ import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
33
33
34
34
export const BLOCK_NAME = 'mrc_call_python_function' ;
35
35
36
- const FUNCTION_KIND_MODULE = 'module' ;
37
- const FUNCTION_KIND_STATIC = 'static' ;
38
- const FUNCTION_KIND_CONSTRUCTOR = 'constructor' ;
39
- const FUNCTION_KIND_INSTANCE = 'instance' ;
40
- const FUNCTION_KIND_INSTANCE_WITHIN = 'instance_within' ;
36
+ enum FunctionKind {
37
+ MODULE = 'module' ,
38
+ STATIC = 'static' ,
39
+ CONSTRUCTOR = 'constructor' ,
40
+ INSTANCE = 'instance' ,
41
+ INSTANCE_WITHIN = 'instance_within' ,
42
+ }
41
43
42
44
const RETURN_TYPE_NONE = 'None' ;
43
45
@@ -48,7 +50,7 @@ export type FunctionArg = {
48
50
49
51
type CallPythonFunctionBlock = Blockly . Block & CallPythonFunctionMixin ;
50
52
interface CallPythonFunctionMixin extends CallPythonFunctionMixinType {
51
- mrcFunctionKind : string , // module, static, constructor, instance, or instance_within.
53
+ mrcFunctionKind : FunctionKind ,
52
54
mrcReturnType : string ,
53
55
mrcArgs : FunctionArg [ ] ,
54
56
mrcTooltip : string ,
@@ -61,7 +63,7 @@ type CallPythonFunctionMixinType = typeof CALL_PYTHON_FUNCTION;
61
63
/** Extra state for serialising call_python_* blocks. */
62
64
type CallPythonFunctionExtraState = {
63
65
/**
64
- * The kind of function: module, static, constructor, instance, or instance_within .
66
+ * The kind of function. Must be one of the FunctionKind enum values as a string .
65
67
*/
66
68
functionKind : string ,
67
69
/**
@@ -105,36 +107,36 @@ const CALL_PYTHON_FUNCTION = {
105
107
this . setTooltip ( ( ) => {
106
108
let tooltip : string ;
107
109
switch ( this . mrcFunctionKind ) {
108
- case FUNCTION_KIND_MODULE : {
110
+ case FunctionKind . MODULE : {
109
111
const moduleName = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
110
112
const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
111
113
tooltip = 'Calls the function ' + moduleName + '.' + functionName + '.' ;
112
114
break ;
113
115
}
114
- case FUNCTION_KIND_STATIC : {
116
+ case FunctionKind . STATIC : {
115
117
const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
116
118
const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
117
119
tooltip = 'Calls the function ' + className + '.' + functionName + '.' ;
118
120
break ;
119
121
}
120
- case FUNCTION_KIND_CONSTRUCTOR : {
122
+ case FunctionKind . CONSTRUCTOR : {
121
123
const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
122
124
tooltip = 'Constructs an instance of the class ' + className + '.' ;
123
125
break ;
124
126
}
125
- case FUNCTION_KIND_INSTANCE : {
127
+ case FunctionKind . INSTANCE : {
126
128
const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
127
129
const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
128
130
tooltip = 'Calls the function ' + className + '.' + functionName + '.' ;
129
131
break ;
130
132
}
131
- case FUNCTION_KIND_INSTANCE_WITHIN : {
133
+ case FunctionKind . INSTANCE_WITHIN : {
132
134
const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
133
135
tooltip = 'Calls the method ' + functionName + '.' ;
134
136
break ;
135
137
}
136
138
default :
137
- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", "instance", or "instance_within".' )
139
+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
138
140
}
139
141
const funcTooltip = this . mrcTooltip ;
140
142
if ( funcTooltip ) {
@@ -178,7 +180,7 @@ const CALL_PYTHON_FUNCTION = {
178
180
this : CallPythonFunctionBlock ,
179
181
extraState : CallPythonFunctionExtraState
180
182
) : void {
181
- this . mrcFunctionKind = extraState . functionKind ;
183
+ this . mrcFunctionKind = extraState . functionKind as FunctionKind ;
182
184
this . mrcReturnType = extraState . returnType ;
183
185
this . mrcArgs = [ ] ;
184
186
extraState . args . forEach ( ( arg ) => {
@@ -218,39 +220,39 @@ const CALL_PYTHON_FUNCTION = {
218
220
}
219
221
// Add the dummy input.
220
222
switch ( this . mrcFunctionKind ) {
221
- case FUNCTION_KIND_MODULE :
223
+ case FunctionKind . MODULE :
222
224
this . appendDummyInput ( )
223
225
. appendField ( 'call' )
224
226
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
225
227
. appendField ( '.' )
226
228
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
227
229
break ;
228
- case FUNCTION_KIND_STATIC :
230
+ case FunctionKind . STATIC :
229
231
this . appendDummyInput ( )
230
232
. appendField ( 'call' )
231
233
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
232
234
. appendField ( '.' )
233
235
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
234
236
break ;
235
- case FUNCTION_KIND_CONSTRUCTOR :
237
+ case FunctionKind . CONSTRUCTOR :
236
238
this . appendDummyInput ( )
237
239
. appendField ( 'create' )
238
240
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
239
241
break ;
240
- case FUNCTION_KIND_INSTANCE :
242
+ case FunctionKind . INSTANCE :
241
243
this . appendDummyInput ( )
242
244
. appendField ( 'call' )
243
245
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
244
246
. appendField ( '.' )
245
247
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
246
248
break ;
247
- case FUNCTION_KIND_INSTANCE_WITHIN :
249
+ case FunctionKind . INSTANCE_WITHIN :
248
250
this . appendDummyInput ( )
249
251
. appendField ( 'call' )
250
252
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
251
253
break ;
252
254
default :
253
- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", "instance", or "instance_within".' )
255
+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
254
256
}
255
257
// Add input sockets for the arguments.
256
258
for ( let i = 0 ; i < this . mrcArgs . length ; i ++ ) {
@@ -279,15 +281,15 @@ export const pythonFromBlock = function(
279
281
let code ;
280
282
let argStartIndex = 0 ;
281
283
switch ( callPythonFunctionBlock . mrcFunctionKind ) {
282
- case FUNCTION_KIND_MODULE : {
284
+ case FunctionKind . MODULE : {
283
285
const moduleName = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
284
286
const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
285
287
? callPythonFunctionBlock . mrcActualFunctionName
286
288
: block . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
287
289
code = moduleName + '.' + functionName ;
288
290
break ;
289
291
}
290
- case FUNCTION_KIND_STATIC : {
292
+ case FunctionKind . STATIC : {
291
293
const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
292
294
const className = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
293
295
const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
@@ -296,13 +298,13 @@ export const pythonFromBlock = function(
296
298
code = className + '.' + functionName ;
297
299
break ;
298
300
}
299
- case FUNCTION_KIND_CONSTRUCTOR : {
301
+ case FunctionKind . CONSTRUCTOR : {
300
302
const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
301
303
const className = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
302
304
code = className ;
303
305
break ;
304
306
}
305
- case FUNCTION_KIND_INSTANCE : {
307
+ case FunctionKind . INSTANCE : {
306
308
const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
307
309
const selfValue = generator . valueToCode ( block , 'ARG0' , Order . MEMBER ) ;
308
310
const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
@@ -312,7 +314,7 @@ export const pythonFromBlock = function(
312
314
argStartIndex = 1 ; // Skip the self argument.
313
315
break ;
314
316
}
315
- case FUNCTION_KIND_INSTANCE_WITHIN : {
317
+ case FunctionKind . INSTANCE_WITHIN : {
316
318
const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
317
319
const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
318
320
? callPythonFunctionBlock . mrcActualFunctionName
@@ -321,7 +323,7 @@ export const pythonFromBlock = function(
321
323
break ;
322
324
}
323
325
default :
324
- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", "instance", or "instance_within".' )
326
+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
325
327
}
326
328
code += '(' + generateCodeForArguments ( callPythonFunctionBlock , generator , argStartIndex ) + ')' ;
327
329
if ( block . outputConnection ) {
0 commit comments