Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions s7/factories/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def create_entity(
) -> type[SOFT7EntityInstance]:
"""Create and return a SOFT7 entity as a pydantic model.

TODO: Utilize the `generated_classes` module and check whether we can return an
already created model based on the inputs given here.
If the entity instance class has already been created, it will be returned
as is from the `generated_classes` module.

TODO: Determine what to do with regards to differing inputs, but similar names.

Expand All @@ -58,6 +58,29 @@ def create_entity(
# Split the identity into its parts
_, _, name = parse_identity(entity.identity)

# Check if the entity has already been created
existing_cls: type[SOFT7EntityInstance] | None = getattr(
module_namespace, f"{name.replace(' ', '')}Entity", None
)

if existing_cls and issubclass(existing_cls, SOFT7EntityInstance):
# Check the existing class' entity attribute
if existing_cls.entity == entity:
LOGGER.debug("The %s entity class already exists.", name)
return existing_cls
raise ValueError(
f"The {name} entity class already exists, but with a different SOFT7 "
"entity."
)

if existing_cls:
raise ValueError(
f"The {name} entity class already exists, but is not a SOFT7EntityInstance "
"type."
)

LOGGER.debug("Creating the %s entity class anew.", name)

# Create the entity model's dimensions
dimensions: dict[str, tuple[Union[type[Optional[int]], object], Any]] = (
# Value must be a (<type>, <default>) or (<type>, <FieldInfo>) tuple
Expand Down