diff --git a/src/dabmodel/__init__.py b/src/dabmodel/__init__.py index 1d734d8..1c243d4 100644 --- a/src/dabmodel/__init__.py +++ b/src/dabmodel/__init__.py @@ -15,8 +15,8 @@ from .__metadata__ import __version__, __Summuary__, __Name__ from .meta.element import ClassMutable, ObjectMutable from .element import Element -from .LAMFields.LAMField import LAMField -from .LAMFields.LAMFieldInfo import LAMFieldInfo +from .lam_field.lam_field import LAMField +from .lam_field.lam_field_info import LAMFieldInfo # from .LAMFields.FrozenLAMField import FrozenLAMField from .appliance import Appliance diff --git a/src/dabmodel/base_element.py b/src/dabmodel/base_element.py index 85875bd..95736b7 100644 --- a/src/dabmodel/base_element.py +++ b/src/dabmodel/base_element.py @@ -1,7 +1,7 @@ from typing import Any # from .LAMFields.FrozenLAMField import FrozenLAMField -from .LAMFields.LAMField import LAMField +from .lam_field.lam_field import LAMField from .exception import ReadOnlyField, NewFieldForbidden, SchemaViolation from .tools import LAMdeepfreeze, is_data_attribute diff --git a/src/dabmodel/LAMFields/__init__.py b/src/dabmodel/lam_field/__init__.py similarity index 100% rename from src/dabmodel/LAMFields/__init__.py rename to src/dabmodel/lam_field/__init__.py diff --git a/src/dabmodel/LAMFields/Constraint.py b/src/dabmodel/lam_field/constraint.py similarity index 100% rename from src/dabmodel/LAMFields/Constraint.py rename to src/dabmodel/lam_field/constraint.py diff --git a/src/dabmodel/LAMFields/LAMField.py b/src/dabmodel/lam_field/lam_field.py similarity index 85% rename from src/dabmodel/LAMFields/LAMField.py rename to src/dabmodel/lam_field/lam_field.py index 9dea20d..5ac08dd 100644 --- a/src/dabmodel/LAMFields/LAMField.py +++ b/src/dabmodel/lam_field/lam_field.py @@ -1,7 +1,8 @@ from typing import Generic, TypeVar, Optional, Any, Self from typeguard import check_type, CollectionCheckStrategy, TypeCheckError -from .LAMFieldInfo import LAMFieldInfo -from .Constraint import Constraint +from copy import deepcopy +from .lam_field_info import LAMFieldInfo +from .constraint import Constraint from ..tools import LAMdeepfreeze from ..exception import InvalidFieldValue, ReadOnlyField @@ -15,14 +16,11 @@ class LAMField(Generic[TV_LABField]): def __init__(self, name: str, v: Optional[TV_LABField], a: Any, i: LAMFieldInfo): self.__name: str = name self.__source: Optional[type] = None - self.__info: LAMFieldInfo = i + self.__info: LAMFieldInfo = deepcopy(i) self.__annotations: Any = LAMdeepfreeze(a) self.validate(v) self.__default_value: Optional[TV_LABField] = v self.__value: Optional[TV_LABField] = v - self.__constraints: list[Constraint[Any]] = i.constraints - self.__frozen_constraints_set = False - self.__frozen_constraints = () self.__frozen = False self.__frozen_value = None self.__frozen_value_set = False @@ -41,7 +39,7 @@ class LAMField(Generic[TV_LABField]): def add_source(self, s: type) -> None: """Adds source Appliance to the Field""" if self.__frozen: - raise ReadOnlyField("Field is frozen") + raise ReadOnlyField("Field is frozen, cannot add source now") self.__source = s @property @@ -53,16 +51,12 @@ class LAMField(Generic[TV_LABField]): """Adds constraint to the Field""" if self.__frozen: raise ReadOnlyField("Field is frozen") - self.__constraints.append(c) - self.__frozen_constraints_set = False + self.__info.add_constraint(c) @property def constraints(self) -> list[Constraint]: """Returns Field's constraint""" - if not self.__frozen_constraints_set: - self.__frozen_constraints = LAMdeepfreeze(self.__info.constraints) - self.__frozen_value_set = True - return self.__frozen_constraints + return LAMdeepfreeze(self.__info.constraints) def validate_self(self): self.validate(self.__value) diff --git a/src/dabmodel/LAMFields/LAMFieldInfo.py b/src/dabmodel/lam_field/lam_field_info.py similarity index 64% rename from src/dabmodel/LAMFields/LAMFieldInfo.py rename to src/dabmodel/lam_field/lam_field_info.py index 68831d6..a1888b8 100644 --- a/src/dabmodel/LAMFields/LAMFieldInfo.py +++ b/src/dabmodel/lam_field/lam_field_info.py @@ -1,24 +1,25 @@ from typing import Optional, Any -from .Constraint import Constraint +from .constraint import Constraint class LAMFieldInfo: """This Class allows to describe a Field in Appliance class""" - def __init__( - self, *, doc: str = "", constraints: Optional[list[Constraint]] = None - ): - self._doc: str = doc + def __init__(self, *, doc: str = "", constraints: Optional[list[Constraint]] = None): + self.__doc: str = doc self.__constraints: list[Constraint] if constraints is None: self.__constraints = [] else: self.__constraints = constraints + def add_constraint(self, constraint: Constraint): + self.__constraints.append(constraint) + @property def doc(self) -> str: """Returns Field's documentation""" - return self._doc + return self.__doc @property def constraints(self) -> list[Constraint[Any]]: diff --git a/src/dabmodel/meta/element.py b/src/dabmodel/meta/element.py index ffe020e..9125791 100644 --- a/src/dabmodel/meta/element.py +++ b/src/dabmodel/meta/element.py @@ -8,8 +8,8 @@ import inspect, ast, textwrap from typeguard import check_type, TypeCheckError, CollectionCheckStrategy from frozendict import frozendict from ..tools import _resolve_annotation, _peel_annotated -from ..LAMFields.LAMField import LAMField -from ..LAMFields.LAMFieldInfo import LAMFieldInfo +from ..lam_field.lam_field import LAMField +from ..lam_field.lam_field_info import LAMFieldInfo from ..defines import ALLOWED_HELPERS_MATH, ALLOWED_HELPERS_DEFAULT, ALLOWED_MODEL_FIELDS_TYPES from ..base_element import BaseElement