@@ -247,13 +247,13 @@ def recyclebin_group(self):
247
247
def groups (self ):
248
248
""":obj:`list` of :obj:`Group`: list of all Group objects in database
249
249
"""
250
- return self ._xpath ( '//Group' , cast = True )
250
+ return self .find_groups ( )
251
251
252
252
@property
253
253
def entries (self ):
254
254
""":obj:`list` of :obj:`Entry`: list of all Entry objects in database,
255
255
excluding history"""
256
- return self ._xpath ( '//Entry' , cast = True )
256
+ return self .find_entries ( )
257
257
258
258
def xml (self ):
259
259
"""Get XML part of database as string
@@ -284,8 +284,7 @@ def dump_xml(self, filename):
284
284
)
285
285
)
286
286
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 ):
289
288
"""Look up elements in the XML payload and return corresponding object.
290
289
291
290
Internal function which searches the payload lxml ElementTree for
@@ -300,8 +299,6 @@ def _xpath(self, xpath_str, tree=None, first=False, history=False,
300
299
first (bool): If True, function returns first result or None. If
301
300
False, function returns list of matches or empty list. Default
302
301
is False.
303
- history (bool): If True, history entries are included in results.
304
- Default is False.
305
302
cast (bool): If True, matches are instead instantiated as
306
303
pykeepass Group, Entry, or Attachment objects. An exception
307
304
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,
319
316
320
317
res = []
321
318
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 ))
332
326
else :
333
- res .append (e )
327
+ raise Exception ('Could not cast element {}' .format (e ))
328
+ else :
329
+ res .append (e )
334
330
335
331
# return first object in list or None
336
332
if first :
@@ -344,6 +340,9 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,
344
340
345
341
xp = ''
346
342
343
+ if not history :
344
+ prefix += '[not(ancestor::History)]'
345
+
347
346
if path is not None :
348
347
349
348
first = True
@@ -364,8 +363,7 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,
364
363
if tree is not None :
365
364
xp += '.'
366
365
367
- if kwargs .keys ():
368
- xp += prefix
366
+ xp += prefix
369
367
370
368
# handle searching custom string fields
371
369
if 'string' in kwargs .keys ():
@@ -394,7 +392,6 @@ def _find(self, prefix, keys_xp, path=None, tree=None, first=False,
394
392
xp ,
395
393
tree = tree ._element if tree else None ,
396
394
first = first ,
397
- history = history ,
398
395
cast = True ,
399
396
** kwargs
400
397
)
@@ -408,7 +405,7 @@ def _can_be_moved_to_recyclebin(self, entry_or_group):
408
405
if recyclebin_group is None :
409
406
return True
410
407
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 )
412
409
return elem is None
413
410
414
411
0 commit comments