aboutsummaryrefslogtreecommitdiffstats
path: root/dotfiles.py
diff options
context:
space:
mode:
authorGravatar Jon Bernard <jbernard@tuxion.com> 2016-01-10 09:52:42 -0500
committerGravatar Jon Bernard <jbernard@tuxion.com> 2016-01-10 09:53:51 -0500
commit0366d9ee47a742613c13998e0f4621bd32c8f681 (patch)
treee70eef6466f9f139cbba29c7eee813fb4e6fd4ab /dotfiles.py
parent5566e5669258f923906fc3d7a4db40b6e32f27f1 (diff)
downloaddotfiles-0366d9ee47a742613c13998e0f4621bd32c8f681.tar.gz
dotfiles-0366d9ee47a742613c13998e0f4621bd32c8f681.tar.bz2
dotfiles-0366d9ee47a742613c13998e0f4621bd32c8f681.zip
Introduce custom exceptions for better error handling
Diffstat (limited to 'dotfiles.py')
-rw-r--r--dotfiles.py37
1 files changed, 27 insertions, 10 deletions
diff --git a/dotfiles.py b/dotfiles.py
index 8d6320d..3641ecd 100644
--- a/dotfiles.py
+++ b/dotfiles.py
@@ -10,6 +10,24 @@ DEFAULT_REPO_PATH = os.path.expanduser('~/Dotfiles')
DEFAULT_REPO_IGNORE = ['.git', '.gitignore']
+class DotfileException(Exception):
+ """An exception the CLI can handle and show to the user."""
+
+ def __init__(self, message):
+ Exception.__init__(self, message)
+ self.message = message
+
+ def __str__(self):
+ return 'Error: %s' % self.message
+
+
+class TargetIgnored(DotfileException):
+
+ def __init__(self, path):
+ message = '%s targets an ignored file' % path.basename
+ DotfileException.__init__(self, message)
+
+
class Repository(object):
"""A repository is a directory that contains dotfiles.
@@ -41,12 +59,11 @@ class Repository(object):
return self.repodir.join(name.basename[1:])
def dotfile(self, name):
- """Return a valid dotfile for the given path.
+ """Return a valid dotfile for the given path."""
- Sanity checks occur here to ensure validity. Specifically, the file
- must exist and not reside within the repository. Further validation
- will occur during dotfile operation.
- """
+ target = self._name_to_target(name)
+ if target.basename in self.ignore:
+ raise TargetIgnored(name)
if name.check(dir=1):
raise Exception('%s is a directory' % name.basename)
@@ -70,10 +87,7 @@ class Repository(object):
if name.basename[0] != '.':
raise Exception('%s is not a dotfile' % name.basename)
- if self._name_to_target(name).basename in self.ignore:
- raise Exception('%s targets an ignored file' % name.basename)
-
- return Dotfile(name, self._name_to_target(name))
+ return Dotfile(name, target)
def contents(self):
"""Return a list of all dotfiles in the repository path."""
@@ -180,7 +194,10 @@ def cli(ctx, repository):
def add(repo, verbose, files):
"""Replace file with symlink."""
for filename in files:
- repo.dotfile(py.path.local(filename)).add(verbose)
+ try:
+ repo.dotfile(py.path.local(filename)).add(verbose)
+ except DotfileException as err:
+ click.echo(err)
@cli.command()