Skip to content

Commit 12eb462

Browse files
author
Steve Canny
committed
api: Document loads default template
1 parent ba0e2e1 commit 12eb462

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

docx/api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@
1313
opc.OpcPackage graph.
1414
"""
1515

16+
import os
17+
1618
from opc import OpcPackage
1719

1820

21+
thisdir = os.path.split(__file__)[0]
22+
_default_docx_path = os.path.join(thisdir, 'templates', 'default.docx')
23+
24+
1925
def Document(docx=None):
2026
"""
2127
Return a |_Document| instance loaded from *docx*, where *docx* can be
2228
either a path to a ``.docx`` file (a string) or a file-like object. If
2329
*docx* is missing or ``None``, the built-in default document "template"
2430
is loaded.
2531
"""
32+
if docx is None:
33+
docx = _default_docx_path
2634
pkg = OpcPackage.open(docx)
2735
document_part = pkg.main_document
2836
return _Document(pkg, document_part)

tests/test_api.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from docx.api import Document, _Document
1717

18-
from .unitutil import class_mock
18+
from .unitutil import class_mock, var_mock
1919

2020

2121
class DescribeDocument(object):
@@ -24,6 +24,14 @@ class DescribeDocument(object):
2424
def _Document_(self, request):
2525
return class_mock('docx.api._Document', request)
2626

27+
@pytest.fixture
28+
def default_docx(self, request):
29+
return var_mock('docx.api._default_docx_path', request)
30+
31+
@pytest.fixture
32+
def OpcPackage_(self, OpcPackage_mockery):
33+
return OpcPackage_mockery[0]
34+
2735
@pytest.fixture
2836
def OpcPackage_mockery(self, request):
2937
OpcPackage_ = class_mock('docx.api.OpcPackage', request)
@@ -45,3 +53,8 @@ def it_opens_a_docx_file_on_construction(self, OpcPackage_mockery,
4553
main_document.assert_called_once_with()
4654
_Document_.assert_called_once_with(pkg, document_part)
4755
assert isinstance(doc, _Document)
56+
57+
def it_uses_default_if_no_file_provided(self, OpcPackage_, _Document_,
58+
default_docx):
59+
Document()
60+
OpcPackage_.open.assert_called_once_with(default_docx)

0 commit comments

Comments
 (0)