Skip to content

Commit 9db3241

Browse files
author
Steve Canny
committed
add CT_Relationships.add_rel()
1 parent 0b3798d commit 9db3241

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

opc/oxml.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ class CT_Relationships(OxmlBaseElement):
194194
"""
195195
``<Relationships>`` element, the root element in a .rels file.
196196
"""
197+
def add_rel(self, rId, reltype, target, is_external=False):
198+
"""
199+
Add a child ``<Relationship>`` element with attributes set according
200+
to parameter values.
201+
"""
202+
target_mode = RTM.EXTERNAL if is_external else RTM.INTERNAL
203+
relationship = CT_Relationship.new(rId, reltype, target, target_mode)
204+
self.append(relationship)
205+
197206
@staticmethod
198207
def new():
199208
"""
@@ -258,3 +267,4 @@ def overrides(self):
258267

259268
pr_namespace = element_class_lookup.get_namespace(nsmap['pr'])
260269
pr_namespace['Relationship'] = CT_Relationship
270+
pr_namespace['Relationships'] = CT_Relationships

tests/test_oxml.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
oxml_tostring
1616
)
1717

18-
from .unitdata import a_Default, an_Override, a_Relationship, a_Types
18+
from .unitdata import (
19+
a_Default, an_Override, a_Relationship, a_Relationships, a_Types
20+
)
1921

2022

2123
class DescribeCT_Default(object):
@@ -83,6 +85,19 @@ def it_can_construct_a_new_relationships_element(self):
8385
)
8486
assert actual_xml == expected_xml
8587

88+
def it_can_build_rels_element_incrementally(self):
89+
# setup ------------------------
90+
rels = CT_Relationships.new()
91+
# exercise ---------------------
92+
rels.add_rel('rId1', 'http://reltype1', 'docProps/core.xml')
93+
rels.add_rel('rId2', 'http://linktype', 'http://some/link', True)
94+
rels.add_rel('rId3', 'http://reltype2', '../slides/slide1.xml')
95+
# verify -----------------------
96+
expected_rels_xml = a_Relationships().xml
97+
actual_xml = oxml_tostring(rels, encoding='unicode',
98+
pretty_print=True)
99+
assert actual_xml == expected_rels_xml
100+
86101

87102
class DescribeCT_Types(object):
88103

tests/unitdata.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,37 @@ def xml(self):
154154
self._target, self.target_mode)
155155

156156

157+
class CT_RelationshipsBuilder(BaseBuilder):
158+
"""
159+
Test data builder for CT_Relationships (Relationships) XML element, the
160+
root element in .rels files.
161+
"""
162+
def __init__(self):
163+
"""Establish instance variables with default values"""
164+
self._rels = (
165+
('rId1', 'http://reltype1', 'docProps/core.xml', 'Internal'),
166+
('rId2', 'http://linktype', 'http://some/link', 'External'),
167+
('rId3', 'http://reltype2', '../slides/slide1.xml', 'Internal'),
168+
)
169+
170+
@property
171+
def xml(self):
172+
"""
173+
Return XML string based on settings accumulated via method calls.
174+
"""
175+
xml = '<Relationships xmlns="%s">\n' % NS.OPC_RELATIONSHIPS
176+
for rId, reltype, target, target_mode in self._rels:
177+
xml += (a_Relationship().with_rId(rId)
178+
.with_reltype(reltype)
179+
.with_target(target)
180+
.with_target_mode(target_mode)
181+
.with_indent(2)
182+
.without_namespace()
183+
.xml)
184+
xml += '</Relationships>\n'
185+
return xml
186+
187+
157188
class CT_TypesBuilder(BaseBuilder):
158189
"""
159190
Test data builder for CT_Types (<Types>) XML element, the root element in
@@ -216,6 +247,11 @@ def a_Relationship():
216247
return CT_RelationshipBuilder()
217248

218249

250+
def a_Relationships():
251+
"""Return a CT_RelationshipsBuilder instance"""
252+
return CT_RelationshipsBuilder()
253+
254+
219255
def a_Types():
220256
"""Return a CT_TypesBuilder instance"""
221257
return CT_TypesBuilder()

0 commit comments

Comments
 (0)