Another refactor, but this time it actually makes sense

This commit is contained in:
object-Object 2023-07-25 08:42:26 -04:00
parent 178d7c2af5
commit 9d9febe43c
27 changed files with 85 additions and 112 deletions

View file

@ -16,8 +16,7 @@
"name": "Python: Generate Docs",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}/doc",
"module": "hexdoc",
"module": "hexdoc.hexdoc",
"args": [
"properties.toml",
],

View file

@ -18,7 +18,7 @@ requires-python = ">=3.11"
dependencies = [
"typing_extensions~=4.7.0",
"typed-argument-parser~=1.8.0",
"pydantic~=2.0",
"pydantic~=2.0.3",
"Jinja2~=3.1.2",
]
@ -31,7 +31,7 @@ dev = [
]
[project.scripts]
hexdoc = "hexdoc.scripts.hexdoc:main"
hexdoc = "hexdoc.hexdoc:main"
[project.entry-points."hexdoc.Page"]
hexdoc-patchouli = "hexdoc.patchouli.page.pages"

View file

@ -2,9 +2,7 @@ from pathlib import Path
from typing import Any, Generic, TypeVar
from hexdoc.patchouli import AnyBookContext, Book, BookContext
from hexdoc.properties import Properties
from hexdoc.resource import ResourceLocation
from hexdoc.utils import AnyContext
from hexdoc.utils import AnyContext, Properties, ResourceLocation
from .pattern import Direction, PatternInfo

View file

@ -6,8 +6,7 @@ from hexdoc.minecraft.recipe import (
MinecraftItemIdIngredient,
MinecraftItemTagIngredient,
)
from hexdoc.resource import ResourceLocation
from hexdoc.utils import HexDocModel
from hexdoc.utils import HexDocModel, ResourceLocation
from .hex_book import HexContext

View file

@ -5,7 +5,7 @@ from pydantic import ValidationInfo, model_validator
from hexdoc.minecraft import LocalizedStr
from hexdoc.patchouli.page import PageWithText
from hexdoc.resource import ResourceLocation
from hexdoc.utils import ResourceLocation
from ..hex_book import AnyHexContext, HexContext
from ..pattern import RawPatternInfo

View file

@ -5,7 +5,7 @@ from pydantic import ValidationInfo, model_validator
from hexdoc.minecraft import LocalizedStr
from hexdoc.minecraft.recipe import CraftingRecipe
from hexdoc.patchouli.page import PageWithText, PageWithTitle
from hexdoc.resource import ResourceLocation
from hexdoc.utils import ResourceLocation
from ..hex_book import HexContext
from ..hex_recipes import BrainsweepRecipe

View file

@ -3,8 +3,7 @@ from typing import Annotated, Any
from pydantic import BeforeValidator
from hexdoc.resource import ResourceLocation
from hexdoc.utils import HexDocModel
from hexdoc.utils import HexDocModel, ResourceLocation
class Direction(Enum):

View file

