Skip to content

Commit 3b57bfa

Browse files
author
Steve Canny
committed
add OpcPackage.parts
Adds parts property to return a tuple snapshot of parts in package for when a sequence is preferable to an iterator.
1 parent e43b7b9 commit 3b57bfa

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

opc/package.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def open(pkg_file):
3636
Unmarshaller.unmarshal(pkg_reader, pkg, PartFactory)
3737
return pkg
3838

39+
@property
40+
def parts(self):
41+
"""
42+
Return an immutable sequence (tuple) containing a reference to each
43+
of the parts in this package.
44+
"""
45+
return tuple([p for p in self._walk_parts(self._rels)])
46+
3947
@property
4048
def rels(self):
4149
"""
@@ -52,6 +60,13 @@ def _add_relationship(self, reltype, target, rId, external=False):
5260
"""
5361
return self._rels.add_relationship(reltype, target, rId, external)
5462

63+
@staticmethod
64+
def _walk_parts(rels, visited_parts=None):
65+
"""
66+
Generate exactly one reference to each of the parts in the package by
67+
performing a depth-first traversal of the rels graph.
68+
"""
69+
5570

5671
class Part(object):
5772
"""

tests/test_package.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import pytest
1313

14-
from mock import call, Mock
14+
from mock import call, Mock, patch
1515

1616
from opc.package import (
1717
OpcPackage, Part, PartFactory, _Relationship, RelationshipCollection,
@@ -73,6 +73,14 @@ def it_can_add_a_relationship_to_a_part(self):
7373
pkg._rels.add_relationship.assert_called_once_with(reltype, target,
7474
rId, False)
7575

76+
def it_has_an_immutable_sequence_containing_its_parts(self):
77+
# mockery ----------------------
78+
parts = [Mock(name='part1'), Mock(name='part2')]
79+
pkg = OpcPackage()
80+
# verify -----------------------
81+
with patch.object(OpcPackage, '_walk_parts', return_value=parts):
82+
assert pkg.parts == (parts[0], parts[1])
83+
7684

7785
class DescribePart(object):
7886

0 commit comments

Comments
 (0)