Selfie
Loading...
Searching...
No Matches
TypedPath.py
Go to the documentation of this file.
1from functools import total_ordering
2
3
4@total_ordering
6 def __init__(self, absolute_path: str):
7 self.absolute_path = absolute_path
8
9 def __hash__(self):
10 return hash(self.absolute_path)
11
12 def __str__(self) -> str:
13 return self.absolute_path
14
15 @property
16 def name(self) -> str:
17 if self.absolute_path.endswith("/"):
18 path = self.absolute_path[:-1]
19 else:
20 path = self.absolute_path
21 last_slash = path.rfind("/")
22 return path[last_slash + 1 :]
23
24 @property
25 def is_folder(self) -> bool:
26 return self.absolute_path.endswith("/")
27
28 def assert_folder(self) -> None:
29 if not self.is_folder:
30 raise AssertionError(
31 f"Expected {self} to be a folder but it doesn't end with `/`"
32 )
33
34 def parent_folder(self) -> "TypedPath":
35 if self.absolute_path == "/":
36 raise ValueError("Path does not have a parent folder")
37 trimmed_path = self.absolute_path.rstrip("/")
38 last_idx = trimmed_path.rfind("/")
39 return TypedPath.of_folder(trimmed_path[: last_idx + 1])
40
41 def resolve_file(self, child: str) -> "TypedPath":
42 self.assert_folder()
43 if child.startswith("/") or child.endswith("/"):
44 raise ValueError("Child path is not valid for file resolution")
45 return self.of_file(f"{self.absolute_path}{child}")
46
47 def resolve_folder(self, child: str) -> "TypedPath":
48 self.assert_folder()
49 if child.startswith("/"):
50 raise ValueError("Child path starts with a slash")
51 return self.of_folder(f"{self.absolute_path}{child}/")
52
53 def relativize(self, child: "TypedPath") -> str:
54 self.assert_folder()
55 if not child.absolute_path.startswith(self.absolute_path):
56 raise ValueError(f"Expected {child} to start with {self.absolute_path}")
57 return child.absolute_path[len(self.absolute_path) :]
58
59 def __eq__(self, other: object) -> bool:
60 if not isinstance(other, TypedPath):
61 return NotImplemented
62 return self.absolute_path == other.absolute_path
63
64 def __lt__(self, other: "TypedPath") -> bool:
65 return self.absolute_path < other.absolute_path
66
67 @classmethod
68 def of_folder(cls, path: str) -> "TypedPath":
69 unix_path = path.replace("\\", "/")
70 if not unix_path.endswith("/"):
71 unix_path += "/"
72 return cls(unix_path)
73
74 @classmethod
75 def of_file(cls, path: str) -> "TypedPath":
76 unix_path = path.replace("\\", "/")
77 if unix_path.endswith("/"):
78 raise ValueError("Expected path to not end with a slash for a file")
79 return cls(unix_path)
"TypedPath" of_file(cls, str path)
Definition TypedPath.py:75
str relativize(self, "TypedPath" child)
Definition TypedPath.py:53
"TypedPath" resolve_file(self, str child)
Definition TypedPath.py:41
bool __lt__(self, "TypedPath" other)
Definition TypedPath.py:64
bool __eq__(self, object other)
Definition TypedPath.py:59
"TypedPath" resolve_folder(self, str child)
Definition TypedPath.py:47
"TypedPath" of_folder(cls, str path)
Definition TypedPath.py:68
"TypedPath" parent_folder(self)
Definition TypedPath.py:34
__init__(self, str absolute_path)
Definition TypedPath.py:6