Coverage for pass_import/managers/revelation.py: 95%

42 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.xml import XML 

10 

11 

12class Revelation(XML): 

13 """Importer for Revelation in XML format.""" 

14 name = 'revelation' 

15 url = 'https://revelation.olasagasti.info' 

16 hexport = 'File > Export: XML' 

17 himport = 'pass import revelation file.xml' 

18 xml_header = {'root': 'revelationdata'} 

19 keyslist = [ 

20 'title', 'password', 'login', 'database', 'host', 'port', 'url', 

21 'email', 'phone', 'location', 'description', 'comments' 

22 ] 

23 keys = { 

24 'title': 'name', 

25 'password': 'generic-password', 

26 'login': 'generic-username', 

27 'database': 'generic-database', 

28 'host': 'generic-hostname', 

29 'hostdomain': 'generic-domain', 

30 'port': 'generic-port', 

31 'url': 'generic-url', 

32 'email': 'generic-email', 

33 'phone': 'phone-phonenumber', 

34 'location': 'generic-location', 

35 'description': 'description', 

36 'group': '', 

37 'comments': 'notes' 

38 } 

39 

40 @classmethod 

41 def _getvalue(cls, element): 

42 key = element.tag 

43 if key == 'field': 

44 key = element.attrib.get('id', '') 

45 if key in ('generic-pin', 'generic-code'): 

46 key = 'generic-password' 

47 if key == 'updated': 

48 return key, '' 

49 return key, element.text 

50 

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

52 for xmlentry in element.findall('entry'): 

53 if xmlentry.attrib.get('type', '') == 'folder': 

54 _path = os.path.join(path, xmlentry.find('name').text) 

55 self._import(xmlentry, _path) 

56 else: 

57 entry = self._getentry(xmlentry) 

58 entry['group'] = path 

59 

60 host = entry.get('host', None) 

61 # Fix older Revelation storing Websites in Generic entries 

62 if host and 'url' not in entry: 

63 for protocol in ['http://', 'https://']: 

64 if host.startswith(protocol): 

65 entry['url'] = entry.pop('host') 

66 break 

67 domain = entry.pop('hostdomain', None) 

68 if domain: 

69 if host: 

70 if not host.endswith(domain): 

71 entry['host'] = f'{host}.{domain}' 

72 else: 

73 entry['host'] = domain 

74 

75 self.data.append(entry) 

76 

77 

78register_managers(Revelation)