@@ -33,11 +33,13 @@ import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
3333
3434export const BLOCK_NAME = 'mrc_call_python_function' ;
3535
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+ }
4143
4244const RETURN_TYPE_NONE = 'None' ;
4345
@@ -48,7 +50,7 @@ export type FunctionArg = {
4850
4951type CallPythonFunctionBlock = Blockly . Block & CallPythonFunctionMixin ;
5052interface CallPythonFunctionMixin extends CallPythonFunctionMixinType {
51- mrcFunctionKind : string , // module, static, constructor, instance, or instance_within.
53+ mrcFunctionKind : FunctionKind ,
5254 mrcReturnType : string ,
5355 mrcArgs : FunctionArg [ ] ,
5456 mrcTooltip : string ,
@@ -61,7 +63,7 @@ type CallPythonFunctionMixinType = typeof CALL_PYTHON_FUNCTION;
6163/** Extra state for serialising call_python_* blocks. */
6264type CallPythonFunctionExtraState = {
6365 /**
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 .
6567 */
6668 functionKind : string ,
6769 /**
@@ -105,36 +107,36 @@ const CALL_PYTHON_FUNCTION = {
105107 this . setTooltip ( ( ) => {
106108 let tooltip : string ;
107109 switch ( this . mrcFunctionKind ) {
108- case FUNCTION_KIND_MODULE : {
110+ case FunctionKind . MODULE : {
109111 const moduleName = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
110112 const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
111113 tooltip = 'Calls the function ' + moduleName + '.' + functionName + '.' ;
112114 break ;
113115 }
114- case FUNCTION_KIND_STATIC : {
116+ case FunctionKind . STATIC : {
115117 const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
116118 const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
117119 tooltip = 'Calls the function ' + className + '.' + functionName + '.' ;
118120 break ;
119121 }
120- case FUNCTION_KIND_CONSTRUCTOR : {
122+ case FunctionKind . CONSTRUCTOR : {
121123 const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
122124 tooltip = 'Constructs an instance of the class ' + className + '.' ;
123125 break ;
124126 }
125- case FUNCTION_KIND_INSTANCE : {
127+ case FunctionKind . INSTANCE : {
126128 const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
127129 const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
128130 tooltip = 'Calls the function ' + className + '.' + functionName + '.' ;
129131 break ;
130132 }
131- case FUNCTION_KIND_INSTANCE_WITHIN : {
133+ case FunctionKind . INSTANCE_WITHIN : {
132134 const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
133135 tooltip = 'Calls the method ' + functionName + '.' ;
134136 break ;
135137 }
136138 default :
137- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", "instance", or "instance_within".' )
139+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
138140 }
139141 const funcTooltip = this . mrcTooltip ;
140142 if ( funcTooltip ) {
@@ -178,7 +180,7 @@ const CALL_PYTHON_FUNCTION = {
178180 this : CallPythonFunctionBlock ,
179181 extraState : CallPythonFunctionExtraState
180182 ) : void {
181- this . mrcFunctionKind = extraState . functionKind ;
183+ this . mrcFunctionKind = extraState . functionKind as FunctionKind ;
182184 this . mrcReturnType = extraState . returnType ;
183185 this . mrcArgs = [ ] ;
184186 extraState . args . forEach ( ( arg ) => {
@@ -218,39 +220,39 @@ const CALL_PYTHON_FUNCTION = {
218220 }
219221 // Add the dummy input.
220222 switch ( this . mrcFunctionKind ) {
221- case FUNCTION_KIND_MODULE :
223+ case FunctionKind . MODULE :
222224 this . appendDummyInput ( )
223225 . appendField ( 'call' )
224226 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
225227 . appendField ( '.' )
226228 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
227229 break ;
228- case FUNCTION_KIND_STATIC :
230+ case FunctionKind . STATIC :
229231 this . appendDummyInput ( )
230232 . appendField ( 'call' )
231233 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
232234 . appendField ( '.' )
233235 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
234236 break ;
235- case FUNCTION_KIND_CONSTRUCTOR :
237+ case FunctionKind . CONSTRUCTOR :
236238 this . appendDummyInput ( )
237239 . appendField ( 'create' )
238240 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
239241 break ;
240- case FUNCTION_KIND_INSTANCE :
242+ case FunctionKind . INSTANCE :
241243 this . appendDummyInput ( )
242244 . appendField ( 'call' )
243245 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
244246 . appendField ( '.' )
245247 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
246248 break ;
247- case FUNCTION_KIND_INSTANCE_WITHIN :
249+ case FunctionKind . INSTANCE_WITHIN :
248250 this . appendDummyInput ( )
249251 . appendField ( 'call' )
250252 . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
251253 break ;
252254 default :
253- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", "instance", or "instance_within".' )
255+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
254256 }
255257 // Add input sockets for the arguments.
256258 for ( let i = 0 ; i < this . mrcArgs . length ; i ++ ) {
@@ -279,15 +281,15 @@ export const pythonFromBlock = function(
279281 let code ;
280282 let argStartIndex = 0 ;
281283 switch ( callPythonFunctionBlock . mrcFunctionKind ) {
282- case FUNCTION_KIND_MODULE : {
284+ case FunctionKind . MODULE : {
283285 const moduleName = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
284286 const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
285287 ? callPythonFunctionBlock . mrcActualFunctionName
286288 : block . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
287289 code = moduleName + '.' + functionName ;
288290 break ;
289291 }
290- case FUNCTION_KIND_STATIC : {
292+ case FunctionKind . STATIC : {
291293 const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
292294 const className = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
293295 const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
@@ -296,13 +298,13 @@ export const pythonFromBlock = function(
296298 code = className + '.' + functionName ;
297299 break ;
298300 }
299- case FUNCTION_KIND_CONSTRUCTOR : {
301+ case FunctionKind . CONSTRUCTOR : {
300302 const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
301303 const className = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
302304 code = className ;
303305 break ;
304306 }
305- case FUNCTION_KIND_INSTANCE : {
307+ case FunctionKind . INSTANCE : {
306308 const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
307309 const selfValue = generator . valueToCode ( block , 'ARG0' , Order . MEMBER ) ;
308310 const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
@@ -312,7 +314,7 @@ export const pythonFromBlock = function(
312314 argStartIndex = 1 ; // Skip the self argument.
313315 break ;
314316 }
315- case FUNCTION_KIND_INSTANCE_WITHIN : {
317+ case FunctionKind . INSTANCE_WITHIN : {
316318 const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
317319 const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
318320 ? callPythonFunctionBlock . mrcActualFunctionName
@@ -321,7 +323,7 @@ export const pythonFromBlock = function(
321323 break ;
322324 }
323325 default :
324- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", "instance", or "instance_within".' )
326+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
325327 }
326328 code += '(' + generateCodeForArguments ( callPythonFunctionBlock , generator , argStartIndex ) + ')' ;
327329 if ( block . outputConnection ) {
0 commit comments