Defines an optional child element for MetaOxmlElement.
| 538 | |
| 539 | |
| 540 | class ZeroOrOne(_BaseChildElement): |
| 541 | """Defines an optional child element for MetaOxmlElement.""" |
| 542 | |
| 543 | def populate_class_members(self, element_cls: MetaOxmlElement, prop_name: str) -> None: |
| 544 | """Add the appropriate methods to `element_cls`.""" |
| 545 | super(ZeroOrOne, self).populate_class_members(element_cls, prop_name) |
| 546 | self._add_getter() |
| 547 | self._add_creator() |
| 548 | self._add_inserter() |
| 549 | self._add_adder() |
| 550 | self._add_get_or_adder() |
| 551 | self._add_remover() |
| 552 | |
| 553 | def _add_get_or_adder(self): |
| 554 | """Add a ``get_or_add_x()`` method to the element class for this child |
| 555 | element.""" |
| 556 | |
| 557 | def get_or_add_child(obj: BaseOxmlElement): |
| 558 | child = getattr(obj, self._prop_name) |
| 559 | if child is None: |
| 560 | add_method = getattr(obj, self._add_method_name) |
| 561 | child = add_method() |
| 562 | return child |
| 563 | |
| 564 | get_or_add_child.__doc__ = ( |
| 565 | "Return the ``<%s>`` child element, newly added if not present." |
| 566 | ) % self._nsptagname |
| 567 | self._add_to_class(self._get_or_add_method_name, get_or_add_child) |
| 568 | |
| 569 | def _add_remover(self): |
| 570 | """Add a ``_remove_x()`` method to the element class for this child element.""" |
| 571 | |
| 572 | def _remove_child(obj: BaseOxmlElement): |
| 573 | obj.remove_all(self._nsptagname) |
| 574 | |
| 575 | _remove_child.__doc__ = ("Remove all ``<%s>`` child elements.") % self._nsptagname |
| 576 | self._add_to_class(self._remove_method_name, _remove_child) |
| 577 | |
| 578 | @lazyproperty |
| 579 | def _get_or_add_method_name(self): |
| 580 | return "get_or_add_%s" % self._prop_name |
| 581 | |
| 582 | |
| 583 | class ZeroOrOneChoice(_BaseChildElement): |
no outgoing calls
no test coverage detected
searching dependent graphs…