Skip to content

Commit e17d371

Browse files
author
Steve Canny
committed
xmlch: add public adder for ZeroOrMore
1 parent 564d512 commit e17d371

3 files changed

Lines changed: 35 additions & 39 deletions

File tree

docx/oxml/parts/document.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,9 @@ class CT_Body(BaseOxmlElement):
2525
p = ZeroOrMore('w:p', successors=('w:sectPr',))
2626
tbl = ZeroOrMore('w:tbl', successors=('w:sectPr',))
2727

28-
def add_p(self):
29-
"""
30-
Return a new <w:p> element that has been added at the end of any
31-
existing body content.
32-
"""
33-
return self._add_p()
34-
3528
def _insert_p(self, p):
3629
return self._append_blocklevelelt(p)
3730

38-
def add_tbl(self):
39-
"""
40-
Return a new <w:tbl> element that has been added at the end of any
41-
existing body content.
42-
"""
43-
return self._add_tbl()
44-
4531
def _insert_tbl(self, tbl):
4632
return self._append_blocklevelelt(tbl)
4733

docx/oxml/xmlchemy.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,21 @@ def _add_list_getter(self):
332332
def _add_method_name(self):
333333
return '_add_%s' % self._prop_name
334334

335+
def _add_public_adder(self):
336+
"""
337+
Add a public ``add_x()`` method to the parent element class.
338+
"""
339+
def add_child(obj):
340+
private_add_method = getattr(obj, self._add_method_name)
341+
child = private_add_method()
342+
return child
343+
344+
add_child.__doc__ = (
345+
'Add a new ``<%s>`` child element unconditionally, inserted in t'
346+
'he correct sequence.' % self._nsptagname
347+
)
348+
self._add_to_class(self._public_add_method_name, add_child)
349+
335350
def _add_to_class(self, name, method):
336351
"""
337352
Add *method* to the target class as *name*, unless *name* is already
@@ -384,6 +399,16 @@ def get_child_element_list(obj):
384399
)
385400
return get_child_element_list
386401

402+
@lazyproperty
403+
def _public_add_method_name(self):
404+
"""
405+
add_childElement() is public API for a repeating element, allowing
406+
new elements to be added to the sequence. May be overridden to
407+
provide a friendlier API to clients having domain appropriate
408+
parameter names for required attributes.
409+
"""
410+
return 'add_%s' % self._prop_name
411+
387412
@lazyproperty
388413
def _remove_method_name(self):
389414
return '_remove_%s' % self._prop_name
@@ -519,31 +544,6 @@ def populate_class_members(self, element_cls, prop_name):
519544
self._add_public_adder()
520545
delattr(element_cls, prop_name)
521546

522-
def _add_public_adder(self):
523-
"""
524-
Add a public ``add_x()`` method to the parent element class.
525-
"""
526-
def add_child(obj):
527-
private_add_method = getattr(obj, self._add_method_name)
528-
child = private_add_method()
529-
return child
530-
531-
add_child.__doc__ = (
532-
'Add a new ``<%s>`` child element unconditionally, inserted in t'
533-
'he correct sequence.' % self._nsptagname
534-
)
535-
self._add_to_class(self._public_add_method_name, add_child)
536-
537-
@lazyproperty
538-
def _public_add_method_name(self):
539-
"""
540-
add_childElement() is public API for a repeating element, allowing
541-
new elements to be added to the sequence. May be overridden to
542-
provide a friendlier API to clients having domain appropriate
543-
parameter names for required attributes.
544-
"""
545-
return 'add_%s' % self._prop_name
546-
547547

548548
class ZeroOrMore(_BaseChildElement):
549549
"""
@@ -560,6 +560,7 @@ def populate_class_members(self, element_cls, prop_name):
560560
self._add_creator()
561561
self._add_inserter()
562562
self._add_adder()
563+
self._add_public_adder()
563564
delattr(element_cls, prop_name)
564565

565566

tests/oxml/test_xmlchemy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ def it_adds_an_add_method_for_the_child_element(self, add_fixture):
527527
'Add a new ``<w:zomChild>`` child element '
528528
)
529529

530+
def it_adds_a_public_add_method_for_the_child_element(self, add_fixture):
531+
parent, expected_xml = add_fixture
532+
zomChild = parent.add_zomChild()
533+
assert parent.xml == expected_xml
534+
assert isinstance(zomChild, CT_ZomChild)
535+
assert parent._add_zomChild.__doc__.startswith(
536+
'Add a new ``<w:zomChild>`` child element '
537+
)
538+
530539
def it_removes_the_property_root_name_used_for_declaration(self):
531540
assert not hasattr(CT_Parent, 'zomChild')
532541

0 commit comments

Comments
 (0)