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

Class Choice

src/pptx/oxml/xmlchemy.py:415–471  ·  view source on GitHub ↗

Defines a child element belonging to a group, only one of which may appear as a child.

Source from the content-addressed store, hash-verified

413
414
415class Choice(_BaseChildElement):
416 """Defines a child element belonging to a group, only one of which may appear as a child."""
417
418 @property
419 def nsptagname(self):
420 return self._nsptagname
421
422 def populate_class_members( # pyright: ignore[reportIncompatibleMethodOverride]
423 self, element_cls: Type[BaseOxmlElement], group_prop_name: str, successors: Sequence[str]
424 ):
425 """Add the appropriate methods to `element_cls`."""
426 self._element_cls = element_cls
427 self._group_prop_name = group_prop_name
428 self._successors = successors
429
430 self._add_getter()
431 self._add_creator()
432 self._add_inserter()
433 self._add_adder()
434 self._add_get_or_change_to_method()
435
436 def _add_get_or_change_to_method(self) -> None:
437 """Add a `get_or_change_to_x()` method to the element class for this child element."""
438
439 def get_or_change_to_child(obj: BaseOxmlElement):
440 child = getattr(obj, self._prop_name)
441 if child is not None:
442 return child
443 remove_group_method = getattr(obj, self._remove_group_method_name)
444 remove_group_method()
445 add_method = getattr(obj, self._add_method_name)
446 child = add_method()
447 return child
448
449 get_or_change_to_child.__doc__ = (
450 "Return the ``<%s>`` child, replacing any other group element if" " found."
451 ) % self._nsptagname
452 self._add_to_class(self._get_or_change_to_method_name, get_or_change_to_child)
453
454 @property
455 def _prop_name(self):
456 """
457 Calculate property name from tag name, e.g. a:schemeClr -> schemeClr.
458 """
459 if ":" in self._nsptagname:
460 start = self._nsptagname.index(":") + 1
461 else:
462 start = 0
463 return self._nsptagname[start:]
464
465 @lazyproperty
466 def _get_or_change_to_method_name(self):
467 return "get_or_change_to_%s" % self._prop_name
468
469 @lazyproperty
470 def _remove_group_method_name(self):
471 return "_remove_%s" % self._group_prop_name
472

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…