Skip to content

Commit 74fad8a

Browse files
author
Evidlo
committed
move history kwarg from _xpath to _find
1 parent dde1d0a commit 74fad8a

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

pykeepass/entry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _get_string_field(self, key):
8888
(str or None): field value
8989
"""
9090

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

@@ -102,7 +102,7 @@ def _set_string_field(self, key, value, protected=True):
102102
in other tools. This property is ignored in PyKeePass and all
103103
fields are decrypted immediately upon opening the database.
104104
"""
105-
field = self._xpath('String/Key[text()="{}"]/..'.format(key), first=True)
105+
field = self._xpath('String/Key[text()="{}"]/..'.format(key), history=True, first=True)
106106
if field is not None:
107107
self._element.remove(field)
108108
self._element.append(E.String(E.Key(key), E.Value(value, Protected=str(protected))))

pykeepass/pykeepass.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ def recyclebin_group(self):
247247
def groups(self):
248248
""":obj:`list` of :obj:`Group`: list of all Group objects in database
249249
"""
250-
return self._xpath('//Group', cast=True)
250+
return self.find_groups()
251251

252252
@property
253253
def entries(self):
254254
""":obj:`list` of :obj:`Entry`: list of all Entry objects in database,
255255
excluding history"""
256-
return self._xpath('//Entry', cast=True)
256+
return self.find_entries()
257257

258258
def xml(self):
259259
"""Get XML part of database as string
@@ -284,8 +284,7 @@ def dump_xml(self, filename):
284284
)
285285
)
286286

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

320317
res = []
321318
for e in elements:
322-
if history or e.getparent().tag != 'History':
323-
if cast:
324-
if e.tag == 'Entry':
325-
res.append(Entry(element=e, kp=self))
326-
elif e.tag == 'Group':
327-
res.append(Group(element=e, kp=self))
328-
elif e.tag == 'Binary' and e.getparent().tag == 'Entry':
329-
res.append(Attachment(element=e, kp=self))
330-
else:
331-
raise Exception('Could not cast element {}'.format(e))
319+
if cast:
320+
if e.tag == 'Entry':
321+
res.append(Entry(element=e, kp=self))
322+
elif e.tag == 'Group':
323+
res.append(Group(element=e, kp=self))
324+
elif e.tag == 'Binary' and e.getparent().tag == 'Entry':
325+
res.append(Attachment(element=e, kp=self))
332326
else:
333-
res.append(e)
327+
raise Exception('Could not cast element {}'.format(e))
328+
else:
329+
res.append(e)
334330

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

345341
xp = ''
346342

343+
if not history:
344+
prefix += '[not(ancestor::History)]'
345+
347346
if path is not None:
348347

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

367-
if kwargs.keys():
368-
xp += prefix
366+
xp += prefix
369367

370368
# handle searching custom string fields
371369
if 'string' in kwargs.keys():
@@ -394,7 +392,6 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,
394392
xp,
395393
tree=tree._element if tree else None,
396394
first=first,
397-
history=history,
398395
cast=True,
399396
**kwargs
400397
)
@@ -408,7 +405,7 @@ def _can_be_moved_to_recyclebin(self, entry_or_group):
408405
if recyclebin_group is None:
409406
return True
410407
uuid_str = base64.b64encode( entry_or_group.uuid.bytes).decode('utf-8')
411-
elem = self._xpath('./UUID[text()="{}"]/..'.format(uuid_str), tree=recyclebin_group._element, first=True, history=False, cast=False)
408+
elem = self._xpath('./UUID[text()="{}"]/..'.format(uuid_str), tree=recyclebin_group._element, first=True, cast=False)
412409
return elem is None
413410

414411

0 commit comments

Comments
 (0)