diff --git a/src/music_kraken/download/page_attributes.py b/src/music_kraken/download/page_attributes.py index cec3b5e..4c06c06 100644 --- a/src/music_kraken/download/page_attributes.py +++ b/src/music_kraken/download/page_attributes.py @@ -1,26 +1,32 @@ -from typing import Tuple, Type, Dict +from typing import Tuple, Type, Dict, List +import threading +from queue import Queue from ..utils.enums.source import SourcePages +from ..utils.support_classes import Query, EndThread from ..pages import Page, EncyclopaediaMetallum, Musify - -NAME_PAGE_MAP: Dict[str, Page] = dict() -PAGE_NAME_MAP: Dict[Page, str] = dict() -SOURCE_PAGE_MAP: Dict[SourcePages, Page] = dict() - -ALL_PAGES: Tuple[Page, ...] = ( - EncyclopaediaMetallum(), - Musify()) +ALL_PAGES: Tuple[Type[Page], ...] = ( + EncyclopaediaMetallum, + Musify ) -AUDIO_PAGES: Tuple[Page, ...] = ( - Musify(), +AUDIO_PAGES: Tuple[Type[Page], ...] = ( + Musify, ) -SHADY_PAGES: Tuple[Page, ...] = ( - Musify(), +SHADY_PAGES: Tuple[Type[Page], ...] = ( + Musify, ) + +exit_threads = EndThread() +search_queue: Queue[Query] = Queue() + +_page_threads: Dict[Type[Page], List[Page]] = dict() + + +""" # this needs to be case-insensitive SHORTHANDS = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') for i, page in enumerate(ALL_PAGES): @@ -30,4 +36,4 @@ for i, page in enumerate(ALL_PAGES): PAGE_NAME_MAP[type(page)] = SHORTHANDS[i] SOURCE_PAGE_MAP[page.SOURCE_TYPE] = page - \ No newline at end of file +""" \ No newline at end of file diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index 65df60f..fe6c3b0 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -24,8 +24,8 @@ from ..utils.enums.source import SourcePages from ..utils.enums.album import AlbumType from ..audio import write_metadata_to_target, correct_codec from ..utils import shared -from ..utils.shared import DEFAULT_VALUES, DOWNLOAD_PATH, DOWNLOAD_FILE -from ..utils.support_classes import Query, DownloadResult, DefaultTarget +from ..utils.shared import DEFAULT_VALUES, DOWNLOAD_PATH, DOWNLOAD_FILE, THREADED +from ..utils.support_classes import Query, DownloadResult, DefaultTarget, EndThread, FinishedSearch INDEPENDENT_DB_OBJECTS = Union[Label, Album, Artist, Song] @@ -104,7 +104,14 @@ def merge_together(old_object: DatabaseObject, new_object: DatabaseObject) -> Da return old_object -class Page(threading.Thread): +class LoreIpsum: + pass + + +Parent = threading.Thread if THREADED else LoreIpsum + + +class Page(Parent): """ This is an abstract class, laying out the functionality for every other class fetching something @@ -113,14 +120,24 @@ class Page(threading.Thread): SOURCE_TYPE: SourcePages LOGGER = logging.getLogger("this shouldn't be used") - def __init__(self, search_queue: Queue, search_result_queue: Queue): + 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 - threading.Thread.__init__(self) + Parent.__init__(self) + + @property + def _empty_working_queues(self): + return self.search_queue.empty() def run(self) -> None: - pass + while bool(self.end_event) and self._empty_working_queues: + if not self.search_queue.empty(): + 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 @@ -146,7 +163,9 @@ class Page(threading.Thread): r = [] for default_query in query.default_search: - r.extend(self.general_search(default_query)) + for single_option in self.general_search(default_query): + r.append(single_option) + self.search_result_queue.put(single_option) return r diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index ef396e2..b17d3cd 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -103,3 +103,5 @@ SORT_BY_DATE = AUDIO_SECTION.SORT_BY_DATE.object_from_value SORT_BY_ALBUM_TYPE = AUDIO_SECTION.SORT_BY_ALBUM_TYPE.object_from_value ALBUM_TYPE_BLACKLIST: Set[AlbumType] = set(AUDIO_SECTION.ALBUM_TYPE_BLACKLIST.object_from_value) + +THREADED = False diff --git a/src/music_kraken/utils/support_classes/__init__.py b/src/music_kraken/utils/support_classes/__init__.py index 625ed41..0789e87 100644 --- a/src/music_kraken/utils/support_classes/__init__.py +++ b/src/music_kraken/utils/support_classes/__init__.py @@ -1,3 +1,4 @@ from .default_target import DefaultTarget from .download_result import DownloadResult from .query import Query +from .thread_classes import EndThread, FinishedSearch diff --git a/src/music_kraken/utils/support_classes/thread_classes.py b/src/music_kraken/utils/support_classes/thread_classes.py new file mode 100644 index 0000000..1a17e57 --- /dev/null +++ b/src/music_kraken/utils/support_classes/thread_classes.py @@ -0,0 +1,12 @@ +class EndThread: + _has_ended: bool = False + + def __bool__(self): + return self._has_ended + + def exit(self): + self._has_ended + +class FinishedSearch: + pass + \ No newline at end of file