Fix Path.get_relative

This commit is contained in:
László Károlyi 2024-03-17 14:07:46 +01:00
parent f1faf9a7fc
commit 5137746f83
Signed by: karolyi
GPG Key ID: 2DCAF25E55735BFE
1 changed files with 8 additions and 7 deletions

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from logging import getLogger
from os import chown
from os import name as os_name
from os import umask
from os import pardir, umask
from pathlib import Path as PathBase
from pathlib import PosixPath as PosixPathBase
from pathlib import WindowsPath as WindowsPathBase
@ -90,13 +90,14 @@ class Path(PathBase):
"""
if not self.is_absolute() or not to.is_absolute():
raise ValueError(f'{self!r} or {to!r} is not absolute.')
items_from = self.parts
items_to = to.parts
parts_from = self.parts
parts_to = to.parts
# Remove identical path prefix parts
while items_from[0] == items_to[0]:
items_from = items_from[1:]
items_to = items_to[1:]
return Path(*('..' for x in range(1, len(items_from))), *items_to)
while parts_from and parts_to and parts_from[0] == parts_to[0]:
parts_from = parts_from[1:]
parts_to = parts_to[1:]
parents = len(parts_from) - 1
return Path(*(pardir,) * parents, *parts_to)
def remove_up_to(self, parent: Path):
'Remove the paths in `self` until the passed `parent`.'