Add NotFile, NotDir exceptions and assert methods.

master
Ethan Dalool 2018-09-21 21:33:05 -07:00
parent b4aafbc9bc
commit 958903e349
1 changed files with 21 additions and 1 deletions

View File

@ -2,6 +2,19 @@ import glob
import os
import re
class PathclassException(Exception):
pass
class NotDirectory(PathclassException):
pass
class NotFile(PathclassException):
pass
class Path:
'''
I started to use pathlib.Path, but it was too much of a pain.
@ -35,6 +48,14 @@ class Path:
def __repr__(self):
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
def basename(self):
return os.path.basename(self.absolute_path)
@ -134,7 +155,6 @@ class Path:
return self.join(os.path.basename(basename))
def common_path(paths, fallback):
'''
Given a list of file paths, determine the deepest path which all