aboutsummaryrefslogtreecommitdiffstats
path: root/dotfiles/repository.py
blob: 2a461f2330f226f90724a51b9130ce2ac047a14e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import py
from .dotfile import Dotfile
from operator import attrgetter


class Repository(object):
    """
    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.lstrip()

    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