music-kraken-core/music_kraken/objects/collection.py

329 lines
11 KiB
Python
Raw Normal View History

2023-12-19 12:58:39 +00:00
from __future__ import annotations
2023-02-07 18:26:14 +00:00
2023-12-19 12:58:39 +00:00
from collections import defaultdict
2024-01-15 09:50:24 +00:00
from typing import TypeVar, Generic, Dict, Optional, Iterable, List, Iterator, Tuple
2023-12-19 12:58:39 +00:00
from .parents import OuterProxy
2023-02-23 22:52:41 +00:00
2023-12-19 12:58:39 +00:00
T = TypeVar('T', bound=OuterProxy)
2023-08-28 18:59:19 +00:00
2023-12-19 12:58:39 +00:00
class Collection(Generic[T]):
__is_collection__ = True
2023-08-28 18:59:19 +00:00
_data: List[T]
2023-02-23 22:52:41 +00:00
2023-10-24 09:44:00 +00:00
_indexed_values: Dict[str, set]
_indexed_to_objects: Dict[any, list]
shallow_list = property(fget=lambda self: self.data)
def __init__(
2023-12-19 12:58:39 +00:00
self,
data: Optional[Iterable[T]] = None,
sync_on_append: Dict[str, Collection] = None,
contain_given_in_attribute: Dict[str, Collection] = None,
contain_attribute_in_given: Dict[str, Collection] = None,
append_object_to_attribute: Dict[str, T] = None
2023-10-24 09:44:00 +00:00
) -> None:
2023-10-24 15:41:42 +00:00
self._contains_ids = set()
2023-10-24 09:44:00 +00:00
self._data = []
2023-12-19 12:58:39 +00:00
self.parents: List[Collection[T]] = []
self.children: List[Collection[T]] = []
2023-10-24 09:44:00 +00:00
# List of collection attributes that should be modified on append
# Key: collection attribute (str) of appended element
# Value: main collection to sync to
self.contain_given_in_attribute: Dict[str, Collection] = contain_given_in_attribute or {}
2023-10-24 11:46:52 +00:00
self.contain_attribute_in_given: Dict[str, Collection] = contain_attribute_in_given or {}
2023-12-19 12:58:39 +00:00
self.append_object_to_attribute: Dict[str, T] = append_object_to_attribute or {}
2023-10-24 09:44:00 +00:00
self.contain_self_on_append: List[str] = []
self._indexed_values = defaultdict(set)
self._indexed_to_objects = defaultdict(list)
2023-12-19 12:58:39 +00:00
2023-10-24 09:44:00 +00:00
self.extend(data)
2023-02-07 18:26:14 +00:00
2023-10-24 15:41:42 +00:00
def _map_element(self, __object: T, from_map: bool = False):
2024-01-15 09:50:24 +00:00
if __object.id in self._contains_ids:
return
2023-10-24 15:41:42 +00:00
self._contains_ids.add(__object.id)
2023-10-24 09:44:00 +00:00
for name, value in __object.indexing_values:
2023-03-10 17:38:32 +00:00
if value is None:
continue
2023-10-24 09:44:00 +00:00
self._indexed_values[name].add(value)
self._indexed_to_objects[value].append(__object)
2023-03-24 14:58:21 +00:00
if not from_map:
2023-10-24 15:41:42 +00:00
for attribute, new_object in self.contain_given_in_attribute.items():
__object.__getattribute__(attribute).contain_collection_inside(new_object)
2023-12-19 12:58:39 +00:00
2023-12-20 11:31:53 +00:00
for attribute, new_object in self.contain_attribute_in_given.items():
2023-10-24 15:41:42 +00:00
new_object.contain_collection_inside(__object.__getattribute__(attribute))
for attribute, new_object in self.append_object_to_attribute.items():
2023-12-20 11:31:53 +00:00
__object.__getattribute__(attribute).append(new_object)
2023-12-19 12:58:39 +00:00
2023-10-24 09:44:00 +00:00
def _unmap_element(self, __object: T):
2024-01-15 09:50:24 +00:00
if __object.id in self._contains_ids:
self._contains_ids.remove(__object.id)
2023-10-24 15:41:42 +00:00
2023-10-24 09:44:00 +00:00
for name, value in __object.indexing_values:
2023-03-22 11:58:11 +00:00
if value is None:
continue
2023-10-24 09:44:00 +00:00
if value not in self._indexed_values[name]:
continue
2023-12-19 12:58:39 +00:00
2023-10-24 09:44:00 +00:00
try:
self._indexed_to_objects[value].remove(__object)
except ValueError:
continue
2023-03-24 14:58:21 +00:00
2023-10-24 09:44:00 +00:00
if not len(self._indexed_to_objects[value]):
self._indexed_values[name].remove(value)
2023-03-09 21:14:39 +00:00
2023-10-24 09:44:00 +00:00
def _contained_in_self(self, __object: T) -> bool:
2023-12-19 12:58:39 +00:00
if __object.id in self._contains_ids:
return True
2023-10-24 09:44:00 +00:00
for name, value in __object.indexing_values:
if value is None:
continue
if value in self._indexed_values[name]:
return True
return False
2023-12-19 12:58:39 +00:00
def _contained_in_sub(self, __object: T, break_at_first: bool = True) -> List[Collection]:
2023-12-20 11:31:53 +00:00
"""
Gets the collection this object is found in, if it is found in any.
:param __object:
:param break_at_first:
:return:
"""
results = []
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
if self._contained_in_self(__object):
return [self]
2023-12-19 12:58:39 +00:00
2024-04-16 15:50:01 +00:00
for collection in self.children:
results.extend(collection._contained_in_sub(__object, break_at_first=break_at_first))
2023-12-20 11:31:53 +00:00
if break_at_first:
return results
return results
2023-12-19 12:58:39 +00:00
2023-12-20 11:31:53 +00:00
def _get_root_collections(self) -> List[Collection]:
if not len(self.parents):
return [self]
root_collections = []
for upper_collection in self.parents:
root_collections.extend(upper_collection._get_root_collections())
return root_collections
@property
def _is_root(self) -> bool:
return len(self.parents) <= 0
def _get_parents_of_multiple_contained_children(self, __object: T):
results = []
2023-12-19 12:58:39 +00:00
if len(self.children) < 2 or self._contained_in_self(__object):
return results
2023-12-19 12:58:39 +00:00
count = 0
2023-12-19 12:58:39 +00:00
for collection in self.children:
sub_results = collection._get_parents_of_multiple_contained_children(__object)
2023-12-19 12:58:39 +00:00
if len(sub_results) > 0:
count += 1
results.extend(sub_results)
2023-12-19 12:58:39 +00:00
if count >= 2:
results.append(self)
return results
2023-12-19 12:58:39 +00:00
def merge_into_self(self, __object: T, from_map: bool = False):
2023-10-24 11:46:52 +00:00
"""
1. find existing objects
2. merge into existing object
3. remap existing object
"""
2023-10-24 15:41:42 +00:00
if __object.id in self._contains_ids:
return
2023-12-19 12:58:39 +00:00
existing_object: T = None
2023-10-24 11:46:52 +00:00
for name, value in __object.indexing_values:
if value is None:
continue
2023-12-20 11:31:53 +00:00
2023-10-24 11:46:52 +00:00
if value in self._indexed_values[name]:
existing_object = self._indexed_to_objects[value][0]
2023-12-19 12:58:39 +00:00
if existing_object.id == __object.id:
2023-10-24 15:41:42 +00:00
return None
2023-12-19 12:58:39 +00:00
break
2023-10-24 11:46:52 +00:00
if existing_object is None:
return None
2023-10-24 15:41:42 +00:00
2023-12-19 12:58:39 +00:00
existing_object.merge(__object)
2023-10-24 11:46:52 +00:00
# just a check if it really worked
if existing_object.id != __object.id:
raise ValueError("This should NEVER happen. Merging doesn't work.")
2023-12-19 12:58:39 +00:00
self._map_element(existing_object, from_map=from_map)
2023-10-24 09:44:00 +00:00
def contains(self, __object: T) -> bool:
return len(self._contained_in_sub(__object)) > 0
2023-03-24 14:58:21 +00:00
2023-10-24 15:41:42 +00:00
def _append(self, __object: T, from_map: bool = False):
2023-12-29 20:50:40 +00:00
print(self, __object)
2023-10-24 15:41:42 +00:00
self._map_element(__object, from_map=from_map)
2023-10-24 09:44:00 +00:00
self._data.append(__object)
2023-03-02 15:23:02 +00:00
2024-01-15 09:50:24 +00:00
def _find_object_in_self(self, __object: T) -> Optional[T]:
for name, value in __object.indexing_values:
2024-04-11 14:53:20 +00:00
if value is None or value == __object._default_factories.get(name, lambda: None)():
2024-01-15 09:50:24 +00:00
continue
if value in self._indexed_values[name]:
return self._indexed_to_objects[value][0]
2024-04-16 15:50:01 +00:00
def _find_object(self, __object: T, no_sibling: bool = False) -> Tuple[Collection[T], Optional[T]]:
2024-01-15 09:50:24 +00:00
other_object = self._find_object_in_self(__object)
if other_object is not None:
return self, other_object
for c in self.children:
o, other_object = c._find_object(__object)
if other_object is not None:
return o, other_object
2024-04-16 15:50:01 +00:00
if no_sibling:
return self, None
# find in siblings and all children of siblings
for parent in self.parents:
for sibling in parent.children:
if sibling is self:
continue
o, other_object = sibling._find_object(__object, no_sibling=True)
if other_object is not None:
return o, other_object
2024-01-15 09:50:24 +00:00
return self, None
2023-10-24 15:41:42 +00:00
def append(self, __object: Optional[T], already_is_parent: bool = False, from_map: bool = False):
2024-01-15 09:50:24 +00:00
"""
If an object, that represents the same entity exists in a relevant collection,
merge into this object. (and remap)
Else append to this collection.
:param __object:
:param already_is_parent:
:param from_map:
:return:
"""
if __object is None or __object.id in self._contains_ids:
2023-10-24 15:41:42 +00:00
return
2023-12-19 12:58:39 +00:00
2024-01-15 09:50:24 +00:00
append_to, existing_object = self._find_object(__object)
if existing_object is None:
# append
append_to._data.append(__object)
2024-04-16 15:50:01 +00:00
append_to._map_element(__object, from_map=from_map)
2024-01-15 09:50:24 +00:00
else:
# merge
append_to._unmap_element(existing_object)
existing_object.merge(__object)
2024-04-16 15:50:01 +00:00
append_to._map_element(existing_object, from_map=from_map)
2024-01-15 09:50:24 +00:00
2023-03-24 14:58:21 +00:00
2024-01-15 09:50:24 +00:00
def extend(self, __iterable: Optional[Iterable[T]], from_map: bool = False):
2023-10-24 09:44:00 +00:00
if __iterable is None:
2023-10-12 17:24:35 +00:00
return
2023-12-19 12:58:39 +00:00
2023-10-24 09:44:00 +00:00
for __object in __iterable:
2024-01-15 09:50:24 +00:00
self.append(__object, from_map=from_map)
2023-10-12 17:24:35 +00:00
2023-12-19 12:58:39 +00:00
def sync_with_other_collection(self, equal_collection: Collection):
2023-10-24 09:44:00 +00:00
"""
If two collections always need to have the same values, this can be used.
2023-12-19 12:58:39 +00:00
2023-10-24 09:44:00 +00:00
Internally:
1. import the data from other to self
- _data
- contained_collections
2. replace all refs from the other object, with refs from this object
"""
if equal_collection is self:
return
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
# don't add the elements from the subelements from the other collection.
# this will be done in the next step.
self.extend(equal_collection._data)
# add all submodules
2023-12-19 12:58:39 +00:00
for equal_sub_collection in equal_collection.children:
2023-10-24 09:44:00 +00:00
self.contain_collection_inside(equal_sub_collection)
2023-10-12 17:24:35 +00:00
2023-12-20 11:31:53 +00:00
def contain_collection_inside(self, sub_collection: Collection):
2023-10-24 09:44:00 +00:00
"""
This collection will ALWAYS contain everything from the passed in collection
"""
2023-12-20 11:31:53 +00:00
if self is sub_collection or sub_collection in self.children:
2023-10-24 09:44:00 +00:00
return
2023-12-19 12:58:39 +00:00
self.children.append(sub_collection)
sub_collection.parents.append(self)
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
@property
def data(self) -> List[T]:
2024-04-16 15:50:01 +00:00
return list(i for i in self.__iter__())
2023-12-19 12:58:39 +00:00
2023-10-24 09:44:00 +00:00
def __len__(self) -> int:
2023-12-19 12:58:39 +00:00
return len(self._data) + sum(len(collection) for collection in self.children)
2023-03-03 11:32:08 +00:00
2023-12-19 21:11:46 +00:00
@property
def empty(self) -> bool:
2024-02-28 13:27:35 +00:00
return self.__len__() <= 0
2023-12-19 21:11:46 +00:00
2024-04-16 15:50:01 +00:00
def __iter__(self, finished_ids: set = None) -> Iterator[T]:
_finished_ids = finished_ids or set()
2023-12-20 11:31:53 +00:00
for element in self._data:
2024-04-16 15:50:01 +00:00
if element.id in _finished_ids:
continue
_finished_ids.add(element.id)
2023-12-19 12:58:39 +00:00
yield element
2023-12-29 15:15:54 +00:00
2024-01-15 09:50:24 +00:00
for c in self.children:
2024-04-16 15:50:01 +00:00
yield from c.__iter__(finished_ids=finished_ids)
2024-01-15 09:50:24 +00:00
2023-12-29 15:15:54 +00:00
def __merge__(self, __other: Collection, override: bool = False):
2024-01-15 11:48:36 +00:00
self.extend(__other._data, from_map=True)
2024-01-15 09:50:24 +00:00
def __getitem__(self, item: int):
if item < len(self._data):
return self._data[item]
2024-02-28 13:27:35 +00:00
item = item - len(self._data)
2024-01-15 09:50:24 +00:00
for c in self.children:
if item < len(c):
2024-02-28 13:27:35 +00:00
return c.__getitem__(item)
item = item - len(c._data)
2024-01-15 09:50:24 +00:00
raise IndexError