diff --git a/tosca-package/tosca/_tosca.py b/tosca-package/tosca/_tosca.py index 71b64dec..34f400e9 100644 --- a/tosca-package/tosca/_tosca.py +++ b/tosca-package/tosca/_tosca.py @@ -1621,12 +1621,15 @@ def Artifact( class _GetName: + # use this to lazily evaluate a template's name because might not be set correctly until yaml generation time. def __init__(self, obj: Union["ToscaType", Type["ToscaType"]]): self.obj = obj def __str__(self) -> str: if self.obj._type_name in ("inputs", "outputs"): return f"root::{self.obj._type_name}" + if isinstance(self.obj, _OwnedToscaType): + return self.obj.get_embedded_name() or self.obj._name return self.obj._name @@ -2543,6 +2546,8 @@ def __post_init__(self): elif getattr(field, "deferred_property_assignments", None): for name, value in field.deferred_property_assignments.items(): setattr(val, name, value) + if isinstance(val, _ToscaType): + val._set_parent(self, field.name) self._initialized = True def _enforce_required_fields(self) -> bool: @@ -2912,9 +2917,9 @@ def to_template_yaml(self, converter: "PythonToYaml") -> dict: self._name + "_" + field.name + (str(i) if i else ""), ) if shorthand or req: - body.setdefault("requirements", []).append( - {field.tosca_name: shorthand or req} - ) + body.setdefault("requirements", []).append({ + field.tosca_name: shorthand or req + }) elif field.section in ["capabilities", "artifacts"]: if value: assert isinstance(value, (CapabilityType, ArtifactType)) @@ -3135,7 +3140,7 @@ def _get_expr_prefix( return ["", _GetName(cls_or_obj)] elif isinstance(cls_or_obj, ToscaType): return ["", _GetName(cls_or_obj)] - # XXX elif isinstance(cls_or_obj, type): return f"*[type={cls_or_obj._tosca_typename}]::" + # XXX elif isinstance(cls_or_obj, type): return f"*[.type={cls_or_obj._tosca_typename}]::" return [] @@ -3251,6 +3256,11 @@ def _set_parent(self, parent: "_ToscaType", name: str): self._node = parent self._local_name = name + def get_embedded_name(self) -> str: + if self._node: + return f"{self._node._name}::{self._local_name}" + return self._name + class _BaseDataType(ToscaObject): @classmethod @@ -3266,7 +3276,7 @@ def get_tosca_datatype(cls): class ValueType(_BaseDataType): "ValueTypes are user-defined TOSCA data types that are derived from simple TOSCA datatypes, as opposed to complex TOSCA data types." - _template_section: ClassVar[str] = "data_types" + _type_section: ClassVar[str] = "data_types" _constraints: ClassVar[Optional[List[dict]]] = None @classmethod @@ -3350,6 +3360,11 @@ def to_template_yaml(self, converter: "PythonToYaml") -> dict: del tpl["type"] return tpl + def get_embedded_name(self) -> str: + if self._node: + return f"{self._node._name}::.capabilities[.name={self._local_name}]" + return self._name + CapabilityType = CapabilityEntity @@ -3388,6 +3403,11 @@ def __getitem__(self, target: Node) -> Self: self._target = target return self + def get_embedded_name(self) -> str: + if self._node: + return f"{self._node._name}::.requirements[.name={self._local_name}]" + return self._name + RelationshipType = Relationship @@ -3442,6 +3462,11 @@ def execute(self, *args: ToscaInputs, **kw): self.inputs = ToscaInputs._get_inputs(*args, **kw) return self + def get_embedded_name(self) -> str: + if self._node: + return f"{self._node._name}::.artifacts::{self._local_name}" + return self._name + ArtifactType = ArtifactEntity # deprecated