Skip to content

Commit

Permalink
refactor(explorer): update file import handling and improve supported…
Browse files Browse the repository at this point in the history
… extensions
  • Loading branch information
afarago committed Feb 9, 2025
1 parent 185bf25 commit fe10439
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/explorer/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022-2024 The Pybricks Authors
// Copyright (c) 2022-2025 The Pybricks Authors

import { PyConverterOptions, convertFlipperProjectToPython } from 'blocklypy';
import { convertProjectToPython, supportedExtensions } from 'blocklypy';

Check failure on line 4 in src/explorer/sagas.ts

View workflow job for this annotation

GitHub Actions / test

'"blocklypy"' has no exported member named 'convertProjectToPython'. Did you mean 'convertFlipperProjectToPython'?

Check failure on line 4 in src/explorer/sagas.ts

View workflow job for this annotation

GitHub Actions / test

Module '"blocklypy"' has no exported member 'supportedExtensions'.
import { fileOpen, fileSave } from 'browser-fs-access';
import JSZip from 'jszip';
import {
Expand All @@ -14,11 +14,7 @@ import {
takeEvery,
} from 'typed-redux-saga/macro';
import { alertsShowAlert } from '../alerts/actions';
import {
legoBlocklyFileExtensions,
zipFileExtension,
zipFileMimeType,
} from '../app/constants';
import { zipFileExtension, zipFileMimeType } from '../app/constants';
import {
editorActivateFile,
editorCloseFile,
Expand Down Expand Up @@ -349,7 +345,7 @@ function* handleExplorerImportFiles(): Generator {
extensions: [
pythonFileExtension,
zipFileExtension,
...legoBlocklyFileExtensions,
...supportedExtensions(),
],
description: 'Supported Files (Python, ZIP, LEGO Blockly)',
multiple: true,
Expand All @@ -370,19 +366,24 @@ function* handleExplorerImportFiles(): Generator {
: '';
let text;
let filename = file.name;
if (legoBlocklyFileExtensions.includes(extension)) {
if (extension === pythonFileExtension) {
// getting the text now to catch possible error *before* user interaction
text = yield* call(() => file.text());
} else {
// if (supportedExtensions().includes(extension)) {
const arraybuffer = yield* call(() => file.arrayBuffer());
const inputfiles = [
{ name: filename, buffer: arraybuffer },
];
const result = yield* call(() =>
convertFlipperProjectToPython(arraybuffer, {
debug: { showExplainingComments: true },
} as PyConverterOptions),
convertProjectToPython(inputfiles, {}),
);
// console.log(result);
text = result.pycode ?? '';
filename = filename.replace(/[^a-zA-Z0-9_]/gi, '_') + '.py';
} else {
// getting the text now to catch possible error *before* user interaction
text = yield* call(() => file.text());
if (Array.isArray(result.pycode)) {

Check failure on line 381 in src/explorer/sagas.ts

View workflow job for this annotation

GitHub Actions / test

'result' is of type 'unknown'.
text = result.pycode?.join('\n');

Check failure on line 382 in src/explorer/sagas.ts

View workflow job for this annotation

GitHub Actions / test

'result' is of type 'unknown'.
} else {
text = result.pycode ?? '';

Check failure on line 384 in src/explorer/sagas.ts

View workflow job for this annotation

GitHub Actions / test

'result' is of type 'unknown'.
}
filename = filename.replace('.', '_') + '.py';
}
yield* importPythonFile(filename, text, context);
}
Expand Down

0 comments on commit fe10439

Please sign in to comment.