Add NotFile, NotDir exceptions and assert methods.
This commit is contained in:
parent
b4aafbc9bc
commit
958903e349
1 changed files with 21 additions and 1 deletions
|
@ -2,6 +2,19 @@ import glob
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class PathclassException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NotDirectory(PathclassException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NotFile(PathclassException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Path:
|
class Path:
|
||||||
'''
|
'''
|
||||||
I started to use pathlib.Path, but it was too much of a pain.
|
I started to use pathlib.Path, but it was too much of a pain.
|
||||||
|
@ -35,6 +48,14 @@ class Path:
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '{c}({path})'.format(c=self.__class__.__name__, path=repr(self.absolute_path))
|
return '{c}({path})'.format(c=self.__class__.__name__, path=repr(self.absolute_path))
|
||||||
|
|
||||||
|
def assert_is_file(self):
|
||||||
|
if not self.is_file:
|
||||||
|
raise NotFile(self)
|
||||||
|
|
||||||
|
def assert_is_directory(self):
|
||||||
|
if not self.is_dir:
|
||||||
|
raise NotDirectory(self)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def basename(self):
|
def basename(self):
|
||||||
return os.path.basename(self.absolute_path)
|
return os.path.basename(self.absolute_path)
|
||||||
|
@ -134,7 +155,6 @@ class Path:
|
||||||
return self.join(os.path.basename(basename))
|
return self.join(os.path.basename(basename))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def common_path(paths, fallback):
|
def common_path(paths, fallback):
|
||||||
'''
|
'''
|
||||||
Given a list of file paths, determine the deepest path which all
|
Given a list of file paths, determine the deepest path which all
|
||||||
|
|
Loading…
Reference in a new issue