Skip to content

Commit b1b6a53

Browse files
authored
Store modules with double extension: .robot.json, .mechanism.json, .opmode.json. (#203)
* Make an enum for ModuleType. * Updated REGEX_MODULE_PATH to not allow _ or leading lowercase letters. * Change type of moduleType variables from string to storageModule.ModuleType. Add _moduleType parameter to storageNames.makeModulePath. It is currently unused. * Store modules with double extension: .robot.json, .mechanism.json, .opmode.json. Add getModuleType(modulePath) to names.ts. * Fixed download and upload code.
1 parent a0c14c9 commit b1b6a53

20 files changed

+378
-254
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ const CALL_PYTHON_FUNCTION = {
553553
let foundComponent = false;
554554
const componentsInScope: storageModuleContent.Component[] = [];
555555
componentsInScope.push(...this.getComponentsFromRobot());
556-
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
556+
if (editor.getCurrentModuleType() === storageModule.ModuleType.MECHANISM) {
557557
componentsInScope.push(...editor.getComponentsFromWorkspace());
558558
}
559559
for (const component of componentsInScope) {
@@ -600,7 +600,7 @@ const CALL_PYTHON_FUNCTION = {
600600
// If the robot method has changed, update the block if possible or put a
601601
// visible warning on it.
602602
if (this.mrcFunctionKind === FunctionKind.INSTANCE_ROBOT) {
603-
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
603+
if (editor.getCurrentModuleType() === storageModule.ModuleType.MECHANISM) {
604604
warnings.push('This block is not allowed to be used inside a mechanism.');
605605
} else {
606606
let foundRobotMethod = false;
@@ -646,7 +646,7 @@ const CALL_PYTHON_FUNCTION = {
646646
// If the method has changed, update the block if possible or put a
647647
// visible warning on it.
648648
if (this.mrcFunctionKind === FunctionKind.INSTANCE_MECHANISM) {
649-
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
649+
if (editor.getCurrentModuleType() === storageModule.ModuleType.MECHANISM) {
650650
warnings.push('This block is not allowed to be used inside a mechanism.');
651651
} else {
652652
let foundMechanism = false;
@@ -805,11 +805,11 @@ export function pythonFromBlock(
805805
: block.getFieldValue(FIELD_FUNCTION_NAME);
806806
// Generate the correct code depending on the module type.
807807
switch (generator.getModuleType()) {
808-
case storageModule.MODULE_TYPE_ROBOT:
809-
case storageModule.MODULE_TYPE_MECHANISM:
808+
case storageModule.ModuleType.ROBOT:
809+
case storageModule.ModuleType.MECHANISM:
810810
code = 'self.';
811811
break;
812-
case storageModule.MODULE_TYPE_OPMODE:
812+
case storageModule.ModuleType.OPMODE:
813813
code = 'self.robot.';
814814
break;
815815
}
@@ -830,13 +830,13 @@ export function pythonFromBlock(
830830
: block.getFieldValue(FIELD_FUNCTION_NAME);
831831
// Generate the correct code depending on the module type.
832832
switch (generator.getModuleType()) {
833-
case storageModule.MODULE_TYPE_ROBOT:
833+
case storageModule.ModuleType.ROBOT:
834834
code = 'self.' + mechanismName;
835835
break;
836-
case storageModule.MODULE_TYPE_OPMODE:
836+
case storageModule.ModuleType.OPMODE:
837837
code = 'self.robot.' + mechanismName;
838838
break;
839-
case storageModule.MODULE_TYPE_MECHANISM:
839+
case storageModule.ModuleType.MECHANISM:
840840
// The INSTANCE_MECHANISM version should not be used in a mechanism.
841841
// TODO(lizlooney): What if the user copies a block from an robot or opmode and pastes
842842
// it into a mechanism?

src/blocks/mrc_class_method_def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export const pythonFromBlock = function (
422422
// After executing the function body, revisit this block for the return.
423423
xfix2 = xfix1;
424424
}
425-
if (block.mrcPythonMethodName == '__init__') {
425+
if (block.mrcPythonMethodName === '__init__') {
426426
let class_specific = generator.getClassSpecificForInit();
427427
branch = generator.INDENT + 'super().__init__(' + class_specific + ')\n' +
428428
generator.generateInitStatements() + branch;
@@ -440,7 +440,7 @@ export const pythonFromBlock = function (
440440

441441
let params = block.mrcParameters;
442442
let paramString = "self";
443-
if (generator.getModuleType() == storageModule.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
443+
if (generator.getModuleType() === storageModule.ModuleType.MECHANISM && block.mrcPythonMethodName === '__init__') {
444444
const ports: string[] = generator.getComponentPortParameters();
445445
if (ports.length) {
446446
paramString += ', ' + ports.join(', ');

src/blocks/mrc_component.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const COMPONENT = {
130130
*/
131131
updateBlock_: function (this: ComponentBlock): void {
132132
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
133-
if (editor && editor.getCurrentModuleType() === storageModule.MODULE_TYPE_ROBOT) {
133+
if (editor && editor.getCurrentModuleType() === storageModule.ModuleType.ROBOT) {
134134
// Add input sockets for the arguments.
135135
for (let i = 0; i < this.mrcArgs.length; i++) {
136136
const input = this.appendValueInput('ARG' + i)
@@ -207,7 +207,7 @@ export const pythonFromBlock = function (
207207
if (i != 0) {
208208
code += ', ';
209209
}
210-
if (generator.getModuleType() === storageModule.MODULE_TYPE_ROBOT) {
210+
if (generator.getModuleType() === storageModule.ModuleType.ROBOT) {
211211
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, 'ARG' + i, Order.NONE);
212212
} else {
213213
code += block.mrcArgs[i].name + ' = ' + block.getArgName(i);
@@ -217,7 +217,8 @@ export const pythonFromBlock = function (
217217
return code;
218218
}
219219

220-
export function getAllPossibleComponents(moduleType: string): toolboxItems.ContentsType[] {
220+
export function getAllPossibleComponents(
221+
moduleType: storageModule.ModuleType): toolboxItems.ContentsType[] {
221222
const contents: toolboxItems.ContentsType[] = [];
222223
// Iterate through all the components subclasses and add definition blocks.
223224
const componentTypes = getSubclassNames('component.Component');
@@ -244,7 +245,10 @@ export function getAllPossibleComponents(moduleType: string): toolboxItems.Conte
244245
}
245246

246247
function createComponentBlock(
247-
componentName: string, classData: ClassData, staticFunctionData: FunctionData, moduleType: string): toolboxItems.Block {
248+
componentName: string,
249+
classData: ClassData,
250+
staticFunctionData: FunctionData,
251+
moduleType: storageModule.ModuleType): toolboxItems.Block {
248252
const extraState: ComponentExtraState = {
249253
importModule: classData.moduleName,
250254
staticFunctionName: staticFunctionData.functionName,
@@ -260,7 +264,7 @@ function createComponentBlock(
260264
'name': argData.name,
261265
'type': argData.type,
262266
});
263-
if (moduleType == storageModule.MODULE_TYPE_ROBOT) {
267+
if (moduleType == storageModule.ModuleType.ROBOT) {
264268
if (argData.type === 'int') {
265269
const portType = getPortTypeForArgument(argData.name);
266270
if (portType) {

src/blocks/mrc_event_handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ function generateRegisterEventHandler(
393393
fullSender = 'self.' + sender;
394394
} else if (block.mrcSenderType === SenderType.MECHANISM) {
395395
switch (generator.getModuleType()) {
396-
case storageModule.MODULE_TYPE_ROBOT:
396+
case storageModule.ModuleType.ROBOT:
397397
fullSender = 'self.' + sender;
398398
break;
399-
case storageModule.MODULE_TYPE_OPMODE:
399+
case storageModule.ModuleType.OPMODE:
400400
fullSender = 'self.robot.' + sender;
401401
break;
402402
}

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ export const pythonFromBlock = function (
254254
block: MechanismComponentHolderBlock,
255255
generator: ExtendedPythonGenerator) {
256256
switch (generator.getModuleType()) {
257-
case storageModule.MODULE_TYPE_ROBOT:
257+
case storageModule.ModuleType.ROBOT:
258258
pythonFromBlockInRobot(block, generator);
259259
break;
260-
case storageModule.MODULE_TYPE_MECHANISM:
260+
case storageModule.ModuleType.MECHANISM:
261261
pythonFromBlockInMechanism(block, generator);
262262
break;
263263
}

src/editor/editor.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ export class Editor {
227227
return this.blocklyWorkspace;
228228
}
229229

230-
public getCurrentModuleType(): string {
230+
public getCurrentModuleType(): storageModule.ModuleType | null {
231231
if (this.currentModule) {
232232
return this.currentModule.moduleType;
233233
}
234-
return storageModule.MODULE_TYPE_UNKNOWN;
234+
return null;
235235
}
236236

237237
private getModuleContentText(): string {
@@ -248,8 +248,8 @@ export class Editor {
248248
const components: storageModuleContent.Component[] = this.getComponentsFromWorkspace();
249249
const events: storageModuleContent.Event[] = this.getEventsFromWorkspace();
250250
const methods: storageModuleContent.Method[] = (
251-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
252-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM)
251+
this.currentModule?.moduleType === storageModule.ModuleType.ROBOT ||
252+
this.currentModule?.moduleType === storageModule.ModuleType.MECHANISM)
253253
? this.getMethodsForOutsideFromWorkspace()
254254
: [];
255255
return storageModuleContent.makeModuleContentText(
@@ -258,16 +258,16 @@ export class Editor {
258258

259259
public getMechanismsFromWorkspace(): storageModuleContent.MechanismInRobot[] {
260260
const mechanisms: storageModuleContent.MechanismInRobot[] = [];
261-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
261+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
262262
mechanismComponentHolder.getMechanisms(this.blocklyWorkspace, mechanisms);
263263
}
264264
return mechanisms;
265265
}
266266

267267
public getComponentsFromWorkspace(): storageModuleContent.Component[] {
268268
const components: storageModuleContent.Component[] = [];
269-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
270-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
269+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT ||
270+
this.currentModule?.moduleType === storageModule.ModuleType.MECHANISM) {
271271
mechanismComponentHolder.getComponents(this.blocklyWorkspace, components);
272272
}
273273
return components;
@@ -294,8 +294,8 @@ export class Editor {
294294

295295
public getEventsFromWorkspace(): storageModuleContent.Event[] {
296296
const events: storageModuleContent.Event[] = [];
297-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
298-
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
297+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT ||
298+
this.currentModule?.moduleType === storageModule.ModuleType.MECHANISM) {
299299
mechanismComponentHolder.getEvents(this.blocklyWorkspace, events);
300300
}
301301
return events;
@@ -329,7 +329,7 @@ export class Editor {
329329
* Returns the mechanisms defined in the robot.
330330
*/
331331
public getMechanismsFromRobot(): storageModuleContent.MechanismInRobot[] {
332-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
332+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
333333
return this.getMechanismsFromWorkspace();
334334
}
335335
if (this.robotContent) {
@@ -342,7 +342,7 @@ export class Editor {
342342
* Returns the components defined in the robot.
343343
*/
344344
public getComponentsFromRobot(): storageModuleContent.Component[] {
345-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
345+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
346346
return this.getComponentsFromWorkspace();
347347
}
348348
if (this.robotContent) {
@@ -355,7 +355,7 @@ export class Editor {
355355
* Returns the events defined in the robot.
356356
*/
357357
public getEventsFromRobot(): storageModuleContent.Event[] {
358-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
358+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
359359
return this.getEventsFromWorkspace();
360360
}
361361
if (this.robotContent) {
@@ -368,7 +368,7 @@ export class Editor {
368368
* Returns the methods defined in the robot.
369369
*/
370370
public getMethodsFromRobot(): storageModuleContent.Method[] {
371-
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
371+
if (this.currentModule?.moduleType === storageModule.ModuleType.ROBOT) {
372372
return this.getMethodsForWithinFromWorkspace();
373373
}
374374
if (this.robotContent) {

src/editor/extended_python_generator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
111111
generateInitStatements() : string {
112112
let initStatements = '';
113113

114-
if (this.getModuleType() === storageModule.MODULE_TYPE_MECHANISM && this.hasAnyComponents) {
114+
if (this.getModuleType() === storageModule.ModuleType.MECHANISM && this.hasAnyComponents) {
115115
initStatements += this.INDENT + 'self.define_hardware(' +
116116
this.getComponentPortParameters().join(', ') + ')\n';
117117
}
@@ -134,7 +134,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
134134

135135
this.hasAnyComponents = false;
136136
this.componentPorts = Object.create(null);
137-
if (this.getModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
137+
if (this.getModuleType() === storageModule.ModuleType.MECHANISM) {
138138
this.hasAnyComponents = mechanismContainerHolder.hasAnyComponents(this.workspace);
139139
mechanismContainerHolder.getComponentPorts(this.workspace, this.componentPorts);
140140
}
@@ -147,7 +147,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
147147
return code;
148148
}
149149

150-
getModuleType(): string | null {
150+
getModuleType(): storageModule.ModuleType | null {
151151
if (this.context) {
152152
return this.context.getModuleType();
153153
}

src/editor/generator_context.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class GeneratorContext {
3434
this.module = module;
3535
}
3636

37-
getModuleType(): string | null {
37+
getModuleType(): storageModule.ModuleType | null {
3838
if (this.module) {
3939
return this.module.moduleType;
4040
}
@@ -52,14 +52,13 @@ export class GeneratorContext {
5252
if (!this.module) {
5353
throw new Error('getParentClassName: this.module is null.');
5454
}
55-
if (this.module.moduleType === storageModule.MODULE_TYPE_ROBOT) {
56-
return CLASS_NAME_ROBOT_BASE;
57-
}
58-
if (this.module.moduleType === storageModule.MODULE_TYPE_OPMODE) {
59-
return CLASS_NAME_OPMODE;
60-
}
61-
if (this.module.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
62-
return CLASS_NAME_MECHANISM;
55+
switch (this.module.moduleType) {
56+
case storageModule.ModuleType.ROBOT:
57+
return CLASS_NAME_ROBOT_BASE;
58+
case storageModule.ModuleType.OPMODE:
59+
return CLASS_NAME_OPMODE;
60+
case storageModule.ModuleType.MECHANISM:
61+
return CLASS_NAME_MECHANISM;
6362
}
6463
return '';
6564
}

src/reactComponents/AddTabDialog.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ export default function AddTabDialog(props: AddTabDialogProps) {
129129
return;
130130
}
131131

132-
const storageType = tabType === TabType.MECHANISM ?
133-
storageModule.MODULE_TYPE_MECHANISM :
134-
storageModule.MODULE_TYPE_OPMODE;
132+
const moduleType = (tabType === TabType.MECHANISM)
133+
? storageModule.ModuleType.MECHANISM
134+
: storageModule.ModuleType.OPMODE;
135135

136136
await storageProject.addModuleToProject(
137-
props.storage, props.project, storageType, newClassName);
137+
props.storage, props.project, moduleType, newClassName);
138138

139139
const newModule = storageProject.findModuleByClassName(props.project, newClassName);
140140
if (newModule) {

0 commit comments

Comments
 (0)