Skip to content

Commit

Permalink
Merge pull request #119 from openzim/move_logger
Browse files Browse the repository at this point in the history
Move logger to Context
  • Loading branch information
benoit74 authored Dec 16, 2024
2 parents 362e1ce + ca6e085 commit 21f752f
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 19 deletions.
4 changes: 3 additions & 1 deletion scraper/src/mindtouch2zim/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
import tempfile

from mindtouch2zim.constants import logger
from mindtouch2zim.context import Context
from mindtouch2zim.entrypoint import prepare_context

logger = Context.logger


def main():
try:
Expand Down
2 changes: 1 addition & 1 deletion scraper/src/mindtouch2zim/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from zimscraperlib.rewriting.url_rewriting import HttpUrl, ZimPath
from zimscraperlib.zim import Creator

from mindtouch2zim.constants import logger
from mindtouch2zim.context import Context
from mindtouch2zim.download import stream_file
from mindtouch2zim.errors import (
Expand Down Expand Up @@ -46,6 +45,7 @@
WEBP_OPTIONS = WebpMedium().options

context = Context.get()
logger = context.logger


class HeaderData(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion scraper/src/mindtouch2zim/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from pydantic import BaseModel
from requests import Response

from mindtouch2zim.constants import logger
from mindtouch2zim.context import Context
from mindtouch2zim.errors import APITokenRetrievalError, MindtouchParsingError
from mindtouch2zim.html import get_soup

context = Context.get()
logger = context.logger


class MindtouchHome(BaseModel):
Expand Down
9 changes: 0 additions & 9 deletions scraper/src/mindtouch2zim/constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import logging
import pathlib

from zimscraperlib.logging import DEFAULT_FORMAT_WITH_THREADS, getLogger

from mindtouch2zim.__about__ import __version__

NAME = "mindtouch2zim"
Expand All @@ -15,9 +12,3 @@
STANDARD_KNOWN_BAD_ASSETS_REGEX = (
r"https?:\/\/(a\.mtstatic\.com\/@(cache|style)|localhost(:|\/))"
)

# logger to use everywhere (not part of Context class because we need it early, before
# Context has been initialized)
logger: logging.Logger = getLogger(
NAME, level=logging.DEBUG, log_format=DEFAULT_FORMAT_WITH_THREADS
)
11 changes: 9 additions & 2 deletions scraper/src/mindtouch2zim/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dataclasses
import logging
import os
import re
import threading
Expand All @@ -7,12 +8,12 @@
import requests
from zimscraperlib.constants import NAME as SCRAPERLIB_NAME
from zimscraperlib.constants import VERSION as SCRAPERLIB_VERSION
from zimscraperlib.logging import DEFAULT_FORMAT_WITH_THREADS, getLogger

from mindtouch2zim.constants import (
NAME,
STANDARD_KNOWN_BAD_ASSETS_REGEX,
VERSION,
logger,
)

MINDTOUCH_TMP = os.getenv("MINDTOUCH_TMP")
Expand Down Expand Up @@ -106,6 +107,12 @@ class Context:
# Maximum number of pixels of images that will be pushed to the ZIM
maximum_image_pixels: int = 1280 * 720

# logger to use everywhere (do not mind about mutability, we want to reuse same
# logger everywhere)
logger: logging.Logger = getLogger( # noqa: RUF009
NAME, level=logging.DEBUG, log_format=DEFAULT_FORMAT_WITH_THREADS
)

@classmethod
def setup(cls, **kwargs):
new_instance = cls(**kwargs)
Expand All @@ -132,7 +139,7 @@ def current_thread_workitem(self) -> str:
@current_thread_workitem.setter
def current_thread_workitem(self, value: str):
self._current_thread_workitem.value = value
logger.debug(f"Processing {value}")
Context.logger.debug(f"Processing {value}")

@property
def wm_user_agent(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion scraper/src/mindtouch2zim/html_rewriting.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

from mindtouch2zim.asset import AssetManager
from mindtouch2zim.client import LibraryPage
from mindtouch2zim.constants import logger
from mindtouch2zim.context import Context
from mindtouch2zim.utils import is_better_srcset_descriptor
from mindtouch2zim.vimeo import get_vimeo_thumbnail_url

context = Context.get()
logger = context.logger

# remove all standard rules, they are not adapted to Vue.JS UI
html_rules.rewrite_attribute_rules.clear()
Expand Down
2 changes: 1 addition & 1 deletion scraper/src/mindtouch2zim/libretexts/detailed_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from zimscraperlib.rewriting.html import HtmlRewriter

from mindtouch2zim.client import LibraryPage, MindtouchClient
from mindtouch2zim.constants import logger
from mindtouch2zim.context import Context
from mindtouch2zim.libretexts.errors import BadBookPageError

context = Context.get()
logger = context.logger


class LicenseStatistic(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion scraper/src/mindtouch2zim/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
NAME,
ROOT_DIR,
VERSION,
logger,
)
from mindtouch2zim.context import Context
from mindtouch2zim.download import stream_file
Expand All @@ -64,6 +63,7 @@
from mindtouch2zim.zimconfig import ZimConfig

context = Context.get()
logger = context.logger


class ContentFilter(BaseModel):
Expand Down
5 changes: 4 additions & 1 deletion scraper/src/mindtouch2zim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from typing import Any
from urllib.parse import urlparse

from mindtouch2zim.constants import logger
from mindtouch2zim.context import Context

context = Context.get()
logger = context.logger


def get_asset_path_from_url(online_url: str, already_used_paths: list[Path]) -> Path:
Expand Down
2 changes: 1 addition & 1 deletion scraper/src/mindtouch2zim/vimeo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from mindtouch2zim.constants import logger
from mindtouch2zim.context import Context
from mindtouch2zim.errors import VimeoThumbnailError

context = Context.get()
logger = context.logger


def get_vimeo_thumbnail_url(video_url: str) -> str:
Expand Down
5 changes: 5 additions & 0 deletions scraper/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def context_defaults():
return CONTEXT_DEFAULTS


def test_context_logger():
# ensure we have only one logger object everywhere
assert Context.logger == Context.get().logger


def test_context_defaults():
context = Context.get()
assert context == processor_context # check both objects are same
Expand Down

0 comments on commit 21f752f

Please sign in to comment.