Skip to content

Commit 5d77dab

Browse files
authored
Generate "from <module> import <name>" import statements (#285)
* Generate "from <module> import <name1>, <name2>, ..." statements. * In python.ts, added function isExistingPythonModule. In ClassNameComponent.tsx, check whether the class name entered by the user would cause a collision with an existing python module.
1 parent 80ea080 commit 5d77dab

13 files changed

+159
-87
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ export function pythonFromBlock(
865865
generator: ExtendedPythonGenerator,
866866
) {
867867
if (block.mrcImportModule) {
868-
generator.addImport(block.mrcImportModule);
868+
generator.importModule(block.mrcImportModule);
869869
}
870870
let code = '';
871871
let needOpenParen = true;

src/blocks/mrc_component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export const pythonFromBlock = function (
246246
generator: ExtendedPythonGenerator,
247247
) {
248248
if (block.mrcImportModule) {
249-
generator.addImport(block.mrcImportModule);
249+
generator.importModule(block.mrcImportModule);
250250
}
251251
let code = 'self.' + block.getFieldValue(FIELD_NAME) + ' = ' + block.getFieldValue(FIELD_TYPE) + "(";
252252

src/blocks/mrc_get_python_enum_value.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const pythonFromBlock = function(
150150
const enumClassName = block.getFieldValue(FIELD_ENUM_CLASS_NAME);
151151
const enumValue = block.getFieldValue(FIELD_ENUM_VALUE);
152152
if (getPythonEnumValueBlock.mrcImportModule) {
153-
generator.addImport(getPythonEnumValueBlock.mrcImportModule);
153+
generator.importModule(getPythonEnumValueBlock.mrcImportModule);
154154
}
155155
const code = enumClassName + '.' + enumValue;
156156
return [code, Order.MEMBER];

src/blocks/mrc_get_python_variable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ export const pythonFromBlock = function(
277277
case VariableKind.MODULE: {
278278
const moduleName = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
279279
if (getPythonVariableBlock.mrcImportModule) {
280-
generator.addImport(getPythonVariableBlock.mrcImportModule);
280+
generator.importModule(getPythonVariableBlock.mrcImportModule);
281281
}
282282
const code = moduleName + '.' + varName;
283283
return [code, Order.MEMBER];
284284
}
285285
case VariableKind.CLASS: {
286286
const className = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
287287
if (getPythonVariableBlock.mrcImportModule) {
288-
generator.addImport(getPythonVariableBlock.mrcImportModule);
288+
generator.importModule(getPythonVariableBlock.mrcImportModule);
289289
}
290290
const code = className + '.' + varName;
291291
return [code, Order.MEMBER];

src/blocks/mrc_mechanism.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export const pythonFromBlock = function (
328328
generator: ExtendedPythonGenerator,
329329
) {
330330
if (block.mrcImportModule) {
331-
generator.addImport(block.mrcImportModule);
331+
generator.importModule(block.mrcImportModule);
332332
}
333333
let code = 'self.' + block.getFieldValue(FIELD_NAME) + ' = ' + block.mrcImportModule + '.' + block.getFieldValue(FIELD_TYPE) + '(';
334334

src/blocks/mrc_port.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const PORT = {
115115
appendFields(this.appendDummyInput(), PORT_TYPE_EXPANSION_HUB_SERVO_PORT, iField++);
116116
break;
117117
default:
118-
throw new Error('Unexpected portType: ' + state.portType)
118+
throw new Error('Unexpected portType: ' + state.portType);
119119
}
120120
this.mrcPortType = state.portType;
121121
this.mrcPortCount = iField;
@@ -129,17 +129,20 @@ export const setup = function () {
129129
export const pythonFromBlock = function (
130130
block: PortBlock,
131131
generator: ExtendedPythonGenerator) {
132-
generator.addImport('port');
133-
132+
134133
const ports: string[] = [];
135134
for (let i = 0; i < block.mrcPortCount; i++) {
136135
ports.push(block.getFieldValue(FIELD_PREFIX_PORT_NUM + i));
137136
}
138137

139-
let code = 'port.';
138+
const portType = generator.importModuleName('port', 'PortType');
139+
const simplePort = generator.importModuleName('port', 'SimplePort');
140+
const compoundPort = (ports.length === 2) ? generator.importModuleName('port', 'CompoundPort') : '';
141+
142+
let code = '';
140143

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

144147
} else if (ports.length === 2) {
145148
let port1Type = 'UNKNOWN';
@@ -159,9 +162,9 @@ export const pythonFromBlock = function (
159162
port2Type = PORT_TYPE_EXPANSION_HUB_SERVO_PORT;
160163
break;
161164
}
162-
code += `CompoundPort(port_type = port.PortType.${block.mrcPortType},\n`;
163-
code += `${generator.INDENT}port1 = port.SimplePort(port_type = port.PortType.${port1Type}, location = ${ports[0]}),\n`;
164-
code += `${generator.INDENT}port2 = port.SimplePort(port_type = port.PortType.${port2Type}, location = ${ports[1]}))`;
165+
code += `${compoundPort}(port_type = ${portType}.${block.mrcPortType},\n`;
166+
code += `${generator.INDENT}port1 = ${simplePort}(port_type = ${portType}.${port1Type}, location = ${ports[0]}),\n`;
167+
code += `${generator.INDENT}port2 = ${simplePort}(port_type = ${portType}.${port2Type}, location = ${ports[1]}))`;
165168
}
166169

167170
return [code, Order.FUNCTION_CALL];

src/blocks/mrc_set_python_variable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export const pythonFromBlock = function(
268268
const moduleName = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
269269
const value = generator.valueToCode(block, 'VALUE', Order.NONE);
270270
if (setPythonVariableBlock.mrcImportModule) {
271-
generator.addImport(setPythonVariableBlock.mrcImportModule);
271+
generator.importModule(setPythonVariableBlock.mrcImportModule);
272272
}
273273
const code = moduleName + '.' + varName + ' = ' + value + '\n';
274274
return code;
@@ -277,7 +277,7 @@ export const pythonFromBlock = function(
277277
const className = block.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
278278
const value = generator.valueToCode(block, 'VALUE', Order.NONE);
279279
if (setPythonVariableBlock.mrcImportModule) {
280-
generator.addImport(setPythonVariableBlock.mrcImportModule);
280+
generator.importModule(setPythonVariableBlock.mrcImportModule);
281281
}
282282
const code = className + '.' + varName + ' = ' + value + '\n';
283283
return code;

src/blocks/utils/python.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,15 @@ export function getLegalName(proposedName: string, existingNames: string[]){
287287
}
288288
return newName;
289289
}
290+
291+
export function isExistingPythonModule(moduleName: string): boolean {
292+
for (const pythonData of allPythonData) {
293+
// Process modules.
294+
for (const moduleData of pythonData.modules) {
295+
if (moduleData.moduleName === moduleName) {
296+
return true;
297+
}
298+
}
299+
}
300+
return false;
301+
}

0 commit comments

Comments
 (0)