Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2023-11-29 00:39:14 -08:00
parent bc3d698539
commit faaeb3a472
74 changed files with 496 additions and 1985 deletions

View File

@ -1,8 +1,6 @@
from typing import Any, Dict, Type, TypeVar, Union
from typing import Union
import attr
from pydantic import BaseModel, GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema
from pydantic import BaseModel, RootModel
@ -44,86 +42,12 @@ class mesh_by_name(BaseModel):
type: str = "mesh_by_name"
GY = TypeVar("GY", bound="Selection")
@attr.s(auto_attribs=True)
class Selection:
"""Data item selection."""
type: Union[
Selection = RootModel[
Union[
default_scene,
scene_by_index,
scene_by_name,
mesh_by_index,
mesh_by_name,
]
def __init__(
self,
type: Union[
default_scene,
scene_by_index,
scene_by_name,
mesh_by_index,
mesh_by_name,
],
):
self.type = type
def model_dump(self) -> Dict[str, Any]:
if isinstance(self.type, default_scene):
JO: default_scene = self.type
return JO.model_dump()
elif isinstance(self.type, scene_by_index):
TE: scene_by_index = self.type
return TE.model_dump()
elif isinstance(self.type, scene_by_name):
WY: scene_by_name = self.type
return WY.model_dump()
elif isinstance(self.type, mesh_by_index):
QV: mesh_by_index = self.type
return QV.model_dump()
elif isinstance(self.type, mesh_by_name):
BP: mesh_by_name = self.type
return BP.model_dump()
raise Exception("Unknown type")
@classmethod
def from_dict(cls: Type[GY], d: Dict[str, Any]) -> GY:
if d.get("type") == "default_scene":
OF: default_scene = default_scene(**d)
return cls(type=OF)
elif d.get("type") == "scene_by_index":
OV: scene_by_index = scene_by_index(**d)
return cls(type=OV)
elif d.get("type") == "scene_by_name":
FK: scene_by_name = scene_by_name(**d)
return cls(type=FK)
elif d.get("type") == "mesh_by_index":
PE: mesh_by_index = mesh_by_index(**d)
return cls(type=PE)
elif d.get("type") == "mesh_by_name":
FP: mesh_by_name = mesh_by_name(**d)
return cls(type=FP)
raise Exception("Unknown type")
@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema:
return core_schema.no_info_after_validator_function(
cls,
handler(
Union[
default_scene,
scene_by_index,
scene_by_name,
mesh_by_index,
mesh_by_name,
]
),
)
]