mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Move download summary into a separate class
This commit is contained in:
parent
6c034209b6
commit
739522a151
50
PFERD/download_summary.py
Normal file
50
PFERD/download_summary.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
class DownloadSummary:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._new_files = []
|
||||||
|
self._changed_files = []
|
||||||
|
self._deleted_files = []
|
||||||
|
|
||||||
|
def merge(self, summary: 'DownloadSummary') -> None:
|
||||||
|
self._new_files += summary._new_files
|
||||||
|
self._changed_files += summary._changed_files
|
||||||
|
self._deleted_files += summary._deleted_files
|
||||||
|
|
||||||
|
def add_deleted_file(self, path) -> None:
|
||||||
|
self._deleted_files.append(path)
|
||||||
|
|
||||||
|
def add_changed_file(self, path) -> None:
|
||||||
|
self._changed_files.append(path)
|
||||||
|
|
||||||
|
def add_new_file(self, path) -> None:
|
||||||
|
self._new_files.append(path)
|
||||||
|
|
||||||
|
def _has_no_updates(self) -> bool:
|
||||||
|
return len(self._new_files) == 0 and len(self._changed_files) == 0 and len(self._deleted_files) == 0
|
||||||
|
|
||||||
|
def print(self, logger, pretty) -> None:
|
||||||
|
logger.info("")
|
||||||
|
logger.info("Summary: ")
|
||||||
|
if self._has_no_updates():
|
||||||
|
logger.info("nothing changed")
|
||||||
|
else:
|
||||||
|
if len(self._new_files) > 0:
|
||||||
|
logger.info("New Files:")
|
||||||
|
for file in self._new_files:
|
||||||
|
pretty.new_file(file)
|
||||||
|
|
||||||
|
logger.info("")
|
||||||
|
|
||||||
|
if len(self._changed_files) > 0:
|
||||||
|
logger.info("Modified Files:")
|
||||||
|
for file in self._changed_files:
|
||||||
|
pretty.modified_file(file)
|
||||||
|
|
||||||
|
logger.info("")
|
||||||
|
|
||||||
|
if len(self._deleted_files) > 0:
|
||||||
|
logger.info("Deleted Files:")
|
||||||
|
for file in self._deleted_files:
|
||||||
|
pretty.deleted_file(file)
|
||||||
|
|
||||||
|
logger.info("")
|
@ -9,6 +9,7 @@ import shutil
|
|||||||
from pathlib import Path, PurePath
|
from pathlib import Path, PurePath
|
||||||
from typing import List, Set
|
from typing import List, Set
|
||||||
|
|
||||||
|
from .download_summary import DownloadSummary
|
||||||
from .location import Location
|
from .location import Location
|
||||||
from .logging import PrettyLogger
|
from .logging import PrettyLogger
|
||||||
from .utils import prompt_yes_no
|
from .utils import prompt_yes_no
|
||||||
@ -24,10 +25,6 @@ class FileAcceptException(Exception):
|
|||||||
class Organizer(Location):
|
class Organizer(Location):
|
||||||
"""A helper for managing downloaded files."""
|
"""A helper for managing downloaded files."""
|
||||||
|
|
||||||
new_files = []
|
|
||||||
modified_files = []
|
|
||||||
deleted_files = []
|
|
||||||
|
|
||||||
def __init__(self, path: Path):
|
def __init__(self, path: Path):
|
||||||
"""Create a new organizer for a given path."""
|
"""Create a new organizer for a given path."""
|
||||||
super().__init__(path)
|
super().__init__(path)
|
||||||
@ -36,6 +33,8 @@ class Organizer(Location):
|
|||||||
# Keep the root dir
|
# Keep the root dir
|
||||||
self._known_files.add(path.resolve())
|
self._known_files.add(path.resolve())
|
||||||
|
|
||||||
|
self.download_summary = DownloadSummary()
|
||||||
|
|
||||||
def accept_file(self, src: Path, dst: PurePath) -> None:
|
def accept_file(self, src: Path, dst: PurePath) -> None:
|
||||||
"""Move a file to this organizer and mark it."""
|
"""Move a file to this organizer and mark it."""
|
||||||
src_absolute = src.resolve()
|
src_absolute = src.resolve()
|
||||||
@ -73,10 +72,10 @@ class Organizer(Location):
|
|||||||
dst_absolute.touch()
|
dst_absolute.touch()
|
||||||
return
|
return
|
||||||
|
|
||||||
self.modified_files.append(dst_absolute)
|
self.download_summary.add_changed_file(dst_absolute)
|
||||||
PRETTY.modified_file(dst_absolute)
|
PRETTY.modified_file(dst_absolute)
|
||||||
else:
|
else:
|
||||||
self.new_files.append(dst_absolute)
|
self.download_summary.add_new_file(dst_absolute)
|
||||||
PRETTY.new_file(dst_absolute)
|
PRETTY.new_file(dst_absolute)
|
||||||
|
|
||||||
# Create parent dir if needed
|
# Create parent dir if needed
|
||||||
@ -127,5 +126,5 @@ class Organizer(Location):
|
|||||||
prompt = f"Do you want to delete {path}"
|
prompt = f"Do you want to delete {path}"
|
||||||
|
|
||||||
if prompt_yes_no(prompt, False):
|
if prompt_yes_no(prompt, False):
|
||||||
self.deleted_files.append(path)
|
self.download_summary.add_deleted_file(path)
|
||||||
path.unlink()
|
path.unlink()
|
||||||
|
@ -9,6 +9,7 @@ from typing import Callable, List, Optional, Union
|
|||||||
from .cookie_jar import CookieJar
|
from .cookie_jar import CookieJar
|
||||||
from .diva import (DivaDownloader, DivaDownloadStrategy, DivaPlaylistCrawler,
|
from .diva import (DivaDownloader, DivaDownloadStrategy, DivaPlaylistCrawler,
|
||||||
diva_download_new)
|
diva_download_new)
|
||||||
|
from .download_summary import DownloadSummary
|
||||||
from .errors import FatalException, swallow_and_print_errors
|
from .errors import FatalException, swallow_and_print_errors
|
||||||
from .ilias import (IliasAuthenticator, IliasCrawler, IliasDirectoryFilter,
|
from .ilias import (IliasAuthenticator, IliasCrawler, IliasDirectoryFilter,
|
||||||
IliasDownloader, IliasDownloadInfo, IliasDownloadStrategy,
|
IliasDownloader, IliasDownloadInfo, IliasDownloadStrategy,
|
||||||
@ -34,10 +35,6 @@ class Pferd(Location):
|
|||||||
useful shortcuts for running synchronizers in a single interface.
|
useful shortcuts for running synchronizers in a single interface.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
new_files = []
|
|
||||||
modified_files = []
|
|
||||||
deleted_files = []
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
base_dir: Path,
|
base_dir: Path,
|
||||||
@ -46,6 +43,7 @@ class Pferd(Location):
|
|||||||
):
|
):
|
||||||
super().__init__(Path(base_dir))
|
super().__init__(Path(base_dir))
|
||||||
|
|
||||||
|
self._download_summary = DownloadSummary()
|
||||||
self._tmp_dir = TmpDir(self.resolve(tmp_dir))
|
self._tmp_dir = TmpDir(self.resolve(tmp_dir))
|
||||||
self._test_run = test_run
|
self._test_run = test_run
|
||||||
|
|
||||||
@ -156,38 +154,12 @@ class Pferd(Location):
|
|||||||
clean=clean,
|
clean=clean,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.new_files += organizer.new_files
|
self._download_summary.merge(organizer.download_summary)
|
||||||
self.modified_files += organizer.modified_files
|
|
||||||
self.deleted_files += organizer.deleted_files
|
|
||||||
|
|
||||||
return organizer
|
return organizer
|
||||||
|
|
||||||
def print_summary(self):
|
def print_summary(self):
|
||||||
LOGGER.info("")
|
self._download_summary.print(LOGGER, PRETTY)
|
||||||
LOGGER.info("Summary: ")
|
|
||||||
if len(self.new_files) == 0 and len(self.modified_files) == 0 and len(self.deleted_files) == 0:
|
|
||||||
LOGGER.info("Nothing changed")
|
|
||||||
|
|
||||||
if len(self.new_files) > 0:
|
|
||||||
LOGGER.info("New Files:")
|
|
||||||
for file in self.new_files:
|
|
||||||
PRETTY.new_file(file)
|
|
||||||
|
|
||||||
LOGGER.info("")
|
|
||||||
|
|
||||||
if len(self.modified_files) > 0:
|
|
||||||
LOGGER.info("Modified Files:")
|
|
||||||
for file in self.modified_files:
|
|
||||||
PRETTY.modified_file(file)
|
|
||||||
|
|
||||||
LOGGER.info("")
|
|
||||||
|
|
||||||
if len(self.deleted_files) > 0:
|
|
||||||
LOGGER.info("Deleted Files:")
|
|
||||||
for file in self.deleted_files:
|
|
||||||
PRETTY.deleted_file(file)
|
|
||||||
|
|
||||||
LOGGER.info("")
|
|
||||||
|
|
||||||
@swallow_and_print_errors
|
@swallow_and_print_errors
|
||||||
def ilias_kit_personal_desktop(
|
def ilias_kit_personal_desktop(
|
||||||
|
Loading…
Reference in New Issue
Block a user