mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 19:53:00 -05:00
Retain path structure for loading paths (#845)
* update retain dir structure logic to use emscripten FS APIs * remove unused import * replace error message
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { Runtime } from './runtime';
|
||||
import { getLastPath } from './utils';
|
||||
import { getLogger } from './logger';
|
||||
import type { loadPyodide as loadPyodideDeclaration, PyodideInterface } from 'pyodide';
|
||||
// eslint-disable-next-line
|
||||
@@ -91,11 +90,25 @@ export class PyodideRuntime extends Runtime {
|
||||
}
|
||||
|
||||
async loadFromFile(path: string): Promise<void> {
|
||||
const filename = getLastPath(path);
|
||||
const pathArr = path.split('/');
|
||||
const filename = pathArr.pop();
|
||||
for(let i=0; i<pathArr.length; i++)
|
||||
{
|
||||
const eachPath = pathArr.slice(0, i+1).join('/');
|
||||
const {exists, parentExists} = this.interpreter.FS.analyzePath(eachPath);
|
||||
if (!parentExists) {
|
||||
throw new Error(`'INTERNAL ERROR! cannot create ${path}, this should never happen'`)
|
||||
}
|
||||
if (!exists)
|
||||
{
|
||||
this.interpreter.FS.mkdir(eachPath);
|
||||
}
|
||||
}
|
||||
const response = await fetch(path);
|
||||
const buffer = await response.arrayBuffer();
|
||||
const data = new Uint8Array(buffer);
|
||||
const stream = this.interpreter.FS.open(filename, 'w');
|
||||
pathArr.push(filename);
|
||||
const stream = this.interpreter.FS.open(pathArr.join('/'), 'w');
|
||||
this.interpreter.FS.write(stream, data, 0, data.length, 0);
|
||||
this.interpreter.FS.close(stream);
|
||||
}
|
||||
|
||||
@@ -10,10 +10,6 @@ export function removeClasses(element: HTMLElement, classes: string[]) {
|
||||
}
|
||||
}
|
||||
|
||||
export function getLastPath(str: string): string {
|
||||
return str.split('\\').pop().split('/').pop();
|
||||
}
|
||||
|
||||
export function escape(str: string): string {
|
||||
return str.replace(/</g, "<").replace(/>/g, ">")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user