This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
from collections import defaultdict
|
||||
from typing import TypeVar, Generic, Dict, Optional, Iterable, List, Iterator, Tuple, Generator, Union
|
||||
from .parents import OuterProxy
|
||||
from ..utils import object_trace
|
||||
|
||||
T = TypeVar('T', bound=OuterProxy)
|
||||
|
||||
@@ -25,6 +26,8 @@ class Collection(Generic[T]):
|
||||
contain_attribute_in_given: Dict[str, Collection] = None,
|
||||
append_object_to_attribute: Dict[str, T] = None
|
||||
) -> None:
|
||||
self._collection_for: dict = dict()
|
||||
|
||||
self._contains_ids = set()
|
||||
self._data = []
|
||||
|
||||
@@ -44,6 +47,9 @@ class Collection(Generic[T]):
|
||||
|
||||
self.extend(data)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Collection({id(self)})"
|
||||
|
||||
def _map_element(self, __object: T, from_map: bool = False):
|
||||
self._contains_ids.add(__object.id)
|
||||
|
||||
@@ -224,16 +230,25 @@ class Collection(Generic[T]):
|
||||
append_to._data.append(__object)
|
||||
append_to._map_element(__object)
|
||||
|
||||
# only modify collections if the object actually has been appended
|
||||
for collection_attribute, child_collection in self.contain_given_in_attribute.items():
|
||||
__object.__getattribute__(collection_attribute).contain_collection_inside(child_collection, __object)
|
||||
|
||||
for attribute, new_object in self.append_object_to_attribute.items():
|
||||
__object.__getattribute__(attribute).append(new_object)
|
||||
|
||||
for attribute, collection in self.sync_on_append.items():
|
||||
collection.extend(__object.__getattribute__(attribute))
|
||||
__object.__setattr__(attribute, collection)
|
||||
|
||||
# only modify collections if the object actually has been appended
|
||||
for attribute, a in self.sync_on_append.items():
|
||||
b = __object.__getattribute__(attribute)
|
||||
object_trace(f"Syncing [{a}{id(a)}] = [{b}{id(b)}]")
|
||||
|
||||
data_to_extend = b.data
|
||||
|
||||
a._collection_for.update(b._collection_for)
|
||||
for synced_with, key in b._collection_for.items():
|
||||
synced_with.__setattr__(key, a)
|
||||
|
||||
a.extend(data_to_extend)
|
||||
|
||||
|
||||
else:
|
||||
# merge only if the two objects are not the same
|
||||
|
@@ -44,8 +44,13 @@ class InnerData:
|
||||
self._default_values[name] = factory()
|
||||
|
||||
for key, value in kwargs.items():
|
||||
if hasattr(value, "__is_collection__"):
|
||||
value._collection_for[self] = key
|
||||
self.__setattr__(key, value)
|
||||
|
||||
def __hash__(self):
|
||||
return self.id
|
||||
|
||||
def __merge__(self, __other: InnerData, override: bool = False):
|
||||
"""
|
||||
:param __other:
|
||||
@@ -116,6 +121,7 @@ class OuterProxy:
|
||||
self._inner._refers_to_instances.add(self)
|
||||
|
||||
object_trace(f"creating {type(self).__name__} [{self.title_string}]")
|
||||
|
||||
self.__init_collections__()
|
||||
|
||||
for name, data_list in collection_data.items():
|
||||
|
@@ -204,6 +204,7 @@ class Album(Base):
|
||||
notes: FormattedText
|
||||
|
||||
source_collection: SourceCollection
|
||||
|
||||
artist_collection: Collection[Artist]
|
||||
song_collection: Collection[Song]
|
||||
label_collection: Collection[Label]
|
||||
|
Reference in New Issue
Block a user