MCPcopy Create free account
hub / github.com/scanny/python-pptx / _Relationship

Class _Relationship

src/pptx/opc/package.py:680–762  ·  view source on GitHub ↗

Value object describing link from a part or package to another part.

Source from the content-addressed store, hash-verified

678
679
680class _Relationship:
681 """Value object describing link from a part or package to another part."""
682
683 def __init__(self, base_uri: str, rId: str, reltype: str, target_mode: str, target: Part | str):
684 self._base_uri = base_uri
685 self._rId = rId
686 self._reltype = reltype
687 self._target_mode = target_mode
688 self._target = target
689
690 @classmethod
691 def from_xml(
692 cls, base_uri: str, rel: CT_Relationship, parts: dict[PackURI, Part]
693 ) -> _Relationship:
694 """Return |_Relationship| object based on CT_Relationship element `rel`."""
695 target = (
696 rel.target_ref
697 if rel.targetMode == RTM.EXTERNAL
698 else parts[PackURI.from_rel_ref(base_uri, rel.target_ref)]
699 )
700 return cls(base_uri, rel.rId, rel.reltype, rel.targetMode, target)
701
702 @lazyproperty
703 def is_external(self) -> bool:
704 """True if target_mode is `RTM.EXTERNAL`.
705
706 An external relationship is a link to a resource outside the package, such as a
707 web-resource (URL).
708 """
709 return self._target_mode == RTM.EXTERNAL
710
711 @lazyproperty
712 def reltype(self) -> str:
713 """Member of RELATIONSHIP_TYPE describing relationship of target to source."""
714 return self._reltype
715
716 @lazyproperty
717 def rId(self) -> str:
718 """str relationship-id, like 'rId9'.
719
720 Corresponds to the `Id` attribute on the `CT_Relationship` element and uniquely identifies
721 this relationship within its peers for the source-part or package.
722 """
723 return self._rId
724
725 @lazyproperty
726 def target_part(self) -> Part:
727 """|Part| or subtype referred to by this relationship."""
728 if self.is_external:
729 raise ValueError(
730 "`.target_part` property on _Relationship is undefined when "
731 "target-mode is external"
732 )
733 assert isinstance(self._target, Part)
734 return self._target
735
736 @lazyproperty
737 def target_partname(self) -> PackURI:

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…