From e87075a8097b83838f049b6f0504ff25d0bc809f Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Thu, 16 May 2024 17:39:12 +0200 Subject: [PATCH] feat: changed syncing from event based to reference --- music_kraken/objects/collection.py | 19 +++++++++++++++++++ music_kraken/objects/song.py | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/music_kraken/objects/collection.py b/music_kraken/objects/collection.py index 466d84b..2e68f54 100644 --- a/music_kraken/objects/collection.py +++ b/music_kraken/objects/collection.py @@ -162,6 +162,25 @@ class Collection(Generic[T]): object_trace(f"Appending {other.option_string} to {self}") + for attribute, a in self.sync_on_append.items(): + # syncing two collections by reference + b = other.__getattribute__(attribute) + if a is b: + continue + + object_trace(f"Syncing [{a}] = [{b}]") + + b_data = b.data.copy() + b_collection_for = b._collection_for.copy() + + del b + + for synced_with, key in b_collection_for.items(): + synced_with.__setattr__(key, a) + a._collection_for[synced_with] = key + + a.extend(b_data, **kwargs) + # switching collection in the case of push to for c in self.push_to: r = c._find_object(other) diff --git a/music_kraken/objects/song.py b/music_kraken/objects/song.py index 18a29cd..2aa8438 100644 --- a/music_kraken/objects/song.py +++ b/music_kraken/objects/song.py @@ -148,7 +148,7 @@ class Song(Base): self.feature_artist_collection.push_to = [self.artist_collection] self.artist_collection.pull_from = [self.feature_artist_collection] - self.album_collection.extend_object_to_attribute = { + self.album_collection.sync_on_append = { "artist_collection": self.artist_collection, } @@ -310,7 +310,7 @@ class Album(Base): self.song_collection.append_object_to_attribute = { "album_collection": self } - self.song_collection.extend_object_to_attribute = { + self.song_collection.sync_on_append = { "artist_collection": self.artist_collection }