$extens("include.py") include("import os", []) def demo_register_block_v3() -> dict: import os import disnet handler_code = '''"""block: a tiny language for the blocks of a small website. A block is a JSON document with one of seven types and plain-text fields. No markup, no scripts, no links can be expressed: the handler escapes every string, so a visitor can add blocks to a live site safely. Pure Python, standard library only, no backslashes (the source is embedded into device handlers). {"type": "hero", "title": "...", "text": "...", "button": "..."} {"type": "features", "title": "...", "items": [{"title": "...", "text": "..."}]} 1..4 items {"type": "price", "title": "...", "currency": "USD|EUR|KZT|RUB", "plans": [{"name", "price": number, "period": "month|year|once", "features": ["..."]}]} 1..3 plans {"type": "faq", "title": "...", "items": [{"q": "...", "a": "..."}]} 1..5 items {"type": "quote", "text": "...", "author": "...", "role": "...", "rating": 1..5} {"type": "cta", "title": "...", "text": "...", "button": "..."} {"type": "text", "title": "...", "text": "..."} Rendering belongs to the handler on the device: version 1 is editorial, version 2 is compact dark. Laws (a run parameter): "vat" shows prices with 12% VAT; "ru" puts interface labels in Russian. """ import hashlib import json TYPES = ("hero", "features", "price", "faq", "quote", "cta", "text") FIELDS = { "hero": {"title": 80, "text": 240, "button": 40}, "features": {"title": 80, "items": None}, "price": {"title": 80, "currency": 3, "plans": None}, "faq": {"title": 80, "items": None}, "quote": {"text": 300, "author": 60, "role": 60, "rating": None}, "cta": {"title": 80, "text": 200, "button": 40}, "text": {"title": 80, "text": 600}, } REQUIRED = {"hero": ("title",), "features": ("items",), "price": ("plans",), "faq": ("items",), "quote": ("text",), "cta": ("title",), "text": ("text",)} CURRENCIES = ("USD", "EUR", "KZT", "RUB") PERIODS = ("month", "year", "once") LAWS = ("vat", "ru") VAT = 0.12 def _s(v, n): return isinstance(v, str) and 0 < len(v.strip()) <= n def validate(block): if not isinstance(block, dict): return False, "a block must be an object" t = block.get("type") if t not in TYPES: return False, "type must be one of " + ", ".join(TYPES) allowed = FIELDS[t] for k in block: if k != "type" and k not in allowed: return False, "'%s' is not a word in block type %s" % (k, t) for k in REQUIRED[t]: if k not in block: return False, "block type %s needs '%s'" % (t, k) for k, n in allowed.items(): if k in block and n is not None and not _s(block[k], n): return False, "'%s' must be text up to %d characters" % (k, n) if t == "features": items = block.get("items") if not isinstance(items, list) or not 1 <= len(items) <= 4: return False, "features needs 1 to 4 items" for i, it in enumerate(items): if not isinstance(it, dict) or set(it) - {"title", "text"} or not _s(it.get("title"), 60) or not _s(it.get("text", "x"), 160): return False, "item %d: title up to 60 and text up to 160 characters, nothing else" % (i + 1) if t == "faq": items = block.get("items") if not isinstance(items, list) or not 1 <= len(items) <= 5: return False, "faq needs 1 to 5 items" for i, it in enumerate(items): if not isinstance(it, dict) or set(it) - {"q", "a"} or not _s(it.get("q"), 120) or not _s(it.get("a"), 300): return False, "item %d: q up to 120 and a up to 300 characters, nothing else" % (i + 1) if t == "price": if block.get("currency", "USD") not in CURRENCIES: return False, "currency must be one of " + ", ".join(CURRENCIES) plans = block.get("plans") if not isinstance(plans, list) or not 1 <= len(plans) <= 3: return False, "price needs 1 to 3 plans" for i, pl in enumerate(plans): if not isinstance(pl, dict) or set(pl) - {"name", "price", "period", "features"}: return False, "plan %d: name, price, period, features only" % (i + 1) if not _s(pl.get("name"), 40): return False, "plan %d needs a name" % (i + 1) if not isinstance(pl.get("price"), (int, float)) or isinstance(pl.get("price"), bool) or not 0 <= pl["price"] <= 1000000: return False, "plan %d: price must be a number from 0 to 1000000" % (i + 1) if pl.get("period", "month") not in PERIODS: return False, "plan %d: period must be month, year or once" % (i + 1) f = pl.get("features", []) if not isinstance(f, list) or len(f) > 6 or not all(_s(x, 80) for x in f): return False, "plan %d: up to 6 features, each text up to 80 characters" % (i + 1) if t == "quote": r = block.get("rating", 5) if not isinstance(r, int) or isinstance(r, bool) or not 1 <= r <= 5: return False, "rating must be a whole number from 1 to 5" return True, "ok" def esc(s): s = str(s) return s.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """) def source_sha(block): return hashlib.sha256(json.dumps(block, sort_keys=True, ensure_ascii=False).encode("utf-8")).hexdigest() HANDLER_SHA = "ec7064a968f93fed0f7351a0ee94557a2ec0c8aedaace45f73c0ce6220690781" # Handler v3 of the block language: plain text. Written separately from the HTML handlers (v1, v2): it shares # only the grammar of the language (validate) and renders the same documents as text for a terminal, a README # or a messenger. No HTML is built here; the last lines only wrap the text so the demo page can show it. # Embedded into a device registrar: no backslashes and no triple quotes in this file. NL = chr(10) T_LABELS = { "en": {"month": "per month", "year": "per year", "once": "one-time", "vat": "incl. 12% VAT", "faq": "Questions", "features": "What you get", "plans": "Plans"}, "ru": {"month": "в месяц", "year": "в год", "once": "разово", "vat": "с НДС 12%", "faq": "Вопросы", "features": "Что вы получаете", "plans": "Тарифы"}, } T_CODE = {"USD": "USD", "EUR": "EUR", "KZT": "KZT", "RUB": "RUB"} def t_money(v, cur): num = str(int(v)) if float(v) == int(v) else "{:.2f}".format(v) return num + " " + T_CODE.get(cur, "") def t_wrap(s, width=72): words, lines, line = str(s).split(), [], "" for w in words: if line and len(line) + 1 + len(w) > width: lines.append(line) line = w else: line = (line + " " + w) if line else w if line: lines.append(line) return NL.join(lines) def render_text(block, law=None): L = T_LABELS["ru"] if law == "ru" else T_LABELS["en"] t = block["type"] out = [] if t == "hero": out += [block["title"].upper(), "=" * min(72, len(block["title"]))] if block.get("text"): out += ["", t_wrap(block["text"])] if block.get("button"): out += ["", "[ " + block["button"] + " ]"] elif t == "features": out += [block.get("title") or L["features"], "-" * 24] for it in block["items"]: out.append("* " + it["title"] + (": " + it["text"] if it.get("text") else "")) elif t == "price": cur = block.get("currency", "USD") out += [block.get("title") or L["plans"], "-" * 24] for pl in block["plans"]: amount = pl["price"] * 1.12 if law == "vat" else pl["price"] line = pl["name"].ljust(12) + " " + t_money(amount, cur) + " " + L[pl.get("period", "month")] if law == "vat": line += " (" + L["vat"] + ")" out.append(line) for f in pl.get("features", []): out.append(" - " + f) elif t == "faq": out += [block.get("title") or L["faq"], "-" * 24] for it in block["items"]: out += ["Q: " + it["q"], t_wrap("A: " + it["a"]), ""] elif t == "quote": out.append("*" * int(block.get("rating", 5)) + " (" + str(int(block.get("rating", 5))) + "/5)") out.append(t_wrap(chr(8220) + block["text"] + chr(8221))) if block.get("author"): out.append(" -- " + block["author"] + (", " + block["role"] if block.get("role") else "")) elif t == "cta": out += [block["title"], "-" * 24] if block.get("text"): out.append(t_wrap(block["text"])) if block.get("button"): out += ["", "[ " + block["button"] + " ]"] elif t == "text": if block.get("title"): out += [block["title"], "-" * 24] out.append(t_wrap(block["text"])) return NL.join(out).rstrip() T_CSS = "pre.b{margin:0;padding:18px 28px;font:14px/1.55 ui-monospace,SFMono-Regular,Menlo,monospace;color:#1A1A1A;background:#F7F6F1;border-bottom:1px dashed #C9C4B8;white-space:pre-wrap}" def block(code = "", filtered_source_code = "", func_name = "", **extra): import json, platform, socket source = code or filtered_source_code or "" params = dict(extra) inner = extra.get("kwargs") if isinstance(inner, dict): params.update(inner) try: doc = json.loads(source) except Exception as err: return {"status": "rejected", "reason": "block is not valid JSON: " + str(err)[:120]} ok, reason = validate(doc) if not ok: return {"status": "rejected", "reason": reason} law = params.get("law") or None if law not in LAWS: law = None text = render_text(doc, law) return {"status": "success", "language": "block", "expert": func_name, "block_type": doc["type"], "text": text, "html": '
' + esc(text) + "", "css": T_CSS, "handler_version": 3, "handler_name": "plain text", "handler_sha": HANDLER_SHA, "law": law, "source_sha": source_sha(doc), "model_calls": 0, "host": socket.gethostname(), "os": platform.system() + " " + platform.machine()} ''' d = disnet.Disnet(url="https://disnet.extella.ai") envelope = {"filtered_source_code": "", "func_name": "", "args": None, "kwargs": None, "cspl": "block", "response_format": None} d.container(code=handler_code, func_name="block", container_path="block", args=None, kwargs=envelope, cspl_type="fython", push=False) return {"status": "success", "handler": "block v3", "version": 3, "bytes": len(handler_code), "folder": os.path.abspath("containers_folder")}