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
« 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."""
3import logging
4from typing import Any, Protocol
6import lxml.objectify # nosec B410
8RootType = lxml.objectify.ObjectifiedElement
11class Subtree(Protocol):
12 tree: dict[str, Any] = None # type: ignore
13 some_error: bool = False
15 def __init__(self) -> None:
16 self.tree = {}
17 self.some_error = False
19 def always(self, root: RootType) -> None:
20 pass
22 def sometimes(self, root: RootType) -> None:
23 pass
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)
35 def dump(self) -> dict[str, Any]:
36 return self.tree
38 def has_errors(self) -> bool:
39 return self.some_error