magiplace 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- magiplace-0.1.0/PKG-INFO +50 -0
- magiplace-0.1.0/README.md +36 -0
- magiplace-0.1.0/pyproject.toml +86 -0
- magiplace-0.1.0/pyproject.toml.orig +60 -0
- magiplace-0.1.0/src/magiplace/__init__.py +5 -0
- magiplace-0.1.0/src/magiplace/auth.py +79 -0
- magiplace-0.1.0/src/magiplace/automations.py +91 -0
- magiplace-0.1.0/src/magiplace/boot.py +124 -0
- magiplace-0.1.0/src/magiplace/cli.py +179 -0
- magiplace-0.1.0/src/magiplace/contract.py +180 -0
- magiplace-0.1.0/src/magiplace/desktop.py +118 -0
- magiplace-0.1.0/src/magiplace/events.py +241 -0
- magiplace-0.1.0/src/magiplace/host.py +92 -0
- magiplace-0.1.0/src/magiplace/inline_views.py +1153 -0
- magiplace-0.1.0/src/magiplace/liveshare.py +138 -0
- magiplace-0.1.0/src/magiplace/liveshare_viewer.py +151 -0
- magiplace-0.1.0/src/magiplace/marketplace.py +314 -0
- magiplace-0.1.0/src/magiplace/mcp_accounts.py +152 -0
- magiplace-0.1.0/src/magiplace/pages.py +407 -0
- magiplace-0.1.0/src/magiplace/providers.py +129 -0
- magiplace-0.1.0/src/magiplace/settings_modal.py +699 -0
- magiplace-0.1.0/src/magiplace/shell_controller.py +1615 -0
- magiplace-0.1.0/src/magiplace/shortcuts_modal.py +330 -0
- magiplace-0.1.0/src/magiplace/startup.py +146 -0
- magiplace-0.1.0/src/magiplace/web_app.py +77 -0
magiplace-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: magiplace
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: NiceGUI Web and Desktop hosts for Magimoire
|
|
5
|
+
Requires-Dist: animas>=2.1,<3
|
|
6
|
+
Requires-Dist: magimoire>=0.3.0
|
|
7
|
+
Requires-Dist: magirunes>=0.3.0
|
|
8
|
+
Requires-Dist: nicegui>=3.18.0
|
|
9
|
+
Requires-Dist: nicegui-ai>=0.2.0
|
|
10
|
+
Requires-Dist: httpx>=0.27.0
|
|
11
|
+
Requires-Dist: python-dotenv>=1.0
|
|
12
|
+
Requires-Python: >=3.13
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<picture>
|
|
17
|
+
<source
|
|
18
|
+
media="(prefers-color-scheme: dark)"
|
|
19
|
+
srcset="https://raw.githubusercontent.com/magitech-ai/.github/main/brand/collateral/readme-banner-dark.svg"
|
|
20
|
+
/>
|
|
21
|
+
<source
|
|
22
|
+
media="(prefers-color-scheme: light)"
|
|
23
|
+
srcset="https://raw.githubusercontent.com/magitech-ai/.github/main/brand/collateral/readme-banner-light.svg"
|
|
24
|
+
/>
|
|
25
|
+
<img
|
|
26
|
+
alt="Magitech"
|
|
27
|
+
src="https://raw.githubusercontent.com/magitech-ai/.github/main/brand/collateral/readme-banner-light.svg"
|
|
28
|
+
width="100%"
|
|
29
|
+
/>
|
|
30
|
+
</picture>
|
|
31
|
+
</p>
|
|
32
|
+
|
|
33
|
+
# magiplace
|
|
34
|
+
|
|
35
|
+
NiceGUI Web and Desktop hosts for Magimoire. Owns PARITY rows 120–127
|
|
36
|
+
under ADR 0025 decision 2. Depends on `magimoire` and the shared
|
|
37
|
+
`nicegui-ai` component layer; the harness never imports this package.
|
|
38
|
+
|
|
39
|
+
## Entry points
|
|
40
|
+
|
|
41
|
+
- `magi-web` — Web host (`--host`, `--port`, `--public-url`, `--trusted-host`,
|
|
42
|
+
`--tls-cert`/`--tls-key`, `--no-open`).
|
|
43
|
+
- `magi-desktop` — Desktop host (`--host`, `--port`, `--window-size`,
|
|
44
|
+
`--no-frameless`, `--no-update-check`).
|
|
45
|
+
|
|
46
|
+
Both hosts use one explicit, versioned host-facing contract
|
|
47
|
+
(`HostContract`): every call carries a `Principal`, unknown methods fail
|
|
48
|
+
closed, and the Desktop host additionally requires its in-memory owner
|
|
49
|
+
credential. The Plugins page is backed by the `magirunes` marketplace
|
|
50
|
+
with Notion as the only ported connector.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source
|
|
4
|
+
media="(prefers-color-scheme: dark)"
|
|
5
|
+
srcset="https://raw.githubusercontent.com/magitech-ai/.github/main/brand/collateral/readme-banner-dark.svg"
|
|
6
|
+
/>
|
|
7
|
+
<source
|
|
8
|
+
media="(prefers-color-scheme: light)"
|
|
9
|
+
srcset="https://raw.githubusercontent.com/magitech-ai/.github/main/brand/collateral/readme-banner-light.svg"
|
|
10
|
+
/>
|
|
11
|
+
<img
|
|
12
|
+
alt="Magitech"
|
|
13
|
+
src="https://raw.githubusercontent.com/magitech-ai/.github/main/brand/collateral/readme-banner-light.svg"
|
|
14
|
+
width="100%"
|
|
15
|
+
/>
|
|
16
|
+
</picture>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
# magiplace
|
|
20
|
+
|
|
21
|
+
NiceGUI Web and Desktop hosts for Magimoire. Owns PARITY rows 120–127
|
|
22
|
+
under ADR 0025 decision 2. Depends on `magimoire` and the shared
|
|
23
|
+
`nicegui-ai` component layer; the harness never imports this package.
|
|
24
|
+
|
|
25
|
+
## Entry points
|
|
26
|
+
|
|
27
|
+
- `magi-web` — Web host (`--host`, `--port`, `--public-url`, `--trusted-host`,
|
|
28
|
+
`--tls-cert`/`--tls-key`, `--no-open`).
|
|
29
|
+
- `magi-desktop` — Desktop host (`--host`, `--port`, `--window-size`,
|
|
30
|
+
`--no-frameless`, `--no-update-check`).
|
|
31
|
+
|
|
32
|
+
Both hosts use one explicit, versioned host-facing contract
|
|
33
|
+
(`HostContract`): every call carries a `Principal`, unknown methods fail
|
|
34
|
+
closed, and the Desktop host additionally requires its in-memory owner
|
|
35
|
+
credential. The Plugins page is backed by the `magirunes` marketplace
|
|
36
|
+
with Notion as the only ported connector.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "magiplace"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "NiceGUI Web and Desktop hosts for Magimoire"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.13"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"animas>=2.1,<3",
|
|
9
|
+
"magimoire>=0.3.0",
|
|
10
|
+
"magirunes>=0.3.0",
|
|
11
|
+
"nicegui>=3.18.0",
|
|
12
|
+
"nicegui-ai>=0.2.0",
|
|
13
|
+
"httpx>=0.27.0",
|
|
14
|
+
"python-dotenv>=1.0",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
magi-web = "magiplace.cli:web_main"
|
|
19
|
+
magi-desktop = "magiplace.cli:desktop_main"
|
|
20
|
+
|
|
21
|
+
[tool.uv.sources.animas]
|
|
22
|
+
path = "../animas"
|
|
23
|
+
editable = true
|
|
24
|
+
|
|
25
|
+
[tool.uv.sources.magimoire]
|
|
26
|
+
path = "../magimoire"
|
|
27
|
+
editable = true
|
|
28
|
+
|
|
29
|
+
[tool.uv.sources.magirunes]
|
|
30
|
+
path = "../magirunes"
|
|
31
|
+
editable = true
|
|
32
|
+
|
|
33
|
+
[tool.uv.sources.nicegui-ai]
|
|
34
|
+
path = "../nicegui-ai"
|
|
35
|
+
editable = true
|
|
36
|
+
|
|
37
|
+
[tool.pytest.ini_options]
|
|
38
|
+
asyncio_mode = "auto"
|
|
39
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
40
|
+
addopts = "--import-mode=importlib --cov=magiplace --cov-branch --cov-report=term-missing --cov-fail-under=100"
|
|
41
|
+
|
|
42
|
+
[tool.mypy]
|
|
43
|
+
python_version = "3.13"
|
|
44
|
+
strict = true
|
|
45
|
+
warn_unreachable = true
|
|
46
|
+
exclude = ["tests/"]
|
|
47
|
+
|
|
48
|
+
[tool.ruff]
|
|
49
|
+
line-length = 88
|
|
50
|
+
target-version = "py313"
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint]
|
|
53
|
+
select = [
|
|
54
|
+
"E",
|
|
55
|
+
"W",
|
|
56
|
+
"F",
|
|
57
|
+
"I",
|
|
58
|
+
"UP",
|
|
59
|
+
"B",
|
|
60
|
+
"SIM",
|
|
61
|
+
]
|
|
62
|
+
ignore = [
|
|
63
|
+
"E501",
|
|
64
|
+
"SIM117",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
[tool.coverage.report]
|
|
68
|
+
exclude_lines = [
|
|
69
|
+
"if TYPE_CHECKING:",
|
|
70
|
+
"pragma: no cover",
|
|
71
|
+
"@abstractmethod",
|
|
72
|
+
'^\s*\.\.\.\s*$',
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
[build-system]
|
|
76
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
77
|
+
build-backend = "uv_build"
|
|
78
|
+
|
|
79
|
+
[dependency-groups]
|
|
80
|
+
dev = [
|
|
81
|
+
"mypy>=1.15.0",
|
|
82
|
+
"pytest>=9.2.0",
|
|
83
|
+
"pytest-asyncio>=1.4.1.post1",
|
|
84
|
+
"pytest-cov>=7.1.0",
|
|
85
|
+
"ruff>=0.17.4",
|
|
86
|
+
]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "magiplace"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "NiceGUI Web and Desktop hosts for Magimoire"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.13"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"animas>=2.1,<3",
|
|
9
|
+
"magimoire>=0.3.0",
|
|
10
|
+
"magirunes>=0.3.0",
|
|
11
|
+
"nicegui>=3.18.0",
|
|
12
|
+
"nicegui-ai>=0.2.0",
|
|
13
|
+
"httpx>=0.27.0",
|
|
14
|
+
"python-dotenv>=1.0",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
magi-web = "magiplace.cli:web_main"
|
|
19
|
+
magi-desktop = "magiplace.cli:desktop_main"
|
|
20
|
+
|
|
21
|
+
[tool.uv.sources]
|
|
22
|
+
animas = { path = "../animas", editable = true }
|
|
23
|
+
magimoire = { path = "../magimoire", editable = true }
|
|
24
|
+
magirunes = { path = "../magirunes", editable = true }
|
|
25
|
+
nicegui-ai = { path = "../nicegui-ai", editable = true }
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
29
|
+
build-backend = "uv_build"
|
|
30
|
+
|
|
31
|
+
[dependency-groups]
|
|
32
|
+
dev = [
|
|
33
|
+
"mypy>=1.15.0",
|
|
34
|
+
"pytest>=9.2.0",
|
|
35
|
+
"pytest-asyncio>=1.4.1.post1",
|
|
36
|
+
"pytest-cov>=7.1.0",
|
|
37
|
+
"ruff>=0.17.4",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[tool.pytest.ini_options]
|
|
41
|
+
asyncio_mode = "auto"
|
|
42
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
43
|
+
addopts = "--import-mode=importlib --cov=magiplace --cov-branch --cov-report=term-missing --cov-fail-under=100"
|
|
44
|
+
|
|
45
|
+
[tool.mypy]
|
|
46
|
+
python_version = "3.13"
|
|
47
|
+
strict = true
|
|
48
|
+
warn_unreachable = true
|
|
49
|
+
exclude = ["tests/"]
|
|
50
|
+
|
|
51
|
+
[tool.ruff]
|
|
52
|
+
line-length = 88
|
|
53
|
+
target-version = "py313"
|
|
54
|
+
|
|
55
|
+
[tool.ruff.lint]
|
|
56
|
+
select = ["E", "W", "F", "I", "UP", "B", "SIM"]
|
|
57
|
+
ignore = ["E501", "SIM117"]
|
|
58
|
+
|
|
59
|
+
[tool.coverage.report]
|
|
60
|
+
exclude_lines = ["if TYPE_CHECKING:", "pragma: no cover", "@abstractmethod", "^\\s*\\.\\.\\.\\s*$"]
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Desktop and Web credential verification helpers for magiplace."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hmac
|
|
6
|
+
import re
|
|
7
|
+
import secrets
|
|
8
|
+
from collections.abc import Callable, Mapping
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from hashlib import sha256
|
|
11
|
+
|
|
12
|
+
COOKIE_NAME = "mth-desktop-session"
|
|
13
|
+
WEB_COOKIE_NAME = "mth-web-session"
|
|
14
|
+
SECRET_RE = re.compile(r"^[A-Za-z0-9_-]{43}$")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def make_secret() -> str:
|
|
18
|
+
"""Create one 43-character urlsafe startup credential."""
|
|
19
|
+
return secrets.token_urlsafe(32)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class DesktopAuth:
|
|
23
|
+
"""Verify the owner credential supplied on a host request."""
|
|
24
|
+
|
|
25
|
+
def __init__(self, read_secret: Callable[[], str]) -> None:
|
|
26
|
+
self.read_secret = read_secret
|
|
27
|
+
|
|
28
|
+
def authenticate(self, cookie_header: str | None) -> bool:
|
|
29
|
+
"""Return True only for one well-formed, matching owner cookie."""
|
|
30
|
+
if not isinstance(cookie_header, str):
|
|
31
|
+
return False
|
|
32
|
+
value: str | None = None
|
|
33
|
+
for part in cookie_header.split(";"):
|
|
34
|
+
key, separator, raw = part.partition("=")
|
|
35
|
+
if not separator or key.strip() != COOKIE_NAME:
|
|
36
|
+
continue
|
|
37
|
+
if value is not None:
|
|
38
|
+
return False
|
|
39
|
+
value = raw.strip()
|
|
40
|
+
if value is None or not SECRET_RE.fullmatch(value):
|
|
41
|
+
return False
|
|
42
|
+
return hmac.compare_digest(value, self.read_secret())
|
|
43
|
+
|
|
44
|
+
def public_view(self) -> dict[str, str]:
|
|
45
|
+
"""Renderer-safe descriptor that never carries the credential."""
|
|
46
|
+
return {
|
|
47
|
+
"kind": "desktop-owner",
|
|
48
|
+
"cookie": COOKIE_NAME,
|
|
49
|
+
"credential": "<redacted>",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True)
|
|
54
|
+
class WebAuth:
|
|
55
|
+
"""Tokenized startup authentication gate matching `dsh web` security model."""
|
|
56
|
+
|
|
57
|
+
token: str = field(default_factory=make_secret)
|
|
58
|
+
|
|
59
|
+
def is_valid_token(self, candidate: str | None) -> bool:
|
|
60
|
+
"""Verify an incoming query or form startup token."""
|
|
61
|
+
if not candidate:
|
|
62
|
+
return False
|
|
63
|
+
return hmac.compare_digest(candidate, self.token)
|
|
64
|
+
|
|
65
|
+
def cookie_value(self) -> str:
|
|
66
|
+
"""Return the HttpOnly cookie digest exchanged for a valid token."""
|
|
67
|
+
return sha256(f"web-cookie:{self.token}".encode()).hexdigest()
|
|
68
|
+
|
|
69
|
+
def is_authorized(self, cookies: Mapping[str, str]) -> bool:
|
|
70
|
+
"""Validate the active browser session cookie."""
|
|
71
|
+
presented = cookies.get(WEB_COOKIE_NAME)
|
|
72
|
+
if not presented:
|
|
73
|
+
return False
|
|
74
|
+
return hmac.compare_digest(presented, self.cookie_value())
|
|
75
|
+
|
|
76
|
+
def entry_url(self, base_url: str) -> str:
|
|
77
|
+
"""Attach the startup `?token=...` query parameter to a root URL."""
|
|
78
|
+
sep = "&" if "?" in base_url else "?"
|
|
79
|
+
return f"{base_url}{sep}token={self.token}"
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Host-backed automations over the contract's ``schedule/*`` surface.
|
|
2
|
+
|
|
3
|
+
Active only when the transport exposes controllers with a mounted schedule
|
|
4
|
+
service (a ``magimoire.schedule.engine:ScheduleService`` profile row);
|
|
5
|
+
otherwise the Automations page shows an honest unavailable state instead of a
|
|
6
|
+
local-only list that silently persists to UI settings.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from magiplace.contract import HostContract, Principal
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class AutomationsHost:
|
|
17
|
+
"""Host-backed schedule model over the ``schedule/*`` methods."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, contract: HostContract, principal: Principal) -> None:
|
|
20
|
+
self.contract = contract
|
|
21
|
+
self.principal = principal
|
|
22
|
+
self.tasks: list[dict[str, Any]] | None = None
|
|
23
|
+
|
|
24
|
+
async def refresh(self) -> list[dict[str, Any]]:
|
|
25
|
+
result = await self.contract.invoke(self.principal, "schedule.list")
|
|
26
|
+
tasks = result.get("tasks", []) if isinstance(result, dict) else []
|
|
27
|
+
self.tasks = [dict(task) for task in tasks if isinstance(task, dict)]
|
|
28
|
+
return self.tasks
|
|
29
|
+
|
|
30
|
+
async def create(
|
|
31
|
+
self, name: str, instruction: str, cron: str, timezone: str
|
|
32
|
+
) -> dict[str, Any]:
|
|
33
|
+
timing: dict[str, Any] = {"kind": "cron", "expression": cron}
|
|
34
|
+
if timezone:
|
|
35
|
+
timing["time_zone"] = timezone
|
|
36
|
+
result = await self.contract.invoke(
|
|
37
|
+
self.principal,
|
|
38
|
+
"schedule.create",
|
|
39
|
+
{"name": name, "instruction": instruction, "timing": timing},
|
|
40
|
+
)
|
|
41
|
+
return dict(result.get("task", {})) if isinstance(result, dict) else {}
|
|
42
|
+
|
|
43
|
+
async def update(
|
|
44
|
+
self, task_id: str, title: str, instruction: str, cron: str, timezone: str
|
|
45
|
+
) -> dict[str, Any]:
|
|
46
|
+
timing: dict[str, Any] = {"kind": "cron", "expression": cron}
|
|
47
|
+
if timezone:
|
|
48
|
+
timing["time_zone"] = timezone
|
|
49
|
+
result = await self.contract.invoke(
|
|
50
|
+
self.principal,
|
|
51
|
+
"schedule.edit",
|
|
52
|
+
{
|
|
53
|
+
"taskId": task_id,
|
|
54
|
+
"action": "update",
|
|
55
|
+
"patch": {"title": title, "prompt": instruction, "timing": timing},
|
|
56
|
+
},
|
|
57
|
+
)
|
|
58
|
+
return dict(result.get("task", {})) if isinstance(result, dict) else {}
|
|
59
|
+
|
|
60
|
+
async def set_active(self, task_id: str, active: bool) -> dict[str, Any]:
|
|
61
|
+
result = await self.contract.invoke(
|
|
62
|
+
self.principal,
|
|
63
|
+
"schedule.edit",
|
|
64
|
+
{"taskId": task_id, "action": "resume" if active else "pause"},
|
|
65
|
+
)
|
|
66
|
+
return dict(result.get("task", {})) if isinstance(result, dict) else {}
|
|
67
|
+
|
|
68
|
+
async def delete(self, task_id: str) -> dict[str, Any]:
|
|
69
|
+
result = await self.contract.invoke(
|
|
70
|
+
self.principal,
|
|
71
|
+
"schedule.edit",
|
|
72
|
+
{"taskId": task_id, "action": "delete"},
|
|
73
|
+
)
|
|
74
|
+
return dict(result) if isinstance(result, dict) else {}
|
|
75
|
+
|
|
76
|
+
async def run(self, task_id: str) -> dict[str, Any]:
|
|
77
|
+
result = await self.contract.invoke(
|
|
78
|
+
self.principal, "schedule.run", {"taskId": task_id}
|
|
79
|
+
)
|
|
80
|
+
return dict(result) if isinstance(result, dict) else {}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def automations_host_from_transport(
|
|
84
|
+
contract: HostContract, principal: Principal
|
|
85
|
+
) -> AutomationsHost | None:
|
|
86
|
+
"""Build a host automations model when the transport owns a schedule."""
|
|
87
|
+
controllers = getattr(contract.transport, "controllers", None)
|
|
88
|
+
ctx = getattr(controllers, "ctx", None)
|
|
89
|
+
if ctx is None or ctx.get("schedule", None, strict=False) is None:
|
|
90
|
+
return None
|
|
91
|
+
return AutomationsHost(contract, principal)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""Two-stage client boot kernel with a module table and activation audit.
|
|
2
|
+
|
|
3
|
+
The dsh browser boot kernel is re-expressed in Python: immediate-tier prefetch,
|
|
4
|
+
concurrent activation, and a required-entry audit before renderer handoff.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
from collections.abc import Awaitable, Callable, Mapping, Sequence
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
|
|
13
|
+
Loader = Callable[[], Awaitable[object]]
|
|
14
|
+
Prefetch = Callable[[str], Awaitable[None]]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class BootError(Exception):
|
|
18
|
+
"""Boot failed with a stable machine code."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, code: str, message: str) -> None:
|
|
21
|
+
super().__init__(f"{code}: {message}")
|
|
22
|
+
self.code = code
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class ModuleEntry:
|
|
27
|
+
"""One client module in the boot table."""
|
|
28
|
+
|
|
29
|
+
id: str
|
|
30
|
+
required: bool = True
|
|
31
|
+
immediately: bool = False
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True)
|
|
35
|
+
class BootManifest:
|
|
36
|
+
"""The accepted module table handed to the renderer."""
|
|
37
|
+
|
|
38
|
+
plugins: tuple[ModuleEntry, ...]
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def names(self) -> tuple[str, ...]:
|
|
42
|
+
return tuple(entry.id for entry in self.plugins)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class BootPage:
|
|
47
|
+
"""Framework-free boot page state; the NiceGUI page renders from it."""
|
|
48
|
+
|
|
49
|
+
total: int = 0
|
|
50
|
+
states: dict[str, str] = field(default_factory=dict)
|
|
51
|
+
failure: str | None = None
|
|
52
|
+
|
|
53
|
+
def set_total(self, value: int) -> None:
|
|
54
|
+
self.total = value
|
|
55
|
+
|
|
56
|
+
def set_state(self, name: str, state: str) -> None:
|
|
57
|
+
self.states[name] = state
|
|
58
|
+
|
|
59
|
+
def fail(self, reason: str) -> None:
|
|
60
|
+
self.failure = reason
|
|
61
|
+
|
|
62
|
+
def dispose(self) -> None:
|
|
63
|
+
self.states.clear()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class BootKernel:
|
|
67
|
+
"""Load and audit the client module table before renderer handoff."""
|
|
68
|
+
|
|
69
|
+
def __init__(
|
|
70
|
+
self,
|
|
71
|
+
entries: Sequence[ModuleEntry],
|
|
72
|
+
loaders: Mapping[str, Loader],
|
|
73
|
+
*,
|
|
74
|
+
prefetch: Prefetch | None = None,
|
|
75
|
+
page: BootPage | None = None,
|
|
76
|
+
) -> None:
|
|
77
|
+
self.entries = tuple(entries)
|
|
78
|
+
self.loaders = dict(loaders)
|
|
79
|
+
self.prefetch = prefetch
|
|
80
|
+
self.page = page or BootPage()
|
|
81
|
+
self.failures: dict[str, str] = {}
|
|
82
|
+
|
|
83
|
+
async def run(self) -> BootManifest:
|
|
84
|
+
"""Prefetch immediate entries, activate all, then audit required ones."""
|
|
85
|
+
self.page.set_total(len(self.entries))
|
|
86
|
+
if self.prefetch is not None:
|
|
87
|
+
await asyncio.gather(
|
|
88
|
+
*(
|
|
89
|
+
self.prefetch(entry.id)
|
|
90
|
+
for entry in self.entries
|
|
91
|
+
if entry.immediately
|
|
92
|
+
),
|
|
93
|
+
return_exceptions=True,
|
|
94
|
+
)
|
|
95
|
+
await asyncio.gather(*(self._activate(entry) for entry in self.entries))
|
|
96
|
+
required_failed = [
|
|
97
|
+
entry.id
|
|
98
|
+
for entry in self.entries
|
|
99
|
+
if entry.required and entry.id in self.failures
|
|
100
|
+
]
|
|
101
|
+
if required_failed:
|
|
102
|
+
reason = f"required client entries failed: {', '.join(required_failed)}"
|
|
103
|
+
self.page.fail(reason)
|
|
104
|
+
raise BootError("REQUIRED_ENTRY_FAILED", reason)
|
|
105
|
+
accepted = tuple(
|
|
106
|
+
entry for entry in self.entries if entry.id not in self.failures
|
|
107
|
+
)
|
|
108
|
+
return BootManifest(accepted)
|
|
109
|
+
|
|
110
|
+
async def _activate(self, entry: ModuleEntry) -> None:
|
|
111
|
+
loader = self.loaders.get(entry.id)
|
|
112
|
+
if loader is None:
|
|
113
|
+
self._record(entry, "missing loader")
|
|
114
|
+
return
|
|
115
|
+
try:
|
|
116
|
+
await loader()
|
|
117
|
+
except Exception as exc: # noqa: BLE001 - module boundary
|
|
118
|
+
self._record(entry, str(exc))
|
|
119
|
+
else:
|
|
120
|
+
self.page.set_state(entry.id, "active")
|
|
121
|
+
|
|
122
|
+
def _record(self, entry: ModuleEntry, reason: str) -> None:
|
|
123
|
+
self.failures[entry.id] = reason
|
|
124
|
+
self.page.set_state(entry.id, "failed")
|