File size: 1,752 Bytes
b4f2238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
"""Module for path."""

from __future__ import annotations

import contextlib
import importlib
import os
from collections.abc import Iterator

_pathlib = importlib.import_module("pathlib")
_BasePath = _pathlib.WindowsPath if os.name == "nt" else _pathlib.PosixPath


class Path(_BasePath):  # type: ignore[misc, valid-type]
    def abspath(self) -> str:
        return str(self.resolve())

    @property
    def ext(self) -> str:
        return str(self.suffix)

    def dirname(self) -> Path:
        return Path(str(self.parent))

    def isdir(self) -> bool:
        return bool(self.is_dir())

    def makedirs_p(self) -> Path:
        self.mkdir(parents=True, exist_ok=True)
        return self

    def remove_p(self) -> Path:
        with contextlib.suppress(FileNotFoundError):
            self.unlink()
        return self

    def text(self, encoding: str = "utf-8") -> str:
        return str(self.read_text(encoding=encoding))

    def write_text(self, data: str, encoding: str = "utf-8") -> Path:
        super().write_text(data, encoding=encoding)
        return self

    def write_bytes(self, data: bytes) -> Path:
        super().write_bytes(data)
        return self

    def bytes(self) -> bytes:
        return bytes(self.read_bytes())

    def walkfiles(self, pattern: str = "*") -> Iterator[Path]:
        for p in self.rglob(pattern):
            pp = Path(str(p))
            if pp.is_file():
                yield pp

    def files(self, pattern: str = "*") -> list[Path]:
        return [Path(str(p)) for p in self.glob(pattern) if Path(str(p)).is_file()]

    def dirs(self, pattern: str = "*") -> list[Path]:
        return [Path(str(p)) for p in self.glob(pattern) if Path(str(p)).is_dir()]


__all__: list[str] = ["Path"]