Skip to content

Commit

Permalink
use UUID object for uuids instead of base64 string
Browse files Browse the repository at this point in the history
  • Loading branch information
Evidlo committed Oct 16, 2019
1 parent ab485d4 commit 616a23f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Finding Entries

**find_entries** (title=None, username=None, password=None, url=None, notes=None, path=None, uuid=None, string=none, group=None, recursive=True, regex=False, flags=None, history=False, first=False)

Returns entries which match all provided parameters, where ``title``, ``username``, ``password``, ``url``, ``notes``, ``path``, ``uuid`` and ``autotype_sequence`` are strings, ``string`` is a dict, ``autotype_enabled`` is a boolean. This function has optional ``regex`` boolean and ``flags`` string arguments, which means to interpret search strings as `XSLT style`_ regular expressions with `flags`_.
Returns entries which match all provided parameters, where ``title``, ``username``, ``password``, ``url``, ``notes``, ``path``, and ``autotype_sequence`` are strings, ``string`` is a dict, ``autotype_enabled`` is a boolean, ``uuid`` is a ``uuid.UUID``. This function has optional ``regex`` boolean and ``flags`` string arguments, which means to interpret search strings as `XSLT style`_ regular expressions with `flags`_.

.. _XSLT style: https://www.xml.com/pub/a/2003/06/04/tr.html
.. _flags: https://www.w3.org/TR/xpath-functions/#flags
Expand Down Expand Up @@ -106,7 +106,7 @@ Finding Groups

**find_groups** (name=None, path=None, uuid=None, notes=None, group=None, recursive=True, regex=False, flags=None, first=False)

where ``name``, ``path``, ``uuid`` and ``notes`` are strings. This function has optional ``regex`` boolean and ``flags`` string arguments, which means to interpret search strings as `XSLT style`_ regular expressions with `flags`_.
where ``name``, ``path``, and ``notes`` are strings, ``uuid`` is a ``uuid.UUID``. This function has optional ``regex`` boolean and ``flags`` string arguments, which means to interpret search strings as `XSLT style`_ regular expressions with `flags`_.

.. _XSLT style: https://www.xml.com/pub/a/2003/06/04/tr.html
.. _flags: https://www.w3.org/TR/xpath-functions/#flags
Expand Down
10 changes: 7 additions & 3 deletions pykeepass/baseelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ def dump_xml(self, pretty_print=False):

@property
def uuid(self):
return self._get_subelement_text('UUID')
"""Returns uuid of this element as a uuid.UUID object"""
b64_uuid = self._get_subelement_text('UUID')
return uuid.UUID(bytes=base64.b64decode(b64_uuid))

@uuid.setter
def uuid(self, value):
return self._set_subelement_text('UUID', value)
def uuid(self, uuid):
"""Set element uuid. `uuid` is a uuid.UUID object"""
b64_uuid = base64.b64encode(uuid.bytes).decode('utf-8')
return self._set_subelement_text('UUID', b64_uuid)

@property
def icon(self):
Expand Down
4 changes: 4 additions & 0 deletions pykeepass/pykeepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,

kwargs.pop('string')

# convert uuid to base64 form before building xpath
if 'uuid' in kwargs.keys():
kwargs['uuid'] = base64.b64encode(kwargs['uuid'].bytes).decode('utf-8')

# build xpath to filter results with specified attributes
for key, value in kwargs.items():
if key not in keys_xp[regex].keys():
Expand Down
19 changes: 12 additions & 7 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pykeepass.kdbx_parsing import KDBX
from pykeepass.exceptions import BinaryError
from lxml.etree import Element
import uuid
import os
import shutil
import unittest
Expand Down Expand Up @@ -98,16 +99,18 @@ def test_find_entries_by_path(self):
self.assertEqual('group_entry', results.title)

def test_find_entries_by_uuid(self):
results = self.kp.find_entries_by_uuid('zF9+zSoASMqWIcIio0ewuw==')[0]
uu = uuid.UUID('cc5f7ecd-2a00-48ca-9621-c222a347b0bb')
results = self.kp.find_entries_by_uuid(uu)[0]
self.assertIsInstance(results, Entry)
self.assertEqual('zF9+zSoASMqWIcIio0ewuw==', results.uuid)
self.assertEqual(uu, results.uuid)
self.assertEqual('foobar_user', results.username)

def test_find_entries_by_string(self):
results = self.kp.find_entries_by_string({'custom_field': 'custom field value'})[0]
self.assertIsInstance(results, Entry)
self.assertEqual('custom field value', results.get_custom_property('custom_field'))
self.assertEqual('HnN4bHSVjEybPf8nOq1bVA==', results.uuid)
uu = uuid.UUID('1e73786c-7495-8c4c-9b3d-ff273aad5b54')
self.assertEqual(uu, results.uuid)

def test_find_entries_by_autotype_sequence(self):
results = self.kp.find_entries(autotype_sequence='{TAB}', regex=True)
Expand Down Expand Up @@ -272,15 +275,17 @@ def test_find_groups_by_path(self):
self.assertEqual(results, None)

def test_find_groups_by_uuid(self):
results = self.kp.find_groups_by_uuid('lRVaMlMXoQ/U5NDCAwJktg==', first=True)
uu = uuid.UUID('95155a32-5317-a10f-d4e4-d0c2030264b6')
results = self.kp.find_groups_by_uuid(uu, first=True)
self.assertIsInstance(results, Group)
results = self.kp.find_groups(uuid='^lRVaMlMX|^kwTZdSoU', regex=True)
self.assertEqual(len(results), 2)
results = self.kp.find_groups(uuid=uu, regex=True)
self.assertEqual(len(results), 1)

def test_find_groups_by_notes(self):
results = self.kp.find_groups(notes='group notes')
self.assertEqual(len(results), 1)
self.assertEqual(results[0].uuid, 'lRVaMlMXoQ/U5NDCAwJktg==')
uu = uuid.UUID('95155a32-5317-a10f-d4e4-d0c2030264b6')
self.assertEqual(results[0].uuid, uu)

def test_groups(self):
results = self.kp.groups
Expand Down

0 comments on commit 616a23f

Please sign in to comment.