Warn when a marked file is added again

This commit is contained in:
I-Al-Istannen 2020-05-10 21:37:48 +02:00
parent c1b21f7772
commit e7b08420ba
2 changed files with 16 additions and 2 deletions

View File

@ -1,3 +1,6 @@
"""
Utility functions and a scraper/downloader for the KIT DIVA portal.
"""
import logging import logging
import re import re
from dataclasses import dataclass from dataclasses import dataclass

View File

@ -16,8 +16,6 @@ from .utils import prompt_yes_no
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
PRETTY = PrettyLogger(LOGGER) PRETTY = PrettyLogger(LOGGER)
# TODO prevent overwriting an already marked file
class FileAcceptException(Exception): class FileAcceptException(Exception):
"""An exception while accepting a file.""" """An exception while accepting a file."""
@ -47,6 +45,12 @@ class Organizer(Location):
LOGGER.debug("Copying %s to %s", src_absolute, dst_absolute) 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 # Destination file is directory
if dst_absolute.exists() and dst_absolute.is_dir(): if dst_absolute.exists() and dst_absolute.is_dir():
if prompt_yes_no(f"Overwrite folder {dst_absolute} with file?", default=False): 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) self._known_files.add(absolute_path)
LOGGER.debug("Tracked %s", 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: def cleanup(self) -> None:
"""Remove all untracked files in the organizer's dir.""" """Remove all untracked files in the organizer's dir."""
LOGGER.debug("Deleting all untracked files...") LOGGER.debug("Deleting all untracked files...")