Skip to content

Commit 6ed4db0

Browse files
committed
Get robot code to generate correctly
1 parent 7821f9c commit 6ed4db0

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,12 @@ export const pythonFromBlock = function (
362362
}
363363
if (block.mrcPythonMethodName == '__init__') {
364364
let class_specific = generator.getClassSpecificForInit();
365-
branch = generator.INDENT + 'super.__init__(' + class_specific + ')\n' +
365+
branch = generator.INDENT + 'super().__init__(' + class_specific + ')\n' +
366366
generator.defineClassVariables() + branch;
367367
}
368368
else if (generator.inBaseClassMethod(blocklyName)){
369369
// Special case for update, to also call the update method of the base class
370-
branch = generator.INDENT + 'super.' + blocklyName + '()\n' + branch;
370+
branch = generator.INDENT + 'super().' + blocklyName + '()\n' + branch;
371371
}
372372
if (returnValue) {
373373
returnValue = generator.INDENT + 'return ' + returnValue + '\n';

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export const setup = function () {
178178
}
179179

180180
function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
181-
let code = 'def define_hardware(self):\n' + generator.INDENT + 'self.hardware = []\n';
181+
let code = 'def define_hardware(self):\n';
182182

183183
let mechanisms = '';
184184
let components = '';
@@ -189,23 +189,21 @@ function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator:
189189
const body = mechanisms + components;
190190
if (body != '') {
191191
code += body;
192+
generator.addClassMethodDefinition('define_hardware', code);
192193
}
193-
194-
generator.addClassMethodDefinition('define_hardware', code);
195194
}
196195

197196
function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
198197
let components = '';
199198

200199
components = generator.statementToCode(block, 'COMPONENTS');
201200

202-
let code = 'def define_hardware(self' + generator.getListOfPorts(false) + '):\n' +
203-
generator.INDENT + 'self.hardware = []\n';
201+
let code = 'def define_hardware(self' + generator.getListOfPorts(false) + '):\n';
204202

205203
if (components != '') {
206204
code += components;
207-
}
208-
generator.addClassMethodDefinition('define_hardware', code);
205+
generator.addClassMethodDefinition('define_hardware', code);
206+
}
209207
}
210208

211209
export const pythonFromBlock = function (

src/editor/extended_python_generator.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ export class ExtendedPythonGenerator extends PythonGenerator {
8484
let variableDefinitions = '';
8585

8686
if (this.context?.getHasHardware()) {
87-
variableDefinitions += this.INDENT + "self.define_hardware(";
88-
variableDefinitions += this.getListOfPorts(true);
89-
variableDefinitions += ')\n';
87+
if ('define_hardware' in this.classMethods) {
88+
variableDefinitions += this.INDENT + "self.define_hardware(";
89+
variableDefinitions += this.getListOfPorts(true);
90+
variableDefinitions += ')\n';
91+
}
9092
if (this.events && Object.keys(this.events).length > 0){
9193
variableDefinitions += this.INDENT + "self.register_events()\n";
9294
}
@@ -129,7 +131,14 @@ export class ExtendedPythonGenerator extends PythonGenerator {
129131
* Add an import statement for a python module.
130132
*/
131133
addImport(importModule: string): void {
132-
this.definitions_['import_' + importModule] = 'import ' + importModule;
134+
const baseClasses = ['RobotBase', 'OpMode', 'Mechanism'];
135+
if (baseClasses.includes(importModule)) {
136+
this.definitions_['import_' + importModule] = 'from blocks_base_classes import ' + importModule;
137+
}
138+
else{
139+
this.definitions_['import_' + importModule] = 'import ' + importModule;
140+
}
141+
133142
}
134143

135144
/**

0 commit comments

Comments
 (0)