From 4ce385b262daa1064002d000ea75ea9f705c151e Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Sat, 5 Dec 2020 14:03:43 +0100 Subject: [PATCH] Treat file overwrite and marked file overwrite differently --- PFERD/organizer.py | 29 ++++++++++++++++++++++------- sync_url.py | 2 ++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/PFERD/organizer.py b/PFERD/organizer.py index a41d0d2..1038ae7 100644 --- a/PFERD/organizer.py +++ b/PFERD/organizer.py @@ -24,34 +24,44 @@ class ConflictType(Enum): """ The type of the conflict. A file might not exist anymore and will be deleted or it might be overwritten with a newer version. + + FILE_OVERWRITTEN: An existing file will be updated + MARKED_FILE_OVERWRITTEN: A file is written for the second+ time in this run + FILE_DELETED: The file was deleted """ FILE_OVERWRITTEN = "overwritten" + MARKED_FILE_OVERWRITTEN = "marked_file_overwritten" FILE_DELETED = "deleted" class FileConflictResolution(Enum): """ The reaction when confronted with a file conflict: + + DESTROY_EXISTING: Delete/overwrite the current file + KEEP_EXISTING: Keep the current file + DEFAULT: Do whatever the PFERD authors thought is sensible + PROMPT: Interactively ask the user """ DESTROY_EXISTING = "destroy" - """Delete/overwrite the current file""" KEEP_EXISTING = "keep" - """Keep the current file""" DEFAULT = "default" - """Do whatever the PFERD authors thought is sensible""" PROMPT = "prompt" - """Interactively ask the user""" FileConflictResolver = Callable[[PurePath, ConflictType], FileConflictResolution] -def resolve_prompt_user(_path: PurePath, _conflict: ConflictType) -> FileConflictResolution: - """Resolves conflicts by always asking the user.""" +def resolve_prompt_user(_path: PurePath, conflict: ConflictType) -> FileConflictResolution: + """ + Resolves conflicts by asking the user if a file was written twice or will be deleted. + """ + if conflict == ConflictType.FILE_OVERWRITTEN: + return FileConflictResolution.DESTROY_EXISTING return FileConflictResolution.PROMPT @@ -105,7 +115,7 @@ class Organizer(Location): if self._is_marked(dst): PRETTY.warning(f"File {str(dst_absolute)!r} was already written!") - conflict = ConflictType.FILE_OVERWRITTEN + conflict = ConflictType.MARKED_FILE_OVERWRITTEN if self._resolve_conflict(f"Overwrite file?", dst_absolute, conflict, default=False): PRETTY.ignored_file(dst_absolute, "file was written previously") return None @@ -128,6 +138,11 @@ class Organizer(Location): self.mark(dst) return dst_absolute + prompt = f"Overwrite file {dst_absolute}?" + conflict = ConflictType.FILE_OVERWRITTEN + if not self._resolve_conflict(prompt, dst_absolute, conflict, default=True): + return None + self.download_summary.add_modified_file(dst_absolute) PRETTY.modified_file(dst_absolute) else: diff --git a/sync_url.py b/sync_url.py index 97c0c81..c6231e4 100755 --- a/sync_url.py +++ b/sync_url.py @@ -30,6 +30,8 @@ def _resolve_no_delete(_path: PurePath, conflict: ConflictType) -> FileConflictR # Update files if conflict == ConflictType.FILE_OVERWRITTEN: return FileConflictResolution.DESTROY_EXISTING + if conflict == ConflictType.MARKED_FILE_OVERWRITTEN: + return FileConflictResolution.DESTROY_EXISTING # But do not delete them return FileConflictResolution.KEEP_EXISTING