Skip to content

Commit 041d901

Browse files
author
Steve Canny
committed
parts: integrate _Document with opc part factory
1 parent 2cb9893 commit 041d901

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

docx/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@
1010
from docx.api import Document # noqa
1111

1212
__version__ = '0.3.0d1'
13+
14+
15+
from opc import PartFactory
16+
from opc.constants import CONTENT_TYPE as CT
17+
18+
from docx.parts import _Document
19+
20+
21+
PartFactory.part_type_for[CT.WML_DOCUMENT_MAIN] = _Document

docx/parts.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@
1313

1414
from opc import Part
1515

16+
from docx.oxml.base import oxml_fromstring
17+
1618

1719
class _Document(Part):
1820
"""
1921
Main document part of a WordprocessingML (WML) package, aka a .docx file.
2022
"""
23+
def __init__(self, document_elm):
24+
self._element = document_elm
25+
2126
@property
2227
def body(self):
2328
"""
2429
The |_Body| instance containing the content for this document.
2530
"""
31+
32+
@staticmethod
33+
def load(partname, content_type, blob):
34+
document_elm = oxml_fromstring(blob)
35+
document = _Document(document_elm)
36+
super(_Document, document).__init__(partname, content_type)
37+
return document

tests/test_parts.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# test_parts.py
4+
#
5+
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6+
#
7+
# This module is part of python-docx and is released under the MIT License:
8+
# http://www.opensource.org/licenses/mit-license.php
9+
10+
"""Test suite for the docx.parts module."""
11+
12+
from docx.parts import _Document
13+
14+
import pytest
15+
16+
from mock import Mock
17+
18+
from .unitutil import function_mock, initializer_mock
19+
20+
21+
class Describe_Document(object):
22+
23+
@pytest.fixture
24+
def init(self, request):
25+
return initializer_mock(_Document, request)
26+
27+
@pytest.fixture
28+
def oxml_fromstring_(self, request):
29+
return function_mock('docx.parts.oxml_fromstring', request)
30+
31+
def it_can_be_constructed_by_opc_part_factory(self, oxml_fromstring_,
32+
init):
33+
# mockery ----------------------
34+
partname, content_type, blob, document_elm = (
35+
Mock(name='partname'), Mock(name='content_type'),
36+
Mock(name='blob'), Mock(name='document_elm')
37+
)
38+
oxml_fromstring_.return_value = document_elm
39+
# exercise ---------------------
40+
doc = _Document.load(partname, content_type, blob)
41+
# verify -----------------------
42+
oxml_fromstring_.assert_called_once_with(blob)
43+
init.assert_called_once_with(document_elm)
44+
assert isinstance(doc, _Document)

0 commit comments

Comments
 (0)