from __future__ import annotations import json from typing import Any, Tuple def url_from_host_port(host: str, port: int) -> str: host = (host or "").strip() if not host: host = "127.0.0.1" if host.startswith("http://") or host.startswith("https://"): return f"{host}:{port}" return f"http://{host}:{port}" def parse_host_port(base_url: str) -> Tuple[str, int]: s = (base_url or "").strip() s = s.replace("http://", "").replace("https://", "") if ":" not in s: return (s or "127.0.0.1"), 8000 host, port_s = s.split(":", 1) try: port = int(port_s) except Exception: port = 8000 return (host or "127.0.0.1"), port def short_json(x: Any, max_len: int = 500) -> str: try: s = json.dumps(x, ensure_ascii=False) except Exception: s = str(x) if len(s) > max_len: return s[:max_len] + "..." return s def short_path(p: str, max_len: int = 160) -> str: p = p or "" if len(p) > max_len: return "..." + p[-(max_len - 3):] return p