aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-07-04 13:29:23 +0100
committerMichael Williamson <mike@zwobble.org>2015-07-04 13:29:23 +0100
commit044398430b84b73fa6fce57405254f4a63c42424 (patch)
tree61812a72d68dcbb7cd09f921b9088c2d41299342
parent8fa1fcd70c807dfae3de83e52c273857e6a9db50 (diff)
Split out body reader construction
-rw-r--r--mammoth/docx/__init__.py43
-rw-r--r--mammoth/docx/document_xml.py17
-rw-r--r--tests/docx/document_xml_tests.py4
3 files changed, 33 insertions, 31 deletions
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py
index 6e45882..3e7c0e0 100644
--- a/mammoth/docx/__init__.py
+++ b/mammoth/docx/__init__.py
@@ -8,6 +8,7 @@ from .relationships_xml import read_relationships_xml_element
from .numbering_xml import read_numbering_xml_element, Numbering
from .styles_xml import read_styles_xml_element
from .notes_xml import read_footnotes_xml_element, read_endnotes_xml_element
+from . import body_xml
_namespaces = [
@@ -25,17 +26,8 @@ _namespaces = [
def read(fileobj):
zip_file = zipfile.ZipFile(fileobj)
- with _open_entry(zip_file, "[Content_Types].xml") as content_types_fileobj:
- content_types = read_content_types_xml_element(_parse_docx_xml(content_types_fileobj))
-
- with _open_entry(zip_file, "word/_rels/document.xml.rels") as relationships_fileobj:
- relationships = read_relationships_xml_element(_parse_docx_xml(relationships_fileobj))
-
- numbering = _try_read_entry_or_default(
- zip_file, "word/numbering.xml", read_numbering_xml_element, default=Numbering({}))
-
- with _open_entry(zip_file, "word/styles.xml") as styles_fileobj:
- styles = read_styles_xml_element(_parse_docx_xml(styles_fileobj))
+ body_readers = _body_readers(zip_file)
+ body_reader = body_readers("document")
footnote_elements = _try_read_entry_or_default(
zip_file, "word/footnotes.xml", read_footnotes_xml_element, default=[])
@@ -47,14 +39,35 @@ def read(fileobj):
document_xml = _parse_docx_xml(document_fileobj)
return read_document_xml_element(
document_xml,
+ body_reader=body_reader,
+ footnote_elements=footnote_elements,
+ endnote_elements=endnote_elements,
+ )
+
+
+def _body_readers(zip_file):
+ with _open_entry(zip_file, "[Content_Types].xml") as content_types_fileobj:
+ content_types = read_content_types_xml_element(_parse_docx_xml(content_types_fileobj))
+
+ numbering = _try_read_entry_or_default(
+ zip_file, "word/numbering.xml", read_numbering_xml_element, default=Numbering({}))
+
+ with _open_entry(zip_file, "word/styles.xml") as styles_fileobj:
+ styles = read_styles_xml_element(_parse_docx_xml(styles_fileobj))
+
+ def for_name(name):
+ with _open_entry(zip_file, "word/_rels/{0}.xml.rels".format(name)) as relationships_fileobj:
+ relationships = read_relationships_xml_element(_parse_docx_xml(relationships_fileobj))
+
+ return body_xml.reader(
+ numbering=numbering,
content_types=content_types,
relationships=relationships,
- docx_file=zip_file,
- numbering=numbering,
styles=styles,
- footnote_elements=footnote_elements,
- endnote_elements=endnote_elements,
+ docx_file=zip_file,
)
+
+ return for_name
def _parse_docx_xml(fileobj):
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index 7288dc2..3cdbec2 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -1,27 +1,14 @@
from .. import documents
from .. import results
-from . import body_xml
def read_document_xml_element(element,
- numbering=None,
- content_types=None,
- relationships=None,
- styles=None,
+ body_reader,
footnote_elements=None,
- endnote_elements=None,
- docx_file=None):
+ endnote_elements=None):
note_elements = (footnote_elements or []) + (endnote_elements or [])
- body_reader = body_xml.reader(
- numbering=numbering,
- content_types=content_types,
- relationships=relationships,
- styles=styles,
- docx_file=docx_file,
- )
-
def _read_note(element):
return body_reader.read_all(element.body).map(
lambda body: documents.note(element.note_type, element.id, body))
diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py
index 00c7770..b09f481 100644
--- a/tests/docx/document_xml_tests.py
+++ b/tests/docx/document_xml_tests.py
@@ -4,6 +4,7 @@ from mammoth import documents
from mammoth.docx.xmlparser import element as xml_element, text as xml_text
from mammoth.docx.document_xml import read_document_xml_element
from mammoth.docx.notes_xml import NoteElement
+from mammoth.docx import body_xml
@istest
@@ -31,7 +32,8 @@ class ReadXmlElementTests(object):
def _read_and_get_document_xml_element(*args, **kwargs):
- result = read_document_xml_element(*args, **kwargs)
+ body_reader = body_xml.reader()
+ result = read_document_xml_element(*args, body_reader=body_reader, **kwargs)
assert_equal([], result.messages)
return result.value