MCPcopy Index your code
hub / github.com/python-openxml/python-docx / Choice

Class Choice

src/docx/oxml/xmlchemy.py:425–480  ·  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

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

Callers 1

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…