@ -15,8 +15,9 @@ from jinja2 import (
from tap import Tap
from hexdoc.hexcasting import HexBook
from hexdoc.properties import Properties
from hexdoc.utils.jinja_extensions import IncludeRawExtension, hexdoc_block, hexdoc_wrap
from hexdoc.utils import Properties
from .jinja_extensions import IncludeRawExtension, hexdoc_block, hexdoc_wrap
def strip_empty_lines(text: str) -> str:

View file

@ -9,9 +9,13 @@ from pydantic.dataclasses import dataclass
from pydantic.functional_validators import ModelWrapValidatorHandler
from typing_extensions import TypedDict
from hexdoc.properties import Properties
from hexdoc.resource import ItemStack, ResourceLocation
from hexdoc.utils import DEFAULT_CONFIG, HexDocModel
from hexdoc.utils import (
DEFAULT_CONFIG,
HexDocModel,
ItemStack,
Properties,
ResourceLocation,
)
from hexdoc.utils.deserialize import isinstance_or_raise, load_json

View file

@ -2,8 +2,7 @@ from typing import Any, Self, cast
from pydantic import ValidationInfo, model_validator
from hexdoc.properties import AnyPropsContext
from hexdoc.resource import ResourceLocation, TypeTaggedUnion
from hexdoc.utils import AnyPropsContext, ResourceLocation, TypeTaggedUnion
from hexdoc.utils.deserialize import load_json

View file

@ -1,7 +1,6 @@
from typing import Any
from hexdoc.resource import ResourceLocation, TypeTaggedUnion
from hexdoc.utils import AnyContext, NoValue
from hexdoc.utils import AnyContext, NoValue, ResourceLocation, TypeTaggedUnion
class ItemIngredient(

View file

@ -9,8 +9,8 @@ __all__ = [
]
from .book import Book
from .book_models import AnyBookContext, BookContext
from .category import Category
from .entry import Entry
from .model import AnyBookContext, BookContext
from .page import Page
from .text import FormatTree

View file

@ -3,14 +3,20 @@ from typing import Any, Generic, Literal, Self, cast
from pydantic import Field, ValidationInfo, model_validator
from hexdoc.minecraft import I18n, LocalizedStr
from hexdoc.properties import Properties
from hexdoc.resource import ItemStack, ResLoc, ResourceLocation
from hexdoc.utils import AnyContext, Color, HexDocModel
from hexdoc.utils import (
AnyContext,
Color,
HexDocModel,
ItemStack,
Properties,
ResLoc,
ResourceLocation,
)
from hexdoc.utils.deserialize import isinstance_or_raise, load_json
from .book_models import AnyBookContext, BookContext
from .category import Category
from .entry import Entry
from .model import AnyBookContext, BookContext
from .text import DEFAULT_MACROS, FormatTree

View file

@ -4,9 +4,7 @@ from typing import Any, Generic, TypeVar, cast, dataclass_transform
from pydantic import ValidationInfo, model_validator
from hexdoc.properties import Properties
from hexdoc.resource import ResourceLocation
from hexdoc.utils import AnyContext, HexDocFileModel
from hexdoc.utils import AnyContext, HexDocFileModel, Properties, ResourceLocation
from .text.formatting import FormatContext

View file

@ -4,12 +4,11 @@ from typing import Self
from pydantic import Field
from hexdoc.minecraft import LocalizedStr
from hexdoc.properties import Properties
from hexdoc.resource import ItemStack, ResourceLocation
from hexdoc.utils import ItemStack, Properties, ResourceLocation
from hexdoc.utils.types import Sortable, sorted_dict
from .book_models import BookContext, BookFileModel
from .entry import Entry
from .model import BookContext, BookFileModel
from .text import FormatTree

View file

@ -4,12 +4,10 @@ from typing import cast
from pydantic import Field, ValidationInfo, model_validator
from hexdoc.minecraft import LocalizedStr
from hexdoc.properties import Properties
from hexdoc.resource import ItemStack, ResourceLocation
from hexdoc.utils import Color
from hexdoc.utils import Color, ItemStack, Properties, ResourceLocation
from hexdoc.utils.types import Sortable
from .model import BookContext, BookFileModel
from .book_models import BookContext, BookFileModel
from .page.pages import Page

View file

@ -4,9 +4,9 @@ from pydantic import model_validator
from pydantic.functional_validators import ModelWrapValidatorHandler
from hexdoc.minecraft import LocalizedStr
from hexdoc.resource import ResourceLocation, TypeTaggedUnion
from hexdoc.utils import ResourceLocation, TypeTaggedUnion
from ..model import AnyBookContext
from ..book_models import AnyBookContext
from ..text import FormatTree

View file

@ -2,9 +2,9 @@ from typing import Any
from hexdoc.minecraft import LocalizedItem, LocalizedStr
from hexdoc.minecraft.recipe import CraftingRecipe
from hexdoc.resource import Entity, ItemStack, ResourceLocation
from hexdoc.utils import Entity, ItemStack, ResourceLocation
from ..model import BookContext
from ..book_models import BookContext
from ..text import FormatTree
from .abstract_pages import Page, PageWithText, PageWithTitle

View file

@ -14,8 +14,7 @@ from pydantic.functional_validators import ModelWrapValidatorHandler
from hexdoc.minecraft import LocalizedStr
from hexdoc.minecraft.i18n import I18nContext
from hexdoc.properties import PropsContext
from hexdoc.utils import DEFAULT_CONFIG, HexDocModel
from hexdoc.utils import DEFAULT_CONFIG, HexDocModel, PropsContext
from hexdoc.utils.types import TryGetEnum
from .html import HTMLElement, HTMLStream

View file

@ -9,6 +9,14 @@ __all__ = [
"NoValue",
"NoValueType",
"TagValue",
"AnyPropsContext",
"Properties",
"PropsContext",
"Entity",
"ItemStack",
"ResLoc",
"ResourceLocation",
"TypeTaggedUnion",
]
from .model import (
@ -18,5 +26,13 @@ from .model import (
HexDocFileModel,
HexDocModel,
)
from .tagged_union import InternallyTaggedUnion, NoValue, NoValueType, TagValue
from .properties import AnyPropsContext, Properties, PropsContext
from .resource import Entity, ItemStack, ResLoc, ResourceLocation
from .tagged_union import (
InternallyTaggedUnion,
NoValue,
NoValueType,
TagValue,
TypeTaggedUnion,
)
from .types import Color

View file

@ -13,9 +13,9 @@ from pydantic import (
)
from typing_extensions import TypedDict
from hexdoc.resource import ResourceLocation
from hexdoc.utils.model import HexDocModel
from hexdoc.utils.toml_placeholders import load_toml
from .model import HexDocModel
from .resource import ResourceLocation
from .toml_placeholders import load_toml
NoTrailingSlashHttpUrl = Annotated[
str,

View file

@ -12,13 +12,7 @@ from pydantic import field_validator, model_serializer, model_validator
from pydantic.dataclasses import dataclass
from pydantic.functional_validators import ModelWrapValidatorHandler
from hexdoc.utils import (
DEFAULT_CONFIG,
AnyContext,
InternallyTaggedUnion,
NoValueType,
TagValue,
)
from .model import DEFAULT_CONFIG
def _make_regex(count: bool = False, nbt: bool = False) -> re.Pattern[str]:
@ -128,20 +122,3 @@ class Entity(BaseResourceLocation, regex=_make_regex(nbt=True)):
if self.nbt is not None:
s += self.nbt
return s
class TypeTaggedUnion(InternallyTaggedUnion[AnyContext], key="type", value=None):
type: ResourceLocation | NoValueType | None
def __init_subclass__(
cls,
*,
group: str | None = None,
type: TagValue | None,
) -> None:
super().__init_subclass__(group=group, value=type)
match type:
case str():
cls.type = ResourceLocation.from_str(type)
case _:
cls.type = type

View file

@ -1,22 +1,17 @@
# pyright: reportPrivateUsage=false
from __future__ import annotations
# from __future__ import annotations
from collections import defaultdict
from enum import Enum
from typing import TYPE_CHECKING, Any, ClassVar, Generator, Self, cast
from typing import Any, ClassVar, Generator, Self, cast
from pkg_resources import iter_entry_points
from pydantic import ValidationInfo, model_validator
from pydantic.functional_validators import ModelWrapValidatorHandler
from .model import AnyContext, HexDocModel
# from hexdoc.minecraft import ResourceLocation
if TYPE_CHECKING:
from pydantic.root_model import Model
from .resource import ResourceLocation
class NoValueType(Enum):
@ -28,12 +23,9 @@ class NoValueType(Enum):
NoValue = NoValueType._token
"""A singleton (like None) representing the value of a nonexistent dict key."""
TagValue = str | NoValueType
_loaded_groups: set[str] = set()
_rebuilt_models: set[type[Any]] = set()
def load_entry_points(group: str):
@ -122,9 +114,7 @@ class InternallyTaggedUnion(HexDocModel[AnyContext]):
return tag_key
@classmethod
def _supertypes(
cls,
) -> Generator[type[InternallyTaggedUnion[AnyContext]], None, None]:
def _supertypes(cls) -> Generator[type[Self], None, None]:
tag_key = cls._tag_key_or_raise()
# we consider a type to be its own supertype/subtype
@ -145,31 +135,6 @@ class InternallyTaggedUnion(HexDocModel[AnyContext]):
def _concrete_subtypes(cls):
return cls.__concrete_subtypes
@classmethod
def model_validate(
cls: type[Model],
obj: Any,
*,
strict: bool | None = None,
from_attributes: bool | None = None,
context: AnyContext | None = None,
) -> Model:
# resolve forward references, because apparently we need to do this
# if cls not in _rebuilt_models:
# _rebuilt_models.add(cls)
# cls.model_rebuild(
# _types_namespace={
# "ResourceLocation": ResourceLocation,
# }
# )
return super().model_validate(
obj,
strict=strict,
from_attributes=from_attributes,
context=context,
)
@model_validator(mode="wrap")
@classmethod
def _resolve_from_dict(
@ -233,3 +198,20 @@ class InternallyTaggedUnion(HexDocModel[AnyContext]):
f"Failed to match {cls} with {cls._tag_key}={tag_value} to any of {tag_types}: {data}",
exceptions,
)
class TypeTaggedUnion(InternallyTaggedUnion[AnyContext], key="type", value=None):
type: ResourceLocation | NoValueType | None
def __init_subclass__(
cls,
*,
group: str | None = None,
type: TagValue | None,
) -> None:
super().__init_subclass__(group=group, value=type)
match type:
case str():
cls.type = ResourceLocation.from_str(type)
case _:
cls.type = type

View file

@ -1,6 +1,6 @@
import pytest
from hexdoc.resource import ItemStack, ResLoc, ResourceLocation
from hexdoc.utils import ItemStack, ResLoc, ResourceLocation
resource_locations: list[tuple[str, ResourceLocation, str]] = [
(

View file

@ -5,7 +5,7 @@ from pathlib import Path
import pytest
from syrupy.assertion import SnapshotAssertion
from hexdoc.scripts.hexdoc import Args, main
from hexdoc.hexdoc import Args, main
_RUN = ["hexdoc"]
_ARGV = ["properties.toml", "-o"]