50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
"""Longest-roster-prefix handle resolution, with the boundary rules spelled out.
|
|
|
|
A check-in filename's handle is resolved against the roster by taking the
|
|
longest roster handle that is either equal to the name or a hyphen-delimited
|
|
prefix of it.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
|
|
def resolve_handle(name: str, roster) -> str | None:
|
|
"""Return the longest roster handle that is `name` or a `-`-prefix of it."""
|
|
if not name:
|
|
return None
|
|
name = name.lower()
|
|
roster = {h.lower() for h in (roster or [])}
|
|
best = ""
|
|
for h in roster:
|
|
if not h:
|
|
continue
|
|
if name == h or name.startswith(h + "-"):
|
|
if len(h) > len(best):
|
|
best = h
|
|
return best or None
|
|
|
|
|
|
def selftest() -> int:
|
|
cases = [
|
|
(("volt", {"volt"}), "volt"),
|
|
(("volt-02", {"volt"}), "volt"),
|
|
(("voltmeter", {"volt"}), None),
|
|
(("volt", {"volt", "vol"}), "volt"),
|
|
(("cassian-q5-census", {"cassian"}), "cassian"),
|
|
(("gandalf", {"volt"}), None),
|
|
(("", {"volt"}), None),
|
|
(("volt-02", set()), None),
|
|
(("VOLT-02", {"volt"}), "volt"),
|
|
(("zephyrus-02", {"zephyrus", "zephyrus-0"}), "zephyrus"),
|
|
]
|
|
bad = 0
|
|
for inp, exp in cases:
|
|
got = resolve_handle(*inp)
|
|
if got != exp:
|
|
print(f"FAIL {inp!r}: got {got!r} want {exp!r}")
|
|
bad += 1
|
|
print(f"selftest: {len(cases) - bad}/{len(cases)} passed")
|
|
return 1 if bad else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(selftest()) |