Skip to content

Commit

Permalink
refactor: Stop using deprecated warning filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Aug 19, 2023
1 parent c3efc5d commit ae3a584
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/mkdocs_spellcheck/backends/codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
)

from mkdocs_spellcheck.backends import Backend
from mkdocs_spellcheck.loggers import get_logger
from mkdocs_spellcheck.loggers import get_plugin_logger

if TYPE_CHECKING:
from mkdocs.structure.pages import Page

logger = get_logger(__name__)
logger = get_plugin_logger(__name__)

DEFAULT_DICTS = _builtin_default.split(",")

Expand Down
4 changes: 2 additions & 2 deletions src/mkdocs_spellcheck/backends/symspellpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from symspellpy import SymSpell, Verbosity

from mkdocs_spellcheck.backends import Backend
from mkdocs_spellcheck.loggers import get_logger
from mkdocs_spellcheck.loggers import get_plugin_logger

if TYPE_CHECKING:
from mkdocs.structure.pages import Page

logger = get_logger(__name__)
logger = get_plugin_logger(__name__)


class SymspellpyBackend(Backend):
Expand Down
41 changes: 7 additions & 34 deletions src/mkdocs_spellcheck/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,19 @@
from __future__ import annotations

import logging
from typing import Any
from typing import Any, MutableMapping

from mkdocs.utils import warning_filter


class LoggerAdapter(logging.LoggerAdapter):
"""A logger adapter to prefix messages."""

def __init__(self, prefix: str, logger: logging.Logger):
"""Initialize the object.
Arguments:
prefix: The string to insert in front of every message.
logger: The logger instance.
"""
# TODO: remove once support for MkDocs <1.5 is dropped (use built-in)
class PrefixedLogger(logging.LoggerAdapter): # noqa: D101
def __init__(self, prefix: str, logger: logging.Logger) -> None: # noqa: D107
super().__init__(logger, {})
self.prefix = prefix

def process(self, msg: str, kwargs: Any) -> tuple[str, Any]:
"""Process the message.
Arguments:
msg: The message:
kwargs: Remaining arguments.
Returns:
The processed message.
"""
def process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, Any]: # noqa: D102
return f"{self.prefix}: {msg}", kwargs


def get_logger(name: str) -> LoggerAdapter:
"""Return a pre-configured logger.
Arguments:
name: The name to use with `logging.getLogger`.
Returns:
A logger configured to work well in MkDocs.
"""
def get_plugin_logger(name: str) -> PrefixedLogger: # noqa: D103
logger = logging.getLogger(f"mkdocs.plugins.{name}")
logger.addFilter(warning_filter)
return LoggerAdapter(name.split(".", 1)[0], logger)
return PrefixedLogger(name.split(".", 1)[0], logger)
4 changes: 2 additions & 2 deletions src/mkdocs_spellcheck/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from mkdocs.plugins import BasePlugin

from mkdocs_spellcheck.backends import codespell, symspellpy
from mkdocs_spellcheck.loggers import get_logger
from mkdocs_spellcheck.loggers import get_plugin_logger
from mkdocs_spellcheck.words import get_words

if TYPE_CHECKING:
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.pages import Page
from symspellpy import SymSpell

logger = get_logger(__name__)
logger = get_plugin_logger(__name__)


backends_map: dict[str, Any] = {
Expand Down

0 comments on commit ae3a584

Please sign in to comment.