Skip to content

Commit 2315947

Browse files
committed
Moved constants and functions related to modules from common_storage.ts to module.ts.
1 parent 81ad54b commit 2315947

19 files changed

+205
-205
lines changed

src/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import * as editor from './editor/editor';
3838
import { extendedPythonGenerator } from './editor/extended_python_generator';
3939

4040
import * as commonStorage from './storage/common_storage';
41+
import * as storageModule from './storage/module';
4142
import * as clientSideStorage from './storage/client_side_storage';
4243

4344
import * as CustomBlocks from './blocks/setup_custom_blocks';
@@ -154,7 +155,7 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
154155
const { settings, updateLanguage, updateTheme, storage, isLoading } = useUserSettings();
155156

156157
const [alertErrorMessage, setAlertErrorMessage] = React.useState('');
157-
const [currentModule, setCurrentModule] = React.useState<commonStorage.Module | null>(null);
158+
const [currentModule, setCurrentModule] = React.useState<storageModule.Module | null>(null);
158159
const [messageApi, contextHolder] = Antd.message.useMessage();
159160
const [generatedCode, setGeneratedCode] = React.useState<string>('');
160161
const [toolboxSettingsModalIsOpen, setToolboxSettingsModalIsOpen] = React.useState(false);
@@ -340,7 +341,7 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
340341
};
341342

