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

32 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 json 

7 

8try: 

9 from defusedxml import ElementTree 

10except ImportError: 

11 from xml.etree import ElementTree 

12 

13from pass_import.core import register_managers 

14from pass_import.errors import FormatError 

15from pass_import.formats.xml import HTML 

16 

17 

18class ClipperzHTML(HTML): 

19 """Importer for Clipperz in HTML+JSON format.""" 

20 name = 'clipperz' 

21 url = 'https://clipperz.is' 

22 hexport = 'Settings > Data > Export: HTML + JSON' 

23 himport = 'pass import clipperz file.html' 

24 html_header = 'body/div/div/textarea' 

25 keys = {'login': 'username'} 

26 

27 def parse(self): 

28 """Parse Clipperz HTML+JSON file.""" 

29 # Extract the json from the html file. 

30 tree = ElementTree.XML(self.file.read()) 

31 found = tree.find(self.html_header) 

32 if found is None: 

33 raise FormatError() 

34 

35 # Parse JSON data 

36 keys = self.invkeys() 

37 for item in json.loads(found.text): 

38 entry = {} 

39 label = item.get('label', ' \ue009').split(' \ue009') 

40 entry['title'] = label[0] 

41 if len(label) > 1: 

42 entry['group'] = label[1] 

43 

44 fields = item.get('currentVersion', {}).get('fields', {}) 

45 for uid in fields: 

46 label = fields[uid].get('label', '') 

47 entry[keys.get(label, label)] = fields[uid].get('value', '') 

48 

49 entry['comments'] = item.get('data', {}).get('notes', '') 

50 self.data.append(entry) 

51 

52 

53register_managers(ClipperzHTML)