From 2fabd09a71579008f3e0fdf9d4ae7b07d44b5617 Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Fri, 26 May 2023 10:11:36 +0200 Subject: [PATCH] much progress in cleaning up --- src/music_kraken/download/page_attributes.py | 44 ++++++++++++++------ src/music_kraken/download/results.py | 25 +++++++++++ src/music_kraken/pages/abstract.py | 21 +++++----- 3 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 src/music_kraken/download/results.py diff --git a/src/music_kraken/download/page_attributes.py b/src/music_kraken/download/page_attributes.py index 4c06c06..4ba72b5 100644 --- a/src/music_kraken/download/page_attributes.py +++ b/src/music_kraken/download/page_attributes.py @@ -1,29 +1,49 @@ -from typing import Tuple, Type, Dict, List -import threading -from queue import Queue +from typing import Tuple, Type, Dict, List, Set from ..utils.enums.source import SourcePages from ..utils.support_classes import Query, EndThread from ..pages import Page, EncyclopaediaMetallum, Musify -ALL_PAGES: Tuple[Type[Page], ...] = ( +ALL_PAGES: Set[Type[Page]] = { EncyclopaediaMetallum, Musify -) +} -AUDIO_PAGES: Tuple[Type[Page], ...] = ( +AUDIO_PAGES: Set[Type[Page]] = { Musify, -) +} -SHADY_PAGES: Tuple[Type[Page], ...] = ( +SHADY_PAGES: Set[Type[Page]] = { Musify, -) +} -exit_threads = EndThread() -search_queue: Queue[Query] = Queue() -_page_threads: Dict[Type[Page], List[Page]] = dict() +class Pages: + def __init__(self, exclude_pages: Set[Type[Page]] = None, exclude_shady: bool = False) -> None: + # initialize all page instances + self._page_instances: Dict[Type[Page], Page] = dict() + + exclude_pages = exclude_pages if exclude_pages is not None else set() + + if exclude_shady: + exclude_pages = exclude_pages.union(SHADY_PAGES) + + if not exclude_pages.issubset(ALL_PAGES): + raise ValueError(f"The excluded pages have to be a subset of all pages: {exclude_pages} | {ALL_PAGES}") + + def _set_to_tuple(page_set: Set[Type[Page]]) -> Tuple[Type[Page], ...]: + return tuple(sorted(page_set, key=lambda page: page.__name__)) + + self.pages: Tuple[Type[Page], ...] = _set_to_tuple(ALL_PAGES.difference(exclude_pages)) + self.audio_pages: Tuple[Type[Page], ...] = _set_to_tuple(set(self.pages).intersection(AUDIO_PAGES)) + + for page_type in ALL_PAGES: + self._page_instances[page_type] = page_type() + + def search(self, query: Query): + for page_type in self.pages: + self._page_instances[page_type].search(query=query) """ diff --git a/src/music_kraken/download/results.py b/src/music_kraken/download/results.py new file mode 100644 index 0000000..a6a27fe --- /dev/null +++ b/src/music_kraken/download/results.py @@ -0,0 +1,25 @@ +from typing import Tuple, Type, Dict, List + +from ..objects import DatabaseObject +from ..utils.enums.source import SourcePages +from ..pages import Page, EncyclopaediaMetallum, Musify + +class SearchResults: + def __init__( + self, + pages: Tuple[Type[Page], ...] + + ) -> None: + self.pages = pages + # this would initialize a list for every page, which I don't think I want + # self.results = Dict[Type[Page], List[DatabaseObject]] = {page: [] for page in self.pages} + self.results = Dict[Type[Page], List[DatabaseObject]] = {} + + def add(self, page: Type[Page], search_result: List[DatabaseObject]): + self.results[page] = search_result + + def __str__(self) -> str: + for page in self.pages: + if page not in self.results: + continue + \ No newline at end of file diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index fe6c3b0..2562f5e 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -104,14 +104,7 @@ def merge_together(old_object: DatabaseObject, new_object: DatabaseObject) -> Da return old_object -class LoreIpsum: - pass - - -Parent = threading.Thread if THREADED else LoreIpsum - - -class Page(Parent): +class Page(): """ This is an abstract class, laying out the functionality for every other class fetching something @@ -120,14 +113,21 @@ class Page(Parent): SOURCE_TYPE: SourcePages LOGGER = logging.getLogger("this shouldn't be used") + + def __init__(self): + super().__init__() + + """ + CODE I NEED WHEN I START WITH MULTITHREADING + def __init__(self, end_event: EndThread, search_queue: Queue, search_result_queue: Queue): self.end_event = end_event self.search_queue = search_queue self.search_result_queue = search_result_queue - Parent.__init__(self) - + super().__init__() + @property def _empty_working_queues(self): return self.search_queue.empty() @@ -138,6 +138,7 @@ class Page(Parent): self.search(self.search_queue.get()) self.search_result_queue.put(FinishedSearch()) continue + """ def get_source_type(self, source: Source) -> Optional[Type[DatabaseObject]]: return None