diff --git a/src/subsearch/__init__.py b/src/subsearch/__init__.py index ae665238..984c75b3 100644 --- a/src/subsearch/__init__.py +++ b/src/subsearch/__init__.py @@ -2,7 +2,6 @@ import time from pathlib import Path - from subsearch import core from subsearch.globals import decorators @@ -18,7 +17,7 @@ def __init__(self) -> None: self.subsearch_core = core.SubsearchCore(PREF_COUNTER) def search_for_subtitles(self) -> None: - self.subsearch_core.search_for_subtitles( + self.subsearch_core.init_search( self.provider_opensubtitles, self.provider_subscene, self.provider_yifysubtitles, diff --git a/src/subsearch/core.py b/src/subsearch/core.py index a35a3382..0c572d90 100644 --- a/src/subsearch/core.py +++ b/src/subsearch/core.py @@ -1,19 +1,15 @@ import ctypes import time from pathlib import Path -from subsearch.globals import propagating_thread -from subsearch.globals.constants import APP_PATHS, DEVICE_INFO, FILE_PATHS, VIDEO_FILE, VERSION +from typing import Callable + +from subsearch.globals import decorators, thread_handle +from subsearch.globals.constants import APP_PATHS, DEVICE_INFO, FILE_PATHS, VIDEO_FILE from subsearch.globals.dataclasses import Subtitle -from subsearch.globals import decorators from subsearch.gui import screen_manager, system_tray from subsearch.gui.screens import download_manager from subsearch.providers import opensubtitles, subscene, yifysubtitles -from subsearch.utils import ( - io_file_system, - io_log, - io_toml, - string_parser, -) +from subsearch.utils import io_file_system, io_log, io_toml, string_parser class Initializer: @@ -94,10 +90,19 @@ def __init__(self, pref_counter: float) -> None: if not self.all_providers_disabled(): io_log.log.brackets("Search started") - def search_for_subtitles(self, *tasks) -> None: - propagating_thread.handle_tasks(*tasks) + @decorators.call_func + def init_search(self, *providers: Callable[..., None]) -> None: + self._create_threads(*providers) io_log.log.task_completed() + def _create_threads(self, *tasks) -> None: + for thread_count, target in enumerate(tasks, start=1): + func_name = str(target.__name__).split("_")[-1] + name = f"thread_{thread_count}_{func_name}" + task_thread = thread_handle.CreateThread(target=target, name=name) + task_thread.start() + task_thread.join() + def start_search(self, provider, flag: str = "") -> None: search_provider = provider(**self.search_kwargs) if flag: diff --git a/src/subsearch/globals/decorators.py b/src/subsearch/globals/decorators.py index 33294203..a5d13458 100644 --- a/src/subsearch/globals/decorators.py +++ b/src/subsearch/globals/decorators.py @@ -1,7 +1,7 @@ import ctypes -from datetime import datetime import inspect import sys +from datetime import datetime from typing import Any, Callable, Union from subsearch import core @@ -91,6 +91,7 @@ def conditions_met(cls: Union["core.SubsearchCore", "core.Initializer"], *args, func_name = kwargs["func_name"] conditions: dict[str, list[bool]] = { + "add_providers": [], "opensubtitles": [ _CoreSubsearchFuncCondtitons.language_compatibility("opensubtitles"), cfg.providers["opensubtitles_hash"] or cfg.providers["opensubtitles_site"], @@ -128,11 +129,11 @@ def conditions_met(cls: Union["core.SubsearchCore", "core.Initializer"], *args, def capture_call_info(func): def wrapper(*args, **kwargs): - frame = inspect.currentframe().f_back + frame = inspect.currentframe().f_back # type: ignore current_time = datetime.now().time() call_time = current_time.strftime("%H:%M:%S.%f")[:-3] - kwargs["call_module"] = frame.f_globals["__name__"].split(".")[-1] - kwargs["call_lineno"] = frame.f_lineno + kwargs["call_module"] = frame.f_globals["__name__"].split(".")[-1] # type: ignore + kwargs["call_lineno"] = frame.f_lineno # type: ignore kwargs["call_ct"] = call_time return func(*args, **kwargs) diff --git a/src/subsearch/globals/propagating_thread.py b/src/subsearch/globals/thread_handle.py similarity index 72% rename from src/subsearch/globals/propagating_thread.py rename to src/subsearch/globals/thread_handle.py index 878c6073..06dd7347 100644 --- a/src/subsearch/globals/propagating_thread.py +++ b/src/subsearch/globals/thread_handle.py @@ -1,14 +1,14 @@ +import threading from typing import Any -from subsearch.utils import io_log -import threading +from subsearch.utils import io_log -class PropagatingThread(threading.Thread): +class CreateThread(threading.Thread): def __init__(self, *args, **kwargs) -> None: self._target = None self._args = () - self._kwargs = {} + self._kwargs = {} # type: ignore super().__init__(*args, **kwargs) self.exception = None self.return_value = None @@ -20,7 +20,7 @@ def run(self) -> None: if self._target: self.return_value = self._target(*self._args, **self._kwargs) except Exception as e: - self.exception = e + self.exception = e # type: ignore finally: del self._target, self._args, self._kwargs @@ -33,12 +33,3 @@ def join(self, timeout=None) -> Any: else: io_log.log.stdout(f"Thread {self.name} finnished", level="debug", print_allowed=False) return self.return_value - - -def handle_tasks(*tasks) -> None: - for thread_count, target in enumerate(tasks, start=1): - func_name = str(target.__name__).split("_")[-1] - name = f"thread_{thread_count}_{func_name}" - task_thread = PropagatingThread(target=target, name=name) - task_thread.start() - task_thread.join()