Skip to content

Commit

Permalink
Use getter/setter decorators for global_asset_id
Browse files Browse the repository at this point in the history
- Use getter/setter decorators for global_asset_id
- Refactor `Entity.__init__`: use setter for `entity_type` and remove `_validate_asset_ids_for_entity_type()` from init because it will be called in `entity_type` setter
- Add return type to some init funcs of abstract classes to calm down MyPy
  • Loading branch information
zrgt committed Oct 29, 2023
1 parent bc66b2a commit 91bfe92
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions basyx/aas/model/aas.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ def _check_constraint_del_spec_asset_id(self, _item_to_del: base.SpecificAssetId
raise base.AASConstraintViolation(
131, "An AssetInformation has to have a globalAssetId or a specificAssetId")

def _get_global_asset_id(self):
@property
def global_asset_id(self):
return self._global_asset_id

def _set_global_asset_id(self, global_asset_id: Optional[base.Identifier]):
@global_asset_id.setter
def global_asset_id(self, global_asset_id: Optional[base.Identifier]):
if global_asset_id is None:
if self.specific_asset_id is None or not self.specific_asset_id:
raise base.AASConstraintViolation(
Expand All @@ -85,8 +87,6 @@ def _set_global_asset_id(self, global_asset_id: Optional[base.Identifier]):
_string_constraints.check_identifier(global_asset_id)
self._global_asset_id = global_asset_id

global_asset_id = property(_get_global_asset_id, _set_global_asset_id)

def __repr__(self) -> str:
return "AssetInformation(assetKind={}, globalAssetId={}, specificAssetId={}, assetType={}, " \
"defaultThumbnail={})".format(self.asset_kind, self._global_asset_id, str(self.specific_asset_id),
Expand Down
8 changes: 4 additions & 4 deletions basyx/aas/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ class Referable(HasExtension, metaclass=abc.ABCMeta):
Default is an empty string, making it use the source of its ancestor, if possible.
"""
@abc.abstractmethod
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._id_short: Optional[NameType] = None
self.display_name: Optional[MultiLanguageNameType] = dict()
Expand Down Expand Up @@ -1258,7 +1258,7 @@ class Identifiable(Referable, metaclass=abc.ABCMeta):
:ivar ~.id: The globally unique id of the element.
"""
@abc.abstractmethod
def __init__(self):
def __init__(self) -> None:
super().__init__()
self.administration: Optional[AdministrativeInformation] = None
# The id attribute is set by all inheriting classes __init__ functions.
Expand Down Expand Up @@ -1516,7 +1516,7 @@ class HasKind(metaclass=abc.ABCMeta):
:ivar _kind: Kind of the element: either type or instance. Default = :attr:`~ModellingKind.INSTANCE`.
"""
@abc.abstractmethod
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._kind: ModellingKind = ModellingKind.INSTANCE

Expand All @@ -1537,7 +1537,7 @@ class Qualifiable(Namespace, metaclass=abc.ABCMeta):
qualifiable element.
"""
@abc.abstractmethod
def __init__(self):
def __init__(self) -> None:
super().__init__()
self.namespace_element_sets: List[NamespaceSet] = []
self.qualifier: NamespaceSet[Qualifier]
Expand Down
18 changes: 9 additions & 9 deletions basyx/aas/model/submodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,26 +1111,29 @@ def __init__(self,
[] if specific_asset_id is None else specific_asset_id,
item_del_hook=self._check_constraint_del_spec_asset_id)
self.global_asset_id: Optional[base.Identifier] = global_asset_id
self._entity_type: base.EntityType = entity_type
self._validate_asset_ids_for_entity_type(self.entity_type, self.global_asset_id, self.specific_asset_id)
self.entity_type: base.EntityType = entity_type

def _check_constraint_del_spec_asset_id(self, _item_to_del: base.SpecificAssetId,
_list: List[base.SpecificAssetId]) -> None:
if self.global_asset_id is None and len(_list) == 1:
raise base.AASConstraintViolation(
131, "An AssetInformation has to have a globalAssetId or a specificAssetId")

def _get_entity_type(self) -> base.EntityType:
@property
def entity_type(self) -> base.EntityType:
return self._entity_type

def _set_entity_type(self, entity_type: base.EntityType) -> None:
@entity_type.setter
def entity_type(self, entity_type: base.EntityType) -> None:
self._validate_asset_ids_for_entity_type(entity_type, self.global_asset_id, self.specific_asset_id)
self._entity_type = entity_type

def _get_global_asset_id(self):
@property
def global_asset_id(self):
return self._global_asset_id

def _set_global_asset_id(self, global_asset_id: Optional[base.Identifier]):
@global_asset_id.setter
def global_asset_id(self, global_asset_id: Optional[base.Identifier]):
self._validate_asset_ids_for_entity_type(self.entity_type, global_asset_id, self.specific_asset_id)
self._global_asset_id = global_asset_id

Expand All @@ -1147,9 +1150,6 @@ def _validate_asset_ids_for_entity_type(entity_type: base.EntityType,
if global_asset_id:
_string_constraints.check_identifier(global_asset_id)

global_asset_id = property(_get_global_asset_id, _set_global_asset_id)
entity_type = property(_get_entity_type, _set_entity_type)


class EventElement(SubmodelElement, metaclass=abc.ABCMeta):
"""
Expand Down

0 comments on commit 91bfe92

Please sign in to comment.