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

Class _Relationships

src/pptx/opc/package.py:493–677  ·  view source on GitHub ↗

Collection of |_Relationship| instances having `dict` semantics. Relationships are keyed by their rId, but may also be found in other ways, such as by their relationship type. |Relationship| objects are keyed by their rId. Iterating this collection has normal mapping semantics, generat

Source from the content-addressed store, hash-verified

491
492
493class _Relationships(Mapping[str, "_Relationship"]):
494 """Collection of |_Relationship| instances having `dict` semantics.
495
496 Relationships are keyed by their rId, but may also be found in other ways, such as by their
497 relationship type. |Relationship| objects are keyed by their rId.
498
499 Iterating this collection has normal mapping semantics, generating the keys (rIds) of the
500 mapping. `rels.keys()`, `rels.values()`, and `rels.items() can be used as they would be for a
501 `dict`.
502 """
503
504 def __init__(self, base_uri: str):
505 self._base_uri = base_uri
506
507 def __contains__(self, rId: object) -> bool:
508 """Implement 'in' operation, like `"rId7" in relationships`."""
509 return rId in self._rels
510
511 def __getitem__(self, rId: str) -> _Relationship:
512 """Implement relationship lookup by rId using indexed access, like rels[rId]."""
513 try:
514 return self._rels[rId]
515 except KeyError:
516 raise KeyError("no relationship with key '%s'" % rId)
517
518 def __iter__(self) -> Iterator[str]:
519 """Implement iteration of rIds (iterating a mapping produces its keys)."""
520 return iter(self._rels)
521
522 def __len__(self) -> int:
523 """Return count of relationships in collection."""
524 return len(self._rels)
525
526 def get_or_add(self, reltype: str, target_part: Part) -> str:
527 """Return str rId of `reltype` to `target_part`.
528
529 The rId of an existing matching relationship is used if present. Otherwise, a new
530 relationship is added and that rId is returned.
531 """
532 existing_rId = self._get_matching(reltype, target_part)
533 return (
534 self._add_relationship(reltype, target_part) if existing_rId is None else existing_rId
535 )
536
537 def get_or_add_ext_rel(self, reltype: str, target_ref: str) -> str:
538 """Return str rId of external relationship of `reltype` to `target_ref`.
539
540 The rId of an existing matching relationship is used if present. Otherwise, a new
541 relationship is added and that rId is returned.
542 """
543 existing_rId = self._get_matching(reltype, target_ref, is_external=True)
544 return (
545 self._add_relationship(reltype, target_ref, is_external=True)
546 if existing_rId is None
547 else existing_rId
548 )
549
550 def load_from_xml(

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…