File: parsers.py

package info (click to toggle)
python-pathable 0.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: python: 968; makefile: 3
file content (51 lines) | stat: -rw-r--r-- 1,490 bytes parent folder | download
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
"""Pathable parsers module"""
from typing import Any
from typing import Hashable
from typing import List
from typing import Union

from pathable.types import PartType

SEPARATOR = "/"


def parse_parts(parts: List[PartType], sep: str = SEPARATOR) -> List[Hashable]:
    """Parse (filter and split) path parts."""
    parsed: List[Hashable] = []
    it = reversed(parts)
    for part in it:
        if isinstance(part, int):
            parsed.append(part)
            continue
        if not part:
            continue
        if sep in part:
            for x in reversed(part.split(sep)):
                if x and x != ".":
                    parsed.append(x)
        else:
            if part and part != ".":
                parsed.append(part)
    parsed.reverse()
    return parsed


def parse_args(args: List[Any], sep: str = SEPARATOR) -> List[Hashable]:
    """Canonicalize path constructor arguments."""
    parts: List[PartType] = []
    for a in args:
        if hasattr(a, "parts"):
            parts += a.parts
        else:
            if isinstance(a, bytes):
                a = a.decode("ascii")
            if isinstance(a, str):
                parts.append(a)
            elif isinstance(a, int):
                parts.append(a)
            else:
                raise TypeError(
                    "argument should be a text object or a Path "
                    "object returning text, binary not %r" % type(a)
                )
    return parse_parts(parts, sep)