summaryrefslogtreecommitdiff
path: root/kazoo/protocol/paths.py
blob: b8bf66507ad910f042d35a07c76ec6494169566c (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def normpath(path, trailing=False):
    """Normalize path, eliminating double slashes, etc."""
    comps = path.split("/")
    new_comps = []
    for comp in comps:
        if comp == "":
            continue
        if comp in (".", ".."):
            raise ValueError("relative paths not allowed")
        new_comps.append(comp)
    new_path = "/".join(new_comps)
    if trailing is True and path.endswith("/"):
        new_path += "/"
    if path.startswith("/") and new_path != "/":
        return "/" + new_path
    return new_path


def join(a, *p):
    """Join two or more pathname components, inserting '/' as needed.

    If any component is an absolute path, all previous path components
    will be discarded.

    """
    path = a
    for b in p:
        if b.startswith("/"):
            path = b
        elif path == "" or path.endswith("/"):
            path += b
        else:
            path += "/" + b
    return path


def isabs(s):
    """Test whether a path is absolute"""
    return s.startswith("/")


def basename(p):
    """Returns the final component of a pathname"""
    i = p.rfind("/") + 1
    return p[i:]


def _prefix_root(root, path, trailing=False):
    """Prepend a root to a path."""
    return normpath(
        join(_norm_root(root), path.lstrip("/")), trailing=trailing
    )


def _norm_root(root):
    return normpath(join("/", root))