Skip to content

Commit

Permalink
feat: refactor sources loading process
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Aug 22, 2024
1 parent 45fa8e6 commit ba69d93
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:
- run: bun install

- name: svelte-check
run: bun check
run: |
echo "export default {} as Record<string, string>;" > reasonify-headless/index.d.ts
bun check
- name: eslint
run: bun lint
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ dist
.idea

/static/whl

index.d.ts
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"rehype-stringify": "^10.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.0",
"rollup-plugin-flatten-dir": "^1.0.1",
"shiki": "^1.12.1",
"svelte": "^4.2.18",
"svelte-check": "^3.8.5",
Expand Down
31 changes: 7 additions & 24 deletions src/lib/py/load.py → reasonify-headless/load.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,24 @@
from asyncio import sleep
from contextlib import suppress
from functools import cache
from os import chdir, getenv
from os import chdir
from pathlib import Path
from typing import TYPE_CHECKING, Awaitable, Callable
from zipfile import BadZipFile

from js import FileSystemDirectoryHandle, window
from micropip import install
from pyodide.ffi import create_once_callable, create_proxy
from pyodide.webloop import PyodideFuture

if TYPE_CHECKING:
sources: dict[str, str] = {}

def with_toast[**Params, Return](message: str) -> Callable[[Callable[Params, Return]], Callable[Params, PyodideFuture[Return]]]: ...


async def get_reasonify_chain():
requirement = getenv("PACKAGE", "reasonify-headless")
if requirement.endswith(".whl"):
r = requirement[requirement.index("reasonify") : requirement.index("-py3-none")].replace("-", "==").replace("_", "-")
else:
r = requirement

@with_toast(f"installing {r}")
@create_once_callable
async def install_reasonify():
while True:
with suppress(BadZipFile):
return await install(requirement)
await sleep(0.1)

await install_reasonify()

from reasonify import chain

return chain
for path, source in sources.items():
file = Path(path)
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
file.write_text(source, "utf-8")


if TYPE_CHECKING:
Expand Down
20 changes: 9 additions & 11 deletions src/lib/py/chain.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { PyProxy } from "pyodide/ffi";

import sources from "../../../reasonify-headless";
import { type PyProxyTo, toJs } from "./common";
import generate from "./generate";
import getGlobals from "./globals";
import { getPy } from "./load";
import requirements from "./requirements";
import { reasonifyReady } from "$lib/stores";
import { startIconAnimation, stopIconAnimation } from "$lib/stores/icon";

Expand All @@ -29,18 +30,15 @@ function asChain(chain: PyProxy) {
export async function initChain() {
startIconAnimation();

const [py, { default: source }] = await Promise.all([
getPy(),
import("./load.py?raw"),
]);
await py.runPythonAsync(source);
const py = await getPy();

const loadReasonify = await getGlobals<() => Promise<PyProxy>>("get_reasonify_chain")();

const chain = await loadReasonify();
reasonifyReady.set(true);
py.globals.set("sources", py.toPy(sources));
await py.pyimport("micropip.install")(requirements);
await py.runPythonAsync(sources["load.py"]);

stopIconAnimation();

return asChain(chain);
reasonifyReady.set(true);

return asChain(py.pyimport("reasonify.chain"));
}
4 changes: 1 addition & 3 deletions src/lib/py/load.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { pyodideReady } from "../stores";
import VERSION from "./version";
import { dev } from "$app/environment";
import * as env from "$env/static/public";
import { cacheSingleton } from "$lib/utils/cache";
Expand All @@ -10,8 +9,7 @@ const indexURL = ("PUBLIC_PYODIDE_INDEX_URL" in env) ? (env.PUBLIC_PYODIDE_INDEX

export const getPy = cacheSingleton(async () => {
const { loadPyodide } = await import("pyodide");
const PACKAGE = dev ? `/whl/reasonify_headless-${VERSION}-py3-none-any.whl` : `reasonify-headless==${VERSION}`;
const py = await loadPyodide({ indexURL, packages: ["micropip", "typing-extensions"], env: { PACKAGE }, args: dev ? [] : ["-O"] });
const py = await loadPyodide({ indexURL, packages: ["micropip", "typing-extensions"], args: dev ? [] : ["-O"] });
pyodideReady.set(true);
py.globals.set("with_toast", withToast);
return py;
Expand Down
7 changes: 7 additions & 0 deletions src/lib/py/requirements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import code from "$lib/../../pyproject.toml?raw";

function getDependencies(toml: string) {
return JSON.parse(/dependencies\s*=\s*(\[[\s\S]*?\])\n/.exec(toml)![1].replace(",\n]", "]")) as string[];
}

export default getDependencies(code);
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { sveltekit } from "@sveltejs/kit/vite";
import flattenDir from "rollup-plugin-flatten-dir";
import Unocss from "unocss/vite";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [Unocss(), sveltekit()],
plugins: [Unocss(), sveltekit(), flattenDir()],
});

0 comments on commit ba69d93

Please sign in to comment.