mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Add --remote-first, --local-first and --no-delete flags
This commit is contained in:
parent
9f6dc56a7b
commit
fcb3884a8f
@ -20,21 +20,37 @@ LOGGER = logging.getLogger(__name__)
|
|||||||
PRETTY = PrettyLogger(LOGGER)
|
PRETTY = PrettyLogger(LOGGER)
|
||||||
|
|
||||||
|
|
||||||
|
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 = "overwritten"
|
||||||
|
FILE_DELETED = "deleted"
|
||||||
|
|
||||||
|
|
||||||
class FileConflictResolution(Enum):
|
class FileConflictResolution(Enum):
|
||||||
"""
|
"""
|
||||||
The reaction when confronted with a file conflict.
|
The reaction when confronted with a file conflict:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
OVERWRITE_EXISTING = "overwrite"
|
DESTROY_EXISTING = "destroy"
|
||||||
|
"""Delete/overwrite the current file"""
|
||||||
|
|
||||||
KEEP_EXISTING = "keep"
|
KEEP_EXISTING = "keep"
|
||||||
|
"""Keep the current file"""
|
||||||
|
|
||||||
DEFAULT = "default"
|
DEFAULT = "default"
|
||||||
|
"""Do whatever the PFERD authors thought is sensible"""
|
||||||
|
|
||||||
PROMPT = "prompt"
|
PROMPT = "prompt"
|
||||||
|
"""Interactively ask the user"""
|
||||||
|
|
||||||
|
|
||||||
FileConflictResolver = Callable[[PurePath], FileConflictResolution]
|
FileConflictResolver = Callable[[PurePath, ConflictType], FileConflictResolution]
|
||||||
|
|
||||||
|
|
||||||
def resolve_prompt_user(_path: PurePath) -> FileConflictResolution:
|
def resolve_prompt_user(_path: PurePath, _conflict: ConflictType) -> FileConflictResolution:
|
||||||
"""Resolves conflicts by always asking the user."""
|
"""Resolves conflicts by always asking the user."""
|
||||||
return FileConflictResolution.PROMPT
|
return FileConflictResolution.PROMPT
|
||||||
|
|
||||||
@ -89,14 +105,16 @@ class Organizer(Location):
|
|||||||
|
|
||||||
if self._is_marked(dst):
|
if self._is_marked(dst):
|
||||||
PRETTY.warning(f"File {str(dst_absolute)!r} was already written!")
|
PRETTY.warning(f"File {str(dst_absolute)!r} was already written!")
|
||||||
if self._resolve_conflict(f"Overwrite file?", dst_absolute, default=False):
|
conflict = ConflictType.FILE_OVERWRITTEN
|
||||||
|
if self._resolve_conflict(f"Overwrite file?", dst_absolute, conflict, default=False):
|
||||||
PRETTY.ignored_file(dst_absolute, "file was written previously")
|
PRETTY.ignored_file(dst_absolute, "file was written previously")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 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():
|
||||||
prompt = f"Overwrite folder {dst_absolute} with file?"
|
prompt = f"Overwrite folder {dst_absolute} with file?"
|
||||||
if self._resolve_conflict(prompt, dst_absolute, default=False):
|
conflict = ConflictType.FILE_OVERWRITTEN
|
||||||
|
if self._resolve_conflict(prompt, dst_absolute, conflict, default=False):
|
||||||
shutil.rmtree(dst_absolute)
|
shutil.rmtree(dst_absolute)
|
||||||
else:
|
else:
|
||||||
PRETTY.warning(f"Could not add file {str(dst_absolute)!r}")
|
PRETTY.warning(f"Could not add file {str(dst_absolute)!r}")
|
||||||
@ -167,20 +185,22 @@ class Organizer(Location):
|
|||||||
def _delete_file_if_confirmed(self, path: Path) -> None:
|
def _delete_file_if_confirmed(self, path: Path) -> None:
|
||||||
prompt = f"Do you want to delete {path}"
|
prompt = f"Do you want to delete {path}"
|
||||||
|
|
||||||
if self._resolve_conflict(prompt, path, default=False):
|
if self._resolve_conflict(prompt, path, ConflictType.FILE_DELETED, default=False):
|
||||||
self.download_summary.add_deleted_file(path)
|
self.download_summary.add_deleted_file(path)
|
||||||
path.unlink()
|
path.unlink()
|
||||||
|
|
||||||
def _resolve_conflict(self, prompt: str, path: Path, default: bool) -> bool:
|
def _resolve_conflict(
|
||||||
|
self, prompt: str, path: Path, conflict: ConflictType, default: bool
|
||||||
|
) -> bool:
|
||||||
if not self.conflict_resolver:
|
if not self.conflict_resolver:
|
||||||
return prompt_yes_no(prompt, default=default)
|
return prompt_yes_no(prompt, default=default)
|
||||||
|
|
||||||
result = self.conflict_resolver(path)
|
result = self.conflict_resolver(path, conflict)
|
||||||
if result == FileConflictResolution.DEFAULT:
|
if result == FileConflictResolution.DEFAULT:
|
||||||
return default
|
return default
|
||||||
if result == FileConflictResolution.KEEP_EXISTING:
|
if result == FileConflictResolution.KEEP_EXISTING:
|
||||||
return False
|
return False
|
||||||
if result == FileConflictResolution.OVERWRITE_EXISTING:
|
if result == FileConflictResolution.DESTROY_EXISTING:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return prompt_yes_no(prompt, default=default)
|
return prompt_yes_no(prompt, default=default)
|
||||||
|
39
sync_url.py
39
sync_url.py
@ -12,17 +12,26 @@ from PFERD import Pferd
|
|||||||
from PFERD.cookie_jar import CookieJar
|
from PFERD.cookie_jar import CookieJar
|
||||||
from PFERD.ilias import (IliasCrawler, IliasElementType,
|
from PFERD.ilias import (IliasCrawler, IliasElementType,
|
||||||
KitShibbolethAuthenticator)
|
KitShibbolethAuthenticator)
|
||||||
from PFERD.organizer import FileConflictResolution, resolve_prompt_user
|
from PFERD.organizer import (ConflictType, FileConflictResolution,
|
||||||
|
FileConflictResolver, resolve_prompt_user)
|
||||||
from PFERD.transform import sanitize_windows_path
|
from PFERD.transform import sanitize_windows_path
|
||||||
from PFERD.utils import to_path
|
from PFERD.utils import to_path
|
||||||
|
|
||||||
|
|
||||||
def _resolve_overwrite(_path: PurePath) -> FileConflictResolution:
|
def _resolve_remote_first(_path: PurePath, _conflict: ConflictType) -> FileConflictResolution:
|
||||||
return FileConflictResolution.OVERWRITE_EXISTING
|
return FileConflictResolution.DESTROY_EXISTING
|
||||||
|
|
||||||
|
|
||||||
def _resolve_default(_path: PurePath) -> FileConflictResolution:
|
def _resolve_local_first(_path: PurePath, _conflict: ConflictType) -> FileConflictResolution:
|
||||||
return FileConflictResolution.DEFAULT
|
return FileConflictResolution.KEEP_EXISTING
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_no_delete(_path: PurePath, conflict: ConflictType) -> FileConflictResolution:
|
||||||
|
# Update files
|
||||||
|
if conflict == ConflictType.FILE_OVERWRITTEN:
|
||||||
|
return FileConflictResolution.DESTROY_EXISTING
|
||||||
|
# But do not delete them
|
||||||
|
return FileConflictResolution.KEEP_EXISTING
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
@ -30,10 +39,12 @@ def main() -> None:
|
|||||||
parser.add_argument("--test-run", action="store_true")
|
parser.add_argument("--test-run", action="store_true")
|
||||||
parser.add_argument('-c', '--cookies', nargs='?', default=None, help="File to store cookies in")
|
parser.add_argument('-c', '--cookies', nargs='?', default=None, help="File to store cookies in")
|
||||||
parser.add_argument('--no-videos', nargs='?', default=None, help="Don't download videos")
|
parser.add_argument('--no-videos', nargs='?', default=None, help="Don't download videos")
|
||||||
parser.add_argument('-d', '--default', action="store_true",
|
parser.add_argument('--local-first', action="store_true",
|
||||||
help="Don't prompt for confirmations and use sane defaults")
|
help="Don't prompt for confirmation, keep existing files")
|
||||||
parser.add_argument('-r', '--remove', action="store_true",
|
parser.add_argument('--remote-first', action="store_true",
|
||||||
help="Remove and overwrite files without prompting for confirmation")
|
help="Don't prompt for confirmation, delete and overwrite local files")
|
||||||
|
parser.add_argument('--no-delete', action="store_true",
|
||||||
|
help="Don't prompt for confirmation, overwrite local files, don't delete")
|
||||||
parser.add_argument('url', help="URL to the course page")
|
parser.add_argument('url', help="URL to the course page")
|
||||||
parser.add_argument('folder', nargs='?', default=None, help="Folder to put stuff into")
|
parser.add_argument('folder', nargs='?', default=None, help="Folder to put stuff into")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@ -68,10 +79,12 @@ def main() -> None:
|
|||||||
return element not in [IliasElementType.VIDEO_FILE, IliasElementType.VIDEO_FOLDER]
|
return element not in [IliasElementType.VIDEO_FILE, IliasElementType.VIDEO_FOLDER]
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if args.default:
|
if args.remote_first:
|
||||||
file_confilict_resolver = _resolve_default
|
file_confilict_resolver: FileConflictResolver = _resolve_remote_first
|
||||||
elif args.remove:
|
elif args.local_first:
|
||||||
file_confilict_resolver = _resolve_overwrite
|
file_confilict_resolver = _resolve_local_first
|
||||||
|
elif args.no_delete:
|
||||||
|
file_confilict_resolver = _resolve_no_delete
|
||||||
else:
|
else:
|
||||||
file_confilict_resolver = resolve_prompt_user
|
file_confilict_resolver = resolve_prompt_user
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user