diff options
Diffstat (limited to 'dotfiles/repository.py')
-rw-r--r-- | dotfiles/repository.py | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/dotfiles/repository.py b/dotfiles/repository.py index 8d118cf..c231a55 100644 --- a/dotfiles/repository.py +++ b/dotfiles/repository.py @@ -6,6 +6,7 @@ from fnmatch import fnmatch from operator import attrgetter from .dotfile import Dotfile +from .exceptions import RepositoryNotFound from .exceptions import DotfileException, TargetIgnored from .exceptions import NotRootedInHome, InRepository, IsDirectory @@ -26,6 +27,7 @@ class Repositories(object): class Repository(object): """A repository is a directory that contains dotfiles.""" + CREATE_REPOSITORY = False REMOVE_LEADING_DOT = True IGNORE_PATTERNS = ['.git/*', '.gitignore', 'README*', '*~'] @@ -34,8 +36,11 @@ class Repository(object): self.home = Path(home).expanduser().resolve() if not self.path.exists(): - echo('Creating new repository: %s' % self.path) - self.path.mkdir(parents=True, exist_ok=True) + if self.CREATE_REPOSITORY: + self.path.mkdir(parents=True, exist_ok=True) + echo('Created new repository: %s' % self.path) + else: + raise RepositoryNotFound(self.path) if not self.home.exists(): raise FileNotFoundError(self.home) @@ -83,6 +88,7 @@ class Repository(object): if not fnmatch(str(path), '%s/*' % self.home): raise NotRootedInHome(path) if fnmatch(str(path), '%s/*' % self.path): + print('oh no') raise InRepository(path) if self._ignore(target): raise TargetIgnored(path) @@ -98,12 +104,15 @@ class Repository(object): return [x for x in dir.rglob('*') if not skip(x)] - def contents(self): + def contents(self, subdir=None): """Return dotfile objects for each file in the repository.""" def construct(target): return Dotfile(self._dotfile_path(target), target) - contents = self._contents(self.path) + path = subdir if subdir else self.path + path.relative_to(self.path) + + contents = self._contents(path) return sorted(map(construct, contents), key=attrgetter('name')) def dotfiles(self, paths): @@ -116,9 +125,14 @@ class Repository(object): dotfiles is returned to the caller. """ paths = [Path(x).expanduser().absolute() for x in paths] + dotfiles = [] + + # if dir doesn't exist, this breaks + # need to get entire list and compare each one as a subset for path in paths: if path.is_dir(): + dotfiles.extend(self.contents(self._dotfile_target(path))) paths.extend(self._contents(path)) paths.remove(path) @@ -129,7 +143,11 @@ class Repository(object): echo(err) return None - return [d for d in map(construct, paths) if d is not None] + # return [d for d in map(construct, paths) if d is not None] + + dotfiles.extend([d for d in map(construct, paths) if d]) + return sorted(set(dotfiles)) + def prune(self, debug=False): """Remove any empty directories in the repository. |