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

151 lines
5.1 KiB
Python
Raw Normal View History

2023-10-24 09:44:00 +00:00
from typing import List, Iterable, Iterator, Optional, TypeVar, Generic, Dict, Type
2023-03-09 17:35:56 +00:00
from collections import defaultdict
2023-02-07 18:26:14 +00:00
2023-03-09 17:19:49 +00:00
from .parents import DatabaseObject
2023-10-24 09:44:00 +00:00
from ..utils.support_classes.hacking import MetaClass
2023-02-07 18:26:14 +00:00
2023-02-23 22:52:41 +00:00
2023-08-28 18:59:19 +00:00
T = TypeVar('T', bound=DatabaseObject)
2023-10-24 09:44:00 +00:00
class Collection(Generic[T], metaclass=MetaClass):
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__(
self, data: Optional[Iterable[T]],
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, DatabaseObject] = None
) -> None:
self._data = []
self.upper_collections: List[Collection[T]] = []
self.contained_collections: List[Collection[T]] = []
# List of collection attributes that should be modified on append
# Key: collection attribute (str) of appended element
# Value: main collection to sync to
self.sync_on_append: Dict[str, Collection] = sync_on_append or {}
self.contain_given_in_attribute: Dict[str, Collection] = contain_given_in_attribute or {}
self.contain_attribute_in_given: Dict[str, Collection] = contain_given_in_attribute or {}
self.append_object_to_attribute: Dict[str, DatabaseObject] = append_object_to_attribute or {}
self.contain_self_on_append: List[str] = []
self._indexed_values = defaultdict(set)
self._indexed_to_objects = defaultdict(list)
2023-03-09 17:19:49 +00:00
2023-10-24 09:44:00 +00:00
self.extend(data)
2023-02-07 18:26:14 +00:00
2023-10-24 09:44:00 +00:00
def _map_element(self, __object: T):
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
2023-10-24 09:44:00 +00:00
def _unmap_element(self, __object: T):
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
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:
for name, value in __object.indexing_values:
if value is None:
continue
if value in self._indexed_values[name]:
return True
return False
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
def _contained_in(self, __object: T) -> Optional["Collection"]:
if self._contained_in_self(__object):
return self
2023-04-03 14:23:30 +00:00
2023-10-24 09:44:00 +00:00
for collection in self.contained_collections:
if collection._contained_in_self(__object):
return collection
return None
def contains(self, __object: T) -> bool:
return self._contained_in(__object) is not None
2023-03-24 14:58:21 +00:00
2023-10-24 09:44:00 +00:00
def _append(self, __object: T):
self._map_element(__object)
self._data.append(__object)
2023-03-02 15:23:02 +00:00
2023-10-24 09:44:00 +00:00
def append(self, __object: Optional[T]):
if __object is None:
return
2023-02-08 12:16:48 +00:00
2023-10-24 09:44:00 +00:00
self._append(__object)
2023-03-24 14:58:21 +00:00
2023-10-24 09:44:00 +00:00
def extend(self, __iterable: Optional[Iterable[T]]):
if __iterable is None:
2023-10-12 17:24:35 +00:00
return
2023-10-24 09:44:00 +00:00
for __object in __iterable:
self.append(__object)
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
def sync_with_other_collection(self, equal_collection: "Collection"):
"""
If two collections always need to have the same values, this can be used.
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
for equal_sub_collection in equal_collection.contained_collections:
self.contain_collection_inside(equal_sub_collection)
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
# now the ugly part
# replace all refs of the other element with this one
self.merge(equal_collection)
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
def contain_collection_inside(self, sub_collection: "Collection"):
"""
This collection will ALWAYS contain everything from the passed in collection
"""
if sub_collection in self.contained_collections:
return
self.contained_collections.append(sub_collection)
sub_collection.upper_collections.append(self)
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
@property
def data(self) -> List[T]:
return [*self._data, *(__object for collection in self.contained_collections for __object in collection.shallow_list)]
2023-10-12 17:24:35 +00:00
2023-10-24 09:44:00 +00:00
def __len__(self) -> int:
return len(self._data) + sum(len(collection) for collection in self.contained_collections)
2023-03-03 11:32:08 +00:00
2023-08-30 19:14:03 +00:00
def __iter__(self) -> Iterator[T]:
2023-10-12 17:24:35 +00:00
for element in self._data:
2023-10-24 09:44:00 +00:00
yield element