From ece903d298d84b899c3d7910469e01b0a90c1452 Mon Sep 17 00:00:00 2001 From: strNophix Date: Mon, 3 Oct 2022 21:12:02 +0200 Subject: [PATCH] Moved torrent diffing logic to ChangeMap --- qbit_ci/change_map.py | 18 ++++++++++++++++++ qbit_ci/torrent_dict.py | 8 +------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/qbit_ci/change_map.py b/qbit_ci/change_map.py index 58494c9..32d9283 100644 --- a/qbit_ci/change_map.py +++ b/qbit_ci/change_map.py @@ -1,5 +1,9 @@ import typing +import qbittorrentapi + +Torrent = qbittorrentapi.TorrentDictionary + class ChangeMap: def __init__(self, changes: typing.Dict[typing.Any, typing.Any]) -> None: self._changes: typing.Dict[typing.Any, typing.Any] = changes @@ -7,4 +11,18 @@ class ChangeMap: def __getattr__(self, __name: str) -> typing.Any: return self._changes.get(__name) + @staticmethod + def diff_torrents(torrent: typing.Optional[Torrent] = None, other_torrent: typing.Optional[Torrent] = None) -> "ChangeMap": + changes: typing.Any = [] + + if not torrent and other_torrent: + changes = other_torrent.items() + elif torrent and not other_torrent: + changes = torrent.items() + elif torrent and other_torrent: + changes = torrent.items() ^ other_torrent.items() + + return ChangeMap({k: v for k, v in changes}) + + __all__ = ("ChangeMap",) diff --git a/qbit_ci/torrent_dict.py b/qbit_ci/torrent_dict.py index 10b4dfd..730123f 100644 --- a/qbit_ci/torrent_dict.py +++ b/qbit_ci/torrent_dict.py @@ -10,13 +10,7 @@ class TorrentStateStore: def update(self, torrent: TorrentDictionary) -> ChangeMap: torrent_name: str = torrent.hash old_torrent: typing.Optional[TorrentDictionary] = self._torrents.get(torrent_name) - changes: typing.Any - if old_torrent: - changes = old_torrent.items() ^ torrent.items() - else: - changes = torrent.items() - - change_map = ChangeMap({k: v for k, v in changes}) + change_map = ChangeMap.diff_torrents(torrent, old_torrent) self._torrents[torrent_name] = torrent return change_map