Add passive/no_prompt flag

This commit is contained in:
Christophe 2020-12-02 16:58:36 +01:00
parent ecdbca8fb6
commit f3a4663491
3 changed files with 22 additions and 7 deletions

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()
@ -57,7 +59,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
) )