From 7502a38b5861bb80367be687f63ec755785c66d3 Mon Sep 17 00:00:00 2001 From: Hellow Date: Fri, 3 Mar 2023 12:32:08 +0100 Subject: [PATCH] added merging --- src/music_kraken/objects/collection.py | 8 ++++++-- src/music_kraken/objects/parents.py | 14 +++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 700d961..8ec764a 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Iterable from .source import SourceAttribute from ..utils import string_processing @@ -60,7 +60,7 @@ class Collection: if unified in self._by_attribute[name]: return self._by_attribute[name][unified] - def append(self, element: SourceAttribute, merge_on_conflict: bool = True): + def append(self, element, merge_on_conflict: bool = True): if type(element) is not self.element_type and self.element_type is not None: raise TypeError(f"{type(element)} is not the set type {self.element_type}") @@ -76,6 +76,10 @@ class Collection: self._data.append(element) self.map_element(element) + def extend(self, element_list: Iterable, merge_on_conflict: bool = True): + for element in element_list: + self.append(element, merge_on_conflict=merge_on_conflict) + def __iter__(self): for element in self._data: yield element diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index 74819ff..8110173 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -25,7 +25,19 @@ class DatabaseObject: self.dynamic = dynamic def merge(self, other, override: bool = False): - for collection in + if type(other) != type(self): + LOGGER.warning(f"can't merge \"{type(other)}\" into \"{type(self)}\"") + return + + for collection in type(self).COLLECTION_ATTRIBUTES: + getattr(self, collection).extend(collection) + + for simple_attribute in type(self).SIMPLE_ATTRIBUTES: + if getattr(other, simple_attribute) is None: + continue + + if override or getattr(self, simple_attribute) is None: + setattr(self, simple_attribute, getattr(other, simple_attribute)) class MainObject(DatabaseObject):