From 1c653fa87c99311d19ebb8d3551e666e64da1238 Mon Sep 17 00:00:00 2001 From: object-Object Date: Tue, 7 Nov 2023 00:04:41 -0500 Subject: [PATCH] Consolidate hexdoc hooks --- pyproject.toml | 5 +++-- src/hexdoc/_cli/app.py | 2 +- src/hexdoc/_cli/utils/render.py | 11 ++++------- src/hexdoc/_hooks.py | 19 ++++++++++++++++++- src/hexdoc/core/properties.py | 20 +++++++++++++++++--- src/hexdoc/minecraft/_hooks.py | 12 ------------ src/hexdoc/patchouli/_hooks.py | 22 ---------------------- 7 files changed, 43 insertions(+), 48 deletions(-) delete mode 100644 src/hexdoc/minecraft/_hooks.py delete mode 100644 src/hexdoc/patchouli/_hooks.py diff --git a/pyproject.toml b/pyproject.toml index aa0c9ccfd..a22bcb3f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,8 +103,6 @@ hexdoc = "hexdoc._cli.app:app" [project.entry-points.hexdoc] hexdoc = "hexdoc._hooks:HexdocPlugin" -minecraft = "hexdoc.minecraft._hooks:MinecraftPlugin" -patchouli = "hexdoc.patchouli._hooks:PatchouliPlugin" # Pytest @@ -139,6 +137,9 @@ extend-exclude = [ "[{][{]cookiecutter.output_directory[}][}]", ] +[tool.ruff.lint.isort] +combine-as-imports = true + # type checking [tool.pyright] diff --git a/src/hexdoc/_cli/app.py b/src/hexdoc/_cli/app.py index 7d388917e..be3369e37 100644 --- a/src/hexdoc/_cli/app.py +++ b/src/hexdoc/_cli/app.py @@ -155,7 +155,7 @@ def render( template_names = ( props.template.render - if props.template.was_render_set + if props.template.override_default_render else pm.default_rendered_templates(props.template.include) ) | props.template.extend_render diff --git a/src/hexdoc/_cli/utils/render.py b/src/hexdoc/_cli/utils/render.py index 223d97df3..df9c8f419 100644 --- a/src/hexdoc/_cli/utils/render.py +++ b/src/hexdoc/_cli/utils/render.py @@ -4,7 +4,7 @@ import logging import shutil from pathlib import Path -from typing import Any +from typing import Any, Sequence from _hexdoc_favicons import Favicons from jinja2 import ( @@ -20,6 +20,7 @@ from jinja2.sandbox import SandboxedEnvironment from hexdoc.core import Properties, ResourceLocation +from hexdoc.core.properties import JINJA_NAMESPACE_ALIASES from hexdoc.data import HexdocMetadata from hexdoc.jinja import ( IncludeRawExtension, @@ -37,10 +38,6 @@ class HexdocTemplateLoader(BaseLoader): - NAMESPACE_ALIASES = { - "patchouli": "hexdoc", - } - def __init__( self, included: dict[str, PackageLoader], @@ -57,7 +54,7 @@ def __init__( self.props_file = props_file def get_source(self, environment: Environment, template: str): - for alias, replacement in self.NAMESPACE_ALIASES.items(): + for alias, replacement in JINJA_NAMESPACE_ALIASES.items(): if template.startswith(f"{alias}:"): logging.getLogger(__name__).info( f"Replacing {alias} with {replacement} for template {template}" @@ -80,7 +77,7 @@ def get_source(self, environment: Environment, template: str): raise -def create_jinja_env(pm: PluginManager, include: list[str], props_file: Path): +def create_jinja_env(pm: PluginManager, include: Sequence[str], props_file: Path): included, extra = pm.load_jinja_templates(include) env = SandboxedEnvironment( diff --git a/src/hexdoc/_hooks.py b/src/hexdoc/_hooks.py index 9ec464060..6f5e3ab64 100644 --- a/src/hexdoc/_hooks.py +++ b/src/hexdoc/_hooks.py @@ -2,17 +2,24 @@ from pathlib import Path import hexdoc +from hexdoc.minecraft.recipe import ( + ingredients as minecraft_ingredients, + recipes as minecraft_recipes, +) +from hexdoc.patchouli.page import pages as patchouli_pages from hexdoc.plugin import ( DefaultRenderedTemplatesImpl, HookReturn, LoadJinjaTemplatesImpl, LoadResourceDirsImpl, + LoadTaggedUnionsImpl, ModVersionImpl, hookimpl, ) class HexdocPlugin( + LoadTaggedUnionsImpl, ModVersionImpl, LoadResourceDirsImpl, LoadJinjaTemplatesImpl, @@ -21,7 +28,8 @@ class HexdocPlugin( @staticmethod @hookimpl def hexdoc_mod_version(): - return "(TODO: remove)" + # need to implement this to be able to use `hexdoc export` + return "" @staticmethod @hookimpl @@ -30,6 +38,15 @@ def hexdoc_load_resource_dirs() -> HookReturn[Package]: return [generated, resources] + @staticmethod + @hookimpl + def hexdoc_load_tagged_unions() -> HookReturn[Package]: + return [ + patchouli_pages, + minecraft_recipes, + minecraft_ingredients, + ] + @staticmethod @hookimpl def hexdoc_load_jinja_templates() -> tuple[Package, str]: diff --git a/src/hexdoc/core/properties.py b/src/hexdoc/core/properties.py index 020d6aa69..84d5858d3 100644 --- a/src/hexdoc/core/properties.py +++ b/src/hexdoc/core/properties.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import Any, Self, Sequence -from pydantic import Field, PrivateAttr +from pydantic import Field, PrivateAttr, field_validator from pydantic_settings import BaseSettings, SettingsConfigDict from hexdoc.model.strip_hidden import StripHiddenModel @@ -14,10 +14,15 @@ load_toml_with_placeholders, relative_path_root, ) +from hexdoc.utils.types import PydanticOrderedSet from .resource import ResourceLocation from .resource_dir import ResourceDir +JINJA_NAMESPACE_ALIASES = { + "patchouli": "hexdoc", +} + class EnvironmentVariableProps(BaseSettings): model_config = SettingsConfigDict(env_file=".env", extra="allow") @@ -63,7 +68,7 @@ def _github_repository_parts(self): class TemplateProps(StripHiddenModel, validate_assignment=True): static_dir: RelativePath | None = None icon: RelativePath - include: list[str] + include: PydanticOrderedSet[str] render: dict[Path, str] = Field(default_factory=dict) extend_render: dict[Path, str] = Field(default_factory=dict) @@ -73,9 +78,18 @@ class TemplateProps(StripHiddenModel, validate_assignment=True): _was_render_set: bool = PrivateAttr(False) @property - def was_render_set(self): + def override_default_render(self): return self._was_render_set + @field_validator("include", mode="after") + @classmethod + def _resolve_aliases(cls, values: PydanticOrderedSet[str]): + for alias, replacement in JINJA_NAMESPACE_ALIASES.items(): + if alias in values: + values.remove(alias) + values.add(replacement) + return values + class MinecraftAssetsProps(StripHiddenModel): ref: str diff --git a/src/hexdoc/minecraft/_hooks.py b/src/hexdoc/minecraft/_hooks.py deleted file mode 100644 index 93ceba667..000000000 --- a/src/hexdoc/minecraft/_hooks.py +++ /dev/null @@ -1,12 +0,0 @@ -from importlib.resources import Package - -from hexdoc.plugin import LoadTaggedUnionsImpl, hookimpl - -from .recipe import ingredients, recipes - - -class MinecraftPlugin(LoadTaggedUnionsImpl): - @staticmethod - @hookimpl - def hexdoc_load_tagged_unions() -> Package | list[Package]: - return [ingredients, recipes] diff --git a/src/hexdoc/patchouli/_hooks.py b/src/hexdoc/patchouli/_hooks.py deleted file mode 100644 index 0e0bebf1f..000000000 --- a/src/hexdoc/patchouli/_hooks.py +++ /dev/null @@ -1,22 +0,0 @@ -from importlib.resources import Package - -import hexdoc -from hexdoc.plugin import ( - LoadJinjaTemplatesImpl, - LoadTaggedUnionsImpl, - hookimpl, -) - -from .page import pages - - -class PatchouliPlugin(LoadTaggedUnionsImpl, LoadJinjaTemplatesImpl): - @staticmethod - @hookimpl - def hexdoc_load_tagged_unions() -> Package | list[Package]: - return pages - - @staticmethod - @hookimpl - def hexdoc_load_jinja_templates() -> tuple[Package, str]: - return hexdoc, "_templates"