Repository navigation
Expand file tree
/
Copy pathplugin.py
More file actions
99 lines (77 loc) · 3.66 KB
/
Copy pathplugin.py
File metadata and controls
99 lines (77 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""WS_COLLAB workbench plugin entrypoint.
The workbench plugin loader imports this file directly (not as part of a package)
and calls ``create_router(manifest)``. We add this directory to ``sys.path`` so
the ``ws_collab`` package is importable, then delegate to one of two runners:
* :mod:`ws_collab.standalone` — run WS_COLLAB as its own process; the workbench
reaches it through its ``web_proxy`` (see ``plugin.json``). This is the default.
* :mod:`ws_collab.embedded` — mount WS_COLLAB in-process and return its router.
Select the runner with ``WS_COLLAB_PLUGIN_MODE=standalone|embedded``
(default: ``standalone``).
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
from typing import Any
_HERE = Path(__file__).resolve().parent
_SOURCE_ROOT = _HERE / "src"
if str(_SOURCE_ROOT) not in sys.path:
sys.path.insert(0, str(_SOURCE_ROOT))
# Another plugin may have put the whole plugins root on sys.path, which makes
# THIS plugin directory importable as a namespace package called "ws_collab"
# that shadows the real package in src/. Evict such a cached portion so the
# regular package (with __init__.py) wins the import.
_cached = sys.modules.get("ws_collab")
if _cached is not None and getattr(_cached, "__file__", None) is None:
del sys.modules["ws_collab"]
def _mode() -> str:
return (os.environ.get("WS_COLLAB_PLUGIN_MODE") or "standalone").strip().lower()
def resolve_ui_pages(manifest: dict[str, Any], pages: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Resolve where this plugin's declared pages live.
WS_COLLAB serves its own administration console, so every page is an
absolute URL under the manifest's ``configPage`` base rather than a
workbench-rendered descriptor. A relative descriptor is joined onto that
base, and any fragment the manifest declares is preserved.
"""
base = str(manifest.get("configPage") or "").rstrip("/")
resolved: list[dict[str, Any]] = []
for page in pages:
descriptor = str(page.get("descriptor") or "")
if descriptor.startswith(("http://", "https://")):
address = descriptor
elif base:
address = f"{base}/{descriptor.lstrip('/')}" if descriptor else base
else:
address = descriptor
resolved.append({**page, "address": address, "external": address.startswith(("http://", "https://"))})
return resolved
def create_router(manifest: dict[str, Any] | None = None):
"""Delegate to the selected runner's ``create_router``."""
if _mode() == "embedded":
from ws_collab.embedded import create_router as _create_router
else:
from ws_collab.standalone import create_router as _create_router
return _create_router(manifest)
def start_server(
*,
host: str | None = None,
port: int | None = None,
state_dir: str | Path | None = None,
timeout: float = 20.0,
python_executable: str | Path | None = None,
) -> dict[str, Any]:
"""Start/reuse the standalone service without needing its HTTP server alive.
This local Python API is for the plugin host, not an unauthenticated HTTP
route. The host must authorize any UI/remote action that invokes it.
"""
if _mode() == "embedded":
raise RuntimeError("Embedded WS_COLLAB is owned by its host; standalone startup is unavailable")
from ws_collab import standalone
return standalone.start_server(
host=host if host is not None else standalone.DEFAULT_HOST,
port=port if port is not None else standalone.DEFAULT_PORT,
state_dir=state_dir,
timeout=timeout,
python_executable=python_executable,
)
__all__ = ["create_router", "resolve_ui_pages", "start_server"]