Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move logger to Context #119

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 4 in scraper/src/mindtouch2zim/__main__.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/mindtouch2zim/__main__.py#L4

Added line #L4 was not covered by tests
from mindtouch2zim.entrypoint import prepare_context

logger = Context.logger

Check warning on line 7 in scraper/src/mindtouch2zim/__main__.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/mindtouch2zim/__main__.py#L7

Added line #L7 was not covered by tests


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
Loading