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

Class ZeroOrOneChoice

src/pptx/oxml/xmlchemy.py:600–660  ·  view source on GitHub ↗

An `EG_*` element group where at most one of its members may appear as a child.

Source from the content-addressed store, hash-verified

598
599
600class ZeroOrOneChoice(_BaseChildElement):
601 """An `EG_*` element group where at most one of its members may appear as a child."""
602
603 def __init__(self, choices: Iterable[Choice], successors: Iterable[str] = ()):
604 self._choices = tuple(choices)
605 self._successors = tuple(successors)
606
607 def populate_class_members(self, element_cls: Type[BaseOxmlElement], prop_name: str):
608 """Add the appropriate methods to `element_cls`."""
609 super(ZeroOrOneChoice, self).populate_class_members(element_cls, prop_name)
610 self._add_choice_getter()
611 for choice in self._choices:
612 choice.populate_class_members(element_cls, self._prop_name, self._successors)
613 self._add_group_remover()
614
615 def _add_choice_getter(self):
616 """Add a read-only `.{prop_name}` property to the element class.
617
618 The property returns the present member of this group, or |None| if none are present.
619 """
620 property_ = property(self._choice_getter, None, None)
621 # assign unconditionally to overwrite element name definition
622 setattr(self._element_cls, self._prop_name, property_)
623
624 def _add_group_remover(self):
625 """Add a `._remove_eg_x()` method to the element class for this choice group."""
626
627 def _remove_choice_group(obj: BaseOxmlElement) -> None:
628 for tagname in self._member_nsptagnames:
629 obj.remove_all(tagname)
630
631 _remove_choice_group.__doc__ = "Remove the current choice group child element if present."
632 self._add_to_class(self._remove_choice_group_method_name, _remove_choice_group)
633
634 @property
635 def _choice_getter(self):
636 """
637 Return a function object suitable for the "get" side of the property
638 descriptor.
639 """
640
641 def get_group_member_element(obj: BaseOxmlElement) -> BaseOxmlElement | None:
642 return cast(
643 "BaseOxmlElement | None", obj.first_child_found_in(*self._member_nsptagnames)
644 )
645
646 get_group_member_element.__doc__ = (
647 "Return the child element belonging to this element group, or "
648 "|None| if no member child is present."
649 )
650 return get_group_member_element
651
652 @lazyproperty
653 def _member_nsptagnames(self) -> list[str]:
654 """Sequence of namespace-prefixed tagnames, one for each member element of choice group."""
655 return [choice.nsptagname for choice in self._choices]
656
657 @lazyproperty

Callers 10

CT_LinePropertiesClass · 0.90
CT_ShapePropertiesClass · 0.90
CT_ColorClass · 0.90
CT_GradientStopClass · 0.90
CT_ParentClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…