Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
refactor: add custom `RichHandler` class
NTFSvolume0d3eda9c7e7a
diff --git a/cyberdrop_dl/main.py b/cyberdrop_dl/main.pyindex c0eba8906..a7440f3c9 100644--- a/cyberdrop_dl/main.py+++ b/cyberdrop_dl/main.py@@ -12,8 +12,7 @@ from typing import TYPE_CHECKINGimport browser_cookie3from pydantic import ValidationError-from rich.console import Console-from rich.logging import RichHandler+from rich import print as rich_printfrom cyberdrop_dl import envfrom cyberdrop_dl.clients.errors import InvalidYamlError@@ -24,10 +23,9 @@ from cyberdrop_dl.utils import constantsfrom cyberdrop_dl.utils.apprise import send_apprise_notificationsfrom cyberdrop_dl.utils.dumper import Dumperfrom cyberdrop_dl.utils.logger import (+ CustomRichHandler,QueuedLogger,- RedactedConsole,SplitRichHandler,- add_custom_log_render,log,log_spacer,log_with_color,@@ -133,18 +131,16 @@ def setup_startup_logger(*, first_time_setup: bool = False) -> None:STARTUP_LOGGER_FILE.unlink(missing_ok=True) # Only delete file once. Subsequent calls will append to filedestroy_startup_logger()startup_logger.setLevel(10)- console_handler = RichHandler(**(constants.RICH_HANDLER_CONFIG | {"show_time": False}), level=10)+ console_handler = CustomRichHandler(level=10)startup_logger.addHandler(console_handler)file_io = STARTUP_LOGGER_FILE.open("a", encoding="utf8")- file_console = RedactedConsole(file=file_io, width=constants.DEFAULT_CONSOLE_WIDTH)- file_handler = RichHandler(**constants.RICH_HANDLER_CONFIG, console=file_console, level=10)- add_custom_log_render(file_handler)+ file_handler = CustomRichHandler(level=10, file=file_io, width=constants.DEFAULT_CONSOLE_WIDTH)startup_logger.addHandler(file_handler)def destroy_startup_logger(remove_all_handlers: bool = True) -> None:- handlers: list[RichHandler] = startup_logger.handlers # type: ignore+ handlers: list[CustomRichHandler] = startup_logger.handlers # type: ignorefor handler in handlers[:]: # create copyif not (handler.console._file or remove_all_handlers):continue@@ -192,10 +188,9 @@ def setup_debug_logger(manager: Manager) -> Path | None:startup_logger.exception(str(e))sys.exit(1)- file_console = Console(file=file_io, width=manager.config_manager.settings_data.logs.log_line_width)- file_handler_debug = SplitRichHandler(**constants.RICH_HANDLER_DEBUG_CONFIG, console=file_console, level=log_level)- add_custom_log_render(file_handler_debug)-+ file_handler_debug = SplitRichHandler(+ level=log_level, file=file_io, width=manager.config_manager.settings_data.logs.log_line_width, debug=True+ )queued_logger = QueuedLogger.new(file_handler_debug)manager.loggers["debug"] = queued_loggerdebug_logger.addHandler(queued_logger.handler)@@ -237,12 +232,12 @@ def setup_logger(manager: Manager, config_name: str) -> None:constants.CONSOLE_LEVEL = manager.config_manager.settings_data.runtime_options.console_log_levelconsole_log_level = constants.CONSOLE_LEVEL- file_console = RedactedConsole(file=file_io, width=manager.config_manager.settings_data.logs.log_line_width)- file_handler = SplitRichHandler(**constants.RICH_HANDLER_CONFIG, console=file_console, level=log_level)- console_handler = RichHandler(**(constants.RICH_HANDLER_CONFIG | {"show_time": False}), level=console_log_level)- add_custom_log_render(file_handler)+ console_handler = CustomRichHandler(level=console_log_level)logger.addHandler(console_handler)+ file_handler = SplitRichHandler(+ level=log_level, file=file_io, width=manager.config_manager.settings_data.logs.log_line_width+ )queued_logger = QueuedLogger.new(file_handler)manager.loggers["main"] = queued_loggerlogger.addHandler(queued_logger.handler)@@ -328,7 +323,7 @@ def actual_main() -> None:asyncio.run(director(manager))exit_code = 0except KeyboardInterrupt:- Console().print("Trying to Exit ...")+ rich_print("Trying to Exit ...")finally:asyncio.run(manager.close())loop.close()diff --git a/cyberdrop_dl/utils/constants.py b/cyberdrop_dl/utils/constants.pyindex cf113b088..eea6de9f0 100644--- a/cyberdrop_dl/utils/constants.py+++ b/cyberdrop_dl/utils/constants.py@@ -11,10 +11,8 @@ MAX_NAME_LENGTHS = {"FILE": 95, "FOLDER": 60}DEFAULT_CONSOLE_WIDTH = 240CSV_DELIMITER = ","LOG_OUTPUT_TEXT = Text("")-RICH_HANDLER_CONFIG: dict[str, Any] = {"show_time": True, "rich_tracebacks": True, "tracebacks_show_locals": False}-RICH_HANDLER_DEBUG_CONFIG = {- "show_time": True,- "rich_tracebacks": True,+RICH_HANDLER_CONFIG: dict[str, Any] = {"rich_tracebacks": True, "tracebacks_show_locals": False}+RICH_HANDLER_DEBUG_CONFIG = RICH_HANDLER_CONFIG | {"tracebacks_show_locals": True,"locals_max_string": DEFAULT_CONSOLE_WIDTH,"tracebacks_extra_lines": 2,diff --git a/cyberdrop_dl/utils/logger.py b/cyberdrop_dl/utils/logger.pyindex c0506b917..27f06773e 100644--- a/cyberdrop_dl/utils/logger.py+++ b/cyberdrop_dl/utils/logger.py@@ -7,7 +7,7 @@ import queuefrom dataclasses import dataclassfrom logging.handlers import QueueHandler, QueueListenerfrom pathlib import Path-from typing import TYPE_CHECKING+from typing import IO, TYPE_CHECKINGfrom rich._log_render import LogRenderfrom rich._null_file import NullFile@@ -40,7 +40,29 @@ if TYPE_CHECKING:EXCLUDE_PATH_LOGGING_FROM = "logger.py", "base.py", "session.py", "cache_control.py"-class SplitRichHandler(RichHandler):+class CustomRichHandler(RichHandler):+ """Rich Handler with default settings and custom log render to remove padding in files."""++ def __init__(+ self,+ level: int = logging.NOTSET,+ file: IO[str] | None = None,+ width: int | None = None,+ debug: bool = False,+ **kwargs,+ ) -> None:+ is_file: bool = file is not None+ redacted: bool = is_file and not debug+ console_cls = RedactedConsole if redacted else Console+ console = console_cls(file=file, width=width)+ options = constants.RICH_HANDLER_DEBUG_CONFIG if debug else constants.RICH_HANDLER_CONFIG+ options = options | kwargs+ super().__init__(level, console, show_time=is_file, **options)+ if is_file:+ self._log_render = NoPaddingLogRender(show_level=True)+++class SplitRichHandler(CustomRichHandler):"""Custom class to split the creation of the log renderable and its emition"""def get_log_renderable(self, record: logging.LogRecord) -> ConsoleRenderable:@@ -205,15 +227,6 @@ def indent_text(text: Text, console: Console, indent: int = 30) -> Text:return first_line.append(new_text)-def add_custom_log_render(rich_handler: RichHandler) -> None:- rich_handler._log_render = NoPaddingLogRender(- omit_repeated_times=True,- show_time=True,- show_level=True,- show_path=True,- )--class RedactedConsole(Console):def _render_buffer(self, buffer) -> str:output: str = super()._render_buffer(buffer)