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

104 lines
3.1 KiB
Python
Raw Normal View History

2023-03-03 11:32:08 +00:00
from typing import List, Iterable
2023-02-07 18:26:14 +00:00
from .source import SourceAttribute
2023-03-02 06:59:53 +00:00
from ..utils import string_processing
2023-02-07 18:26:14 +00:00
2023-02-23 22:52:41 +00:00
2023-02-07 18:26:14 +00:00
class Collection:
"""
This an class for the iterables
like tracklist or discography
"""
_data: List[SourceAttribute]
2023-02-23 22:52:41 +00:00
2023-02-07 18:26:14 +00:00
_by_url: dict
_by_attribute: dict
2023-02-08 12:16:48 +00:00
def __init__(self, data: list = None, map_attributes: list = None, element_type=None) -> None:
2023-02-07 18:26:14 +00:00
"""
Attribute needs to point to
"""
self._by_url = dict()
self.map_attributes = map_attributes or []
2023-02-08 12:16:48 +00:00
self.element_type = element_type
2023-02-07 18:26:14 +00:00
self._by_attribute = {attr: dict() for attr in map_attributes}
self._data = data or []
for element in self._data:
self.map_element(element=element)
2023-02-23 22:52:41 +00:00
def sort(self, reverse: bool = False, **kwargs):
self._data.sort(reverse=reverse, **kwargs)
2023-02-07 18:26:14 +00:00
def map_element(self, element: SourceAttribute):
2023-02-08 12:16:48 +00:00
for source_url in element.source_url_map:
self._by_url[source_url] = element
2023-02-07 18:26:14 +00:00
for attr in self.map_attributes:
value = element.__getattribute__(attr)
if type(value) != str:
# this also throws out all none values
continue
self._by_attribute[attr][string_processing.unify(value)] = element
def get_object_with_source(self, url: str) -> any:
"""
Returns either None, or the object, that has a source
matching the url.
"""
if url in self._by_url:
return self._by_url[url]
def get_object_with_attribute(self, name: str, value: str):
if name not in self.map_attributes:
raise ValueError(f"didn't map the attribute {name}")
unified = string_processing.unify(value)
2023-02-08 12:16:48 +00:00
if unified in self._by_attribute[name]:
2023-02-07 18:26:14 +00:00
return self._by_attribute[name][unified]
2023-03-03 11:32:08 +00:00
def append(self, element, merge_on_conflict: bool = True):
2023-02-08 12:16:48 +00:00
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}")
2023-03-02 15:23:02 +00:00
for source_url in element.source_url_map:
if source_url in self._by_url:
return
for attr in self.map_attributes:
value = element.__getattribute__(attr)
if value in self._by_attribute[attr]:
return
2023-02-07 18:26:14 +00:00
self._data.append(element)
self.map_element(element)
2023-02-08 12:16:48 +00:00
2023-03-03 11:32:08 +00:00
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)
2023-02-08 12:16:48 +00:00
def __iter__(self):
for element in self._data:
yield element
def __str__(self) -> str:
2023-03-02 15:23:02 +00:00
return "\n".join([f"{str(j).zfill(2)}: {i.__repr__()}" for j, i in enumerate(self._data)])
2023-02-08 16:14:51 +00:00
def __len__(self) -> int:
return len(self._data)
2023-02-23 22:52:41 +00:00
def __getitem__(self, item):
if type(item) != int:
return ValueError("key needs to be an integer")
return self._data[item]
2023-02-08 16:14:51 +00:00
def copy(self) -> List:
"""
returns a shallow copy of the data list
"""
return self._data.copy()