Merge pull request #17 from TheChristophe/master

Add flag to make sync_url use defaults instead of prompting
This commit is contained in:
I-Al-Istannen 2020-12-02 19:04:46 +01:00 committed by GitHub
commit 56ab473611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 19 deletions

View File

@ -1,4 +1,4 @@
Copyright 2019-2020 Garmelon, I-Al-Istannen, danstooamerican, pavelzw Copyright 2019-2020 Garmelon, I-Al-Istannen, danstooamerican, pavelzw, TheChristophe
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in

View File

@ -26,7 +26,7 @@ class FileAcceptException(Exception):
class Organizer(Location): class Organizer(Location):
"""A helper for managing downloaded files.""" """A helper for managing downloaded files."""
def __init__(self, path: Path): def __init__(self, path: Path, no_prompt: bool = False):
"""Create a new organizer for a given path.""" """Create a new organizer for a given path."""
super().__init__(path) super().__init__(path)
self._known_files: Set[Path] = set() self._known_files: Set[Path] = set()
@ -36,6 +36,8 @@ class Organizer(Location):
self.download_summary = DownloadSummary() self.download_summary = DownloadSummary()
self.not_prompting = no_prompt
def accept_file(self, src: Path, dst: PurePath) -> Optional[Path]: def accept_file(self, src: Path, dst: PurePath) -> Optional[Path]:
""" """
Move a file to this organizer and mark it. Move a file to this organizer and mark it.
@ -67,13 +69,18 @@ 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 not prompt_yes_no(f"Overwrite file?", default=False): default_action: bool = False
if self.not_prompting and not default_action \
or not self.not_prompting and not prompt_yes_no(f"Overwrite file?", default=default_action):
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():
if prompt_yes_no(f"Overwrite folder {dst_absolute} with file?", default=False): default_action: bool = False
if self.not_prompting and default_action \
or not self.not_prompting \
and prompt_yes_no(f"Overwrite folder {dst_absolute} with file?", default=default_action):
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}")
@ -144,6 +151,8 @@ 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 prompt_yes_no(prompt, False): default_action: bool = False
if self.not_prompting and default_action or \
not self.not_prompting and prompt_yes_no(prompt, default_action):
self.download_summary.add_deleted_file(path) self.download_summary.add_deleted_file(path)
path.unlink() path.unlink()

View File

@ -76,12 +76,13 @@ class Pferd(Location):
download_strategy: IliasDownloadStrategy, download_strategy: IliasDownloadStrategy,
timeout: int, timeout: int,
clean: bool = True, clean: bool = True,
no_prompt: bool = None
) -> Organizer: ) -> Organizer:
# pylint: disable=too-many-locals # pylint: disable=too-many-locals
cookie_jar = CookieJar(to_path(cookies) if cookies else None) cookie_jar = CookieJar(to_path(cookies) if cookies else None)
session = cookie_jar.create_session() session = cookie_jar.create_session()
tmp_dir = self._tmp_dir.new_subdir() tmp_dir = self._tmp_dir.new_subdir()
organizer = Organizer(self.resolve(to_path(target))) organizer = Organizer(self.resolve(to_path(target)), no_prompt if no_prompt is not None else False)
crawler = IliasCrawler(base_url, session, authenticator, dir_filter) crawler = IliasCrawler(base_url, session, authenticator, dir_filter)
downloader = IliasDownloader(tmp_dir, organizer, session, downloader = IliasDownloader(tmp_dir, organizer, session,
@ -245,6 +246,7 @@ class Pferd(Location):
download_strategy: IliasDownloadStrategy = download_modified_or_new, download_strategy: IliasDownloadStrategy = download_modified_or_new,
clean: bool = True, clean: bool = True,
timeout: int = 5, timeout: int = 5,
no_prompt: bool = None
) -> Organizer: ) -> Organizer:
""" """
Synchronizes a folder with a given folder on the ILIAS instance of the KIT. Synchronizes a folder with a given folder on the ILIAS instance of the KIT.
@ -289,7 +291,8 @@ class Pferd(Location):
transform=transform, transform=transform,
download_strategy=download_strategy, download_strategy=download_strategy,
clean=clean, clean=clean,
timeout=timeout timeout=timeout,
no_prompt=no_prompt
) )
self._download_summary.merge(organizer.download_summary) self._download_summary.merge(organizer.download_summary)

View File

@ -21,6 +21,8 @@ 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('-p', '--passive', action="store_true",
help="Don't prompt for confirmations and use sane defaults")
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()
@ -35,19 +37,16 @@ def main() -> None:
cookie_jar.load_cookies() cookie_jar.load_cookies()
if args.folder is not None: folder = Path(args.folder)
folder = args.folder if args.folder is None:
# Initialize pferd at the *parent of the passed folder* folder = Path(crawler.find_element_name(args.url))
# This is needed so Pferd's internal protections against escaping the working directory
# do not trigger (e.g. if somebody names a file in ILIAS '../../bad thing.txt')
pferd = Pferd(Path(Path(__file__).parent, folder).parent, test_run=args.test_run)
else:
# fetch course name from ilias
folder = crawler.find_element_name(args.url)
cookie_jar.save_cookies() cookie_jar.save_cookies()
# Initialize pferd at the location of the script # files may not escape the pferd_root with relative paths
pferd = Pferd(Path(__file__).parent, test_run=args.test_run) # note: Path(Path.cwd, Path(folder)) == Path(folder) if it is an absolute path
pferd_root = Path(Path.cwd(), Path(folder)).parent
folder = folder.name
pferd = Pferd(pferd_root, test_run=args.test_run)
def dir_filter(_: Path, element: IliasElementType) -> bool: def dir_filter(_: Path, element: IliasElementType) -> bool:
if args.no_videos: if args.no_videos:
@ -61,7 +60,8 @@ def main() -> None:
full_url=args.url, full_url=args.url,
cookies=args.cookies, cookies=args.cookies,
dir_filter=dir_filter, dir_filter=dir_filter,
transform=sanitize_windows_path transform=sanitize_windows_path,
no_prompt=args.passive
) )