Skip to content

ui.formatting

ui.formatting

Value normalization and formatting helpers for the UI.

Provide small utility functions used when rendering values in the TapMap interface.

safe_str(value)

Return empty string for None, otherwise str(value).

Source code in ui/formatting.py
12
13
14
def safe_str(value: Any) -> str:
    """Return empty string for None, otherwise str(value)."""
    return "" if value is None else str(value)

safe_int(value, default=-1)

Convert value to int, or return default on failure.

Source code in ui/formatting.py
17
18
19
20
21
22
def safe_int(value: Any, default: int = -1) -> int:
    """Convert value to int, or return default on failure."""
    try:
        return int(value)
    except (TypeError, ValueError):
        return default

scope_rank(scope)

Return sort rank for scope values.

Source code in ui/formatting.py
25
26
27
28
def scope_rank(scope: str) -> int:
    """Return sort rank for scope values."""
    order = {"PUBLIC": 0, "LAN": 1, "LOCAL": 2}
    return order.get(scope.upper(), 9)

port_from_local(addr)

Extract port from an 'ip:port' string.

Source code in ui/formatting.py
31
32
33
34
35
36
def port_from_local(addr: str) -> int:
    """Extract port from an 'ip:port' string."""
    try:
        return int(addr.rsplit(":", 1)[-1])
    except (ValueError, TypeError):
        return -1

strip_port(addr)

Remove trailing ':port' from an address string.

Source code in ui/formatting.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def strip_port(addr: str) -> str:
    """Remove trailing ':port' from an address string."""
    if not addr:
        return ""

    s = addr.strip()

    if s.startswith("["):
        end = s.find("]")
        return s[1:end].strip() if end != -1 else s

    if s.count(":") == 1:
        return s.rsplit(":", 1)[0].strip()

    return s

pretty_bind_ip(ip)

Map wildcard bind addresses to readable labels.

Source code in ui/formatting.py
56
57
58
59
60
61
62
def pretty_bind_ip(ip: str) -> str:
    """Map wildcard bind addresses to readable labels."""
    if ip == "0.0.0.0":
        return "ALL (IPv4)"
    if ip == "::":
        return "ALL (IPv6)"
    return ip