Defines an optional child element for MetaOxmlElement.
| 556 | |
| 557 | |
| 558 | class ZeroOrOne(_BaseChildElement): |
| 559 | """Defines an optional child element for MetaOxmlElement.""" |
| 560 | |
| 561 | def populate_class_members(self, element_cls: Type[BaseOxmlElement], prop_name: str): |
| 562 | """Add the appropriate methods to `element_cls`.""" |
| 563 | super(ZeroOrOne, self).populate_class_members(element_cls, prop_name) |
| 564 | self._add_getter() |
| 565 | self._add_creator() |
| 566 | self._add_inserter() |
| 567 | self._add_adder() |
| 568 | self._add_get_or_adder() |
| 569 | self._add_remover() |
| 570 | |
| 571 | def _add_get_or_adder(self): |
| 572 | """Add a `.get_or_add_x()` method to the element class for this child element.""" |
| 573 | |
| 574 | def get_or_add_child(obj: BaseOxmlElement) -> BaseOxmlElement: |
| 575 | child = getattr(obj, self._prop_name) |
| 576 | if child is None: |
| 577 | add_method = getattr(obj, self._add_method_name) |
| 578 | child = add_method() |
| 579 | return child |
| 580 | |
| 581 | get_or_add_child.__doc__ = ( |
| 582 | "Return the ``<%s>`` child element, newly added if not present." |
| 583 | ) % self._nsptagname |
| 584 | self._add_to_class(self._get_or_add_method_name, get_or_add_child) |
| 585 | |
| 586 | def _add_remover(self): |
| 587 | """Add a `._remove_x()` method to the element class for this child element.""" |
| 588 | |
| 589 | def _remove_child(obj: BaseOxmlElement) -> None: |
| 590 | obj.remove_all(self._nsptagname) |
| 591 | |
| 592 | _remove_child.__doc__ = f"Remove all `{self._nsptagname}` child elements." |
| 593 | self._add_to_class(self._remove_method_name, _remove_child) |
| 594 | |
| 595 | @lazyproperty |
| 596 | def _get_or_add_method_name(self): |
| 597 | return "get_or_add_%s" % self._prop_name |
| 598 | |
| 599 | |
| 600 | class ZeroOrOneChoice(_BaseChildElement): |
no outgoing calls
no test coverage detected
searching dependent graphs…