Coverage for muuntaa/subtree.py: 85.19%

27 statements  

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

1"""Named protocol to ensure common interfaces for the subtrees.""" 

2 

3import logging 

4from typing import Any, Protocol 

5 

6import lxml.objectify # nosec B410 

7 

8RootType = lxml.objectify.ObjectifiedElement 

9 

10 

11class Subtree(Protocol): 

12 tree: dict[str, Any] = None # type: ignore 

13 some_error: bool = False 

14 

15 def __init__(self) -> None: 

16 self.tree = {} 

17 self.some_error = False 

18 

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

20 pass 

21 

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

23 pass 

24 

25 def load(self, root: RootType) -> None: 

26 try: 

27 self.always(root) 

28 except Exception as e: 

29 logging.error('ingesting always present element %s failed with %s', root.tag, e) 

30 try: 

31 self.sometimes(root) 

32 except Exception as e: 

33 logging.error('ingesting sometimes present element %s failed with %s', root.tag, e) 

34 

35 def dump(self) -> dict[str, Any]: 

36 return self.tree 

37 

38 def has_errors(self) -> bool: 

39 return self.some_error