Skip to content

Commit

Permalink
move history kwarg from _xpath to _find
Browse files Browse the repository at this point in the history
  • Loading branch information
Evidlo committed Dec 28, 2023
1 parent dde1d0a commit 74fad8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pykeepass/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _get_string_field(self, key):
(str or None): field value
"""

field = self._xpath('String/Key[text()="{}"]/../Value'.format(key), first=True)
field = self._xpath('String/Key[text()="{}"]/../Value'.format(key), history=True, first=True)
if field is not None:
return field.text

Expand All @@ -102,7 +102,7 @@ def _set_string_field(self, key, value, protected=True):
in other tools. This property is ignored in PyKeePass and all
fields are decrypted immediately upon opening the database.
"""
field = self._xpath('String/Key[text()="{}"]/..'.format(key), first=True)
field = self._xpath('String/Key[text()="{}"]/..'.format(key), history=True, first=True)
if field is not None:
self._element.remove(field)
self._element.append(E.String(E.Key(key), E.Value(value, Protected=str(protected))))
Expand Down
39 changes: 18 additions & 21 deletions pykeepass/pykeepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ def recyclebin_group(self):
def groups(self):
""":obj:`list` of :obj:`Group`: list of all Group objects in database
"""
return self._xpath('//Group', cast=True)
return self.find_groups()

@property
def entries(self):
""":obj:`list` of :obj:`Entry`: list of all Entry objects in database,
excluding history"""
return self._xpath('//Entry', cast=True)
return self.find_entries()

def xml(self):
"""Get XML part of database as string
Expand Down Expand Up @@ -284,8 +284,7 @@ def dump_xml(self, filename):
)
)

def _xpath(self, xpath_str, tree=None, first=False, history=False,
cast=False, **kwargs):
def _xpath(self, xpath_str, tree=None, first=False, cast=False, **kwargs):
"""Look up elements in the XML payload and return corresponding object.
Internal function which searches the payload lxml ElementTree for
Expand All @@ -300,8 +299,6 @@ def _xpath(self, xpath_str, tree=None, first=False, history=False,
first (bool): If True, function returns first result or None. If
False, function returns list of matches or empty list. Default
is False.
history (bool): If True, history entries are included in results.
Default is False.
cast (bool): If True, matches are instead instantiated as
pykeepass Group, Entry, or Attachment objects. An exception
is raised if a match cannot be cast. Default is False.
Expand All @@ -319,18 +316,17 @@ def _xpath(self, xpath_str, tree=None, first=False, history=False,

res = []
for e in elements:
if history or e.getparent().tag != 'History':
if cast:
if e.tag == 'Entry':
res.append(Entry(element=e, kp=self))
elif e.tag == 'Group':
res.append(Group(element=e, kp=self))
elif e.tag == 'Binary' and e.getparent().tag == 'Entry':
res.append(Attachment(element=e, kp=self))
else:
raise Exception('Could not cast element {}'.format(e))
if cast:
if e.tag == 'Entry':
res.append(Entry(element=e, kp=self))
elif e.tag == 'Group':
res.append(Group(element=e, kp=self))
elif e.tag == 'Binary' and e.getparent().tag == 'Entry':
res.append(Attachment(element=e, kp=self))
else:
res.append(e)
raise Exception('Could not cast element {}'.format(e))
else:
res.append(e)

# return first object in list or None
if first:
Expand All @@ -344,6 +340,9 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,

xp = ''

if not history:
prefix += '[not(ancestor::History)]'

if path is not None:

first = True
Expand All @@ -364,8 +363,7 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,
if tree is not None:
xp += '.'

if kwargs.keys():
xp += prefix
xp += prefix

# handle searching custom string fields
if 'string' in kwargs.keys():
Expand Down Expand Up @@ -394,7 +392,6 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,
xp,
tree=tree._element if tree else None,
first=first,
history=history,
cast=True,
**kwargs
)
Expand All @@ -408,7 +405,7 @@ def _can_be_moved_to_recyclebin(self, entry_or_group):
if recyclebin_group is None:
return True
uuid_str = base64.b64encode( entry_or_group.uuid.bytes).decode('utf-8')
elem = self._xpath('./UUID[text()="{}"]/..'.format(uuid_str), tree=recyclebin_group._element, first=True, history=False, cast=False)
elem = self._xpath('./UUID[text()="{}"]/..'.format(uuid_str), tree=recyclebin_group._element, first=True, cast=False)
return elem is None


Expand Down

0 comments on commit 74fad8a

Please sign in to comment.