Skip to content

Commit bf988cd

Browse files
authored
Merge pull request #68 from lizlooney/pr_call_method_from_within_same_class
Make mrc_call_python_function work for calling methods in the same class.
2 parents 5f8950b + 5530863 commit bf988cd

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
3333

3434
export 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';
36+
enum FunctionKind {
37+
MODULE = 'module',
38+
STATIC = 'static',
39+
CONSTRUCTOR = 'constructor',
40+
INSTANCE = 'instance',
41+
INSTANCE_WITHIN = 'instance_within',
42+
}
4043

4144
const RETURN_TYPE_NONE = 'None';
4245

@@ -47,7 +50,7 @@ export type FunctionArg = {
4750

4851
type CallPythonFunctionBlock = Blockly.Block & CallPythonFunctionMixin;
4952
interface CallPythonFunctionMixin extends CallPythonFunctionMixinType {
50-
mrcFunctionKind: string, // module, static, constructor, or instance
53+
mrcFunctionKind: FunctionKind,
5154
mrcReturnType: string,
5255
mrcArgs: FunctionArg[],
5356
mrcTooltip: string,
@@ -60,7 +63,7 @@ type CallPythonFunctionMixinType = typeof CALL_PYTHON_FUNCTION;
6063
/** Extra state for serialising call_python_* blocks. */
6164
type CallPythonFunctionExtraState = {
6265
/**
63-
* The kind of function: module, static, constructor, or instance.
66+
* The kind of function. Must be one of the FunctionKind enum values as a string.
6467
*/
6568
functionKind: string,
6669
/**
@@ -104,31 +107,36 @@ const CALL_PYTHON_FUNCTION = {
104107
this.setTooltip(() => {
105108
let tooltip: string;
106109
switch (this.mrcFunctionKind) {
107-
case FUNCTION_KIND_MODULE: {
110+
case FunctionKind.MODULE: {
108111
const moduleName = this.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
109112
const functionName = this.getFieldValue(pythonUtils.FIELD_FUNCTION_NAME);
110113
tooltip = 'Calls the function ' + moduleName + '.' + functionName + '.';
111114
break;
112115
}
113-
case FUNCTION_KIND_STATIC: {
116+
case FunctionKind.STATIC: {
114117
const className = this.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
115118
const functionName = this.getFieldValue(pythonUtils.FIELD_FUNCTION_NAME);
116119
tooltip = 'Calls the function ' + className + '.' + functionName + '.';
117120
break;
118121
}
119-
case FUNCTION_KIND_CONSTRUCTOR: {
122+
case FunctionKind.CONSTRUCTOR: {
120123
const className = this.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
121124
tooltip = 'Constructs an instance of the class ' + className + '.';
122125
break;
123126
}
124-
case FUNCTION_KIND_INSTANCE: {
127+
case FunctionKind.INSTANCE: {
125128
const className = this.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
126129
const functionName = this.getFieldValue(pythonUtils.FIELD_FUNCTION_NAME);
127130
tooltip = 'Calls the function ' + className + '.' + functionName + '.';
128131
break;
129132
}
133+
case FunctionKind.INSTANCE_WITHIN: {
134+
const functionName = this.getFieldValue(pythonUtils.FIELD_FUNCTION_NAME);
135+
tooltip = 'Calls the method ' + functionName + '.';
136+
break;
137+
}
130138
default:
131-
throw new Error('mrcVarKind must be "module", "static", "constructor", or "instance".')
139+
throw new Error('mrcFunctionKind has unexpected value: ' + mrcFunctionKind)
132140
}
133141
const funcTooltip = this.mrcTooltip;
134142
if (funcTooltip) {
@@ -172,7 +180,7 @@ const CALL_PYTHON_FUNCTION = {
172180
this: CallPythonFunctionBlock,
173181
extraState: CallPythonFunctionExtraState
174182
): void {
175-
this.mrcFunctionKind = extraState.functionKind;
183+
this.mrcFunctionKind = extraState.functionKind as FunctionKind;
176184
this.mrcReturnType = extraState.returnType;
177185
this.mrcArgs = [];
178186
extraState.args.forEach((arg) => {
@@ -212,34 +220,39 @@ const CALL_PYTHON_FUNCTION = {
212220
}
213221
// Add the dummy input.
214222
switch (this.mrcFunctionKind) {
215-
case FUNCTION_KIND_MODULE:
223+
case FunctionKind.MODULE:
216224
this.appendDummyInput()
217225
.appendField('call')
218226
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_MODULE_OR_CLASS_NAME)
219227
.appendField('.')
220228
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_FUNCTION_NAME);
221229
break;
222-
case FUNCTION_KIND_STATIC:
230+
case FunctionKind.STATIC:
223231
this.appendDummyInput()
224232
.appendField('call')
225233
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_MODULE_OR_CLASS_NAME)
226234
.appendField('.')
227235
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_FUNCTION_NAME);
228236
break;
229-
case FUNCTION_KIND_CONSTRUCTOR:
237+
case FunctionKind.CONSTRUCTOR:
230238
this.appendDummyInput()
231239
.appendField('create')
232240
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
233241
break;
234-
case FUNCTION_KIND_INSTANCE:
242+
case FunctionKind.INSTANCE:
235243
this.appendDummyInput()
236244
.appendField('call')
237245
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_MODULE_OR_CLASS_NAME)
238246
.appendField('.')
239247
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_FUNCTION_NAME);
240248
break;
249+
case FunctionKind.INSTANCE_WITHIN:
250+
this.appendDummyInput()
251+
.appendField('call')
252+
.appendField(createFieldNonEditableText(''), pythonUtils.FIELD_FUNCTION_NAME);
253+
break;
241254
default:
242-
throw new Error('mrcVarKind must be "module", "static", "constructor", or "instance".')
255+
throw new Error('mrcFunctionKind has unexpected value: ' + mrcFunctionKind)
243256
}
244257
// Add input sockets for the arguments.
245258
for (let i = 0; i < this.mrcArgs.length; i++) {
@@ -268,15 +281,15 @@ export const pythonFromBlock = function(
268281
let code;
269282
let argStartIndex = 0;
270283
switch (callPythonFunctionBlock.mrcFunctionKind) {
271-
case FUNCTION_KIND_MODULE: {
284+
case FunctionKind.MODULE: {
272285
const moduleName = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
273286
const functionName = (callPythonFunctionBlock.mrcActualFunctionName)
274287
? callPythonFunctionBlock.mrcActualFunctionName
275288
: block.getFieldValue(pythonUtils.FIELD_FUNCTION_NAME);
276289
code = moduleName + '.' + functionName;
277290
break;
278291
}
279-
case FUNCTION_KIND_STATIC: {
292+
case FunctionKind.STATIC: {
280293
const callPythonFunctionBlock = block as CallPythonFunctionBlock;
281294
const className = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
282295
const functionName = (callPythonFunctionBlock.mrcActualFunctionName)
@@ -285,13 +298,13 @@ export const pythonFromBlock = function(
285298
code = className + '.' + functionName;
286299
break;
287300
}
288-
case FUNCTION_KIND_CONSTRUCTOR: {
301+
case FunctionKind.CONSTRUCTOR: {
289302
const callPythonFunctionBlock = block as CallPythonFunctionBlock;
290303
const className = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
291304
code = className;
292305
break;
293306
}
294-
case FUNCTION_KIND_INSTANCE: {
307+
case FunctionKind.INSTANCE: {
295308
const callPythonFunctionBlock = block as CallPythonFunctionBlock;
296309
const selfValue = generator.valueToCode(block, 'ARG0', Order.MEMBER);
297310
const functionName = (callPythonFunctionBlock.mrcActualFunctionName)
@@ -301,6 +314,16 @@ export const pythonFromBlock = function(
301314
argStartIndex = 1; // Skip the self argument.
302315
break;
303316
}
317+
case FunctionKind.INSTANCE_WITHIN: {
318+
const callPythonFunctionBlock = block as CallPythonFunctionBlock;
319+
const functionName = (callPythonFunctionBlock.mrcActualFunctionName)
320+
? callPythonFunctionBlock.mrcActualFunctionName
321+
: block.getFieldValue(pythonUtils.FIELD_FUNCTION_NAME);
322+
code = 'self.' + functionName;
323+
break;
324+
}
325+
default:
326+
throw new Error('mrcFunctionKind has unexpected value: ' + mrcFunctionKind)
304327
}
305328
code += '(' + generateCodeForArguments(callPythonFunctionBlock, generator, argStartIndex) + ')';
306329
if (block.outputConnection) {

0 commit comments

Comments
 (0)