Add macros, declarative handlers (defhandler), and convert all fragment routes to sx
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Phase 1 — Macros: defmacro + quasiquote syntax (`, ,, ,@) in parser,
evaluator, HTML renderer, and JS mirror. Macro type, expansion, and
round-trip serialization.
Phase 2 — Expanded primitives: app-url, url-for, asset-url, config,
format-date, parse-int (pure); service, request-arg, request-path,
nav-tree, get-children (I/O); jinja-global, relations-from (pure).
Updated _io_service to accept (service "registry-name" "method" :kwargs)
with auto kebab→snake conversion. DTO-to-dict now expands datetime fields
into year/month/day convenience keys. Tuple returns converted to lists.
Phase 3 — Declarative handlers: HandlerDef type, defhandler special form,
handler registry (service → name → HandlerDef), async evaluator+renderer
(async_eval.py) that awaits I/O primitives inline within control flow.
Handler loading from .sx files, execute_handler, blueprint factory.
Phase 4 — Convert all fragment routes: 13 Python fragment handlers across
8 services replaced with declarative .sx handler files. All routes.py
simplified to uniform sx dispatch pattern. Two Jinja HTML handlers
(events/container-cards, events/account-page) kept as Python.
New files: shared/sx/async_eval.py, shared/sx/handlers.py,
shared/sx/tests/test_handlers.py, plus 13 handler .sx files under
{service}/sx/handlers/. MarketService.product_by_slug() added.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -408,6 +408,84 @@ def prim_into(target: Any, coll: Any) -> Any:
|
||||
raise ValueError(f"into: unsupported target type {type(target).__name__}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# URL helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@register_primitive("app-url")
|
||||
def prim_app_url(service: str, path: str = "/") -> str:
|
||||
"""``(app-url "blog" "/my-post/")`` → full URL for service."""
|
||||
from shared.infrastructure.urls import app_url
|
||||
return app_url(service, path)
|
||||
|
||||
|
||||
@register_primitive("url-for")
|
||||
def prim_url_for(endpoint: str, **kwargs: Any) -> str:
|
||||
"""``(url-for "endpoint")`` → quart.url_for."""
|
||||
from quart import url_for
|
||||
return url_for(endpoint, **kwargs)
|
||||
|
||||
|
||||
@register_primitive("asset-url")
|
||||
def prim_asset_url(path: str = "") -> str:
|
||||
"""``(asset-url "/img/logo.png")`` → versioned static URL."""
|
||||
from shared.infrastructure.urls import asset_url
|
||||
return asset_url(path)
|
||||
|
||||
|
||||
@register_primitive("config")
|
||||
def prim_config(key: str) -> Any:
|
||||
"""``(config "key")`` → shared.config.config()[key]."""
|
||||
from shared.config import config
|
||||
cfg = config()
|
||||
return cfg.get(key)
|
||||
|
||||
|
||||
@register_primitive("jinja-global")
|
||||
def prim_jinja_global(key: str, default: Any = None) -> Any:
|
||||
"""``(jinja-global "key")`` → current_app.jinja_env.globals[key]."""
|
||||
from quart import current_app
|
||||
return current_app.jinja_env.globals.get(key, default)
|
||||
|
||||
|
||||
@register_primitive("relations-from")
|
||||
def prim_relations_from(entity_type: str) -> list[dict]:
|
||||
"""``(relations-from "page")`` → list of RelationDef dicts."""
|
||||
from shared.sx.relations import relations_from
|
||||
return [
|
||||
{
|
||||
"name": d.name, "from_type": d.from_type, "to_type": d.to_type,
|
||||
"cardinality": d.cardinality, "nav": d.nav,
|
||||
"nav_icon": d.nav_icon, "nav_label": d.nav_label,
|
||||
}
|
||||
for d in relations_from(entity_type)
|
||||
]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Format helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@register_primitive("format-date")
|
||||
def prim_format_date(date_str: Any, fmt: str) -> str:
|
||||
"""``(format-date date-str fmt)`` → formatted date string."""
|
||||
from datetime import datetime
|
||||
try:
|
||||
dt = datetime.fromisoformat(str(date_str))
|
||||
return dt.strftime(fmt)
|
||||
except (ValueError, TypeError):
|
||||
return str(date_str) if date_str else ""
|
||||
|
||||
|
||||
@register_primitive("parse-int")
|
||||
def prim_parse_int(val: Any, default: Any = 0) -> int | Any:
|
||||
"""``(parse-int val default?)`` → int(val) with fallback."""
|
||||
try:
|
||||
return int(val)
|
||||
except (ValueError, TypeError):
|
||||
return default
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Assertions
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user