Skip to content

Commit

Permalink
hdr: add BaseStoryPart.next_id
Browse files Browse the repository at this point in the history
  • Loading branch information
scanny committed Jan 6, 2019
1 parent 002e37f commit 3923fe8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 43 deletions.
14 changes: 0 additions & 14 deletions docx/parts/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,6 @@ def inline_shapes(self):
"""
return InlineShapes(self._element.body, self)

@property
def next_id(self):
"""Next available positive integer id value in this document.
Calculated by incrementing maximum existing id value. Gaps in the
existing id sequence are not filled. The id attribute value is unique
in the document, without regard to the element type it appears on.
"""
id_str_lst = self._element.xpath('//@id')
used_ids = [int(id_str) for id_str in id_str_lst if id_str.isdigit()]
if not used_ids:
return 1
return max(used_ids) + 1

@lazyproperty
def numbering_part(self):
"""
Expand Down
6 changes: 5 additions & 1 deletion docx/parts/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def next_id(self):
the existing id sequence are not filled. The id attribute value is unique in the
document, without regard to the element type it appears on.
"""
raise NotImplementedError
id_str_lst = self._element.xpath('//@id')
used_ids = [int(id_str) for id_str in id_str_lst if id_str.isdigit()]
if not used_ids:
return 1
return max(used_ids) + 1

@lazyproperty
def _document_part(self):
Expand Down
2 changes: 0 additions & 2 deletions features/hdr-header-footer.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Feature: Header and footer behaviors
Then header.paragraphs[0].style.name == "Normal"


@wip
Scenario: _Header allows image insertion
Given a _Run object from a header as run
When I call run.add_picture()
Expand Down Expand Up @@ -83,7 +82,6 @@ Feature: Header and footer behaviors
Then footer.paragraphs[0].style.name == "Normal"


@wip
Scenario: _Footer allows image insertion
Given a _Run object from a footer as run
When I call run.add_picture()
Expand Down
26 changes: 0 additions & 26 deletions tests/parts/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from docx.styles.styles import Styles

from ..oxml.parts.unitdata.document import a_body, a_document
from ..oxml.unitdata.text import a_p
from ..unitutil.mock import class_mock, instance_mock, method_mock, property_mock


Expand Down Expand Up @@ -132,10 +131,6 @@ def and_it_creates_a_numbering_part_if_not_present(
relate_to_.assert_called_once_with(document_part, numbering_part_, RT.NUMBERING)
assert numbering_part is numbering_part_

def it_knows_the_next_available_xml_id(self, next_id_fixture):
document, expected_id = next_id_fixture
assert document.next_id == expected_id

def it_can_get_a_style_by_id(self, styles_prop_, styles_, style_):
styles_prop_.return_value = styles_
styles_.get_by_id.return_value = style_
Expand Down Expand Up @@ -222,27 +217,6 @@ def inline_shapes_fixture(self, request, InlineShapes_):
document = DocumentPart(None, None, document_elm, None)
return document, InlineShapes_, body_elm

@pytest.fixture(params=[
((), 1),
((1,), 2),
((2,), 3),
((1, 2, 3), 4),
((1, 2, 4), 5),
((0, 0), 1),
((0, 0, 1, 3), 4),
(('foo', 1, 2), 3),
((1, 'bar'), 2)
])
def next_id_fixture(self, request):
existing_ids, expected_id = request.param
document_elm = a_document().with_nsdecls().element
for n in existing_ids:
p = a_p().with_nsdecls().element
p.set('id', str(n))
document_elm.append(p)
document = DocumentPart(None, None, document_elm, None)
return document, expected_id

@pytest.fixture
def save_fixture(self, package_):
document_part = DocumentPart(None, None, None, package_)
Expand Down
29 changes: 29 additions & 0 deletions tests/parts/test_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from docx.parts.story import BaseStoryPart
from docx.styles.style import BaseStyle

from ..unitutil.cxml import element
from ..unitutil.file import snippet_text
from ..unitutil.mock import instance_mock, method_mock, property_mock

Expand Down Expand Up @@ -75,6 +76,14 @@ def it_can_create_a_new_pic_inline(self, get_or_add_image_, image_, next_id_prop
image_.scaled_dimensions.assert_called_once_with(100, 200)
assert inline.xml == expected_xml

def it_knows_the_next_available_xml_id(self, next_id_fixture):
story_element, expected_value = next_id_fixture
story_part = BaseStoryPart(None, None, story_element, None)

next_id = story_part.next_id

assert next_id == expected_value

def it_knows_the_main_document_part_to_help(self, package_, document_part_):
package_.main_document_part = document_part_
story_part = BaseStoryPart(None, None, None, package_)
Expand All @@ -83,6 +92,26 @@ def it_knows_the_main_document_part_to_help(self, package_, document_part_):

assert document_part is document_part_

# fixtures -------------------------------------------------------

@pytest.fixture(
params=[
(("w:document"), 1),
(("w:document/w:p{id=1}"), 2),
(("w:document/w:p{id=2}"), 3),
(("w:hdr/(w:p{id=1},w:p{id=2},w:p{id=3})"), 4),
(("w:hdr/(w:p{id=1},w:p{id=2},w:p{id=4})"), 5),
(("w:hdr/(w:p{id=0},w:p{id=0})"), 1),
(("w:ftr/(w:p{id=0},w:p{id=0},w:p{id=1},w:p{id=3})"), 4),
(("w:ftr/(w:p{id=foo},w:p{id=1},w:p{id=2})"), 3),
(("w:ftr/(w:p{id=1},w:p{id=bar})"), 2),
]
)
def next_id_fixture(self, request):
story_cxml, expected_value = request.param
story_element = element(story_cxml)
return story_element, expected_value

# fixture components ---------------------------------------------

@pytest.fixture
Expand Down

0 comments on commit 3923fe8

Please sign in to comment.