Skip to content

Commit 59532a8

Browse files
committed
add tag searching
1 parent 7c0aafe commit 59532a8

5 files changed

Lines changed: 12 additions & 2 deletions

File tree

pykeepass/pykeepass.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,
216216
if 'uuid' in kwargs.keys():
217217
kwargs['uuid'] = base64.b64encode(kwargs['uuid'].bytes).decode('utf-8')
218218

219+
# convert tags to semicolon separated string before building xpath
220+
if 'tags' in kwargs.keys():
221+
kwargs['tags'] = ';'.join(kwargs['tags'])
222+
219223
# build xpath to filter results with specified attributes
220224
for key, value in kwargs.items():
221225
if key not in keys_xp[regex].keys():

pykeepass/xpath.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'url': '/String/Key[text()="URL"]/../Value[text()="{}"]/../..',
3232
'notes': '/String/Key[text()="Notes"]/../Value[text()="{}"]/../..',
3333
'uuid': '/UUID[text()="{}"]/..',
34+
'tags': '/Tags[text()="{}"]/..',
3435
'string': '/String/Key[text()="{}"]/../Value[text()="{}"]/../..',
3536
'autotype_sequence': '/AutoType/DefaultSequence[text()="{}"]/../..',
3637
'autotype_enabled': '/AutoType/Enabled[text()="{}"]/../..',
@@ -42,6 +43,7 @@
4243
'url': '/String/Key[text()="URL"]/../Value[re:test(text(), "{}", "{flags}")]/../..',
4344
'notes': '/String/Key[text()="Notes"]/../Value[re:test(text(), "{}", "{flags}")]/../..',
4445
'uuid': '/UUID[re:test(text(), "{}", "{flags}")]/..',
46+
'tags': '/Tags[re:test(text(), "{}", "{flags}")]/..',
4547
'string': '/String/Key[text()="{}"]/../Value[re:test(text(), "{}", "{flags}")]/../..',
4648
'autotype_sequence': '/AutoType/DefaultSequence[re:test(text(), "{}", "{flags}")]/../..',
4749
'autotype_enabled': '/AutoType/Enabled[re:test(text(), "{}", "{flags}")]/../..',

tests/test3.kdbx

112 Bytes
Binary file not shown.

tests/test4.kdbx

115 Bytes
Binary file not shown.

tests/tests.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ def test_find_entries_by_uuid(self):
110110
self.assertEqual(uu, results.uuid)
111111
self.assertEqual('foobar_user', results.username)
112112

113+
def test_find_entries_by_tags(self):
114+
results = self.kp.find_entries(tags=['tag1', 'tag2'], first=True)
115+
self.assertIsInstance(results, Entry)
116+
self.assertEqual('foobar_entry', results.title)
117+
113118
def test_find_entries_by_string(self):
114119
results = self.kp.find_entries_by_string({'custom_field': 'custom field value'})[0]
115120
self.assertIsInstance(results, Entry)
@@ -797,7 +802,7 @@ def test_open_save(self):
797802
None,
798803
None,
799804
None,
800-
b'5\x99y\xb6\x1e\x9e\x16\xfe`V\xd4\xe8\x12De\xd9[\xdd\xe2\x98\x1a\xa9\xfc-\xc4>\xd2\xc0\xc6(\xec\x0c',
805+
b'\xfb\xb1!\x0e0\x94\xd4\x868\xa5\x04\xe6T\x9b<\xf9+\xb8\x82EN\xbc\xbe\xbc\xc8\xd3\xbbf\xfb\xde\xff.',
801806
b'M\xb7\x08\xf6\xa7\xd1v\xb1{&\x06\x8f\xae\xe9\r\xeb\x9a\x1b\x02b\xce\xf2\x8aR\xaea)7\x1fs\xe9\xc0',
802807
]
803808
keyfiles = [
@@ -850,7 +855,6 @@ def test_open_save(self):
850855
databases, passwords, transformed_keys,
851856
keyfiles, encryption_algorithms, kdf_algorithms, versions
852857
):
853-
854858
kp = PyKeePass(
855859
os.path.join(base_dir, database),
856860
password,

0 commit comments

Comments
 (0)