Provide relationship methods required by both the package and each part.
| 28 | |
| 29 | |
| 30 | class _RelatableMixin: |
| 31 | """Provide relationship methods required by both the package and each part.""" |
| 32 | |
| 33 | def part_related_by(self, reltype: str) -> Part: |
| 34 | """Return (single) part having relationship to this package of `reltype`. |
| 35 | |
| 36 | Raises |KeyError| if no such relationship is found and |ValueError| if more than one such |
| 37 | relationship is found. |
| 38 | """ |
| 39 | return self._rels.part_with_reltype(reltype) |
| 40 | |
| 41 | def relate_to(self, target: Part | str, reltype: str, is_external: bool = False) -> str: |
| 42 | """Return rId key of relationship of `reltype` to `target`. |
| 43 | |
| 44 | If such a relationship already exists, its rId is returned. Otherwise the relationship is |
| 45 | added and its new rId returned. |
| 46 | """ |
| 47 | if isinstance(target, str): |
| 48 | assert is_external |
| 49 | return self._rels.get_or_add_ext_rel(reltype, target) |
| 50 | |
| 51 | return self._rels.get_or_add(reltype, target) |
| 52 | |
| 53 | def related_part(self, rId: str) -> Part: |
| 54 | """Return related |Part| subtype identified by `rId`.""" |
| 55 | return self._rels[rId].target_part |
| 56 | |
| 57 | def target_ref(self, rId: str) -> str: |
| 58 | """Return URL contained in target ref of relationship identified by `rId`.""" |
| 59 | return self._rels[rId].target_ref |
| 60 | |
| 61 | @lazyproperty |
| 62 | def _rels(self) -> _Relationships: |
| 63 | """|_Relationships| object containing relationships from this part to others.""" |
| 64 | raise NotImplementedError( # pragma: no cover |
| 65 | "`%s` must implement `.rels`" % type(self).__name__ |
| 66 | ) |
| 67 | |
| 68 | |
| 69 | class OpcPackage(_RelatableMixin): |
no outgoing calls
searching dependent graphs…