From 1f0ae30f02560d61fe67632a9af6a51baa8cd08a Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Wed, 22 Mar 2023 12:58:11 +0100 Subject: [PATCH] fixed disgusting bug --- src/music_kraken/objects/collection.py | 54 ++++++++++++++------------ src/music_kraken/objects/song.py | 25 ++++++------ src/music_kraken/pages/abstract.py | 13 ------- src/music_kraken/pages/musify.py | 10 ++--- 4 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 841f47e..eb59142 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -49,12 +49,25 @@ class Collection: self._attribute_to_object_map[name][value] = element self._used_ids.add(element.id) + + def unmap_element(self, element: DatabaseObject): + for name, value in element.indexing_values: + if value is None: + continue + + if value in self._attribute_to_object_map[name]: + if element is self._attribute_to_object_map[name][value]: + try: + self._attribute_to_object_map[name].pop(value) + except KeyError: + pass - def append(self, element: DatabaseObject, merge_on_conflict: bool = True) -> DatabaseObject: + def append(self, element: DatabaseObject, merge_on_conflict: bool = True, merge_into_existing: bool = True) -> bool: """ :param element: :param merge_on_conflict: - :return: + :param merge_into_existing: + :return did_not_exist: """ # if the element type has been defined in the initializer it checks if the type matches @@ -68,15 +81,24 @@ class Collection: if merge_on_conflict: # if the object does already exist # thus merging and don't add it afterwards - existing_object.merge(element) - # in case any relevant data has been added (e.g. it remaps the old object) - self.map_element(existing_object) - return existing_object + if merge_into_existing: + existing_object.merge(element) + # in case any relevant data has been added (e.g. it remaps the old object) + self.map_element(existing_object) + else: + element.merge(existing_object) + + exists_at = self._data.index(existing_object) + self._data[exists_at] = element + + self.unmap_element(existing_object) + self.map_element(element) + return True self._data.append(element) self.map_element(element) - return element + return False def extend(self, element_list: Iterable[DatabaseObject], merge_on_conflict: bool = True): for element in element_list: @@ -104,21 +126,3 @@ class Collection: returns a shallow copy of the data list """ return self._data.copy() - - def insecure_append(self, element: DatabaseObject): - if element.id in self._used_ids: - return False - self._used_ids.add(element.id) - - self._data.append(element) - self.map_element(element) - return True - - def insecure_extend(self, element_list: Iterable[DatabaseObject]): - success = False - - for element in element_list: - if self.insecure_append(element): - success = True - - return success diff --git a/src/music_kraken/objects/song.py b/src/music_kraken/objects/song.py index d323e69..ff2df84 100644 --- a/src/music_kraken/objects/song.py +++ b/src/music_kraken/objects/song.py @@ -99,17 +99,18 @@ class Song(MainObject): def compile(self): album: Album for album in self.album_collection: - if album.song_collection.insecure_append(self): + if album.song_collection.append(self, merge_into_existing=False): album.compile() artist: Artist for artist in self.feature_artist_collection: - if artist.feature_song_collection.insecure_append(self): + if artist.feature_song_collection.append(self, merge_into_existing=False): artist.compile() for artist in self.main_artist_collection: - if artist.main_album_collection.insecure_extend(self.album_collection): - artist.compile() + for album in self.album_collection: + if artist.main_album_collection.append(album, merge_into_existing=False): + artist.compile() @property def indexing_values(self) -> List[Tuple[str, object]]: @@ -259,17 +260,17 @@ class Album(MainObject): def compile(self): song: Song for song in self.song_collection: - if song.album_collection.insecure_append(self): + if song.album_collection.append(self, merge_into_existing=False): song.compile() artist: Artist for artist in self.artist_collection: - if artist.main_album_collection.insecure_append(self): + if artist.main_album_collection.append(self, merge_into_existing=False): artist.compile() label: Label for label in self.label_collection: - if label.album_collection.insecure_append(self): + if label.album_collection.append(self, merge_into_existing=False): label.compile() @property @@ -435,17 +436,17 @@ class Artist(MainObject): def compile(self): song: "Song" for song in self.feature_song_collection: - if song.feature_artist_collection.insecure_append(self): + if song.feature_artist_collection.append(self, merge_into_existing=False): song.compile() album: "Album" for album in self.main_album_collection: - if album.artist_collection.insecure_append(self): + if album.artist_collection.append(self, merge_into_existing=False): album.compile() label: Label for label in self.label_collection: - if label.current_artist_collection.insecure_append(self): + if label.current_artist_collection.append(self, merge_into_existing=False): label.compile() @property @@ -579,12 +580,12 @@ class Label(MainObject): def compile(self) -> bool: album: Album for album in self.album_collection: - if album.label_collection.insecure_append(self): + if album.label_collection.append(self, merge_into_existing=False): album.compile() artist: Artist for artist in self.current_artist_collection: - if artist.label_collection.insecure_append(self): + if artist.label_collection.append(self, merge_into_existing=False): artist.compile() @property diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index dfd0030..24d67f4 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -20,20 +20,11 @@ from ..objects import ( ) -class PageCache(Collection): - def clear(self): - self.__init__(element_type=self.element_type) - - class Page: """ This is an abstract class, laying out the functionality for every other class fetching something """ - SONG_CACHE = PageCache(element_type=Song) - ALBUM_CACHE = PageCache(element_type=Album) - ARTIST_CACHE = PageCache(element_type=Artist) - API_SESSION: requests.Session = requests.Session() API_SESSION.proxies = shared.proxies TIMEOUT = 5 @@ -160,10 +151,6 @@ class Page: tracklist of every album of the artist. :return detailed_music_object: IT MODIFIES THE INPUT OBJ """ - - cls.ARTIST_CACHE.clear() - cls.ALBUM_CACHE.clear() - cls.SONG_CACHE.clear() if type(music_object) == Song: song = cls.fetch_song_details(music_object, flat=flat) diff --git a/src/music_kraken/pages/musify.py b/src/music_kraken/pages/musify.py index 7604dfb..e874ca7 100644 --- a/src/music_kraken/pages/musify.py +++ b/src/music_kraken/pages/musify.py @@ -151,11 +151,11 @@ class Musify(Page): artist_thumbnail = image_soup.get("src") - return cls.ARTIST_CACHE.append(Artist( + return Artist( _id=_id, name=name, source_list=source_list - )) + ) @classmethod def parse_album_contact(cls, contact: BeautifulSoup) -> Album: @@ -700,13 +700,13 @@ class Musify(Page): if note_soup is not None: notes.html = note_soup.decode_contents() - return cls.ARTIST_CACHE.append(Artist( + return Artist( _id=url.musify_id, name=name, country=country, source_list=source_list, notes=notes - )) + ) @classmethod def fetch_artist_from_source(cls, source: Source, flat: bool = False) -> Artist: @@ -842,7 +842,7 @@ class Musify(Page): _artist_name = meta_artist_name_text if _artist_name is not None or _artist_src is not None: - artist_list.append(cls.ARTIST_CACHE.append(Artist(name=_artist_name, source_list=_artist_src))) + artist_list.append(Artist(name=_artist_name, source_list=_artist_src)) return Song( title=song_name,