From 751ab5eaaca4d359faf95a74c03e2ea468aca90f Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 20 Jan 2020 21:52:40 -0800 Subject: [PATCH] Add method spawn that inherits force_sep. I am considering some other instance attributes similar to force_sep. And since these need to be carried over into newly spawned Paths, I want to consolidate that into a single method so I don't have to risk forgetting it on a new object. --- voussoirkit/pathclass.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/voussoirkit/pathclass.py b/voussoirkit/pathclass.py index 3a9304e..5848746 100644 --- a/voussoirkit/pathclass.py +++ b/voussoirkit/pathclass.py @@ -38,7 +38,7 @@ class Path: self.absolute_path = normalize_sep(self.absolute_path, self.sep) def __contains__(self, other): - other = Path(other, force_sep=self.force_sep) + other = self.spawn(other) self_norm = self.normcase if not self_norm.endswith(self.sep): @@ -113,7 +113,8 @@ class Path: def join(self, subpath): if not isinstance(subpath, str): raise TypeError('subpath must be a string') - return Path(os.path.join(self.absolute_path, subpath), force_sep=self.force_sep) + path = os.path.join(self.absolute_path, subpath) + return self.spawn(path) def listdir(self): children = os.listdir(self.absolute_path) @@ -129,15 +130,14 @@ class Path: @property def parent(self): parent = os.path.dirname(self.absolute_path) - parent = self.__class__(parent, force_sep=self.force_sep) - return parent + return self.spawn(parent) @property def relative_path(self): return self.relative_to(os.getcwd()) def relative_to(self, other, simple=False): - other = Path(other, force_sep=self.force_sep) + other = self.spawn(other) if self == other: return '.' @@ -158,7 +158,7 @@ class Path: if common is None: return self.absolute_path - common = Path(common, force_sep=self.force_sep) + common = self.spawn(common) backsteps = other.depth - common.depth backsteps = self.sep.join('..' for x in range(backsteps)) common = common.absolute_path @@ -185,6 +185,9 @@ class Path: else: return None + def spawn(self, path): + return self.__class__(path, force_sep=self.force_sep) + @property def stat(self): return os.stat(self.absolute_path) @@ -212,6 +215,7 @@ def common_path(paths, fallback): ''' if isinstance(paths, (str, Path)): raise TypeError('`paths` must be a collection') + paths = [Path(f) for f in paths] if len(paths) == 0: