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.
This commit is contained in:
parent
5e397b1928
commit
751ab5eaac
1 changed files with 10 additions and 6 deletions
|
@ -38,7 +38,7 @@ class Path:
|
||||||
self.absolute_path = normalize_sep(self.absolute_path, self.sep)
|
self.absolute_path = normalize_sep(self.absolute_path, self.sep)
|
||||||
|
|
||||||
def __contains__(self, other):
|
def __contains__(self, other):
|
||||||
other = Path(other, force_sep=self.force_sep)
|
other = self.spawn(other)
|
||||||
|
|
||||||
self_norm = self.normcase
|
self_norm = self.normcase
|
||||||
if not self_norm.endswith(self.sep):
|
if not self_norm.endswith(self.sep):
|
||||||
|
@ -113,7 +113,8 @@ class Path:
|
||||||
def join(self, subpath):
|
def join(self, subpath):
|
||||||
if not isinstance(subpath, str):
|
if not isinstance(subpath, str):
|
||||||
raise TypeError('subpath must be a string')
|
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):
|
def listdir(self):
|
||||||
children = os.listdir(self.absolute_path)
|
children = os.listdir(self.absolute_path)
|
||||||
|
@ -129,15 +130,14 @@ class Path:
|
||||||
@property
|
@property
|
||||||
def parent(self):
|
def parent(self):
|
||||||
parent = os.path.dirname(self.absolute_path)
|
parent = os.path.dirname(self.absolute_path)
|
||||||
parent = self.__class__(parent, force_sep=self.force_sep)
|
return self.spawn(parent)
|
||||||
return parent
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def relative_path(self):
|
def relative_path(self):
|
||||||
return self.relative_to(os.getcwd())
|
return self.relative_to(os.getcwd())
|
||||||
|
|
||||||
def relative_to(self, other, simple=False):
|
def relative_to(self, other, simple=False):
|
||||||
other = Path(other, force_sep=self.force_sep)
|
other = self.spawn(other)
|
||||||
|
|
||||||
if self == other:
|
if self == other:
|
||||||
return '.'
|
return '.'
|
||||||
|
@ -158,7 +158,7 @@ class Path:
|
||||||
if common is None:
|
if common is None:
|
||||||
return self.absolute_path
|
return self.absolute_path
|
||||||
|
|
||||||
common = Path(common, force_sep=self.force_sep)
|
common = self.spawn(common)
|
||||||
backsteps = other.depth - common.depth
|
backsteps = other.depth - common.depth
|
||||||
backsteps = self.sep.join('..' for x in range(backsteps))
|
backsteps = self.sep.join('..' for x in range(backsteps))
|
||||||
common = common.absolute_path
|
common = common.absolute_path
|
||||||
|
@ -185,6 +185,9 @@ class Path:
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def spawn(self, path):
|
||||||
|
return self.__class__(path, force_sep=self.force_sep)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def stat(self):
|
def stat(self):
|
||||||
return os.stat(self.absolute_path)
|
return os.stat(self.absolute_path)
|
||||||
|
@ -212,6 +215,7 @@ def common_path(paths, fallback):
|
||||||
'''
|
'''
|
||||||
if isinstance(paths, (str, Path)):
|
if isinstance(paths, (str, Path)):
|
||||||
raise TypeError('`paths` must be a collection')
|
raise TypeError('`paths` must be a collection')
|
||||||
|
|
||||||
paths = [Path(f) for f in paths]
|
paths = [Path(f) for f in paths]
|
||||||
|
|
||||||
if len(paths) == 0:
|
if len(paths) == 0:
|
||||||
|
|
Loading…
Reference in a new issue