Skip to content

Commit

Permalink
fix!: Fix cs.Parts .name attribute setting
Browse files Browse the repository at this point in the history
Setting a name on the `Part` is only allowed during creation. Else this
won't have any effect since its inferred from its `.type`. The user
should be informed about it.
  • Loading branch information
ewuerger committed Jan 31, 2023
1 parent 66058ae commit 32fb892
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions capellambse/model/common/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"ReferenceSearchingAccessor",
"ElementListCouplingMixin",
"RoleTagAccessor",
"InvalidChangeRequest",
]

import abc
Expand All @@ -46,6 +47,10 @@
_C = t.TypeVar("_C", bound="ElementListCouplingMixin")


class InvalidChangeRequest(Exception):
"""Raised when a change request is invalid."""


class NonUniqueMemberError(ValueError):
"""Raised when a duplicate member is inserted into a list."""

Expand Down
13 changes: 8 additions & 5 deletions capellambse/model/common/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,20 @@ def __init__(
parent.append(self._element)
try:
for key, val in kw.items():
prop = getattr(type(self), key)
is_acc_or_attr_prop = isinstance(
prop, (accessors.Accessor, properties.AttributeProperty)
)
if key == "xtype":
self._element.set(helpers.ATT_XT, val)
elif not isinstance(
getattr(type(self), key),
(accessors.Accessor, properties.AttributeProperty),
elif is_acc_or_attr_prop or (
isinstance(prop, property) and not self._constructed
):
setattr(self, key, val)
else:
raise TypeError(
f"Cannot set {key!r} on {type(self).__name__}"
)
else:
setattr(self, key, val)
self._model._loader.idcache_index(self._element)
except BaseException:
parent.remove(self._element)
Expand Down
24 changes: 22 additions & 2 deletions capellambse/model/crosslayer/cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,30 @@ class Part(c.GenericElement):

deployed_parts: c.Accessor

@property
def name(self) -> str: # type: ignore[override]
@property # type: ignore[override]
def name(self) -> str:
"""Return the name of the Part."""
return self.type.name

@name.setter
def name(self, value: str) -> None:
if not isinstance(value, str):
raise TypeError("Name has to be a string")

if self._constructed:
raise c.InvalidChangeRequest(
"This won't have any effect. The name is inferred from "
"`.type`."
)

if not value:
try:
del self._element.attrib["name"]
except KeyError:
pass
else:
self._element.attrib["name"] = value


@c.xtype_handler(None)
class ExchangeItemAllocation(c.GenericElement):
Expand Down

0 comments on commit 32fb892

Please sign in to comment.