Skip to content

Commit 684331c

Browse files
custProps: allow iteration on custom proeprties
1 parent f47feeb commit 684331c

5 files changed

Lines changed: 33 additions & 3 deletions

File tree

docx/opc/customprops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def __setitem__(self, key, value):
6565
def __len__(self):
6666
return len(self._element)
6767

68+
def __iter__(self):
69+
for child in self._element:
70+
yield child.get("name")
71+
6872
def lookup(self, item):
6973
for child in self._element:
7074
if child.get("name") == item:

features/doc-customprops.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ Feature: Read and write custom document properties
2626
Given a document having no custom properties part
2727
When I assign new values to the custom properties
2828
Then the custom property values match the new values
29+
30+
Scenario: iterate the custom properties of a document
31+
Given a document having known custom properties
32+
Then I can iterate the custom properties object

features/steps/customprops.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def then_the_custom_property_values_match_the_known_values(context):
7878
"got '%s' for custom property '%s'" % (value, name)
7979
)
8080

81-
8281
@then('the custom property values match the new values')
8382
def then_the_custom_property_values_match_the_new_values(context):
8483
custom_properties = context.document.custom_properties
@@ -87,3 +86,11 @@ def then_the_custom_property_values_match_the_new_values(context):
8786
assert value == expected_value, (
8887
"got '%s' for custom property '%s'" % (value, name)
8988
)
89+
90+
@then('I can iterate the custom properties object')
91+
def then_I_can_iterate_the_custom_properties_object(context):
92+
exp_names = iter(['AppVersion', 'CustomPropBool', 'CustomPropInt', 'CustomPropString',
93+
'DocSecurity', 'HyperlinksChanged', 'LinksUpToDate', 'ScaleCrop', 'ShareDoc'])
94+
custom_properties = context.document.custom_properties
95+
for prop_name in custom_properties:
96+
assert prop_name == next(exp_names)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
,mko,haendel.local,15.11.2022 08:06,file:///Users/mko/Library/Application%20Support/LibreOffice/4;

tests/opc/test_customprops.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,28 @@ def it_can_read_existing_prop_values(self, prop_get_fixture):
2121
actual_value = custom_properties[prop_name]
2222
assert actual_value == exp_value
2323

24-
def it_can_change_existing_prop_values(self):
25-
pass
24+
def it_can_change_existing_prop_values(self, custom_properties_default, prop_set_fixture):
25+
_, prop_name, value, _ = prop_set_fixture
26+
assert custom_properties_default[prop_name] != value
27+
custom_properties_default[prop_name] = value
28+
assert custom_properties_default[prop_name] == value
2629

2730
def it_can_set_new_prop_values(self, prop_set_fixture):
2831
custom_properties, prop_name, value, exp_xml = prop_set_fixture
2932
custom_properties[prop_name] = value
3033
assert custom_properties._element.xml == exp_xml
3134

35+
def it_can_iterate_existing_props(self, custom_properties_default):
36+
exp_names = ['CustomPropBool', 'CustomPropInt', 'CustomPropString']
37+
38+
# check 1: as list
39+
assert list(custom_properties_default) == ['CustomPropBool', 'CustomPropInt', 'CustomPropString']
40+
41+
# check 2: use iterator
42+
exp_names_iter = iter(exp_names)
43+
for prop_name in custom_properties_default:
44+
assert prop_name == next(exp_names_iter)
45+
3246
# fixtures -------------------------------------------------------
3347

3448
@pytest.fixture(params=[

0 commit comments

Comments
 (0)