Coverage for muuntaa/notes.py: 67.24%

38 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-21 12:16:47 +00:00

1"""Notes type.""" 

2 

3import logging 

4from typing import Union 

5 

6import lxml.objectify # nosec B410 

7 

8from muuntaa.subtree import Subtree 

9 

10RootType = lxml.objectify.ObjectifiedElement 

11RevHistType = list[dict[str, Union[str, None, tuple[int, ...]]]] 

12 

13 

14class Notes(Subtree): 

15 """Represent any Notes objects. 

16 

17 ( 

18 /cvrf:cvrfdoc/cvrf:DocumentNotes, 

19 - /cvrf:cvrfdoc/vuln:Vulnerability[i+1]/vuln:Notes, 

20 ) 

21 """ 

22 

23 ENUM_CATEGORIES = {'description', 'details', 'faq', 'general', 'legal_disclaimer', 'other', 'summary'} 

24 ENUM_MSG = ','.join(ENUM_CATEGORIES) 

25 

26 def __init__(self, lc_parent_code: str): # TODO: unlitter me and push data upstream 

27 super().__init__() 

28 if lc_parent_code not in ('cvrf', 'vuln'): 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true

29 raise KeyError('Notes can only be hosted by cvrf or vuln') 

30 if lc_parent_code == 'cvrf': 30 ↛ 37line 30 didn't jump to line 37 because the condition on line 30 was always true

31 if self.tree.get('document') is None: 31 ↛ 33line 31 didn't jump to line 33 because the condition on line 31 was always true

32 self.tree['document'] = {} 

33 if self.tree['document'].get('notes') is None: 33 ↛ 35line 33 didn't jump to line 35 because the condition on line 33 was always true

34 self.tree['document']['notes'] = [] 

35 self.hook = self.tree['document']['notes'] 

36 else: 

37 if self.tree.get('vulnerabilities') is None: 

38 self.tree['vulnerabilities'] = {} 

39 if self.tree['vulnerabilities'].get('notes') is None: 

40 self.tree['vulnerabilities']['notes'] = [] 

41 self.hook = self.tree['vulnerabilities']['notes'] 

42 

43 def always(self, root: RootType) -> None: 

44 for data in root.Note: 

45 category = data.attrib.get('Type', '').lower().replace(' ', '_') 

46 record = { # always 

47 'text': data.text, 

48 'category': category, 

49 } 

50 if category not in self.ENUM_CATEGORIES: 50 ↛ 51line 50 didn't jump to line 51 because the condition on line 50 was never true

51 logging.error('Invalid document notes category %s. Should be one of: %s!', category, self.ENUM_MSG) 

52 self.some_error = True 

53 if audience := data.attrib.get('Audience'): # sometimes 53 ↛ 55line 53 didn't jump to line 55 because the condition on line 53 was always true

54 record['audience'] = audience 

55 if title := data.attrib.get('Title'): # sometimes 55 ↛ 57line 55 didn't jump to line 57 because the condition on line 55 was always true

56 record['title'] = title 

57 self.hook.append(record) 

58 

59 def sometimes(self, root: RootType) -> None: 

60 pass