pferd/PFERD/download_summary.py

76 lines
2.1 KiB
Python
Raw Normal View History

2020-06-26 15:37:35 +02:00
"""
Provides a summary that keeps track of new modified or deleted files.
"""
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
2020-11-17 21:36:04 +01:00
def _mergeNoDuplicate(first: List[Path], second: List[Path]) -> List[Path]:
tmp = list(set(first + second))
tmp.sort(key=lambda x: str(x.resolve()))
return tmp
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-26 15:17:44 +02:00
def __init__(self) -> None:
self._new_files: List[Path] = []
self._modified_files: List[Path] = []
self._deleted_files: List[Path] = []
@property
def new_files(self) -> List[Path]:
"""
Returns all new files.
"""
return self._new_files.copy()
@property
def modified_files(self) -> List[Path]:
"""
Returns all modified files.
"""
return self._modified_files.copy()
@property
def deleted_files(self) -> List[Path]:
"""
Returns all deleted files.
"""
return self._deleted_files.copy()
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.
"""
2020-11-17 21:36:04 +01:00
self._new_files = _mergeNoDuplicate(self._new_files, summary.new_files)
self._modified_files = _mergeNoDuplicate(self._modified_files, summary.modified_files)
self._deleted_files = _mergeNoDuplicate(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.
"""
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-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.
"""
self._new_files.append(path)
2020-06-26 15:52:07 +02:00
def has_updates(self) -> bool:
2020-06-26 15:37:35 +02:00
"""
2020-06-26 15:52:07 +02:00
Returns whether this summary has any updates.
2020-06-26 15:37:35 +02:00
"""
return bool(self._new_files or self._modified_files or self._deleted_files)