gradio

Форк
0
60 строк · 1.5 Кб
1
import path from "path-browserify";
2
import type { PyodideInterface } from "pyodide";
3

4
export const globalHomeDir = "/home/pyodide";
5
export const getAppHomeDir = (appId: string): string =>
6
	`${globalHomeDir}/${appId}`;
7
export const resolveAppHomeBasedPath = (
8
	appId: string,
9
	filePath: string
10
): string => {
11
	const normalized = path.normalize(filePath);
12
	return path.resolve(getAppHomeDir(appId), filePath);
13
};
14

15
function ensureParent(pyodide: PyodideInterface, filePath: string): void {
16
	const normalized = path.normalize(filePath);
17

18
	const dirPath = path.dirname(normalized);
19

20
	const dirNames = dirPath.split("/");
21

22
	const chDirNames: string[] = [];
23
	for (const dirName of dirNames) {
24
		chDirNames.push(dirName);
25
		const dirPath = chDirNames.join("/");
26

27
		if (pyodide.FS.analyzePath(dirPath).exists) {
28
			if (pyodide.FS.isDir(dirPath)) {
29
				throw new Error(`"${dirPath}" already exists and is not a directory.`);
30
			}
31
			continue;
32
		}
33

34
		try {
35
			pyodide.FS.mkdir(dirPath);
36
		} catch (err) {
37
			console.error(`Failed to create a directory "${dirPath}"`);
38
			throw err;
39
		}
40
	}
41
}
42

43
export function writeFileWithParents(
44
	pyodide: PyodideInterface,
45
	filePath: string,
46
	data: string | ArrayBufferView,
47
	opts?: Parameters<PyodideInterface["FS"]["writeFile"]>[2]
48
): void {
49
	ensureParent(pyodide, filePath);
50
	pyodide.FS.writeFile(filePath, data, opts);
51
}
52

53
export function renameWithParents(
54
	pyodide: PyodideInterface,
55
	oldPath: string,
56
	newPath: string
57
): void {
58
	ensureParent(pyodide, newPath);
59
	pyodide.FS.rename(oldPath, newPath);
60
}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.