Coverage for C:\src\imod-python\imod\mf6\statusinfo.py: 93%
58 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-08 13:27 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-08 13:27 +0200
1from abc import ABC, abstractmethod
4class StatusInfoBase(ABC):
5 def __init__(self, title: str = ""):
6 self.__title: str = title
8 @property
9 def title(self) -> str:
10 return self.__title
12 @title.setter
13 def title(self, title: str) -> None:
14 self.__title = title
16 @property
17 @abstractmethod
18 def errors(self) -> list[str]:
19 raise NotImplementedError
21 @abstractmethod
22 def has_errors(self) -> bool:
23 raise NotImplementedError
25 @abstractmethod
26 def to_string(self) -> str:
27 raise NotImplementedError
30class StatusInfo(StatusInfoBase):
31 """
32 This class can be used to collect any status messages.
33 In its current state, the object is limited to error messages.
34 But this can be extended later with e.g. warnings.
35 """
37 def __init__(self, title: str = ""):
38 super().__init__(title)
39 self.__errors: list[str] = []
41 def add_error(self, message: str) -> None:
42 self.__errors.append(message)
44 @property
45 def errors(self) -> list[str]:
46 return self.__errors
48 def has_errors(self) -> bool:
49 return any(self.__errors)
51 def to_string(self) -> str:
52 header = self.title + ":" + "\n"
53 indented_errors = "{1}{0}".format("\n".join(self.errors), "\t* ")
54 return header + indented_errors
57class NestedStatusInfo(StatusInfoBase):
58 """
59 This class can be used to collect any nested status messages.
60 """
62 def __init__(self, title: str = ""):
63 super().__init__(title)
64 self.__children: list[StatusInfoBase] = []
66 def add(self, status_info: StatusInfoBase):
67 self.__children.append(status_info)
69 @property
70 def errors(self) -> list[str]:
71 errors: list[str] = []
72 for child in self.__children:
73 errors += child.errors
74 return errors
76 def has_errors(self) -> bool:
77 for child in self.__children:
78 if child.has_errors():
79 return True
80 return False
82 def to_string(self) -> str:
83 string = ""
84 for child in self.__children:
85 string += "\n* " + child.to_string()
87 string = string.replace("\n", "\n\t")
88 return self.title + ":" + string