Coverage for C:\src\imod-python\imod\mf6\statusinfo.py: 93%

58 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-08 14:15 +0200

1from abc import ABC, abstractmethod 

2 

3 

4class StatusInfoBase(ABC): 

5 def __init__(self, title: str = ""): 

6 self.__title: str = title 

7 

8 @property 

9 def title(self) -> str: 

10 return self.__title 

11 

12 @title.setter 

13 def title(self, title: str) -> None: 

14 self.__title = title 

15 

16 @property 

17 @abstractmethod 

18 def errors(self) -> list[str]: 

19 raise NotImplementedError 

20 

21 @abstractmethod 

22 def has_errors(self) -> bool: 

23 raise NotImplementedError 

24 

25 @abstractmethod 

26 def to_string(self) -> str: 

27 raise NotImplementedError 

28 

29 

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 """ 

36 

37 def __init__(self, title: str = ""): 

38 super().__init__(title) 

39 self.__errors: list[str] = [] 

40 

41 def add_error(self, message: str) -> None: 

42 self.__errors.append(message) 

43 

44 @property 

45 def errors(self) -> list[str]: 

46 return self.__errors 

47 

48 def has_errors(self) -> bool: 

49 return any(self.__errors) 

50 

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 

55 

56 

57class NestedStatusInfo(StatusInfoBase): 

58 """ 

59 This class can be used to collect any nested status messages. 

60 """ 

61 

62 def __init__(self, title: str = ""): 

63 super().__init__(title) 

64 self.__children: list[StatusInfoBase] = [] 

65 

66 def add(self, status_info: StatusInfoBase): 

67 self.__children.append(status_info) 

68 

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 

75 

76 def has_errors(self) -> bool: 

77 for child in self.__children: 

78 if child.has_errors(): 

79 return True 

80 return False 

81 

82 def to_string(self) -> str: 

83 string = "" 

84 for child in self.__children: 

85 string += "\n* " + child.to_string() 

86 

87 string = string.replace("\n", "\n\t") 

88 return self.title + ":" + string