Skip to content

Commit 90ad669

Browse files
authored
entry: Add is_custom_property_protected method (#322)
1 parent 0b0b441 commit 90ad669

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

pykeepass/entry.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ def delete_custom_property(self, key):
294294
raise AttributeError('Could not find property element')
295295
self._element.remove(prop)
296296

297+
def is_custom_property_protected(self, key):
298+
"""Whether a custom property is protected.
299+
300+
Return False if the entry does not have a custom property with the
301+
specified key.
302+
303+
Args:
304+
key (:obj:`str`): key of the custom property to check.
305+
306+
Returns:
307+
bool: Whether the custom property is protected.
308+
309+
"""
310+
assert key not in reserved_keys, '{} is a reserved key'.format(key)
311+
field = self._xpath('String/Key[text()="{}"]/../Value'.format(key), first=True)
312+
if field is not None:
313+
return field.attrib.get("Protected", "False") == "True"
314+
return False
315+
297316
@property
298317
def custom_properties(self):
299318
keys = self._get_string_field_keys(exclude_reserved=True)

tests/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,16 @@ def test_add_remove_attachment(self):
597597
self.assertEqual(len(entry.attachments), num_attach + 1)
598598
self.assertEqual(entry.attachments[0].filename, 'foobar2.txt')
599599

600+
def test_is_custom_property_protected(self):
601+
e = self.kp.add_entry(self.kp.root_group, 'test-protect', 'some-user', 'pass')
602+
e.set_custom_property('protected', 'something', protect=True)
603+
e.set_custom_property('explicit-unprotected', 'other', protect=False)
604+
e.set_custom_property('not-protected', 'secret')
605+
self.assertTrue(e.is_custom_property_protected('protected'))
606+
self.assertFalse(e.is_custom_property_protected('explicit-unprotected'))
607+
self.assertFalse(e.is_custom_property_protected('not-protected'))
608+
self.assertFalse(e.is_custom_property_protected('non-existent'))
609+
600610

601611
class EntryHistoryTests3(KDBX3Tests):
602612

0 commit comments

Comments
 (0)