Add relative_to for arbitrary other.

This commit is contained in:
Ethan Dalool 2018-03-13 01:43:56 -07:00
parent 9b833b4e7f
commit b0950c3df1

View file

@ -89,19 +89,24 @@ class Path:
@property @property
def relative_path(self): def relative_path(self):
cwd = Path(os.getcwd()) return self.relative_to(os.getcwd())
cwd.correct_case()
def relative_to(self, other):
other = Path(other)
other.correct_case()
self.correct_case() self.correct_case()
if self == cwd:
if self == other:
return '.' return '.'
if self in cwd: if self in other:
return self.absolute_path.replace(cwd.absolute_path, '.') 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: if common is None:
return self.absolute_path return self.absolute_path
backsteps = cwd.depth - common.depth backsteps = other.depth - common.depth
backsteps = os.sep.join('..' for x in range(backsteps)) backsteps = os.sep.join('..' for x in range(backsteps))
return self.absolute_path.replace(common.absolute_path, backsteps) return self.absolute_path.replace(common.absolute_path, backsteps)