Refactor models
This commit is contained in:
parent
b7fbd29916
commit
948a59fa67
4 changed files with 54 additions and 40 deletions
|
@ -3,13 +3,13 @@ from __future__ import annotations
|
|||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Any, Self, dataclass_transform
|
||||
from typing import Annotated, Any, Self
|
||||
|
||||
from pydantic import AfterValidator, Field, HttpUrl, field_validator, model_validator
|
||||
from pydantic import AfterValidator, Field, HttpUrl, field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
from hexdoc.core.resource import ResourceDir, ResourceLocation
|
||||
from hexdoc.model import HexdocModel
|
||||
from hexdoc.model.strip_hidden import StripHiddenModel
|
||||
from hexdoc.utils.cd import RelativePath, relative_path_root
|
||||
from hexdoc.utils.deserialize.toml import load_toml_with_placeholders
|
||||
|
||||
|
@ -20,22 +20,6 @@ NoTrailingSlashHttpUrl = Annotated[
|
|||
]
|
||||
|
||||
|
||||
@dataclass_transform()
|
||||
class StripHiddenModel(HexdocModel):
|
||||
"""Base model which removes all keys starting with _ before validation."""
|
||||
|
||||
@model_validator(mode="before")
|
||||
def _pre_root_strip_hidden(cls, values: Any) -> Any:
|
||||
if not isinstance(values, dict):
|
||||
return values
|
||||
|
||||
return {
|
||||
key: value
|
||||
for key, value in values.items()
|
||||
if not (isinstance(key, str) and key.startswith("_"))
|
||||
}
|
||||
|
||||
|
||||
class EnvironmentVariableProps(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env")
|
||||
|
||||
|
|
29
doc/src/hexdoc/model/id.py
Normal file
29
doc/src/hexdoc/model/id.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Self, dataclass_transform
|
||||
|
||||
from hexdoc.core.resource import PathResourceDir, ResourceLocation
|
||||
from hexdoc.utils.deserialize.json import JSONDict
|
||||
|
||||
from .base import HexdocModel, ValidationContext
|
||||
|
||||
|
||||
@dataclass_transform()
|
||||
class IDModel(HexdocModel):
|
||||
id: ResourceLocation
|
||||
resource_dir: PathResourceDir
|
||||
|
||||
@classmethod
|
||||
def load(
|
||||
cls,
|
||||
resource_dir: PathResourceDir,
|
||||
id: ResourceLocation,
|
||||
data: JSONDict,
|
||||
context: ValidationContext,
|
||||
) -> Self:
|
||||
logging.getLogger(__name__).debug(f"Load {cls} at {id}")
|
||||
return cls.model_validate(
|
||||
data | {"id": id, "resource_dir": resource_dir},
|
||||
context=context,
|
||||
)
|
|
@ -1,6 +1,5 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Self, dataclass_transform
|
||||
|
||||
|
@ -13,26 +12,7 @@ from hexdoc.utils.deserialize import cast_or_raise
|
|||
from hexdoc.utils.deserialize.json import JSONDict
|
||||
|
||||
from .base import HexdocModel, ValidationContext
|
||||
|
||||
|
||||
@dataclass_transform()
|
||||
class IDModel(HexdocModel):
|
||||
id: ResourceLocation
|
||||
resource_dir: PathResourceDir
|
||||
|
||||
@classmethod
|
||||
def load(
|
||||
cls,
|
||||
resource_dir: PathResourceDir,
|
||||
id: ResourceLocation,
|
||||
data: JSONDict,
|
||||
context: ValidationContext,
|
||||
) -> Self:
|
||||
logging.getLogger(__name__).debug(f"Load {cls} at {id}")
|
||||
return cls.model_validate(
|
||||
data | {"id": id, "resource_dir": resource_dir},
|
||||
context=context,
|
||||
)
|
||||
from .id import IDModel
|
||||
|
||||
|
||||
@dataclass_transform()
|
||||
|
|
21
doc/src/hexdoc/model/strip_hidden.py
Normal file
21
doc/src/hexdoc/model/strip_hidden.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from typing import Any, dataclass_transform
|
||||
|
||||
from pydantic import model_validator
|
||||
|
||||
from hexdoc.model import HexdocModel
|
||||
|
||||
|
||||
@dataclass_transform()
|
||||
class StripHiddenModel(HexdocModel):
|
||||
"""Base model which removes all keys starting with _ before validation."""
|
||||
|
||||
@model_validator(mode="before")
|
||||
def _pre_root_strip_hidden(cls, values: Any) -> Any:
|
||||
if not isinstance(values, dict):
|
||||
return values
|
||||
|
||||
return {
|
||||
key: value
|
||||
for key, value in values.items()
|
||||
if not (isinstance(key, str) and key.startswith("_"))
|
||||
}
|
Loading…
Reference in a new issue