2020-06-26 15:37:35 +02:00
|
|
|
"""
|
|
|
|
Provides a summary that keeps track of new modified or deleted files.
|
|
|
|
"""
|
2020-06-26 13:02:37 +02:00
|
|
|
import logging
|
2020-06-26 15:17:44 +02:00
|
|
|
from pathlib import Path
|
2020-06-26 15:37:35 +02:00
|
|
|
from typing import List
|
|
|
|
|
2020-06-26 13:02:37 +02:00
|
|
|
from .logging import PrettyLogger
|
|
|
|
|
|
|
|
|
2020-06-25 21:55:08 +02:00
|
|
|
class DownloadSummary:
|
2020-06-26 15:37:35 +02:00
|
|
|
"""
|
|
|
|
Keeps track of all new, modified or deleted files and provides a summary.
|
|
|
|
"""
|
2020-06-25 21:55:08 +02:00
|
|
|
|
2020-06-26 15:17:44 +02:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self._new_files: List[Path] = []
|
2020-06-26 15:37:35 +02:00
|
|
|
self._modified_files: List[Path] = []
|
2020-06-26 15:17:44 +02:00
|
|
|
self._deleted_files: List[Path] = []
|
2020-06-25 21:55:08 +02:00
|
|
|
|
|
|
|
def merge(self, summary: 'DownloadSummary') -> None:
|
2020-06-26 15:37:35 +02:00
|
|
|
"""
|
|
|
|
Merges ourselves with the passed summary. Modifies this object, but not the passed one.
|
|
|
|
"""
|
|
|
|
# This is our own class!
|
|
|
|
# pylint: disable=protected-access
|
2020-06-25 21:55:08 +02:00
|
|
|
self._new_files += summary._new_files
|
2020-06-26 15:37:35 +02:00
|
|
|
self._modified_files += summary._modified_files
|
2020-06-25 21:55:08 +02:00
|
|
|
self._deleted_files += summary._deleted_files
|
|
|
|
|
2020-06-26 15:17:44 +02:00
|
|
|
def add_deleted_file(self, path: Path) -> None:
|
2020-06-26 15:37:35 +02:00
|
|
|
"""
|
|
|
|
Registers a file as deleted.
|
|
|
|
"""
|
2020-06-25 21:55:08 +02:00
|
|
|
self._deleted_files.append(path)
|
|
|
|
|
2020-06-26 15:37:35 +02:00
|
|
|
def add_modified_file(self, path: Path) -> None:
|
|
|
|
"""
|
|
|
|
Registers a file as changed.
|
|
|
|
"""
|
|
|
|
self._modified_files.append(path)
|
2020-06-25 21:55:08 +02:00
|
|
|
|
2020-06-26 15:17:44 +02:00
|
|
|
def add_new_file(self, path: Path) -> None:
|
2020-06-26 15:37:35 +02:00
|
|
|
"""
|
|
|
|
Registers a file as new.
|
|
|
|
"""
|
2020-06-25 21:55:08 +02:00
|
|
|
self._new_files.append(path)
|
|
|
|
|
2020-06-26 15:37:35 +02:00
|
|
|
def _has_updates(self) -> bool:
|
|
|
|
return bool(self._new_files or self._modified_files or self._deleted_files)
|
2020-06-25 21:55:08 +02:00
|
|
|
|
2020-06-26 15:17:44 +02:00
|
|
|
def print(self, logger: logging.Logger, pretty: PrettyLogger) -> None:
|
2020-06-26 15:37:35 +02:00
|
|
|
"""
|
|
|
|
Prints this summary.
|
|
|
|
"""
|
2020-06-25 21:55:08 +02:00
|
|
|
logger.info("")
|
|
|
|
logger.info("Summary: ")
|
2020-06-26 15:37:35 +02:00
|
|
|
if not self._has_updates():
|
|
|
|
logger.info("Nothing changed!")
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._new_files:
|
|
|
|
logger.info("New Files:")
|
|
|
|
for file in self._new_files:
|
|
|
|
pretty.new_file(file)
|
|
|
|
|
|
|
|
logger.info("")
|
|
|
|
|
|
|
|
if self._modified_files:
|
|
|
|
logger.info("Modified Files:")
|
|
|
|
for file in self._modified_files:
|
|
|
|
pretty.modified_file(file)
|
|
|
|
|
|
|
|
logger.info("")
|
|
|
|
|
|
|
|
if self._deleted_files:
|
|
|
|
logger.info("Deleted Files:")
|
|
|
|
for file in self._deleted_files:
|
|
|
|
pretty.deleted_file(file)
|