342343
/** Changes current module with automatic saving if modified. */
343-
const changeModule = async (module: commonStorage.Module | null): Promise<void> => {
344+
const changeModule = async (module: storageModule.Module | null): Promise<void> => {
344345
if (currentModule && areBlocksModified()) {
345346
await saveBlocks();
346347
}

src/blocks/mrc_call_python_function.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
3434
import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
3535
import * as toolboxItems from '../toolbox/items';
3636
import * as commonStorage from '../storage/common_storage';
37+
import * as storageModule from '../storage/module';
3738

3839

3940
// A block to call a python function.
@@ -552,7 +553,7 @@ const CALL_PYTHON_FUNCTION = {
552553
let foundComponent = false;
553554
const componentsInScope: commonStorage.Component[] = [];
554555
componentsInScope.push(...this.getComponentsFromRobot());
555-
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
556+
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
556557
componentsInScope.push(...editor.getComponentsFromWorkspace());
557558
}
558559
for (const component of componentsInScope) {
@@ -599,7 +600,7 @@ const CALL_PYTHON_FUNCTION = {
599600
// If the robot method has changed, update the block if possible or put a
600601
// visible warning on it.
601602
if (this.mrcFunctionKind === FunctionKind.INSTANCE_ROBOT) {
602-
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
603+
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
603604
warnings.push('This block is not allowed to be used inside a mechanism.');
604605
} else {
605606
let foundRobotMethod = false;
@@ -645,7 +646,7 @@ const CALL_PYTHON_FUNCTION = {
645646
// If the method has changed, update the block if possible or put a
646647
// visible warning on it.
647648
if (this.mrcFunctionKind === FunctionKind.INSTANCE_MECHANISM) {
648-
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
649+
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
649650
warnings.push('This block is not allowed to be used inside a mechanism.');
650651
} else {
651652
let foundMechanism = false;
@@ -790,11 +791,11 @@ export const pythonFromBlock = function(
790791
: block.getFieldValue(FIELD_FUNCTION_NAME);
791792
// Generate the correct code depending on the module type.
792793
switch (generator.getModuleType()) {
793-
case commonStorage.MODULE_TYPE_ROBOT:
794-
case commonStorage.MODULE_TYPE_MECHANISM:
794+
case storageModule.MODULE_TYPE_ROBOT:
795+
case storageModule.MODULE_TYPE_MECHANISM:
795796
code = 'self.';
796797
break;
797-
case commonStorage.MODULE_TYPE_OPMODE:
798+
case storageModule.MODULE_TYPE_OPMODE:
798799
code = 'self.robot.';
799800
break;
800801
}
@@ -815,13 +816,13 @@ export const pythonFromBlock = function(
815816
: block.getFieldValue(FIELD_FUNCTION_NAME);
816817
// Generate the correct code depending on the module type.
817818
switch (generator.getModuleType()) {
818-
case commonStorage.MODULE_TYPE_ROBOT:
819+
case storageModule.MODULE_TYPE_ROBOT:
819820
code = 'self.' + mechanismName;
820821
break;
821-
case commonStorage.MODULE_TYPE_OPMODE:
822+
case storageModule.MODULE_TYPE_OPMODE:
822823
code = 'self.robot.' + mechanismName;
823824
break;
824-
case commonStorage.MODULE_TYPE_MECHANISM:
825+
case storageModule.MODULE_TYPE_MECHANISM:
825826
// The INSTANCE_MECHANISM version should not be used in a mechanism.
826827
// TODO(lizlooney): What if the user copies a block from an robot or opmode and pastes
827828
// it into a mechanism?

src/blocks/mrc_class_method_def.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { createFieldFlydown } from '../fields/field_flydown';
2626
import { Order } from 'blockly/python';
2727
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2828
import * as commonStorage from '../storage/common_storage';
29+
import * as storageModule from '../storage/module';
2930
import { renameMethodCallers, mutateMethodCallers } from './mrc_call_python_function'
3031
import * as toolboxItems from '../toolbox/items';
3132
import { getClassData } from './utils/python';
@@ -424,7 +425,7 @@ export const pythonFromBlock = function (
424425

425426
let params = block.mrcParameters;
426427
let paramString = "self";
427-
if (generator.getModuleType() == commonStorage.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
428+
if (generator.getModuleType() == storageModule.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
428429
const ports: string[] = generator.getComponentPortParameters();
429430
if (ports.length) {
430431
paramString += ', ' + ports.join(', ');

src/blocks/mrc_component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2929
import { getAllowedTypesForSetCheck, getClassData, getModuleData, getSubclassNames } from './utils/python';
3030
import * as toolboxItems from '../toolbox/items';
3131
import * as commonStorage from '../storage/common_storage';
32+
import * as storageModule from '../storage/module';
3233
import * as storageNames from '../storage/names';
3334
import { createPortShadow } from './mrc_port';
3435
import { createNumberShadowValue } from './utils/value';
@@ -125,7 +126,7 @@ const COMPONENT = {
125126
*/
126127
updateBlock_: function (this: ComponentBlock): void {
127128
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
128-
if (editor && editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_ROBOT) {
129+
if (editor && editor.getCurrentModuleType() === storageModule.MODULE_TYPE_ROBOT) {
129130
// Add input sockets for the arguments.
130131
for (let i = 0; i < this.mrcArgs.length; i++) {
131132
const input = this.appendValueInput('ARG' + i)
@@ -194,7 +195,7 @@ export const pythonFromBlock = function (
194195
if (i != 0) {
195196
code += ', ';
196197
}
197-
if (generator.getModuleType() === commonStorage.MODULE_TYPE_ROBOT) {
198+
if (generator.getModuleType() === storageModule.MODULE_TYPE_ROBOT) {
198199
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, 'ARG' + i, Order.NONE);
199200
} else {
200201
code += block.mrcArgs[i].name + ' = ' + block.getArgName(i);
@@ -247,7 +248,7 @@ function createComponentBlock(
247248
'name': argData.name,
248249
'type': argData.type,
249250
});
250-
if (moduleType == commonStorage.MODULE_TYPE_ROBOT) {
251+
if (moduleType == storageModule.MODULE_TYPE_ROBOT) {
251252
if (argData.type === 'int') {
252253
const portType = getPortTypeForArgument(argData.name);
253254
if (portType) {

src/blocks/mrc_mechanism.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2929
import { getAllowedTypesForSetCheck } from './utils/python';
3030
import * as toolboxItems from '../toolbox/items';
3131
import * as commonStorage from '../storage/common_storage';
32+
import * as storageModule from '../storage/module';
3233
import * as storageNames from '../storage/names';
3334
import * as value from './utils/value';
3435
import { renameMethodCallers } from './mrc_call_python_function'
@@ -172,7 +173,7 @@ const MECHANISM = {
172173
// each module JSON file so we can track mechanisms, etc, even if the name changes.
173174
// Then here, we'd look for the mechanism with the marching UUID, and we'd update the
174175
// FIELD_TYPE value if the mechanism's class name had changed.
175-
let foundMechanism: commonStorage.Mechanism | null = null;
176+
let foundMechanism: storageModule.Mechanism | null = null;
176177
const components: commonStorage.Component[] = []
177178
for (const mechanism of editor.getMechanisms()) {
178179
if (mechanism.className === this.getFieldValue(FIELD_TYPE)) {
@@ -246,7 +247,7 @@ export const pythonFromBlock = function (
246247
}
247248

248249
export function createMechanismBlock(
249-
mechanism: commonStorage.Mechanism, components: commonStorage.Component[]): toolboxItems.Block {
250+
mechanism: storageModule.Mechanism, components: commonStorage.Component[]): toolboxItems.Block {
250251
const snakeCaseName = storageNames.pascalCaseToSnakeCase(mechanism.className);
251252
const mechanismName = 'my_' + snakeCaseName;
252253
const extraState: MechanismExtraState = {

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as ChangeFramework from './utils/change_framework';
2626
import { getLegalName } from './utils/python';
2727
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2828
import * as commonStorage from '../storage/common_storage';
29+
import * as storageModule from '../storage/module';
2930
import { BLOCK_NAME as MRC_MECHANISM_NAME } from './mrc_mechanism';
3031
import { OUTPUT_NAME as MECHANISM_OUTPUT } from './mrc_mechanism';
3132
import { MechanismBlock } from './mrc_mechanism';
@@ -251,10 +252,10 @@ export const pythonFromBlock = function (
251252
block: MechanismComponentHolderBlock,
252253
generator: ExtendedPythonGenerator) {
253254
switch (generator.getModuleType()) {
254-
case commonStorage.MODULE_TYPE_ROBOT:
255+
case storageModule.MODULE_TYPE_ROBOT:
255256
pythonFromBlockInRobot(block, generator);
256257
break;
257-
case commonStorage.MODULE_TYPE_MECHANISM:
258+
case storageModule.MODULE_TYPE_MECHANISM:
258259
pythonFromBlockInMechanism(block, generator);
259260
break;
260261
}

src/editor/editor.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as Blockly from 'blockly/core';
2424
import { extendedPythonGenerator } from './extended_python_generator';
2525
import { GeneratorContext } from './generator_context';
2626
import * as commonStorage from '../storage/common_storage';
27+
import * as storageModule from '../storage/module';
2728
import * as storageNames from '../storage/names';
2829
import * as eventHandler from '../blocks/mrc_event_handler';
2930
import * as classMethodDef from '../blocks/mrc_class_method_def';
@@ -46,7 +47,7 @@ export class Editor {
4647
private blocklyWorkspace: Blockly.WorkspaceSvg;
4748
private generatorContext: GeneratorContext;
4849
private storage: commonStorage.Storage;
49-
private currentModule: commonStorage.Module | null = null;
50+
private currentModule: storageModule.Module | null = null;
5051
private currentProject: commonStorage.Project | null = null;
5152
private modulePath: string = '';
5253
private robotPath: string = '';
@@ -120,7 +121,7 @@ export class Editor {
120121
}
121122

122123
public async loadModuleBlocks(
123-
currentModule: commonStorage.Module | null,
124+
currentModule: storageModule.Module | null,
124125
currentProject: commonStorage.Project | null) {
125126
this.generatorContext.setModule(currentModule);
126127
this.currentModule = currentModule;
@@ -233,7 +234,7 @@ export class Editor {
233234
if (this.currentModule) {
234235
return this.currentModule.moduleType;
235236
}
236-
return commonStorage.MODULE_TYPE_UNKNOWN;
237+
return storageModule.MODULE_TYPE_UNKNOWN;
237238
}
238239

239240
private getModuleContentText(): string {
@@ -249,8 +250,8 @@ export class Editor {
249250
const components: commonStorage.Component[] = this.getComponentsFromWorkspace();
250251
const events: commonStorage.Event[] = this.getEventsFromWorkspace();
251252
const methods: commonStorage.Method[] = (
252-
this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT ||
253-
this.currentModule?.moduleType === commonStorage.MODULE_TYPE_MECHANISM)
253+
this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
254+
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM)
254255
? this.getMethodsForOutsideFromWorkspace()
255256
: [];
256257
return commonStorage.makeModuleContentText(
@@ -259,16 +260,16 @@ export class Editor {
259260

260261
public getMechanismsFromWorkspace(): commonStorage.MechanismInRobot[] {
261262
const mechanisms: commonStorage.MechanismInRobot[] = [];
262-
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT) {
263+
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
263264
mechanismComponentHolder.getMechanisms(this.blocklyWorkspace, mechanisms);
264265
}
265266
return mechanisms;
266267
}
267268

268269
public getComponentsFromWorkspace(): commonStorage.Component[] {
269270
const components: commonStorage.Component[] = [];
270-
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT ||
271-
this.currentModule?.moduleType === commonStorage.MODULE_TYPE_MECHANISM) {
271+
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
272+
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
272273
mechanismComponentHolder.getComponents(this.blocklyWorkspace, components);
273274
}
274275
return components;
@@ -295,8 +296,8 @@ export class Editor {
295296

296297
public getEventsFromWorkspace(): commonStorage.Event[] {
297298
const events: commonStorage.Event[] = [];
298-
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT ||
299-
this.currentModule?.moduleType === commonStorage.MODULE_TYPE_MECHANISM) {
299+
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT ||
300+
this.currentModule?.moduleType === storageModule.MODULE_TYPE_MECHANISM) {
300301
mechanismComponentHolder.getEvents(this.blocklyWorkspace, events);
301302
}
302303
return events;
@@ -322,7 +323,7 @@ export class Editor {
322323
* Returns the mechanisms defined in the robot.
323324
*/
324325
public getMechanismsFromRobot(): commonStorage.MechanismInRobot[] {
325-
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT) {
326+
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
326327
return this.getMechanismsFromWorkspace();
327328
}
328329
if (this.robotContent) {
@@ -335,7 +336,7 @@ export class Editor {
335336
* Returns the components defined in the robot.
336337
*/
337338
public getComponentsFromRobot(): commonStorage.Component[] {
338-
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT) {
339+
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
339340
return this.getComponentsFromWorkspace();
340341
}
341342
if (this.robotContent) {
@@ -348,7 +349,7 @@ export class Editor {
348349
* Returns the events defined in the robot.
349350
*/
350351
public getEventsFromRobot(): commonStorage.Event[] {
351-
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT) {
352+
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
352353
return this.getEventsFromWorkspace();
353354
}
354355
if (this.robotContent) {
@@ -361,7 +362,7 @@ export class Editor {
361362
* Returns the methods defined in the robot.
362363
*/
363364
public getMethodsFromRobot(): commonStorage.Method[] {
364-
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT) {
365+
if (this.currentModule?.moduleType === storageModule.MODULE_TYPE_ROBOT) {
365366
return this.getMethodsForWithinFromWorkspace();
366367
}
367368
if (this.robotContent) {
@@ -373,14 +374,14 @@ export class Editor {
373374
/**
374375
* Returns the mechanisms in this project.
375376
*/
376-
public getMechanisms(): commonStorage.Mechanism[] {
377+
public getMechanisms(): storageModule.Mechanism[] {
377378
return this.currentProject ? this.currentProject.mechanisms : [];
378379
}
379380

380381
/**
381382
* Returns the Mechanism matching the given MechanismInRobot.
382383
*/
383-
public getMechanism(mechanismInRobot: commonStorage.MechanismInRobot): commonStorage.Mechanism | null {
384+
public getMechanism(mechanismInRobot: commonStorage.MechanismInRobot): storageModule.Mechanism | null {
384385
if (this.currentProject) {
385386
for (const mechanism of this.currentProject.mechanisms) {
386387
const fullClassName = storageNames.pascalCaseToSnakeCase(mechanism.className) + '.' + mechanism.className;
@@ -395,7 +396,7 @@ export class Editor {
395396
/**
396397
* Returns the components defined in the given mechanism.
397398
*/
398-
public getComponentsFromMechanism(mechanism: commonStorage.Mechanism): commonStorage.Component[] {
399+
public getComponentsFromMechanism(mechanism: storageModule.Mechanism): commonStorage.Component[] {
399400
if (this.currentModule?.modulePath === mechanism.modulePath) {
400401
return this.getComponentsFromWorkspace();
401402
}
@@ -408,7 +409,7 @@ export class Editor {
408409
/**
409410
* Returns the events defined in the given mechanism.
410411
*/
411-
public getEventsFromMechanism(mechanism: commonStorage.Mechanism): commonStorage.Event[] {
412+
public getEventsFromMechanism(mechanism: storageModule.Mechanism): commonStorage.Event[] {
412413
if (this.currentModule?.modulePath === mechanism.modulePath) {
413414
return this.getEventsFromWorkspace();
414415
}
@@ -421,7 +422,7 @@ export class Editor {
421422
/**
422423
* Returns the methods defined in the given mechanism.
423424
*/
424-
public getMethodsFromMechanism(mechanism: commonStorage.Mechanism): commonStorage.Method[] {
425+
public getMethodsFromMechanism(mechanism: storageModule.Mechanism): commonStorage.Method[] {
425426
if (this.currentModule?.modulePath === mechanism.modulePath) {
426427
return this.getMethodsForWithinFromWorkspace();
427428
}

src/editor/extended_python_generator.ts

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

3434
export class OpModeDetails {
3535
constructor(private name: string, private group : string, private enabled : boolean, private type : string) {}
@@ -112,7 +112,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
112112
generateInitStatements() : string {
113113
let initStatements = '';
114114

115-
if (this.getModuleType() === commonStorage.MODULE_TYPE_MECHANISM && this.hasAnyComponents) {
115+
if (this.getModuleType() === storageModule.MODULE_TYPE_MECHANISM && this.hasAnyComponents) {
116116
initStatements += this.INDENT + 'self.define_hardware(' +
117117
this.getComponentPortParameters().join(', ') + ')\n';
118118
}
@@ -135,7 +135,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
135135

136136
this.hasAnyComponents = false;
137137
this.componentPorts = Object.create(null);
138-
if (this.getModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
138+
if (this.getModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
139139
this.hasAnyComponents = mechanismContainerHolder.hasAnyComponents(this.workspace);
140140
mechanismContainerHolder.getComponentPorts(this.workspace, this.componentPorts);
141141
}

0 commit comments

Comments
 (0)