Add register_sx_layout infrastructure, convert account/federation/orders

Phase 0: Add _ctx_to_env() and render_to_sx_with_env() to shared/sx/helpers.py,
register_sx_layout() to shared/sx/layouts.py, and ~root-header/~root-mobile
wrapper defcomps to layout.sx. Convert built-in "root" layout to .sx.

Phases 1-3: Convert account (65→19 lines), federation (105→97 lines),
and orders (88→21 lines) to use register_sx_layout with .sx defcomps
that read ctx values as free variables from the evaluation environment.
No more Python building SX strings via SxExpr(await root_header_sx(ctx)).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 14:39:53 +00:00
parent 28388540d5
commit 45c5e4a0db
9 changed files with 244 additions and 229 deletions

View File

@@ -15,7 +15,7 @@ from typing import Any, Callable, Awaitable
from .helpers import (
root_header_sx, post_header_sx, post_admin_header_sx,
oob_header_sx, header_child_sx,
oob_header_sx,
mobile_menu_sx, mobile_root_nav_sx,
post_mobile_nav_sx, post_admin_mobile_nav_sx,
)
@@ -87,15 +87,6 @@ def get_layout(name: str) -> Layout | None:
# Built-in layouts
# ---------------------------------------------------------------------------
async def _root_full(ctx: dict, **kw: Any) -> str:
return await root_header_sx(ctx)
async def _root_oob(ctx: dict, **kw: Any) -> str:
root_hdr = await root_header_sx(ctx)
return await oob_header_sx("root-header-child", "root-header-child", root_hdr)
async def _post_full(ctx: dict, **kw: Any) -> str:
root_hdr = await root_header_sx(ctx)
post_hdr = await post_header_sx(ctx)
@@ -127,10 +118,6 @@ async def _post_admin_oob(ctx: dict, **kw: Any) -> str:
return "(<> " + post_hdr + " " + admin_oob + ")"
async def _root_mobile(ctx: dict, **kw: Any) -> str:
return await mobile_root_nav_sx(ctx)
async def _post_mobile(ctx: dict, **kw: Any) -> str:
return mobile_menu_sx(await post_mobile_nav_sx(ctx), await mobile_root_nav_sx(ctx))
@@ -145,11 +132,53 @@ async def _post_admin_mobile(ctx: dict, **kw: Any) -> str:
)
register_layout(Layout("root", _root_full, _root_oob, _root_mobile))
register_layout(Layout("post", _post_full, _post_oob, _post_mobile))
register_layout(Layout("post-admin", _post_admin_full, _post_admin_oob, _post_admin_mobile))
# ---------------------------------------------------------------------------
# register_sx_layout — declarative layout from .sx defcomp names
# ---------------------------------------------------------------------------
# (defined below, used immediately after for built-in "root" layout)
# ---------------------------------------------------------------------------
def register_sx_layout(name: str, full_defcomp: str, oob_defcomp: str,
mobile_defcomp: str | None = None) -> None:
"""Register a layout that delegates entirely to .sx defcomps.
The defcomps read ctx values as free variables from the evaluation
environment (populated by ``_ctx_to_env``). Python layouts become
one-liners::
register_sx_layout("account", "account-layout-full",
"account-layout-oob", "account-layout-mobile")
"""
from .helpers import render_to_sx_with_env, _ctx_to_env
async def full_fn(ctx: dict, **kw: Any) -> str:
env = _ctx_to_env(ctx)
env.update({k.replace("_", "-"): v for k, v in kw.items()})
return await render_to_sx_with_env(full_defcomp, env)
async def oob_fn(ctx: dict, **kw: Any) -> str:
env = _ctx_to_env(ctx, oob=True)
env.update({k.replace("_", "-"): v for k, v in kw.items()})
return await render_to_sx_with_env(oob_defcomp, env)
mobile_fn = None
if mobile_defcomp:
async def mobile_fn(ctx: dict, **kw: Any) -> str:
env = _ctx_to_env(ctx)
env.update({k.replace("_", "-"): v for k, v in kw.items()})
return await render_to_sx_with_env(mobile_defcomp, env)
register_layout(Layout(name, full_fn, oob_fn, mobile_fn))
# Register built-in "root" layout via .sx defcomps
register_sx_layout("root", "layout-root-full", "layout-root-oob", "layout-root-mobile")
# ---------------------------------------------------------------------------
# Callable layout — services register custom Python layout functions
# ---------------------------------------------------------------------------