From e7b08420ba0e843f08141505ed19ee41fb38e5c8 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Sun, 10 May 2020 21:37:48 +0200 Subject: [PATCH] Warn when a marked file is added again --- PFERD/diva.py | 3 +++ PFERD/organizer.py | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/PFERD/diva.py b/PFERD/diva.py index d5c9240..c424d1d 100644 --- a/PFERD/diva.py +++ b/PFERD/diva.py @@ -1,3 +1,6 @@ +""" +Utility functions and a scraper/downloader for the KIT DIVA portal. +""" import logging import re from dataclasses import dataclass diff --git a/PFERD/organizer.py b/PFERD/organizer.py index 752697c..4570957 100644 --- a/PFERD/organizer.py +++ b/PFERD/organizer.py @@ -16,8 +16,6 @@ from .utils import prompt_yes_no LOGGER = logging.getLogger(__name__) PRETTY = PrettyLogger(LOGGER) -# TODO prevent overwriting an already marked file - class FileAcceptException(Exception): """An exception while accepting a file.""" @@ -47,6 +45,12 @@ class Organizer(Location): LOGGER.debug("Copying %s to %s", src_absolute, dst_absolute) + if self._is_marked(dst): + LOGGER.warning("File %r was already written!", str(dst_absolute)) + if not prompt_yes_no(f"Overwrite file?", default=False): + PRETTY.ignored_file(dst_absolute, "file was written previously") + return + # Destination file is directory if dst_absolute.exists() and dst_absolute.is_dir(): if prompt_yes_no(f"Overwrite folder {dst_absolute} with file?", default=False): @@ -82,6 +86,13 @@ class Organizer(Location): self._known_files.add(absolute_path) LOGGER.debug("Tracked %s", absolute_path) + def _is_marked(self, path: PurePath) -> bool: + """ + Checks whether a file is marked. + """ + absolute_path = self.resolve(path) + return absolute_path in self._known_files + def cleanup(self) -> None: """Remove all untracked files in the organizer's dir.""" LOGGER.debug("Deleting all untracked files...")