Skip to content

Commit 35f4fae

Browse files
authored
Add define_hardware method to RobotBase and call it from RobotBase.__init__. (#184)
Modified mrc_mechanism_component_holder pythonFromBlock to determine the module type by calling generator.getModuleType() instead of checking for the presence of the mechanisms input. Modified ExtendedPythonGenerator.generateInitStatements to not generate the call so self.define_hardware() if the module type is robot. Modified ExtendedPythonGenerator.finish to generate the __init__ method before other methods.
1 parent 9a15ee3 commit 35f4fae

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

server_python_scripts/blocks_base_classes/robot_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ def __init__(self):
77
self.hardware = []
88
# In self.event_handlers, the keys are the event names, the values are a list of handlers.
99
self.event_handlers = {}
10+
self.define_hardware()
11+
12+
def define_hardware(self):
13+
pass
1014

1115
def register_event_handler(self, event_name: str, event_handler: Callable) -> None:
1216
if event_name in self.event_handlers:

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license
33
* Copyright 2025 Porpoiseful LLC
4-
*
4+
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
@@ -64,7 +64,7 @@ function setName(block: Blockly.BlockSvg){
6464
otherNames.push(variableBlock.getFieldValue('NAME'));
6565
}
6666
});
67-
const currentName = block.getFieldValue('NAME');
67+
const currentName = block.getFieldValue('NAME');
6868
block.setFieldValue(getLegalName(currentName, otherNames), 'NAME');
6969
}
7070
}
@@ -205,8 +205,8 @@ function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator:
205205
const body = mechanisms + components;
206206
if (body != '') {
207207
code += body;
208-
generator.addClassMethodDefinition('define_hardware', code);
209-
}
208+
generator.addClassMethodDefinition('define_hardware', code);
209+
}
210210
}
211211

212212
function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
@@ -218,19 +218,20 @@ function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, genera
218218

219219
if (components != '') {
220220
code += components;
221-
generator.addClassMethodDefinition('define_hardware', code);
221+
generator.addClassMethodDefinition('define_hardware', code);
222222
}
223223
}
224224

225225
export const pythonFromBlock = function (
226226
block: MechanismComponentHolderBlock,
227-
generator: ExtendedPythonGenerator,
228-
) {
229-
if (block.getInput(INPUT_MECHANISMS)) {
230-
pythonFromBlockInRobot(block, generator);
231-
}
232-
else {
233-
pythonFromBlockInMechanism(block, generator);
227+
generator: ExtendedPythonGenerator) {
228+
switch (generator.getModuleType()) {
229+
case commonStorage.MODULE_TYPE_ROBOT:
230+
pythonFromBlockInRobot(block, generator);
231+
break;
232+
case commonStorage.MODULE_TYPE_MECHANISM:
233+
pythonFromBlockInMechanism(block, generator);
234+
break;
234235
}
235236
return ''
236237
}

src/editor/extended_python_generator.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
CLASS_NAME_OPMODE,
3030
getClassData,
3131
} from '../blocks/utils/python';
32+
import * as commonStorage from '../storage/common_storage';
3233

3334
export class OpModeDetails {
3435
constructor(private name: string, private group : string, private enabled : boolean, private type : string) {}
@@ -70,7 +71,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
7071
private workspace: Blockly.Workspace | null = null;
7172
private context: GeneratorContext | null = null;
7273

73-
// Has components or mechanisms (ie, needs to call self.define_hardware in __init__)
74+
// Has components or mechanisms (ie, might need to call self.define_hardware in __init__)
7475
private hasHardware = false;
7576
private ports: {[key: string]: string} = Object.create(null);
7677

@@ -112,10 +113,20 @@ export class ExtendedPythonGenerator extends PythonGenerator {
112113
let initStatements = '';
113114

114115
if (this.hasHardware) {
115-
initStatements += this.INDENT + "self.define_hardware(";
116-
initStatements += this.getListOfPorts(true);
117-
initStatements += ')\n';
116+
switch (this.getModuleType()) {
117+
case commonStorage.MODULE_TYPE_ROBOT:
118+
// The RobotBase __init__ method already calls self.define_robot(), so
119+
// we don't need to generate that call here.
120+
break;
121+
case commonStorage.MODULE_TYPE_MECHANISM:
122+
// For mechanisms
123+
initStatements += this.INDENT + "self.define_hardware(";
124+
initStatements += this.getListOfPorts(true);
125+
initStatements += ')\n';
126+
break;
127+
}
118128
}
129+
119130
if (this.hasEventHandler) {
120131
initStatements += this.INDENT + "self.register_event_handlers()\n";
121132
}
@@ -225,7 +236,14 @@ export class ExtendedPythonGenerator extends PythonGenerator {
225236
}
226237
classMethods.push(code);
227238
}
239+
// Generate the __init__ method first.
240+
if ('__init__' in this.classMethods) {
241+
classMethods.push(this.classMethods['__init__'])
242+
}
228243
for (const name in this.classMethods) {
244+
if (name === '__init__') {
245+
continue;
246+
}
229247
classMethods.push(this.classMethods[name])
230248
}
231249
this.eventHandlers = Object.create(null);

0 commit comments

Comments
 (0)