-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace.py
More file actions
29 lines (20 loc) · 885 Bytes
/
Copy pathnamespace.py
File metadata and controls
29 lines (20 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import annotations
DEFAULT_NAMESPACE = "default"
def normalize_namespace(namespace: str | None) -> str:
if namespace is None:
return DEFAULT_NAMESPACE
cleaned = namespace.strip().strip("/")
if not cleaned:
return DEFAULT_NAMESPACE
parts = [part for part in cleaned.split("/") if part]
return "/".join(parts)
def namespace_parts(namespace: str | None) -> tuple[str, ...]:
return tuple(normalize_namespace(namespace).split("/"))
def namespace_parent(namespace: str | None) -> str | None:
parts = namespace_parts(namespace)
if len(parts) <= 1:
return None
return "/".join(parts[:-1])
def namespace_children(namespace: str | None, known_namespaces: list[str]) -> list[str]:
prefix = f"{normalize_namespace(namespace)}/"
return sorted(name for name in known_namespaces if name.startswith(prefix))