Coverage for pass_import/managers/keepass.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 12:11 +0000

1# -*- encoding: utf-8 -*- 

2# pass import - Passwords importer swiss army knife 

3# Copyright (C) 2017-2024 Alexandre PUJOL <alexandre@pujol.io>. 

4# 

5 

6import os 

7 

8from pass_import.core import register_managers 

9from pass_import.formats.csv import CSV 

10from pass_import.formats.kdbx import KDBX 

11from pass_import.formats.xml import XML 

12 

13 

14class Keepass(KDBX): 

15 """Importer/Exporter for Keepass encrypted KDBX format.""" 

16 name = 'keepass' 

17 url = 'https://www.keepass.info' 

18 himport = 'pass import keepass file.kdbx' 

19 

20 

21class KeepassCSV(CSV): 

22 """Importer for Keepass in CSV format.""" 

23 name = 'keepass' 

24 default = False 

25 url = 'https://www.keepass.info' 

26 hexport = 'File > Export > Keepass (CSV)' 

27 himport = 'pass import keepass file.csv' 

28 keys = { 

29 'title': 'Account', 

30 'password': 'Password', 

31 'login': 'Login Name', 

32 'url': 'Web Site', 

33 'comments': 'Comments' 

34 } 

35 

36 

37class KeepassXML(XML): 

38 """Importer for Keepass in XML format.""" 

39 name = 'keepass' 

40 default = False 

41 url = 'https://www.keepass.info' 

42 hexport = 'File > Export > Keepass (XML)' 

43 himport = 'pass import keepass file.xml' 

44 xml_header = {'root': 'KeePassFile'} 

45 group = 'Group' 

46 entry = 'Entry' 

47 keys = { 

48 'title': 'Title', 

49 'password': 'Password', 

50 'login': 'UserName', 

51 'url': 'URL', 

52 'comments': 'Notes' 

53 } 

54 

55 @classmethod 

56 def _getroot(cls, tree): 

57 root = tree.find('Root') 

58 return root.find('Group') 

59 

60 @classmethod 

61 def _getpath(cls, element, path=''): 

62 """Generate path name from elements title and current path.""" 

63 title = '' 

64 if element.tag == 'Group': 

65 title = element.find('Name').text 

66 if title is None: 

67 title = '' 

68 return os.path.join(path, title) 

69 

70 @classmethod 

71 def _getvalue(cls, element): 

72 xmlkey = value = '' 

73 for child in element.findall('Key'): 

74 xmlkey = child.text 

75 value = element.find('Value').text 

76 return xmlkey, value 

77 

78 def _import(self, element, path=''): 

79 path = self._getpath(element, path) 

80 for group in element.findall(self.group): 

81 self._import(group, path) 

82 for xmlentry in element.findall(self.entry): 

83 entry = self._getentry(xmlentry) 

84 entry['group'] = path 

85 self.data.append(entry) 

86 

87 

88register_managers(Keepass, KeepassCSV, KeepassXML)