-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
669e6fe
commit 9b42bbd
Showing
8 changed files
with
105 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
from functools import cache | ||
from os import chdir | ||
from pathlib import Path | ||
|
||
from bridge import with_toast | ||
from js import window | ||
from pyodide.ffi import create_once_callable, create_proxy | ||
from utils.fs import NativeFS, mount | ||
|
||
|
||
@cache | ||
def _install_slugify(): | ||
@with_toast("installing slugify") | ||
@create_once_callable | ||
async def install_slugify(): | ||
from reasonify.tools.install import pip_install | ||
|
||
await pip_install("python-slugify") | ||
|
||
return install_slugify() | ||
|
||
|
||
mounted: dict[str, NativeFS] = {} | ||
|
||
|
||
@create_proxy | ||
async def mount_native_fs(): | ||
install_promise = _install_slugify() | ||
|
||
handle = await window.showDirectoryPicker() | ||
while await handle.requestPermission({"mode": "readwrite"}) != "granted": | ||
pass | ||
|
||
await install_promise | ||
|
||
from slugify import slugify | ||
|
||
name = slugify(handle.name) | ||
|
||
fs = await mount(path := str(root / name), handle) | ||
|
||
mounted[path] = fs | ||
|
||
return name | ||
|
||
|
||
root = Path("/workspace/mnt") | ||
root.mkdir(parents=True, exist_ok=True) | ||
chdir(root) | ||
sys.path.append(str(root)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from typing import Callable | ||
|
||
from pyodide.webloop import PyodideFuture | ||
|
||
def with_toast[**Params, Return](message: str) -> Callable[[Callable[Params, Return]], Callable[Params, PyodideFuture[Return]]]: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import TYPE_CHECKING, Awaitable, Callable | ||
|
||
from js import FileSystemDirectoryHandle | ||
|
||
if TYPE_CHECKING: | ||
|
||
class NativeFS: | ||
syncfs: Callable[[], Awaitable[None]] | ||
|
||
def mount(target: str, handle: FileSystemDirectoryHandle) -> Awaitable[NativeFS]: ... | ||
def unmount(target: str) -> None: ... | ||
|
||
else: | ||
from pyodide_js import mountNativeFS as mount | ||
from pyodide_js.FS import unmount | ||
|
||
NativeFS = object | ||
|
||
__all__ = ["mount", "unmount", "NativeFS"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { py } from "./load"; | ||
|
||
const cache = new Map<string, any>(); | ||
|
||
export function getApi<T>(slug: string): T { | ||
if (cache.has(slug)) { | ||
return cache.get(slug) as T; | ||
} else { | ||
const api = py.pyimport(slug) as T; | ||
cache.set(slug, api); | ||
return api; | ||
} | ||
} | ||
|
||
export function clearApiCache() { | ||
cache.clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import getGlobals from "./globals"; | ||
import { getApi } from "./api"; | ||
import { withToast } from "$lib/utils/toast"; | ||
|
||
const getMount = getGlobals<() => Promise<string>>("mount_native_fs"); | ||
|
||
export async function mount(): Promise<string> { | ||
const mount = await getMount(); | ||
const mount = getApi<() => Promise<string>>("api.fs.mount_native_fs"); | ||
return withToast("selecting a directory to mount", (name) => (`mounted ${name}`))(mount)(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters