feat: composer improvements
This commit is contained in:
@@ -29,14 +29,56 @@ def _indent(code: str, level: int = 1) -> str:
|
||||
|
||||
|
||||
def _resolve_vars(text: str) -> str:
|
||||
"""Convert $var references to Python f-string expressions."""
|
||||
"""Convert $var references to Python f-string expressions.
|
||||
|
||||
First escapes literal braces (e.g. @color{ff0}{text} → @color{{ff0}}{{text}})
|
||||
so they survive f-string evaluation, then replaces $var → {var}.
|
||||
"""
|
||||
import re
|
||||
# Use a unique placeholder for $var refs, escape all braces, then restore
|
||||
_PH = "\x00VAR"
|
||||
counter = [0]
|
||||
placeholders: dict[str, str] = {}
|
||||
|
||||
def stash_var(m: re.Match) -> str:
|
||||
var = m.group(1)
|
||||
if "." in var:
|
||||
parts = var.split(".")
|
||||
base = parts[0]
|
||||
chain = "".join(f"['{p}']" for p in parts[1:])
|
||||
expr = "{" + base + chain + "}"
|
||||
else:
|
||||
expr = "{" + var + "}"
|
||||
key = f"{_PH}{counter[0]}\x00"
|
||||
counter[0] += 1
|
||||
placeholders[key] = expr
|
||||
return key
|
||||
|
||||
# 1. Stash $var references with placeholders
|
||||
result = re.sub(r'\$([a-zA-Z_][\w.]*)', stash_var, text)
|
||||
# 2. Escape all remaining braces for f-string safety
|
||||
result = result.replace("{", "{{").replace("}", "}}")
|
||||
# 3. Restore $var placeholders (unescaped)
|
||||
for key, expr in placeholders.items():
|
||||
result = result.replace(key, expr)
|
||||
return result
|
||||
|
||||
|
||||
def _resolve_vars_code(text: str) -> str:
|
||||
"""Convert $var references to bare Python identifiers.
|
||||
|
||||
Simple vars: $name → name
|
||||
Dotted paths: $item.name → item['name'] (dict access)
|
||||
"""
|
||||
import re
|
||||
# Replace $var.attr.attr with {var_attr_attr} and simple $var with {var}
|
||||
def replace_var(m: re.Match) -> str:
|
||||
var = m.group(1)
|
||||
# Replace dots with underscores for Python variable names
|
||||
py_var = var.replace(".", "_")
|
||||
return "{" + py_var + "}"
|
||||
if "." in var:
|
||||
parts = var.split(".")
|
||||
base = parts[0]
|
||||
chain = "".join(f"['{p}']" for p in parts[1:])
|
||||
return base + chain
|
||||
return var
|
||||
return re.sub(r'\$([a-zA-Z_][\w.]*)', replace_var, text)
|
||||
|
||||
|
||||
@@ -67,14 +109,28 @@ def _emit_node(node: IRNode, indent_level: int = 0) -> list[str]:
|
||||
elif node.source_type == SourceType.JSON:
|
||||
lines.append(f"{ind}{var} = _read_json({node.command!r})")
|
||||
elif node.source_type == SourceType.PYTHON:
|
||||
# Restricted eval — only datetime/secrets modules available
|
||||
lines.append(f"{ind}{var} = eval({node.command!r}, {{'datetime': datetime, 'secrets': secrets}})")
|
||||
lines.append(f"{ind}{var} = eval({node.command!r}, {{'datetime': _dt_cls, 'timedelta': timedelta, 'secrets': secrets, 'os': os, 'json': json}})")
|
||||
elif node.source_type == SourceType.PARAM:
|
||||
lines.append(f"{ind}{var} = _get_param({node.command!r})")
|
||||
elif node.source_type == SourceType.RNS:
|
||||
import shlex as _shlex
|
||||
safe_cmd = _shlex.quote(node.command)
|
||||
lines.append(f"{ind}{var} = _shell('rnstatus ' + shlex.quote({safe_cmd!r}), timeout={node.timeout})")
|
||||
elif node.source_type == SourceType.HTTP:
|
||||
method = node.http_method or "GET"
|
||||
url = _resolve_vars(node.command)
|
||||
hdrs = _resolve_vars(node.http_headers) if node.http_headers else ""
|
||||
body = _resolve_vars(node.http_body) if node.http_body else ""
|
||||
if body:
|
||||
lines.append(f"""{ind}{var} = _http(f'''{url}''', method={method!r}, body=f'''{body}''', headers=f'''{hdrs}''', timeout={node.timeout})""")
|
||||
elif hdrs:
|
||||
lines.append(f"""{ind}{var} = _http(f'''{url}''', method={method!r}, headers=f'''{hdrs}''', timeout={node.timeout})""")
|
||||
else:
|
||||
lines.append(f"{ind}{var} = _http({node.command!r}, method={method!r}, timeout={node.timeout})")
|
||||
elif node.source_type == SourceType.SQLITE:
|
||||
lines.append(f"{ind}{var} = _sqlite({node.command!r}, {node.query!r})")
|
||||
elif node.source_type == SourceType.ENV:
|
||||
lines.append(f"{ind}{var} = os.environ.get({node.command!r}, '')")
|
||||
|
||||
elif isinstance(node, CacheControl):
|
||||
lines.append(f"{ind}_cache_seconds = {node.seconds}")
|
||||
@@ -83,8 +139,7 @@ def _emit_node(node: IRNode, indent_level: int = 0) -> list[str]:
|
||||
lines.append(f"{ind}{node.state_name} = _load_state({node.path!r})")
|
||||
|
||||
elif isinstance(node, IfBlock):
|
||||
cond = _resolve_vars(node.condition)
|
||||
# Convert simple comparisons
|
||||
cond = _resolve_vars_code(node.condition)
|
||||
cond = cond.replace("&&", " and ").replace("||", " or ")
|
||||
lines.append(f"{ind}if {cond}:")
|
||||
if node.children:
|
||||
@@ -94,7 +149,7 @@ def _emit_node(node: IRNode, indent_level: int = 0) -> list[str]:
|
||||
lines.append(f"{ind} pass")
|
||||
|
||||
for elif_cond, elif_children in node.elif_branches:
|
||||
ec = _resolve_vars(elif_cond).replace("&&", " and ").replace("||", " or ")
|
||||
ec = _resolve_vars_code(elif_cond).replace("&&", " and ").replace("||", " or ")
|
||||
lines.append(f"{ind}elif {ec}:")
|
||||
if elif_children:
|
||||
for child in elif_children:
|
||||
@@ -108,7 +163,7 @@ def _emit_node(node: IRNode, indent_level: int = 0) -> list[str]:
|
||||
lines.extend(_emit_node(child, indent_level + 1))
|
||||
|
||||
elif isinstance(node, ForLoop):
|
||||
iterable = _resolve_vars(node.iterable)
|
||||
iterable = _resolve_vars_code(node.iterable)
|
||||
lines.append(f"{ind}for {node.var_name} in _iter({iterable}):")
|
||||
if node.children:
|
||||
for child in node.children:
|
||||
@@ -146,13 +201,16 @@ def _emit_node(node: IRNode, indent_level: int = 0) -> list[str]:
|
||||
|
||||
elif isinstance(node, Gauge):
|
||||
label = _resolve_vars(node.label)
|
||||
value = _resolve_vars(str(node.value)) if "$" in str(node.value) else str(node.value)
|
||||
raw_val = getattr(node, "_raw_value", None)
|
||||
value = _resolve_vars(raw_val) if raw_val and "$" in raw_val else str(node.value)
|
||||
raw_max = getattr(node, "_raw_max_val", None)
|
||||
max_val = _resolve_vars(raw_max) if raw_max and "$" in raw_max else str(node.max_val)
|
||||
extra = ""
|
||||
if node.warn is not None:
|
||||
extra += f" warn={node.warn}"
|
||||
if node.crit is not None:
|
||||
extra += f" crit={node.crit}"
|
||||
lines.append(f"{ind}_uf_source_parts.append(f'gauge \"{label}\" {value} {node.max_val} {node.bar_width}{extra}')")
|
||||
lines.append(f"{ind}_uf_source_parts.append(f'gauge \"{label}\" {value} {max_val} {node.bar_width}{extra}')")
|
||||
|
||||
elif isinstance(node, Status):
|
||||
label = _resolve_vars(node.label)
|
||||
@@ -221,6 +279,7 @@ def _build_script(uframe_import: str, page_logic: str, page_title: str, page_wid
|
||||
"# Do not edit — regenerate with: uframe compile <source>.uf",
|
||||
"",
|
||||
"import os, sys, json, subprocess, datetime, secrets, shlex",
|
||||
"from datetime import datetime as _dt_cls, timedelta",
|
||||
"",
|
||||
"# ─── Runtime Helpers ─────────────────────────────────────────",
|
||||
"",
|
||||
@@ -279,6 +338,42 @@ def _build_script(uframe_import: str, page_logic: str, page_title: str, page_wid
|
||||
' return val.strip().splitlines()',
|
||||
' return []',
|
||||
"",
|
||||
'def _http(url, method="GET", body="", headers="", timeout=10):',
|
||||
' """HTTP request, return response body (JSON parsed if possible)."""',
|
||||
' import urllib.request, urllib.error',
|
||||
' try:',
|
||||
' data = body.encode("utf-8") if body else None',
|
||||
' req = urllib.request.Request(url, data=data, method=method)',
|
||||
' req.add_header("User-Agent", "uframe/1.0")',
|
||||
' if body and not headers:',
|
||||
' req.add_header("Content-Type", "application/json")',
|
||||
' if headers:',
|
||||
' for pair in headers.split(";"):',
|
||||
' if ":" in pair:',
|
||||
' k, v = pair.split(":", 1)',
|
||||
' req.add_header(k.strip(), v.strip())',
|
||||
' with urllib.request.urlopen(req, timeout=timeout) as resp:',
|
||||
' raw = resp.read().decode("utf-8")',
|
||||
' try:',
|
||||
' return json.loads(raw)',
|
||||
' except (json.JSONDecodeError, ValueError):',
|
||||
' return raw.strip()',
|
||||
' except Exception as e:',
|
||||
' return {"error": str(e)}',
|
||||
"",
|
||||
'def _sqlite(db_path, query):',
|
||||
' """Run a SQLite query, return list of dicts."""',
|
||||
' import sqlite3',
|
||||
' try:',
|
||||
' conn = sqlite3.connect(db_path)',
|
||||
' conn.row_factory = sqlite3.Row',
|
||||
' cur = conn.execute(query)',
|
||||
' rows = [dict(r) for r in cur.fetchall()]',
|
||||
' conn.close()',
|
||||
' return rows if len(rows) != 1 else rows[0]',
|
||||
' except Exception as e:',
|
||||
' return {"error": str(e)}',
|
||||
"",
|
||||
f"# ─── µFrame Compile ──────────────────────────────────────────",
|
||||
"",
|
||||
uframe_import,
|
||||
|
||||
Reference in New Issue
Block a user