Skip to content

Commit

Permalink
Support for dirty builds
Browse files Browse the repository at this point in the history
  • Loading branch information
daizutabi committed Feb 11, 2024
1 parent 0008c0a commit 26fb68b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ ignore = [
"ANN003",
"ANN101",
"ANN102",
"BLE001",
"COM812",
"D105",
"D406",
Expand All @@ -89,6 +90,7 @@ ignore = [
"FIX002",
"FIX003",
"G004",
"DTZ005",
"ISC001",
"N812",
"PGH003",
Expand Down
55 changes: 27 additions & 28 deletions src/mkapi/pages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Page class that works with other converter."""
from __future__ import annotations

import datetime
import re
from dataclasses import dataclass, field
from dataclasses import dataclass
from typing import TYPE_CHECKING

import mkapi.markdown
Expand All @@ -14,10 +15,17 @@
from mkapi.utils import is_module_cache_dirty, split_filters

if TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Callable, Iterable
from pathlib import Path


def write_source(pages: Iterable[Page]) -> None:
"""Write dummy markdown source."""
for page in pages:
if page.name and is_module_cache_dirty(page.name):
page.write_source()

Check warning on line 26 in src/mkapi/pages.py

View check run for this annotation

Codecov / codecov/patch

src/mkapi/pages.py#L24-L26

Added lines #L24 - L26 were not covered by tests


@dataclass
class Page:
"""Page class."""
Expand All @@ -26,45 +34,37 @@ class Page:
path: Path
filters: list[str]
kind: str
source: str = field(default="", init=False)
markdown: str = field(default="", init=False)

def __post_init__(self) -> None:
# Delete in MkDocs v1.6. Switch to virtual files
if not self.path.exists():
if not self.path.parent.exists():
self.path.parent.mkdir(parents=True)
with self.path.open("w") as file:
file.write("") # Dummy content
self.write_source()

def write_source(self) -> None:
"""Write dummy markdown source."""
if self.kind in ["object", "source"]:
self.set_markdown()

def set_markdown(self) -> None:
"""Set markdown."""
self.source = create_markdown(
self.name,
self.path,
self.filters,
is_source=self.kind == "source",
)
with self.path.open("w") as file:
file.write(f"{datetime.datetime.now()}")

def convert_markdown(self, source: str, anchor: str) -> str:
"""Return converted markdown."""
is_source = self.kind == "source"
if self.kind in ["object", "source"]:
if self.markdown and not is_module_cache_dirty(self.name):
return self.markdown

elif self.kind == "markdown":
self.source = source

self.markdown = convert_markdown(
self.source,
source = create_markdown(

Check warning on line 55 in src/mkapi/pages.py

View check run for this annotation

Codecov / codecov/patch

src/mkapi/pages.py#L55

Added line #L55 was not covered by tests
self.name,
self.path,
self.filters,
is_source=is_source,
)

return convert_markdown(
source,
self.path,
anchor,
is_source=self.kind == "source",
is_source=is_source,
)
return self.markdown


object_paths: dict[str, Path] = {}
Expand All @@ -80,7 +80,6 @@ def create_markdown(
is_source: bool = False,
) -> str:
"""Create object page for an object."""
# if not (obj := get_object(name)) or not isinstance(obj, Module):
if not (module := load_module(name)):
return f"!!! failure\n\n module {name!r} not found.\n"

Expand Down Expand Up @@ -133,7 +132,7 @@ def replace_link(match: re.Match) -> str:


def _get_level_name_filters(match: re.Match) -> tuple[str, int, list[str]]:
heading, name = match.group("heading"), match.group("name")
heading, name = match.groups()
level = len(heading)
name, filters = split_filters(name)
return name, level, filters
Expand Down
14 changes: 12 additions & 2 deletions src/mkapi/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from mkapi import renderers
from mkapi.nav import split_name_depth
from mkapi.pages import Page, convert_source
from mkapi.utils import cache_clear, get_module_path, is_package
from mkapi.utils import cache_clear, get_module_path, is_module_cache_dirty, is_package

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -61,9 +61,19 @@ class MkAPIPlugin(BasePlugin[MkAPIConfig]):
def __init__(self) -> None:
self.api_dirs = []
self.pages = {}
self.dirty = False

def on_startup(self, *, command: str, dirty: bool) -> None:
self.dirty = dirty

Check warning on line 67 in src/mkapi/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/mkapi/plugins.py#L67

Added line #L67 was not covered by tests

def on_config(self, config: MkDocsConfig, **kwargs) -> MkDocsConfig:
cache_clear()

if self.dirty:
for page in self.pages.values():
if page.name and is_module_cache_dirty(page.name):
page.write_source()

Check warning on line 75 in src/mkapi/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/mkapi/plugins.py#L73-L75

Added lines #L73 - L75 were not covered by tests

self.bar = None
self.uri_width = 0

Expand Down Expand Up @@ -120,7 +130,7 @@ def on_page_markdown(self, markdown: str, page: MkDocsPage, **kwargs) -> str:

try:
return page_.convert_markdown(markdown, anchor)
except Exception as e: # noqa: BLE001
except Exception as e:

Check warning on line 133 in src/mkapi/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/mkapi/plugins.py#L133

Added line #L133 was not covered by tests
if self.config.debug:
raise

Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@cache
def f():
return datetime.datetime.now() # noqa: DTZ005
return datetime.datetime.now()


c = cache({})
Expand Down

0 comments on commit 26fb68b

Please sign in to comment.