From 3a8a5714337d54ca239a3add4dd663b9b05ba3af Mon Sep 17 00:00:00 2001 From: Jon Bernard Date: Mon, 11 Jan 2016 12:20:18 -0500 Subject: Add custom exceptions for dotfile validation --- dotfiles.py | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/dotfiles.py b/dotfiles.py index 3641ecd..7bae516 100644 --- a/dotfiles.py +++ b/dotfiles.py @@ -28,6 +28,41 @@ class TargetIgnored(DotfileException): DotfileException.__init__(self, message) +class IsDirectory(DotfileException): + + def __init__(self, path): + message = '%s is a directory' % path.basename + DotfileException.__init__(self, message) + + +class InRepository(DotfileException): + + def __init__(self, path): + message = '%s is within the repository' % path.basename + DotfileException.__init__(self, message) + + +class NotRootedInHome(DotfileException): + + def __init__(self, path): + message = '%s is not rooted in the home directory' % path.basename + DotfileException.__init__(self, message) + + +class IsNested(DotfileException): + + def __init__(self, path): + message = '%s is nested' % path.basename + DotfileException.__init__(self, message) + + +class NotADotfile(DotfileException): + + def __init__(self, path): + message = '%s is not a dotfile' % path.basename + DotfileException.__init__(self, message) + + class Repository(object): """A repository is a directory that contains dotfiles. @@ -66,26 +101,24 @@ class Repository(object): raise TargetIgnored(name) if name.check(dir=1): - raise Exception('%s is a directory' % name.basename) + raise IsDirectory(name) for path in name.parts(): try: if self.repodir.samefile(path): - raise Exception('%s is within the repository' % - name.basename) + raise InRepository(name) except py.error.ENOENT: # this occurs when the symlink does not yet exist continue if not self.homedir.samefile(name.dirname): - raise Exception('%s is not rooted in the home directory' % - name.basename) + raise NotRootedInHome(name) if name.dirname != self.homedir: - raise Exception('%s is nested' % name.basename) + raise IsNested(name) if name.basename[0] != '.': - raise Exception('%s is not a dotfile' % name.basename) + raise NotADotfile(name) return Dotfile(name, target) -- cgit v1.2.3