Skip to content
Permalink
Browse files

fix: Detecting paths relative to template directory in logging

Fixes #166
  • Loading branch information
oprypin committed Nov 1, 2020
1 parent 9815d5a commit a50046b5d58d62df4ba13f4c197e80edd1995eb9
Showing with 11 additions and 4 deletions.
  1. +2 −1 src/mkdocstrings/handlers/base.py
  2. +9 −3 src/mkdocstrings/loggers.py
@@ -23,6 +23,7 @@
from mkdocstrings.loggers import get_template_logger

handlers_cache: Dict[str, Any] = {}
TEMPLATES_DIR = Path(__file__).parent.parent / "templates"


class CollectionError(Exception):
@@ -120,7 +121,7 @@ def __init__(self, directory: str, theme: str, custom_templates: Optional[str] =
if custom_templates is not None:
paths.append(Path(custom_templates) / directory / theme)

themes_dir = Path(__file__).parent.parent / "templates" / directory
themes_dir = TEMPLATES_DIR / directory

paths.append(themes_dir / theme)

@@ -1,12 +1,15 @@
"""Logging functions."""

import logging
from pathlib import Path
from typing import Callable, Optional

from jinja2 import contextfunction
from jinja2.runtime import Context
from mkdocs.utils import warning_filter

from mkdocstrings.handlers import base


class LoggerAdapter(logging.LoggerAdapter):
"""A logger adapter to prefix messages."""
@@ -102,9 +105,12 @@ def get_template_path(context: Context) -> str:
Returns:
The relative path to the template.
"""
template = context.environment.get_template(context.name)
if template.filename:
return template.filename.rsplit("/templates/", 1)[1]
filename = context.environment.get_template(context.name).filename
if filename:
try:
return str(Path(filename).relative_to(base.TEMPLATES_DIR))
except ValueError:
return filename
return context.name


0 comments on commit a50046b

Please sign in to comment.