From 7e93444f28a6790ab66fbdd8cc7eed9ca8970d48 Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 25 Apr 2021 20:12:25 +0200 Subject: Add kind of working delete command --- apply/writer.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'apply/writer.py') diff --git a/apply/writer.py b/apply/writer.py index d6a5550..9c23128 100644 --- a/apply/writer.py +++ b/apply/writer.py @@ -7,6 +7,7 @@ class Writer: self.links_todo = {} self.dirs_todo = [] self.delete_todo = [] + self.apply_hook = [] def create_link(self, target: Path, linkpath: Path): if linkpath in self.links_todo: @@ -26,14 +27,17 @@ class Writer: self.delete_todo.append(path) - def apply_dir(self, path: Path, dry_run): + def attach_hook(self, hook): + self.apply_hook.append(hook) + + def __apply_dir(self, path: Path, dry_run): print(f"mkdir {path}") if not dry_run: path.mkdir() - def apply_delete(self, path: Path, dry_run): - if path.is_dir(): - print(f"rmtree {path}!!!") + def __apply_delete(self, path: Path, dry_run): + if not path.is_symlink() and path.is_dir(): + print(f"rmtree {path}") if not dry_run: shutil.rmtree(str(path)) else: @@ -41,17 +45,20 @@ class Writer: if not dry_run: path.unlink() - def apply_link(self, linkpath: Path, target: Path, dry_run): + def __apply_link(self, linkpath: Path, target: Path, dry_run): print(f"link {linkpath} -> {target}") if not dry_run: linkpath.symlink_to(target) def apply(self, dry_run=True): for d in self.delete_todo: - self.apply_delete(d, dry_run) + self.__apply_delete(d, dry_run) for d in self.dirs_todo: - self.apply_dir(d, dry_run) + self.__apply_dir(d, dry_run) for link, target in self.links_todo.items(): - self.apply_link(link, target, dry_run) + self.__apply_link(link, target, dry_run) + + for hook in self.apply_hook: + hook(dry_run) -- cgit v1.2.3