From b0950c3df161228cf5d4189c56b199843da26cfd Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 13 Mar 2018 01:43:56 -0700 Subject: [PATCH] Add relative_to for arbitrary other. --- Pathclass/pathclass.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Pathclass/pathclass.py b/Pathclass/pathclass.py index 178ea37..a7a6422 100644 --- a/Pathclass/pathclass.py +++ b/Pathclass/pathclass.py @@ -89,19 +89,24 @@ class Path: @property def relative_path(self): - cwd = Path(os.getcwd()) - cwd.correct_case() + return self.relative_to(os.getcwd()) + + def relative_to(self, other): + other = Path(other) + other.correct_case() self.correct_case() - if self == cwd: + + if self == other: return '.' - if self in cwd: - return self.absolute_path.replace(cwd.absolute_path, '.') + if self in other: + return self.absolute_path.replace(other.absolute_path, '.') - common = common_path([os.getcwd(), self.absolute_path], fallback=None) + common = common_path([other.absolute_path, self.absolute_path], fallback=None) + print(common) if common is None: return self.absolute_path - backsteps = cwd.depth - common.depth + backsteps = other.depth - common.depth backsteps = os.sep.join('..' for x in range(backsteps)) return self.absolute_path.replace(common.absolute_path, backsteps)