Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
ontoumlelement.py updated.
Browse files Browse the repository at this point in the history
docformatter removed.
  • Loading branch information
pedropaulofb committed Nov 23, 2023
1 parent 410178e commit 48ef729
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
9 changes: 2 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ repos:
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies: [Flake8-pyproject]
additional_dependencies: [ Flake8-pyproject ]
args: [ --max-line-length=120 ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.0
hooks:
- id: mypy
language: system
pass_filenames: false
args: ['.']
args: [ '.' ]
- repo: https://github.com/PyCQA/pylint
rev: v3.0.1
hooks:
Expand All @@ -74,11 +74,6 @@ repos:
entry: pydocstyle
language: python
types: [ python ]
- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
hooks:
- id: docformatter
args: ['--wrap-summaries', '120', '--in-place', '--wrap-descriptions', '120', '--in-place']
- repo: https://github.com/jendrikseipp/vulture
rev: v2.10
hooks:
Expand Down
35 changes: 18 additions & 17 deletions ontouml_py/classes/ontoumlelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
It includes attributes for unique identification, creation, and modification timestamps, ensuring these properties are
present across all OntoUML elements. The class also incorporates validations to enforce the integrity of these
attributes.
attributes and restricts direct modification of certain fields.
"""
import typing
import uuid
from abc import ABC, abstractmethod
from datetime import datetime
Expand All @@ -19,14 +18,18 @@
class OntoumlElement(ABC, BaseModel):
"""Abstract base class representing a generic element within an OntoUML model.
This class provides base features for OntoUML elements, including a unique identifier, timestamps for creation and
modification, and a relationship to OntoUML projects.
:ivar id: A unique identifier for the element, automatically generated upon instantiation. :vartype id: str :ivar
created: Timestamp when the element was created, defaults to the current time. :vartype created: datetime :ivar
modified: Timestamp when the element was last modified, can be None if not modified. :vartype modified:
Optional[datetime] :ivar in_project: List of projects this element is part of. Direct modification is restricted.
:vartype in_project: list['Project']
Provides base features for OntoUML elements, including a unique identifier, timestamps for creation and
modification, and a relationship to OntoUML projects. It enforces read-only constraints on certain attributes and
includes validation logic to maintain the integrity of these attributes.
:ivar id: A unique identifier for the element, automatically generated upon instantiation.
:vartype id: str
:ivar created: Timestamp when the element was created, defaults to the current time.
:vartype created: datetime
:ivar modified: Timestamp when the element was last modified, can be None if not modified.
:vartype modified: Optional[datetime]
:ivar in_project: List of projects this element is part of. Direct modification is restricted.
:vartype in_project: list[Project]
"""

id: str = Field(default_factory=lambda: str(uuid.uuid4()))
Expand All @@ -39,7 +42,7 @@ class OntoumlElement(ABC, BaseModel):
"validate_assignment": True,
"extra": "forbid",
"str_strip_whitespace": True,
"validate_all": True,
"validate_default": True,
}

@abstractmethod
Expand All @@ -49,7 +52,7 @@ def __init__(self, **data: dict[str, Any]) -> None:
Ensures that 'modified' is not earlier than 'created' and prevents direct initialization of 'in_project'.
:param data: Fields to be set on the model instance, excluding 'in_project'.
:type data: dict
:type data: dict[str, Any]
:raises ValueError: If 'modified' is set to a datetime earlier than 'created', or if 'in_project' is directly
initialized.
"""
Expand Down Expand Up @@ -81,19 +84,17 @@ def __init__(self, **data: dict[str, Any]) -> None:
"This operation should be done via class Project."
)

def __setattr__(self, key: str, value: typing.Any) -> None:
"""Set attribute values, enforcing read-only constraints and logical validation.
def __setattr__(self, key: str, value: Any) -> None:
"""Set attribute values, enforcing logical validation.
Prevents modification of 'id', 'created', and 'in_project'. Validates 'modified' against 'created'.
Prevents modification of 'in_project'. Validates 'modified' against 'created'.
:param key: The attribute name to set.
:type key: str
:param value: The value to set for the attribute.
:type value: Any
:raises ValueError: If trying to modify read-only fields or if 'modified' is set earlier than 'created'.
"""
if key in ["id", "created"] and hasattr(self, key):
raise ValueError(f"Attribute '{key}' is read-only and cannot be modified.")
if key == "in_project" and hasattr(self, key):
raise ValueError(
"Attribute 'in_project' cannot be modified via OntoumlElement. "
Expand Down

0 comments on commit 48ef729

Please sign in to comment.