Skip to content

Commit

Permalink
More performance improvements (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Nov 21, 2024
1 parent d19e8e6 commit 81040e0
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 60 deletions.
20 changes: 14 additions & 6 deletions src/mkdocs_include_markdown_plugin/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import hashlib
import os
import stat
import time
from importlib.util import find_spec


try:
from platformdirs import user_data_dir
platformdirs_spec = find_spec('platformdirs')
except ImportError: # pragma: no cover
CACHE_AVAILABLE = False
else:
Expand Down Expand Up @@ -37,13 +39,19 @@ def generate_unique_key_from_url(cls, url: str) -> str:
return hashlib.blake2b(url.encode(), digest_size=16).digest().hex()

def read_file(self, fpath: str, encoding: str = 'utf-8') -> str: # noqa: D102
with open(fpath, encoding=encoding) as f:
return f.read().split('\n', 1)[1]
f = open(fpath, encoding=encoding) # noqa: SIM115
content = f.read().split('\n', 1)[1]
f.close()
return content

def get_(self, url: str, encoding: str = 'utf-8') -> str | None: # noqa: D102
key = self.generate_unique_key_from_url(url)
fpath = os.path.join(self.cache_dir, key)
if os.path.isfile(fpath):
try:
is_file = stat.S_ISREG(os.stat(fpath).st_mode)
except (FileNotFoundError, OSError): # pragma: no cover
return None
if is_file:
creation_time = self.get_creation_time_from_fpath(fpath)
if time.time() < creation_time + self.expiration_seconds:
return self.read_file(fpath, encoding=encoding)
Expand Down Expand Up @@ -72,9 +80,9 @@ def get_cache_directory() -> str | None:
if not CACHE_AVAILABLE:
return None

from platformdirs import user_data_dir
cache_dir = user_data_dir('mkdocs-include-markdown-plugin')
if not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
os.makedirs(cache_dir, exist_ok=True)

return cache_dir

Expand Down
40 changes: 29 additions & 11 deletions src/mkdocs_include_markdown_plugin/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
import re
import stat
import string
from dataclasses import dataclass
from typing import TYPE_CHECKING
Expand All @@ -22,7 +23,7 @@ class DirectiveBoolArgument: # noqa: D101


if TYPE_CHECKING: # pragma: no cover
from typing import Literal, TypedDict
from typing import Callable, Literal, TypedDict

DirectiveBoolArgumentsDict = dict[str, DirectiveBoolArgument]

Expand Down Expand Up @@ -119,7 +120,7 @@ def str_arg(arg: str) -> re.Pattern[str]:

def warn_invalid_directive_arguments(
arguments_string: str,
directive_lineno: int,
directive_lineno: Callable[[], int],
directive: Literal['include', 'include-markdown'],
page_src_path: str | None,
docs_dir: str,
Expand All @@ -129,13 +130,13 @@ def warn_invalid_directive_arguments(
INCLUDE_DIRECTIVE_ARGS if directive == 'include'
else INCLUDE_MARKDOWN_DIRECTIVE_ARGS
)
for arg_value in re.findall(
WARN_INVALID_DIRECTIVE_ARGS_REGEX,
for arg_match in WARN_INVALID_DIRECTIVE_ARGS_REGEX.finditer(
arguments_string,
):
arg_value = arg_match.group()
if arg_value.split('=', 1)[0] not in valid_args:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
logger.warning(
f"Invalid argument '{arg_value}' in"
Expand Down Expand Up @@ -226,19 +227,28 @@ def resolve_file_paths_to_include( # noqa: PLR0912
return [include_string], True

if process.is_absolute_path(include_string):
if os.name == 'nt': # pragma: nt cover
if os.name == 'nt': # pragma: no cover
# Windows
fpath = os.path.normpath(include_string)
if not os.path.isfile(fpath):
try:
is_file = stat.S_ISREG(os.stat(fpath).st_mode)
except (FileNotFoundError, OSError):
is_file = False
if not is_file:
return [], False

return process.filter_paths(
[fpath], ignore_paths,
), False

try:
is_file = stat.S_ISREG(os.stat(include_string).st_mode)
except (FileNotFoundError, OSError):
is_file = False
return process.filter_paths(
[include_string] if os.path.isfile(include_string)
else glob.iglob(include_string, flags=GLOB_FLAGS),
[include_string] if is_file else glob.iglob(
include_string, flags=GLOB_FLAGS,
),
ignore_paths), False

if process.is_relative_path(include_string):
Expand All @@ -253,7 +263,11 @@ def resolve_file_paths_to_include( # noqa: PLR0912
)
paths = []
include_path = os.path.join(root_dir, include_string)
if os.path.isfile(include_path):
try:
is_file = stat.S_ISREG(os.stat(include_path).st_mode)
except (FileNotFoundError, OSError):
is_file = False
if is_file:
paths.append(include_path)
else:
for fp in glob.iglob(
Expand All @@ -268,7 +282,11 @@ def resolve_file_paths_to_include( # noqa: PLR0912
paths = []
root_dir = docs_dir
include_path = os.path.join(root_dir, include_string)
if os.path.isfile(include_path):
try:
is_file = stat.S_ISREG(os.stat(include_path).st_mode)
except (FileNotFoundError, OSError):
is_file = False
if is_file:
paths.append(include_path)
else:
for fp in glob.iglob(
Expand Down
43 changes: 23 additions & 20 deletions src/mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import functools
import html
import os
import re
Expand Down Expand Up @@ -104,7 +105,8 @@ def found_include_tag( # noqa: PLR0912, PLR0915
match: re.Match[str],
) -> str:
directive_match_start = match.start()
directive_lineno = process.lineno_from_content_start(
directive_lineno = functools.partial(
process.lineno_from_content_start,
markdown,
directive_match_start,
)
Expand All @@ -114,7 +116,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
filename, raw_filename = parse_filename_argument(match)
if filename is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Found no path passed including with 'include'"
Expand All @@ -137,7 +139,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
exclude_string = parse_string_argument(exclude_match)
if exclude_string is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'exclude' argument in 'include'"
Expand All @@ -159,7 +161,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915

if not file_paths_to_include:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
f"No files found including '{raw_filename}' at {location}",
Expand All @@ -176,7 +178,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
)
if invalid_bool_args:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
f"Invalid value for '{invalid_bool_args[0]}' argument of"
Expand All @@ -189,7 +191,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
start = parse_string_argument(start_match)
if start is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'start' argument in 'include' directive at"
Expand All @@ -203,7 +205,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
end = parse_string_argument(end_match)
if end is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'end' argument in 'include' directive at"
Expand All @@ -217,7 +219,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
encoding = parse_string_argument(encoding_match)
if encoding is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'encoding' argument in 'include'"
Expand Down Expand Up @@ -294,7 +296,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
])
plural_suffix = 's' if len(file_paths_to_include) > 1 else ''
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
logger.warning(
f"Delimiter {delimiter_name} '{delimiter_value}'"
Expand All @@ -313,7 +315,8 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
match: re.Match[str],
) -> str:
directive_match_start = match.start()
directive_lineno = process.lineno_from_content_start(
directive_lineno = functools.partial(
process.lineno_from_content_start,
markdown,
directive_match_start,
)
Expand All @@ -324,7 +327,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
filename, raw_filename = parse_filename_argument(match)
if filename is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Found no path passed including with 'include-markdown'"
Expand All @@ -347,7 +350,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
exclude_string = parse_string_argument(exclude_match)
if exclude_string is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'exclude' argument in 'include-markdown'"
Expand All @@ -368,7 +371,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915

if not file_paths_to_include:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
f"No files found including '{raw_filename}' at {location}",
Expand All @@ -388,7 +391,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
)
if invalid_bool_args:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
f"Invalid value for '{invalid_bool_args[0]}' argument of"
Expand All @@ -402,7 +405,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
start = parse_string_argument(start_match)
if start is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'start' argument in 'include-markdown'"
Expand All @@ -416,7 +419,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
end = parse_string_argument(end_match)
if end is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'end' argument in 'include-markdown'"
Expand All @@ -430,7 +433,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
encoding = parse_string_argument(encoding_match)
if encoding is None:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'encoding' argument in 'include-markdown'"
Expand All @@ -447,7 +450,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
offset_raw_value = offset_match[1]
if offset_raw_value == '':
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
"Invalid empty 'heading-offset' argument in"
Expand All @@ -457,7 +460,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
offset = int(offset_raw_value)
except ValueError:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
raise PluginError(
f"Invalid 'heading-offset' argument \"{offset_raw_value}\""
Expand Down Expand Up @@ -579,7 +582,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
])
plural_suffix = 's' if len(file_paths_to_include) > 1 else ''
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
page_src_path, docs_dir, directive_lineno(),
)
logger.warning(
f"Delimiter {delimiter_name} '{delimiter_value}' of"
Expand Down
2 changes: 1 addition & 1 deletion src/mkdocs_include_markdown_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from typing import TYPE_CHECKING

from mkdocs.exceptions import PluginError
from mkdocs.livereload import LiveReloadServer
from mkdocs.plugins import BasePlugin, event_priority


if TYPE_CHECKING: # pragma: no cover
import re

from mkdocs.config.defaults import MkDocsConfig
from mkdocs.livereload import LiveReloadServer
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page

Expand Down
Loading

0 comments on commit 81040e0

Please sign in to comment.