Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/blocks/mrc_call_python_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ export function pythonFromBlock(
generator: ExtendedPythonGenerator,
) {
if (block.mrcImportModule) {
generator.addImport(block.mrcImportModule);
generator.importModule(block.mrcImportModule);
}
let code = '';
let needOpenParen = true;
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/mrc_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export const pythonFromBlock = function (
generator: ExtendedPythonGenerator,
) {
if (block.mrcImportModule) {
generator.addImport(block.mrcImportModule);
generator.importModule(block.mrcImportModule);
}
let code = 'self.' + block.getFieldValue(FIELD_NAME) + ' = ' + block.getFieldValue(FIELD_TYPE) + "(";

Expand Down
2 changes: 1 addition & 1 deletion src/blocks/mrc_get_python_enum_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const pythonFromBlock = function(
const enumClassName = block.getFieldValue(FIELD_ENUM_CLASS_NAME);
const enumValue = block.getFieldValue(FIELD_ENUM_VALUE);
if (getPythonEnumValueBlock.mrcImportModule) {
generator.addImport(getPythonEnumValueBlock.mrcImportModule);
generator.importModule(getPythonEnumValueBlock.mrcImportModule);
}
const code = enumClassName + '.' + enumValue;
return [code, Order.MEMBER];
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/mrc_get_python_variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ export const pythonFromBlock = function(
case VariableKind.MODULE: {
const moduleName = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
if (getPythonVariableBlock.mrcImportModule) {
generator.addImport(getPythonVariableBlock.mrcImportModule);
generator.importModule(getPythonVariableBlock.mrcImportModule);
}
const code = moduleName + '.' + varName;
return [code, Order.MEMBER];
}
case VariableKind.CLASS: {
const className = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
if (getPythonVariableBlock.mrcImportModule) {
generator.addImport(getPythonVariableBlock.mrcImportModule);
generator.importModule(getPythonVariableBlock.mrcImportModule);
}
const code = className + '.' + varName;
return [code, Order.MEMBER];
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export const pythonFromBlock = function (
generator: ExtendedPythonGenerator,
) {
if (block.mrcImportModule) {
generator.addImport(block.mrcImportModule);
generator.importModule(block.mrcImportModule);
}
let code = 'self.' + block.getFieldValue(FIELD_NAME) + ' = ' + block.mrcImportModule + '.' + block.getFieldValue(FIELD_TYPE) + '(';

Expand Down
19 changes: 11 additions & 8 deletions src/blocks/mrc_port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const PORT = {
appendFields(this.appendDummyInput(), PORT_TYPE_EXPANSION_HUB_SERVO_PORT, iField++);
break;
default:
throw new Error('Unexpected portType: ' + state.portType)
throw new Error('Unexpected portType: ' + state.portType);
}
this.mrcPortType = state.portType;
this.mrcPortCount = iField;
Expand All @@ -129,17 +129,20 @@ export const setup = function () {
export const pythonFromBlock = function (
block: PortBlock,
generator: ExtendedPythonGenerator) {
generator.addImport('port');


const ports: string[] = [];
for (let i = 0; i < block.mrcPortCount; i++) {
ports.push(block.getFieldValue(FIELD_PREFIX_PORT_NUM + i));
}

let code = 'port.';
const portType = generator.importModuleName('port', 'PortType');
const simplePort = generator.importModuleName('port', 'SimplePort');
const compoundPort = (ports.length === 2) ? generator.importModuleName('port', 'CompoundPort') : '';

let code = '';

if (ports.length === 1) {
code += `SimplePort(port_type = port.PortType.${block.mrcPortType}, location = ${ports[0]})`;
code += `${simplePort}(port_type = ${portType}.${block.mrcPortType}, location = ${ports[0]})`;

} else if (ports.length === 2) {
let port1Type = 'UNKNOWN';
Expand All @@ -159,9 +162,9 @@ export const pythonFromBlock = function (
port2Type = PORT_TYPE_EXPANSION_HUB_SERVO_PORT;
break;
}
code += `CompoundPort(port_type = port.PortType.${block.mrcPortType},\n`;
code += `${generator.INDENT}port1 = port.SimplePort(port_type = port.PortType.${port1Type}, location = ${ports[0]}),\n`;
code += `${generator.INDENT}port2 = port.SimplePort(port_type = port.PortType.${port2Type}, location = ${ports[1]}))`;
code += `${compoundPort}(port_type = ${portType}.${block.mrcPortType},\n`;
code += `${generator.INDENT}port1 = ${simplePort}(port_type = ${portType}.${port1Type}, location = ${ports[0]}),\n`;
code += `${generator.INDENT}port2 = ${simplePort}(port_type = ${portType}.${port2Type}, location = ${ports[1]}))`;
}

return [code, Order.FUNCTION_CALL];
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/mrc_set_python_variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export const pythonFromBlock = function(
const moduleName = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
const value = generator.valueToCode(block, 'VALUE', Order.NONE);
if (setPythonVariableBlock.mrcImportModule) {
generator.addImport(setPythonVariableBlock.mrcImportModule);
generator.importModule(setPythonVariableBlock.mrcImportModule);
}
const code = moduleName + '.' + varName + ' = ' + value + '\n';
return code;
Expand All @@ -277,7 +277,7 @@ export const pythonFromBlock = function(
const className = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
const value = generator.valueToCode(block, 'VALUE', Order.NONE);
if (setPythonVariableBlock.mrcImportModule) {
generator.addImport(setPythonVariableBlock.mrcImportModule);
generator.importModule(setPythonVariableBlock.mrcImportModule);
}
const code = className + '.' + varName + ' = ' + value + '\n';
return code;
Expand Down
12 changes: 12 additions & 0 deletions src/blocks/utils/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,15 @@ export function getLegalName(proposedName: string, existingNames: string[]){
}
return newName;
}

export function isExistingPythonModule(moduleName: string): boolean {
for (const pythonData of allPythonData) {
// Process modules.
for (const moduleData of pythonData.modules) {
if (moduleData.moduleName === moduleName) {
return true;
}
}
}
return false;
}
Loading