Skip to content

Commit 7e92b9a

Browse files
author
Steve Canny
committed
opc: add CorePropertiesPart.core_properties
1 parent 07127f2 commit 7e92b9a

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

docx/opc/coreprops.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ class CoreProperties(object):
1515
Corresponds to part named ``/docProps/core.xml``, containing the core
1616
document properties for this document package.
1717
"""
18+
def __init__(self, element):
19+
self._element = element

docx/opc/part.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ def __init__(self, partname, content_type, element, package):
219219
def blob(self):
220220
return serialize_part_xml(self._element)
221221

222+
@property
223+
def element(self):
224+
"""
225+
The root XML element of this XML part.
226+
"""
227+
return self._element
228+
222229
@classmethod
223230
def load(cls, partname, content_type, blob, package):
224231
element = parse_xml(blob)

docx/opc/parts/coreprops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from ..coreprops import CoreProperties
1112
from ..part import XmlPart
1213

1314

@@ -30,4 +31,4 @@ def core_properties(self):
3031
A |CoreProperties| object providing read/write access to the core
3132
properties contained in this core properties part.
3233
"""
33-
raise NotImplementedError
34+
return CoreProperties(self.element)

docx/oxml/parts/coreprops.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# encoding: utf-8
2+
3+
"""
4+
lxml custom element classes for core properties-related XML elements.
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..xmlchemy import BaseOxmlElement
12+
13+
14+
class CT_CoreProperties(BaseOxmlElement):
15+
"""
16+
``<cp:coreProperties>`` element, the root element of the Core Properties
17+
part stored as ``/docProps/core.xml``. Implements many of the Dublin Core
18+
document metadata elements. String elements resolve to an empty string
19+
('') if the element is not present in the XML. String elements are
20+
limited in length to 255 unicode characters.
21+
"""

tests/opc/parts/__init__.py

Whitespace-only changes.

tests/opc/parts/test_coreprops.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Unit test suite for the docx.opc.parts.coreprops module
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
import pytest
12+
13+
from docx.opc.coreprops import CoreProperties
14+
from docx.opc.parts.coreprops import CorePropertiesPart
15+
from docx.oxml.parts.coreprops import CT_CoreProperties
16+
17+
from ...unitutil.mock import class_mock, instance_mock
18+
19+
20+
class DescribeCorePropertiesPart(object):
21+
22+
def it_provides_access_to_its_core_props_object(self, coreprops_fixture):
23+
core_properties_part, CoreProperties_ = coreprops_fixture
24+
core_properties = core_properties_part.core_properties
25+
CoreProperties_.assert_called_once_with(core_properties_part.element)
26+
assert isinstance(core_properties, CoreProperties)
27+
28+
# fixtures ---------------------------------------------
29+
30+
@pytest.fixture
31+
def coreprops_fixture(self, element_, CoreProperties_):
32+
core_properties_part = CorePropertiesPart(None, None, element_, None)
33+
return core_properties_part, CoreProperties_
34+
35+
# fixture components -----------------------------------
36+
37+
@pytest.fixture
38+
def CoreProperties_(self, request):
39+
return class_mock(request, 'docx.opc.parts.coreprops.CoreProperties')
40+
41+
@pytest.fixture
42+
def element_(self, request):
43+
return instance_mock(request, CT_CoreProperties)

0 commit comments

Comments
 (0)