Compare commits
No commits in common. "49c37345260e9873c1e387bbc7c15f1ed3bc36b8" and "5d26fdbf9436df8d5ab2bf622d65662ee2d11c79" have entirely different histories.
49c3734526
...
5d26fdbf94
@ -1,14 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, List, Optional, Set, Tuple, Type, TypedDict, Union
|
||||
from typing import List, Optional, Dict, Tuple, Type, Union, TypedDict
|
||||
|
||||
from .collection import Collection
|
||||
from .metadata import (
|
||||
Mapping as id3Mapping,
|
||||
ID3Timestamp,
|
||||
Metadata
|
||||
)
|
||||
from ..utils.string_processing import unify, hash_url
|
||||
|
||||
from .parents import OuterProxy as Base
|
||||
|
||||
from ..utils.config import main_settings
|
||||
from ..utils.string_processing import hash_url, unify
|
||||
from .collection import Collection
|
||||
from .metadata import ID3Timestamp
|
||||
from .metadata import Mapping as id3Mapping
|
||||
from .metadata import Metadata
|
||||
from .parents import OuterProxy as Base
|
||||
|
||||
|
||||
class ArtworkVariant(TypedDict):
|
||||
@ -19,9 +23,7 @@ class ArtworkVariant(TypedDict):
|
||||
|
||||
|
||||
class Artwork:
|
||||
def __init__(self, *variants: List[ArtworkVariant], parent_artworks: Set[Artwork] = None) -> None:
|
||||
self.parent_artworks: Set[Artwork] = parent_artworks or set()
|
||||
|
||||
def __init__(self, *variants: List[ArtworkVariant]) -> None:
|
||||
self._variant_mapping: Dict[str, ArtworkVariant] = {}
|
||||
|
||||
for variant in variants:
|
||||
@ -34,7 +36,7 @@ class Artwork:
|
||||
def append(self, url: str, width: int = main_settings["preferred_artwork_resolution"], height: int = main_settings["preferred_artwork_resolution"], **kwargs) -> None:
|
||||
if url is None:
|
||||
return
|
||||
|
||||
|
||||
self._variant_mapping[hash_url(url=url)] = {
|
||||
"url": url,
|
||||
"width": width,
|
||||
@ -42,36 +44,21 @@ class Artwork:
|
||||
"deviation": self._calculate_deviation(width, height),
|
||||
}
|
||||
|
||||
@property
|
||||
def flat_empty(self) -> bool:
|
||||
return len(self._variant_mapping.keys()) <= 0
|
||||
|
||||
def _get_best_from_list(self, artwork_variants: List[ArtworkVariant]) -> Optional[ArtworkVariant]:
|
||||
return min(artwork_variants, key=lambda x: x["deviation"])
|
||||
|
||||
@property
|
||||
def best_variant(self) -> ArtworkVariant:
|
||||
if self.flat_empty:
|
||||
return self._get_best_from_list([parent.best_variant for parent in self.parent_artworks])
|
||||
return self._get_best_from_list(self._variant_mapping.values())
|
||||
if len(self._variant_mapping.keys()) <= 0:
|
||||
return None
|
||||
return min(self._variant_mapping.values(), key=lambda x: x["deviation"])
|
||||
|
||||
def get_variant_name(self, variant: ArtworkVariant) -> str:
|
||||
return f"artwork_{variant['width']}x{variant['height']}_{hash_url(variant['url']).replace('/', '_')}"
|
||||
|
||||
def __merge__(self, other: Artwork, **kwargs) -> None:
|
||||
self.parent_artworks.update(other.parent_artworks)
|
||||
|
||||
for key, value in other._variant_mapping.items():
|
||||
if key not in self._variant_mapping:
|
||||
self._variant_mapping[key] = value
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return id(self)
|
||||
|
||||
def __eq__(self, other: Artwork) -> bool:
|
||||
if hash(self) == hash(other):
|
||||
return True
|
||||
|
||||
if not isinstance(other, Artwork):
|
||||
return False
|
||||
return any(a == b for a, b in zip(self._variant_mapping.keys(), other._variant_mapping.keys()))
|
||||
|
@ -1,44 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from typing import (Any, Callable, Dict, Generator, Generic, Iterable,
|
||||
Iterator, List, Optional, Set, Tuple, TypeVar, Union)
|
||||
from typing import TypeVar, Generic, Dict, Optional, Iterable, List, Iterator, Tuple, Generator, Union, Any, Set
|
||||
import copy
|
||||
|
||||
from ..utils import BColors, object_trace, output
|
||||
from .parents import InnerData, OuterProxy
|
||||
from .parents import OuterProxy
|
||||
from ..utils import object_trace
|
||||
from ..utils import output, BColors
|
||||
|
||||
T = TypeVar('T', bound=OuterProxy)
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppendHookArguments:
|
||||
"""
|
||||
This class is used to store the arguments for the append hook.
|
||||
The best explanation is with an examples:
|
||||
|
||||
```
|
||||
# this is the action that triggers the append hook
|
||||
album = Album()
|
||||
song = Song()
|
||||
album.song_collection.append(song)
|
||||
```
|
||||
|
||||
In this case, the append hook is triggered with the following arguments:
|
||||
```
|
||||
AppendHookArguments(
|
||||
collection=album.song_collection,
|
||||
new_object=song,
|
||||
collection_root_objects=[album]
|
||||
)
|
||||
```
|
||||
"""
|
||||
|
||||
collection: Collection
|
||||
new_object: T
|
||||
collection_root_objects: Set[InnerData]
|
||||
|
||||
class Collection(Generic[T]):
|
||||
__is_collection__ = True
|
||||
|
||||
@ -55,7 +27,6 @@ class Collection(Generic[T]):
|
||||
sync_on_append: Dict[str, Collection] = None,
|
||||
append_object_to_attribute: Dict[str, T] = None,
|
||||
extend_object_to_attribute: Dict[str, Collection] = None,
|
||||
append_callbacks: List[Callable[[AppendHookArguments], None]] = None,
|
||||
) -> None:
|
||||
self._collection_for: dict = dict()
|
||||
|
||||
@ -70,7 +41,6 @@ class Collection(Generic[T]):
|
||||
self.sync_on_append: Dict[str, Collection] = sync_on_append or {}
|
||||
self.pull_from: List[Collection] = []
|
||||
self.push_to: List[Collection] = []
|
||||
self.append_callbacks: List[Callable[[AppendHookArguments], None]] = append_callbacks or []
|
||||
|
||||
# This is to cleanly unmap previously mapped items by their id
|
||||
self._indexed_from_id: Dict[int, Dict[str, Any]] = defaultdict(dict)
|
||||
@ -171,14 +141,6 @@ class Collection(Generic[T]):
|
||||
for attribute, new_object in self.append_object_to_attribute.items():
|
||||
other.__getattribute__(attribute).append(new_object, **kwargs)
|
||||
|
||||
append_hook_args = AppendHookArguments(
|
||||
collection=self,
|
||||
new_object=other,
|
||||
collection_root_objects=self._collection_for.keys(),
|
||||
)
|
||||
for callback in self.append_callbacks:
|
||||
callback(append_hook_args)
|
||||
|
||||
def append(self, other: Optional[T], **kwargs):
|
||||
"""
|
||||
If an object, that represents the same entity exists in a relevant collection,
|
||||
|
Loading…
Reference in New Issue
Block a user