diff options
-rw-r--r-- | dotfiles/repository.py | 60 | ||||
-rw-r--r-- | tests/test_repository.py | 38 |
2 files changed, 98 insertions, 0 deletions
diff --git a/dotfiles/repository.py b/dotfiles/repository.py new file mode 100644 index 0000000..aa42c65 --- /dev/null +++ b/dotfiles/repository.py @@ -0,0 +1,60 @@ +import py +from .dotfile import Dotfile +from operator import attrgetter + + +class Repository: + """ + This class implements the 'repository' abstraction. + + A repository is a directory that contains dotfiles. + + TODO + """ + + def __init__(self, path, homedir): + self.path = path + self.homedir = homedir + self.dotfiles = self._discover() + + def _target_to_name(self, target): + return py.path.local("{}/.{}".format(self.homedir, target.basename)) + + def _discover(self): + """Given a repository path, discover all existing dotfiles.""" + dotfiles = [] + for target in self.path.listdir(): + target = py.path.local(target) + name = self._target_to_name(target) + dotfiles.append(Dotfile(name, target)) + return sorted(dotfiles, key=attrgetter('name')) + + def _list(self, all=True): + listing = '' + for dotfile in self.dotfiles: + if all or dotfile.invalid(): + listing += '\n{}'.format(dotfile) + return listing + + def __str__(self): + """Returns a string list the all dotfiles in this repository.""" + return self._list() + + def check(self): + """Returns a string of only unsynced or missing dotfiles.""" + return self._list(all=False) + + def sync(self): + raise NotImplementedError + + def unsync(self): + raise NotImplementedError + + def add(self): + raise NotImplementedError + + def remove(self): + raise NotImplementedError + + def move(self): + raise NotImplementedError diff --git a/tests/test_repository.py b/tests/test_repository.py new file mode 100644 index 0000000..60c4cc4 --- /dev/null +++ b/tests/test_repository.py @@ -0,0 +1,38 @@ +from dotfiles.dotfile import Dotfile +from dotfiles.repository import Repository + + +def test_list(tmpdir): + + repo = tmpdir.ensure("Dotfiles", dir=1) + name = tmpdir.join(".vimrc") + target = repo.ensure("vimrc") + + dotfile = Dotfile(name, target) + repository = Repository(repo, tmpdir) + + # manual discovery + repository.dotfiles = [dotfile, dotfile, dotfile] + + expected_list = ("\n" + ".vimrc -> Dotfiles/vimrc (unknown)\n" + ".vimrc -> Dotfiles/vimrc (unknown)\n" + ".vimrc -> Dotfiles/vimrc (unknown)") + + assert expected_list == str(repository) + + +def test_discovery(tmpdir): + + repo = tmpdir.ensure("Dotfiles", dir=1) + + tmpdir.join('.bashrc').mksymlinkto(repo.ensure('bashrc')) + tmpdir.join('.vimrc').mksymlinkto(repo.ensure('vimrc')) + + repository = Repository(repo, tmpdir) + + expected_list = ("\n" + ".bashrc -> Dotfiles/bashrc (unknown)\n" + ".vimrc -> Dotfiles/vimrc (unknown)") + + assert expected_list == str(repository) |